Guest User

Untitled

a guest
Nov 16th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <complex.h>
  5.  
  6. void insertionSort(double *v, int tam) //função de organizar em ordem decrescente
  7. {
  8. int i, j;
  9. double aux;
  10.  
  11. for(i=1; i<tam; i++)
  12. {
  13. j=i;
  14.  
  15. while(v[j]>v[j-1])
  16. {
  17. aux=v[j];
  18. v[j]=v[j-1];
  19. v[j-1]=aux;
  20. j--;
  21.  
  22. if(j==0)
  23. {
  24. break;
  25. }
  26. }
  27. }
  28. }
  29.  
  30. unsigned char* inverseFourier(double complex *coefficients, int N)
  31. {
  32. double complex *audio=(double complex *)calloc(N, sizeof(double complex));
  33. unsigned char *audio8ubits=(unsigned char *)calloc(N, sizeof(unsigned char));
  34.  
  35. int n, k;
  36.  
  37. for (n=0; n<N; n++)
  38. {
  39. for (k=0; k<N; k++)
  40. {
  41. audio[n]=audio[n]+coefficients[k]*cexp((2.0*M_PI*(((k+1)*n*1.0)/(N*1.0)))*_Complex_I);
  42. }
  43. audio[n]=audio[n]/(N*1.0);
  44. audio8ubits[n]=(int)__real__ audio[n];
  45. }
  46. free(audio);
  47. return audio8ubits;
  48. }
  49.  
  50. int main()
  51. {
  52. double complex numero_complexo=3.0+4.0*_Complex_I;
  53. double parte_real=__real__ numero_complexo;
  54. double parte_imaginaria=__imag__ numero_complexo;
  55.  
  56. char nome[10]; //declaração das variáveis
  57. int t, i=0, N=0, k, cont=0;
  58. unsigned char a, *v;
  59. double complex *img;
  60. double *mag, *mag2;
  61. double *magaux;
  62.  
  63. FILE *audio1;
  64.  
  65. scanf("%s", nome); //armazena o nome do arquivo a ser analisado
  66.  
  67. scanf("%d", &t); //armazena a quantidade de coeficientes a serem utilizados
  68.  
  69. audio1=fopen(nome, "rb"); //abre o arquivo de entrada
  70.  
  71. if (audio1==NULL)
  72. {
  73. printf("\nImpossivel abrir o arquivo.\n"); //mensagem para o caso de falha ao abrir o arquivo
  74. return -1;
  75. }
  76.  
  77. while (fread(&a, sizeof(unsigned char), 1, audio1))
  78. {
  79. N++; //observações
  80. }
  81.  
  82. v=(unsigned char*)calloc(N, sizeof(unsigned char));
  83.  
  84. fseek(audio1, 0, SEEK_SET);
  85.  
  86. while (fread(&a, sizeof(unsigned char), 1, audio1))
  87. {
  88. v[i]=(unsigned char)a;
  89. i++;
  90. }
  91.  
  92. printf("%d\n", N); //saída do número de observações
  93.  
  94. img=(double complex*)calloc(N, sizeof(double complex));
  95.  
  96. mag=(double*)malloc(N*sizeof(double));
  97. mag2=(double*)malloc(N*sizeof(double));
  98. magaux=(double*)malloc(N*sizeof(double));
  99.  
  100. for (k=1; k<=N; k++)
  101. {
  102. parte_real=0;
  103. parte_imaginaria=0;
  104. for (i=0; i<N; i++)
  105. {
  106. 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
  107. }
  108. }
  109.  
  110. for (k=0; k<N; k++)
  111. {
  112. parte_real=__real__ img[k];
  113. parte_imaginaria=__imag__ img[k];
  114. mag[k]=sqrt(pow(parte_real, 2)+pow(parte_imaginaria, 2)); //cálculo da magnitude
  115. mag2[k]=mag[k]; //vetor auxiliar contendo os mesmos dados
  116. }
  117.  
  118. insertionSort(mag, N); //chamada da função que organiza o vetor mag
  119.  
  120. for (k=0; k<N; k++)
  121. {
  122. if (__real__ img[k]<0.0 && __imag__ img[k]<0.0)
  123. {
  124. cont++; //contagem de coeficientes negativos
  125. }
  126. }
  127.  
  128. printf("%d\n", cont); //saída do contador de coeficientes negativos
  129.  
  130. for (i=0; i<N; i++)
  131. {
  132. if (i>t)
  133. {
  134. mag[i]=0; //atribui valor zero aos vetores com posições maiores que t
  135. }
  136. }
  137.  
  138. for (i=0; i<N; i++) //erro deve ta aqui nao sei como resolver essa merda
  139. {
  140. for (k=0; k<N; k++)
  141. {
  142. if (mag2[i]==mag[k])
  143. {
  144. mag2[i]=mag[k];
  145. }
  146. else
  147. {
  148. mag2[i]=0.0;
  149. }
  150. }
  151. }
  152.  
  153. for (i=0; i<N; i++)//teste
  154. {
  155. if (mag2[i]!=0)
  156. {
  157. printf("%lf ", mag2[i]);
  158. }
  159. }
  160.  
  161. for (i=0; i<t; i++)
  162. {
  163. magaux[i]=(int)mag[i]; //passando de float para int
  164. printf("%.0lf ", magaux[i]); //saída do vetor de magnitudes devidamente ordenado
  165. }
  166. printf("\n");
  167.  
  168. free(v); //liberando memória do vetor v
  169. free(mag); //liberando memória do vetor mag
  170.  
  171. return 0;
  172. }
Add Comment
Please, Sign In to add comment