fft.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include "fourier.h"
  5. #include "ddcmath.h"
  6. #include "fft.h"
  7. #include "cmsis_os.h"
  8. #include "ImC/imc_extension.h"
  9. #define CHECKPOINTER(p) CheckPointer(p, #p)
  10. static void CheckPointer(void *p, char *name)
  11. {
  12. if (p == NULL)
  13. {
  14. // fprintf(stderr, "Error in fft_float(): %s == NULL\n", name);
  15. printf("Error in fft_float(): %s == NULL\n", name);
  16. exit(1);
  17. }
  18. }
  19. void vFFT()
  20. {
  21. unsigned MAXSIZE;
  22. unsigned MAXWAVES;
  23. unsigned i, j;
  24. int invfft = 0;
  25. MAXSIZE = 128;
  26. MAXWAVES = 4;
  27. // int IMC_REPEAT = 30;
  28. int IMC_REPEAT = imcBENCH_REPEAT_COUNT;
  29. // srand(1);
  30. float RealIn[MAXSIZE];
  31. float ImagIn[MAXSIZE];
  32. float RealOut[MAXSIZE];
  33. float ImagOut[MAXSIZE];
  34. float coeff[MAXWAVES];
  35. float amp[MAXWAVES];
  36. // IMC_UNROLL(1)
  37. for (int imc_repeat=0; imc_repeat < IMC_REPEAT; imc_repeat++) {
  38. /* Makes MAXWAVES waves of random amplitude and period */
  39. IMC_UNROLL(4)
  40. for (i = 0; i < MAXWAVES; i++)
  41. {
  42. // coeff[i] = rand() % 1000;
  43. // amp[i] = rand() % 1000;
  44. coeff[i] = 123;
  45. amp[i] = 456;
  46. // coeff[i] = i;
  47. // amp[i] = i+1;
  48. }
  49. IMC_UNROLL(4)
  50. for (i = 0; i < MAXSIZE; i++)
  51. {
  52. /* RealIn[i]=rand();*/
  53. RealIn[i] = 0;
  54. ImagIn[i] = 0;
  55. float sum = 0;
  56. IMC_UNROLL(4)
  57. for (j = 0; j < MAXWAVES; j++)
  58. {
  59. /* randomly select sin or cos */
  60. if (i % 2)
  61. {
  62. sum += coeff[j] * cos(amp[j] * i);
  63. // RealIn[i] += coeff[j] * cos(amp[j] * i);
  64. }
  65. else
  66. {
  67. // RealIn[i] += coeff[j] * sin(amp[j] * i);
  68. sum += coeff[j] * sin(amp[j] * i);
  69. }
  70. }
  71. RealIn[i] = sum;
  72. }
  73. /* regular*/
  74. {
  75. unsigned NumBits; /* Number of bits needed to store indices */
  76. unsigned i, j, k, n;
  77. unsigned BlockSize, BlockEnd;
  78. double angle_numerator = 2.0 * DDC_PI;
  79. double tr, ti; /* temp real, temp imaginary */
  80. unsigned NumSamples = MAXSIZE;
  81. int InverseTransform = invfft;
  82. if (!IsPowerOfTwo(NumSamples))
  83. {
  84. // fprintf(
  85. // stderr,
  86. // "Error in fft(): NumSamples=%u is not power of two\n",
  87. // NumSamples);
  88. printf(
  89. "Error in fft(): NumSamples=%u is not power of two\n",
  90. NumSamples);
  91. // exit(1);
  92. // return;
  93. }
  94. if (InverseTransform)
  95. angle_numerator = -angle_numerator;
  96. NumBits = NumberOfBitsNeeded(NumSamples);
  97. /*
  98. ** Do simultaneous data copy and bit-reversal ordering into outputs...
  99. */
  100. for (i = 0; i < NumSamples; i++)
  101. {
  102. j = ReverseBits(i, NumBits);
  103. RealOut[j] = RealIn[i];
  104. // ImagOut[j] = (ImagIn == NULL) ? 0.0 : ImagIn[i];
  105. ImagOut[j] = ImagIn[i];
  106. }
  107. /*
  108. ** Do the FFT itself...
  109. */
  110. BlockEnd = 1;
  111. for (BlockSize = 2; BlockSize <= NumSamples; BlockSize <<= 1)
  112. {
  113. double delta_angle = angle_numerator / (double)BlockSize;
  114. double sm2 = sin(-2 * delta_angle);
  115. double sm1 = sin(-delta_angle);
  116. double cm2 = cos(-2 * delta_angle);
  117. double cm1 = cos(-delta_angle);
  118. double w = 2 * cm1;
  119. double ar[3], ai[3];
  120. double temp;
  121. for (i = 0; i < NumSamples; i += BlockSize)
  122. {
  123. ar[2] = cm2;
  124. ar[1] = cm1;
  125. ai[2] = sm2;
  126. ai[1] = sm1;
  127. for (j = i, n = 0; n < BlockEnd; j++, n++)
  128. {
  129. ar[0] = w * ar[1] - ar[2];
  130. ar[2] = ar[1];
  131. ar[1] = ar[0];
  132. ai[0] = w * ai[1] - ai[2];
  133. ai[2] = ai[1];
  134. ai[1] = ai[0];
  135. k = j + BlockEnd;
  136. tr = ar[0] * RealOut[k] - ai[0] * ImagOut[k];
  137. ti = ar[0] * ImagOut[k] + ai[0] * RealOut[k];
  138. RealOut[k] = RealOut[j] - tr;
  139. ImagOut[k] = ImagOut[j] - ti;
  140. RealOut[j] += tr;
  141. ImagOut[j] += ti;
  142. }
  143. }
  144. BlockEnd = BlockSize;
  145. }
  146. /*
  147. ** Need to normalize if inverse transform...
  148. */
  149. if (InverseTransform)
  150. {
  151. double denom = (double)NumSamples;
  152. for (i = 0; i < NumSamples; i++)
  153. {
  154. RealOut[i] /= denom;
  155. ImagOut[i] /= denom;
  156. }
  157. }
  158. }
  159. } //imc_repeat
  160. /*
  161. portDISABLE_INTERRUPTS();
  162. printf("RealOut:\r\n");
  163. portENABLE_INTERRUPTS();
  164. for (i = 0; i < MAXSIZE; i++)
  165. printf("%f \t", RealOut[i]);
  166. printf("\r\n");
  167. printf("ImagOut:\r\n");
  168. for (i = 0; i < MAXSIZE; i++)
  169. printf("%f \t", ImagOut[i]);
  170. printf("\r\n");
  171. */
  172. float real_sum = 0;
  173. float imag_sum = 0;
  174. for (i = 0; i < MAXSIZE; i++) {
  175. real_sum += RealOut[i];
  176. imag_sum += ImagOut[i];
  177. }
  178. portDISABLE_INTERRUPTS();
  179. printf("(OUT) %d, %d\r\n", (int)(real_sum*1000000), (int)(imag_sum*1000000));
  180. portENABLE_INTERRUPTS();
  181. // printf("RealOut: ");
  182. // for (i = 0; i < 3; i++)
  183. // printf("%f\t", RealOut[i]);
  184. // printf("\r\n");
  185. // printf("ImagOut: ");
  186. // for (i = 0; i < 3; i++)
  187. // printf("%f\t", ImagOut[i]);
  188. // printf("\r\n");
  189. }