- 2070 Magh · 12 marks
FFT algorithm and program
Short answer
The radix-2 FFT reorders samples in bit-reversed order and applies butterfly stages, computing the DFT in O(N log N) operations instead of N^2. For x(t)=2 sin(2 pi t)+sin(6 pi t) sampled at 8 Hz, the time domain shows a mixed waveform, while the frequency domain identifies peaks at 1 Hz amplitude 2 and 3 Hz amplitude 1; the Nyquist frequency is 4 Hz.
Write an algorithm and a program (C or Fortran or Matlab) for the fast Fourier transform. With a suitable example explain what parameters can be identified with the help of time domain and frequency domain.
Fast Fourier transform: idea
The DFT of samples is with , needing operations. The Cooley-Tukey radix-2 FFT (for ) splits the series into even and odd samples:
and repeats the split times. The cost is complex multiplications.
Algorithm
- Read (a power of 2) and the samples at interval .
- Reorder the samples in bit-reversed order.
- For stage length : compute ; for each block, for each form , , then set , .
- After the last stage holds . Amplitude , frequency .
x0 --o----o----o-- X0
x4 --o----| |
x2 --o----o | (butterflies:
x6 --o----| | a+Wb , a-Wb)
...
Program (C)
#include <stdio.h>
#include <math.h>
#include <complex.h>
#define PI 3.14159265358979323846
/* in-place radix-2 FFT, n must be a power of 2 */
void fft(double complex x[], int n)
{
for (int i = 1, j = 0; i < n; i++) { /* bit reversal */
int bit = n >> 1;
for (; j & bit; bit >>= 1) j ^= bit;
j ^= bit;
if (i < j) { double complex t = x[i]; x[i] = x[j]; x[j] = t; }
}
for (int len = 2; len <= n; len <<= 1) { /* butterfly stages */
double complex wlen = cexp(-2.0 * I * PI / len);
for (int i = 0; i < n; i += len) {
double complex w = 1.0;
for (int k = 0; k < len / 2; k++) {
double complex u = x[i + k];
double complex v = x[i + k + len / 2] * w;
x[i + k] = u + v;
x[i + k + len / 2] = u - v;
w *= wlen;
}
}
}
}
int main(void)
{
int n = 8; double fs = 8.0; double complex x[8];
for (int i = 0; i < n; i++) { /* 1 Hz (amp 2) + 3 Hz (amp 1) */
double t = i / fs;
x[i] = 2*sin(2*PI*1*t) + 1*sin(2*PI*3*t);
}
fft(x, n);
for (int k = 0; k < n; k++)
printf("f=%.1f Hz amp=%.3f\n", k*fs/n, 2*cabs(x[k])/n);
return 0;
}
Example: time domain and frequency domain
Signal sampled at 8 Hz for 1 s (). The program output is:
| (Hz) | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| amplitude | 0 | 2.000 | 0 | 1.000 | 0 |
(components at are the mirror image of .)
- Time domain shows the amplitude of vibration against time, peak value, duration, arrival time and decay of the signal, but the individual components are mixed together.
- Frequency domain shows the dominant frequencies (1 Hz and 3 Hz), their amplitudes (2 and 1) and phase. For a structure, the peak frequency is the natural frequency, so resonance with earthquake or machine frequency can be checked; noise is separated by filtering.
- Frequency resolution is Hz; the highest frequency identified is the Nyquist frequency Hz.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Discrete and Fast Fourier Transform · Computational Techniques in Civil Engineering, chapter 2
- Hilbert transform in frequency domain · Communication Systems, chapter 2
- Single tone DSB-FC AM in time and frequency domain · Communication Systems, chapter 2
- Angle modulation; single tone FM time and frequency domain · Communication Systems, chapter 3
- How fast is FFT; X(7) by DIT-FFT · Digital Signal Analysis and Processing, chapter 7 · asked 2×
All Computational Techniques in Civil Engineering chapter 2 questions →