5 /*****************************************************************************
6 ************************** Start of Subroutines *****************************
7 *****************************************************************************/
9 /*****************************************************************************
10 * FFT computes fast fourier transform of BLKSIZE samples of data *
11 * uses decimation-in-frequency algorithm described in "Digital *
12 * Signal Processing" by Oppenheim and Schafer, refer to pages 304 *
13 * (flow graph) and 330-332 (Fortran program in problem 5) *
14 * to get the inverse fft, change line 20 from *
15 * w_imag[L] = -sin(PI/le1); *
17 * w_imag[L] = sin(PI/le1); *
19 * required constants: *
20 * #define PI 3.14159265358979 *
21 * #define BLKSIZE 1024 *
22 * #define LOGBLKSIZE 10 *
23 * #define BLKSIZE_S 256 *
24 * #define LOGBLKSIZE_S 8 *
26 *****************************************************************************/
28 #define LOGBLKSIZE_S 8
31 fft (x_real
, x_imag
, energy
, phi
, N
)
32 FLOAT x_real
[BLKSIZE
], x_imag
[BLKSIZE
], energy
[BLKSIZE
], phi
[BLKSIZE
];
38 static double w_real
[2][LOGBLKSIZE
], w_imag
[2][LOGBLKSIZE
];
41 double t_real
, t_imag
, u_real
, u_imag
;
45 memset ((char *) w_real
, 0, sizeof (w_real
)); /* preset statics to 0 */
46 memset ((char *) w_imag
, 0, sizeof (w_imag
)); /* preset statics to 0 */
48 for (L
= 0; L
< M
; L
++)
52 w_real
[0][L
] = cos (PI
/ le1
);
53 w_imag
[0][L
] = -sin (PI
/ le1
);
56 for (L
= 0; L
< M
; L
++)
60 w_real
[1][L
] = cos (PI
/ le1
);
61 w_imag
[1][L
] = -sin (PI
/ le1
);
76 fprintf (stderr
, "Error: Bad FFT Size in subs.c\n");
82 for (L
= 0; L
< MM1
; L
++)
88 for (j
= 0; j
< le1
; j
++)
90 for (i
= j
; i
< N
; i
+= le
)
93 t_real
= x_real
[i
] + x_real
[ip
];
94 t_imag
= x_imag
[i
] + x_imag
[ip
];
95 x_real
[ip
] = x_real
[i
] - x_real
[ip
];
96 x_imag
[ip
] = x_imag
[i
] - x_imag
[ip
];
100 x_real
[ip
] = x_real
[ip
] * u_real
- x_imag
[ip
] * u_imag
;
101 x_imag
[ip
] = x_imag
[ip
] * u_real
+ t_real
* u_imag
;
104 u_real
= u_real
* w_real
[MP
][L
] - u_imag
* w_imag
[MP
][L
];
105 u_imag
= u_imag
* w_real
[MP
][L
] + t_real
* w_imag
[MP
][L
];
108 /* special case: L = M-1; all Wn = 1 */
109 for (i
= 0; i
< N
; i
+= 2)
112 t_real
= x_real
[i
] + x_real
[ip
];
113 t_imag
= x_imag
[i
] + x_imag
[ip
];
114 x_real
[ip
] = x_real
[i
] - x_real
[ip
];
115 x_imag
[ip
] = x_imag
[i
] - x_imag
[ip
];
118 energy
[i
] = x_real
[i
] * x_real
[i
] + x_imag
[i
] * x_imag
[i
];
119 if (energy
[i
] <= 0.0005)
125 phi
[i
] = atan2 ((double) x_imag
[i
], (double) x_real
[i
]);
126 energy
[ip
] = x_real
[ip
] * x_real
[ip
] + x_imag
[ip
] * x_imag
[ip
];
130 phi
[ip
] = atan2 ((double) x_imag
[ip
], (double) x_real
[ip
]);
132 /* this section reorders the data to the correct ordering */
134 for (i
= 0; i
< NM1
; i
++)
138 /* use this section only if you need the FFT in complex number form *
139 * (and in the correct ordering) */
142 x_real
[j
] = x_real
[i
];
143 x_imag
[j
] = x_imag
[i
];
146 /* reorder the energy and phase, phi */
148 energy
[j
] = energy
[i
];