Guest User

Untitled

a guest
Oct 18th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.42 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. Calculo_Magnitude(double complex *coeficientes, int size)
  17. {
  18.     double parte_real;
  19.     double parte_imaginaria;
  20.     double magnitude;
  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.         magnitude = sqrt(pow(parte_real, 2) + pow(parte_imaginaria, 2));
  28.         coeficientes[i] = magnitude;
  29.     }
  30. }
  31.  
  32. Ordenacao(double complex *coeficientes, double complex *auxiliar, int size)
  33. {
  34.     int i, j;
  35.  
  36.     for (i = 0; i < size; i++)
  37.         auxiliar[i] = coeficientes[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 = (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.     coeficientes = (double complex*) calloc(size, sizeof(double complex));
  125.     Fourier(registro, size, coeficientes);
  126.     Calculo_Magnitude(coeficientes, size);
  127.     double complex *auxiliar;
  128.     auxiliar = (double complex*) calloc(size, sizeof(double complex));
  129.     Ordenacao(coeficientes, auxiliar, size);
  130.  
  131.     for (i = 0; i < size; i++)
  132.         if ((int)auxiliar[t] > (int)coeficientes[i])
  133.             coeficientes[i] = 0;
  134.  
  135.     unsigned char *audio8ubits;
  136.     *audio8ubits = (unsigned char *) calloc(size, sizeof(unsigned char));
  137.     inverseFourier(coeficientes, size, audio8ubits);
  138.  
  139.  
  140.     FILE *arq_saida;
  141.     arq_saida = fopen("saida.raw", "wb");
  142.     if (arq_bin == NULL) {
  143.         fprintf(stderr,"Erro ao abrir arquivo arquivo de saida\n\n");
  144.         return 3;
  145.     }
  146.  
  147.     for (i = 0; i < size; i++)
  148.         fwrite ((int)&audio8ubits[i], sizeof(unsigned char), 1, arq_saida);
  149.  
  150.     printf ("%d\n", size);
  151.     int total = 0;
  152.     for ( i = 0; i < size; i++)
  153.         if (__real__ (coeficientes[i]) < 0.0 && __imag__ (coeficientes[k]) < 0.0 )
  154.             total++;
  155.        
  156.     printf ("%d\n", total);
  157.     for (i = 0; i <= t; i++)
  158.             printf ("%d ", auxiliar[i]);
  159.     printf ("\n");
  160.    
  161.  
  162.     free(audio8ubits);
  163.     free(auxiliar);
  164.     free(coeficientes);
  165.     free(registro);
  166.     fclose(arq_bin);
  167.     fclose(arq_saida);
  168.  
  169.     return 0;
  170. }
Add Comment
Please, Sign In to add comment