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 insertionSort(double *v, int tam) //função de organizar em ordem decrescente
- {
- int i, j;
- double aux;
- for(i=1; i<tam; i++)
- {
- j=i;
- while(v[j]>v[j-1])
- {
- aux=v[j];
- v[j]=v[j-1];
- v[j-1]=aux;
- j--;
- if(j==0)
- {
- break;
- }
- }
- }
- }
- unsigned char* inverseFourier(double complex *coefficients, int N)
- {
- double complex *audio=(double complex *)calloc(N, sizeof(double complex));
- unsigned char *audio8ubits=(unsigned char *)calloc(N, sizeof(unsigned char));
- 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);
- return audio8ubits;
- }
- int main()
- {
- double complex numero_complexo=3.0+4.0*_Complex_I;
- double parte_real=__real__ numero_complexo;
- double parte_imaginaria=__imag__ numero_complexo;
- char nome[10]; //declaração das variáveis
- int t, i=0, N=0, k, cont=0;
- unsigned char a, *v;
- double complex *img;
- double *mag, *mag2;
- double *magaux;
- FILE *audio1;
- scanf("%s", nome); //armazena o nome do arquivo a ser analisado
- scanf("%d", &t); //armazena a quantidade de coeficientes a serem utilizados
- audio1=fopen(nome, "rb"); //abre o arquivo de entrada
- if (audio1==NULL)
- {
- printf("\nImpossivel abrir o arquivo.\n"); //mensagem para o caso de falha ao abrir o arquivo
- return -1;
- }
- while (fread(&a, sizeof(unsigned char), 1, audio1))
- {
- N++; //observações
- }
- v=(unsigned char*)calloc(N, sizeof(unsigned char));
- fseek(audio1, 0, SEEK_SET);
- while (fread(&a, sizeof(unsigned char), 1, audio1))
- {
- v[i]=(unsigned char)a;
- i++;
- }
- printf("%d\n", N); //saída do número de observações
- img=(double complex*)calloc(N, sizeof(double complex));
- mag=(double*)malloc(N*sizeof(double));
- mag2=(double*)malloc(N*sizeof(double));
- magaux=(double*)malloc(N*sizeof(double));
- for (k=1; k<=N; k++)
- {
- parte_real=0;
- parte_imaginaria=0;
- for (i=0; i<N; i++)
- {
- img[k-1]=img[k-1]+v[i]*cexp(-_Complex_I*2.0*M_PI*((double)k/(double)N)*(double)i); //fórmula da transformada de fourier
- }
- }
- for (k=0; k<N; k++)
- {
- parte_real=__real__ img[k];
- parte_imaginaria=__imag__ img[k];
- mag[k]=sqrt(pow(parte_real, 2)+pow(parte_imaginaria, 2)); //cálculo da magnitude
- mag2[k]=mag[k]; //vetor auxiliar contendo os mesmos dados
- }
- insertionSort(mag, N); //chamada da função que organiza o vetor mag
- for (k=0; k<N; k++)
- {
- if (__real__ img[k]<0.0 && __imag__ img[k]<0.0)
- {
- cont++; //contagem de coeficientes negativos
- }
- }
- printf("%d\n", cont); //saída do contador de coeficientes negativos
- for (i=0; i<N; i++)
- {
- if (i>t)
- {
- mag[i]=0; //atribui valor zero aos vetores com posições maiores que t
- }
- }
- for (i=0; i<N; i++) //erro deve ta aqui nao sei como resolver essa merda
- {
- for (k=0; k<N; k++)
- {
- if (mag2[i]==mag[k])
- {
- mag2[i]=mag[k];
- }
- else
- {
- mag2[i]=0.0;
- }
- }
- }
- for (i=0; i<N; i++)//teste
- {
- if (mag2[i]!=0)
- {
- printf("%lf ", mag2[i]);
- }
- }
- for (i=0; i<t; i++)
- {
- magaux[i]=(int)mag[i]; //passando de float para int
- printf("%.0lf ", magaux[i]); //saída do vetor de magnitudes devidamente ordenado
- }
- printf("\n");
- free(v); //liberando memória do vetor v
- free(mag); //liberando memória do vetor mag
- return 0;
- }
Add Comment
Please, Sign In to add comment