Guest User

Untitled

a guest
Oct 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <complex.h>
  5.  
  6. void Fourier(unsigned char *registro, int size, double complex *coeficientes)
  7. {
  8.     // N é o total de observacoes
  9.     // M_PI é uma macro que define o valor do PI (math.h)
  10.     int i, j;
  11.     for (i = 0; i < size; i++)
  12.         for (j = 0; j < size; j++)
  13.             coeficientes[i] = coeficientes[i] + registro[j] * cexp( ( -2.0 * M_PI * ( ((j+1) * i * 1.0) / ( size * 1.0 ) ) ) * _Complex_I );
  14. }
  15.  
  16. void Calculo_Magnitude(double complex *coeficientes, int size, double complex *magnitude)
  17. {
  18.     double parte_real;
  19.     double parte_imaginaria;
  20.     double mag;
  21.     int i ;
  22.  
  23.     for (i = 0; i < size; i++)
  24.     {
  25.         parte_real = __real__ coeficientes[i];
  26.         parte_imaginaria = __imag__ coeficientes[i];
  27.         mag = sqrt(pow(parte_real, 2) + pow(parte_imaginaria, 2));
  28.         magnitude[i] = mag;
  29.     }
  30. }
  31.  
  32. void Ordenacao(double complex *magnitude, double complex *auxiliar, int size)
  33. {
  34.     int i, j;
  35.  
  36.     for (i = 0; i < size; i++)
  37.         auxiliar[i] = magnitude[i];
  38.  
  39.  
  40.     for (j = size-2; j >= 0; j--)
  41.     {
  42.     // chave atual de comparacao
  43.     int ckey = auxiliar[j];
  44.     // compara com os valores abaixo
  45.     // e tenta encontrar a posicao correta de ckey anterior
  46.     i = j+1;
  47.     while ((i < size) && ((int)auxiliar[i] > ckey))
  48.     {
  49.         auxiliar[i-1] = auxiliar[i];
  50.         i++;
  51.     }
  52.     // o que significa o valor de i ???
  53.     auxiliar[i-1] = ckey;
  54.     }
  55. }
  56.  
  57. void inverseFourier(double complex *coefficients, int N, unsigned char *audio8ubits)
  58. {
  59.     // N é o total de coeficientes
  60.     // M_PI é uma macro que define o valor do PI (math.h)
  61.     double complex *audio = (double complex *) calloc(N, sizeof(double complex));
  62.     int n, k;
  63.     for (n = 0; n < N; n++)
  64.     {
  65.         for (k = 0; k < N; k++)
  66.         {
  67.             audio[n] = audio[n] + coefficients[k] * cexp( ( 2.0 * M_PI * ( ((k+1) * n * 1.0) / ( N * 1.0 ) ) ) * _Complex_I );
  68.         }
  69.         audio[n] = audio[n] / ( N * 1.0);
  70.         audio8ubits[n] = (int) __real__ audio[n];
  71.     }
  72.     free(audio);
  73. }
  74.  
  75. int main(int argc, char* argv[])
  76. {
  77.     char filename[30];
  78.     FILE *arq_bin;
  79.     int t, size;
  80.     int i = 0;
  81.     unsigned char *registro;
  82.     double complex *coeficientes;
  83.  
  84.     scanf ("%s", filename);
  85.     scanf ("%d", &t);
  86.  
  87.     arq_bin = fopen(filename, "rb");
  88.     if (arq_bin == NULL) {
  89.         fprintf(stderr,"Erro ao abrir arquivo %s\n\n", argv[1]);
  90.         return 2;
  91.     }
  92.  
  93.     /*fseek(arq_bin, 0, SEEK_END);
  94.     size = ftell(arq_bin)/sizeof(unsigned char);
  95.     rewind (arq_bin);
  96.     registro = (unsigned char*) malloc(size*sizeof(unsigned char));
  97.  
  98.     rewind (arq_bin);
  99.     do
  100.     {
  101.         fread(&registro[i], sizeof(unsigned char), 1, arq_bin);
  102.         i++;
  103.     } while (!feof(arq_bin));*/
  104.  
  105.     unsigned char c;
  106.  
  107.     while((fread(&c, 1, 1, arq_bin)) > 0)
  108.        i++;
  109.  
  110.     size = i;
  111.     registro = (unsigned char*) malloc(size*sizeof(unsigned char));
  112.  
  113.     i = 0;
  114.     while((fread(&c, 1, 1, arq_bin)) > 0)
  115.     {
  116.         registro[i] = (int) c;
  117.         i++;
  118.     }
  119.  
  120.     /*
  121.     for (i = 0; i < size; i++)
  122.         printf ("%d ", registro[i]);
  123.     */
  124.  
  125.     coeficientes = (double complex*) calloc(size, sizeof(double complex));
  126.     Fourier(registro, size, coeficientes);
  127.     double complex *magnitude;
  128.     magnitude = (double complex*) calloc (size, sizeof(double complex));
  129.     Calculo_Magnitude(coeficientes, size, magnitude);
  130.     double complex *auxiliar;
  131.     auxiliar = (double complex*) calloc(size, sizeof(double complex));
  132.     Ordenacao(magnitude, auxiliar, size);
  133.  
  134.     for (i = 0; i < size; i++)
  135.         if ((int)auxiliar[t] > (int)magnitude[i])
  136.             coeficientes[i] = 0;
  137.  
  138.     unsigned char *audio8ubits;
  139.     audio8ubits = (unsigned char *) calloc(size, sizeof(unsigned char));
  140.     inverseFourier(coeficientes, size, audio8ubits);
  141.  
  142.  
  143.     FILE *arq_saida;
  144.     arq_saida = fopen("saida.raw", "wb");
  145.     if (arq_bin == NULL) {
  146.         fprintf(stderr,"Erro ao abrir arquivo arquivo de saida\n\n");
  147.         return 3;
  148.     }
  149.  
  150.     for (i = 0; i < size; i++)
  151.         fwrite (&audio8ubits[i], sizeof(unsigned char), 1, arq_saida);
  152.  
  153.     printf ("%d\n", size);
  154.     int total = 0;
  155.     for ( i = 0; i < size; i++)
  156.         if (__real__ (coeficientes[i]) < 0.0 && __imag__ (coeficientes[i]) < 0.0 )
  157.             total++;
  158.  
  159.     printf ("%d\n", total);
  160.     for (i = 0; i <= t; i++)
  161.             printf ("%d ", (int)auxiliar[i]);
  162.     printf ("\n");
  163.  
  164.     free(magnitude);
  165.     free(audio8ubits);
  166.     free(auxiliar);
  167.     free(coeficientes);
  168.     free(registro);
  169.     fclose(arq_bin);
  170.     fclose(arq_saida);
  171.  
  172.     return 0;
  173. }
Add Comment
Please, Sign In to add comment