fftr.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* fftr.c
  2. *
  3. * FFT of Real Valued Sequence
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x[], sine[];
  10. * int m;
  11. *
  12. * fftr( x, m, sine );
  13. *
  14. *
  15. *
  16. * DESCRIPTION:
  17. *
  18. * Computes the (complex valued) discrete Fourier transform of
  19. * the real valued sequence x[]. The input sequence x[] contains
  20. * n = 2**m samples. The program fills array sine[k] with
  21. * n/4 + 1 values of sin( 2 PI k / n ).
  22. *
  23. * Data format for complex valued output is real part followed
  24. * by imaginary part. The output is developed in the input
  25. * array x[].
  26. *
  27. * The algorithm takes advantage of the fact that the FFT of an
  28. * n point real sequence can be obtained from an n/2 point
  29. * complex FFT.
  30. *
  31. * A radix 2 FFT algorithm is used.
  32. *
  33. * Execution time on an LSI-11/23 with floating point chip
  34. * is 1.0 sec for n = 256.
  35. *
  36. *
  37. *
  38. * REFERENCE:
  39. *
  40. * E. Oran Brigham, The Fast Fourier Transform;
  41. * Prentice-Hall, Inc., 1974
  42. *
  43. */
  44. #include <math.h>
  45. static short n0 = 0;
  46. static short n4 = 0;
  47. static short msav = 0;
  48. extern double PI;
  49. #ifdef ANSIPROT
  50. extern double sin ( double );
  51. static int bitrv(int, int);
  52. #else
  53. double sin();
  54. static int bitrv();
  55. #endif
  56. fftr( x, m0, sine )
  57. double x[];
  58. int m0;
  59. double sine[];
  60. {
  61. int th, nd, pth, nj, dth, m;
  62. int n, n2, j, k, l, r;
  63. double xr, xi, tr, ti, co, si;
  64. double a, b, c, d, bc, cs, bs, cc;
  65. double *p, *q;
  66. /* Array x assumed filled with real-valued data */
  67. /* m0 = log2(n0) */
  68. /* n0 is the number of real data samples */
  69. if( m0 != msav )
  70. {
  71. msav = m0;
  72. /* Find n0 = 2**m0 */
  73. n0 = 1;
  74. for( j=0; j<m0; j++ )
  75. n0 <<= 1;
  76. n4 = n0 >> 2;
  77. /* Calculate array of sines */
  78. xr = 2.0 * PI / n0;
  79. for( j=0; j<=n4; j++ )
  80. sine[j] = sin( j * xr );
  81. }
  82. n = n0 >> 1; /* doing half length transform */
  83. m = m0 - 1;
  84. /* fftr.c */
  85. /* Complex Fourier Transform of n Complex Data Points */
  86. /* First, bit reverse the input data */
  87. for( k=0; k<n; k++ )
  88. {
  89. j = bitrv( k, m );
  90. if( j > k )
  91. { /* executed approx. n/2 times */
  92. p = &x[2*k];
  93. tr = *p++;
  94. ti = *p;
  95. q = &x[2*j+1];
  96. *p = *q;
  97. *(--p) = *(--q);
  98. *q++ = tr;
  99. *q = ti;
  100. }
  101. }
  102. /* fftr.c */
  103. /* Radix 2 Complex FFT */
  104. n2 = n/2;
  105. nj = 1;
  106. pth = 1;
  107. dth = 0;
  108. th = 0;
  109. for( l=0; l<m; l++ )
  110. { /* executed log2(n) times, total */
  111. j = 0;
  112. do
  113. { /* executed n-1 times, total */
  114. r = th << 1;
  115. si = sine[r];
  116. co = sine[ n4 - r ];
  117. if( j >= pth )
  118. {
  119. th -= dth;
  120. co = -co;
  121. }
  122. else
  123. th += dth;
  124. nd = j;
  125. do
  126. { /* executed n/2 log2(n) times, total */
  127. r = (nd << 1) + (nj << 1);
  128. p = &x[ r ];
  129. xr = *p++;
  130. xi = *p;
  131. tr = xr * co + xi * si;
  132. ti = xi * co - xr * si;
  133. r = nd << 1;
  134. q = &x[ r ];
  135. xr = *q++;
  136. xi = *q;
  137. *p = xi - ti;
  138. *(--p) = xr - tr;
  139. *q = xi + ti;
  140. *(--q) = xr + tr;
  141. nd += nj << 1;
  142. }
  143. while( nd < n );
  144. }
  145. while( ++j < nj );
  146. n2 >>= 1;
  147. dth = n2;
  148. pth = nj;
  149. nj <<= 1;
  150. }
  151. /* fftr.c */
  152. /* Special trick algorithm */
  153. /* converts to spectrum of real series */
  154. /* Highest frequency term; add space to input array if wanted */
  155. /*
  156. x[2*n] = x[0] - x[1];
  157. x[2*n+1] = 0.0;
  158. */
  159. /* Zero frequency term */
  160. x[0] = x[0] + x[1];
  161. x[1] = 0.0;
  162. n2 = n/2;
  163. for( j=1; j<=n2; j++ )
  164. { /* executed n/2 times */
  165. si = sine[j];
  166. co = sine[ n4 - j ];
  167. p = &x[ 2*j ];
  168. xr = *p++;
  169. xi = *p;
  170. q = &x[ 2*(n-j) ];
  171. tr = *q++;
  172. ti = *q;
  173. a = xr + tr;
  174. b = xi + ti;
  175. c = xr - tr;
  176. d = xi - ti;
  177. bc = b * co;
  178. cs = c * si;
  179. bs = b * si;
  180. cc = c * co;
  181. *p = ( d - bs - cc )/2.0;
  182. *(--p) = ( a + bc - cs )/2.0;
  183. *q = -( d + bs + cc )/2.0;
  184. *(--q) = ( a - bc + cs )/2.0;
  185. }
  186. return(0);
  187. }
  188. /* fftr.c */
  189. /* Bit reverser */
  190. int bitrv( j, m )
  191. int j, m;
  192. {
  193. register int j1, ans;
  194. short k;
  195. ans = 0;
  196. j1 = j;
  197. for( k=0; k<m; k++ )
  198. {
  199. ans = (ans << 1) + (j1 & 1);
  200. j1 >>= 1;
  201. }
  202. return( ans );
  203. }