Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <complex.h>
- void Fourier(unsigned char *registro, int size, double complex *coeficientes)
- {
- // N é o total de observacoes
- // M_PI é uma macro que define o valor do PI (math.h)
- int i, j;
- for (i = 0; i < size; i++)
- for (j = 0; j < size; j++)
- coeficientes[i] = coeficientes[i] + registro[j] * cexp( ( -2.0 * M_PI * ( ((j+1) * i * 1.0) / ( size * 1.0 ) ) ) * _Complex_I );
- }
- Calculo_Magnitude(double complex *coeficientes, int size)
- {
- double parte_real;
- double parte_imaginaria;
- double magnitude;
- int i ;
- for (i = 0; i < size; i++)
- {
- parte_real = __real__ coeficientes[i];
- parte_imaginaria = __imag__ coeficientes[i];
- magnitude = sqrt(pow(parte_real, 2) + pow(parte_imaginaria, 2));
- coeficientes[i] = magnitude;
- }
- }
- Ordenacao(double complex *coeficientes, double complex *auxiliar, int size)
- {
- int i, j;
- for (i = 0; i < size; i++)
- auxiliar[i] = coeficientes[i];
- for (j = size-2; j >= 0; j--)
- {
- // chave atual de comparacao
- int ckey = auxiliar[j];
- // compara com os valores abaixo
- // e tenta encontrar a posicao correta de ckey anterior
- i = j+1;
- while ((i < size) && ((int)auxiliar[i] > ckey))
- {
- auxiliar[i-1] = auxiliar[i];
- i++;
- }
- // o que significa o valor de i ???
- auxiliar[i-1] = ckey;
- }
- }
- void inverseFourier(double complex *coefficients, int N, unsigned char *audio8ubits)
- {
- // N é o total de coeficientes
- // M_PI é uma macro que define o valor do PI (math.h)
- double complex *audio = (double complex *) calloc(N, sizeof(double complex));
- int n, k;
- for (n = 0; n < N; n++)
- {
- for (k = 0; k < N; k++)
- {
- audio[n] = audio[n] + coefficients[k] * cexp( ( 2.0 * M_PI * ( ((k+1) * n * 1.0) / ( N * 1.0 ) ) ) * _Complex_I );
- }
- audio[n] = audio[n] / ( N * 1.0);
- audio8ubits[n] = (int) __real__ audio[n];
- }
- free(audio);
- }
- int main(int argc, char* argv[])
- {
- char filename[30];
- FILE *arq_bin;
- int t, size;
- int i = 0;
- unsigned char *registro;
- double complex *coeficientes;
- scanf ("%s", filename);
- scanf ("%d", &t);
- arq_bin = (filename, "rb");
- if (arq_bin == NULL) {
- fprintf(stderr,"Erro ao abrir arquivo %s\n\n", argv[1]);
- return 2;
- }
- /*fseek(arq_bin, 0, SEEK_END);
- size = ftell(arq_bin)/sizeof(unsigned char);
- rewind (arq_bin);
- registro = (unsigned char*) malloc(size*sizeof(unsigned char));
- rewind (arq_bin);
- do
- {
- fread(®istro[i], sizeof(unsigned char), 1, arq_bin);
- i++;
- } while (!feof(arq_bin));*/
- unsigned char c;
- while((fread(&c, 1, 1, arq_bin)) > 0)
- i++;
- size = i;
- registro = (unsigned char*) malloc(size*sizeof(unsigned char));
- i = 0;
- while((fread(&c, 1, 1, arq_bin)) > 0)
- {
- registro[i] = (int) c;
- i++;
- }
- for (i = 0; i < size; i++)
- printf ("%d ", registro[i]);
- coeficientes = (double complex*) calloc(size, sizeof(double complex));
- Fourier(registro, size, coeficientes);
- Calculo_Magnitude(coeficientes, size);
- double complex *auxiliar;
- auxiliar = (double complex*) calloc(size, sizeof(double complex));
- Ordenacao(coeficientes, auxiliar, size);
- for (i = 0; i < size; i++)
- if ((int)auxiliar[t] > (int)coeficientes[i])
- coeficientes[i] = 0;
- unsigned char *audio8ubits;
- *audio8ubits = (unsigned char *) calloc(size, sizeof(unsigned char));
- inverseFourier(coeficientes, size, audio8ubits);
- FILE *arq_saida;
- arq_saida = fopen("saida.raw", "wb");
- if (arq_bin == NULL) {
- fprintf(stderr,"Erro ao abrir arquivo arquivo de saida\n\n");
- return 3;
- }
- for (i = 0; i < size; i++)
- fwrite ((int)&audio8ubits[i], sizeof(unsigned char), 1, arq_saida);
- printf ("%d\n", size);
- int total = 0;
- for ( i = 0; i < size; i++)
- if (__real__ (coeficientes[i]) < 0.0 && __imag__ (coeficientes[k]) < 0.0 )
- total++;
- printf ("%d\n", total);
- for (i = 0; i <= t; i++)
- printf ("%d ", auxiliar[i]);
- printf ("\n");
- free(audio8ubits);
- free(auxiliar);
- free(coeficientes);
- free(registro);
- fclose(arq_bin);
- fclose(arq_saida);
- return 0;
- }
Add Comment
Please, Sign In to add comment