To perform FFT on time-series data, it is common practice to first window the data to reduce spectral leakage. MATLAB provides a variety of window functions such as Hamming, Hanning, Blackman, etc. for this purpose.
Here's an example code that demonstrates how to apply FFT to time-series data using a Hanning window:
main.m584 chars28 lines
In this code, we first generate a test signal x
that consists of a sine wave with frequency of 10 Hz sampled at 1000 Hz. We then apply a Hanning window win
to the signal by element-wise multiplication xw = x .* win'
.
Next, we perform FFT on the windowed signal xw
using fft
function. We normalize the output of FFT by dividing with the length of the signal N
. We then construct a frequency vector f
based on the sampling frequency Fs
and the length of the signal N
.
Finally, we plot the original signal and the magnitude of the FFT output against frequency. The FFT output is plotted only up to the Nyquist frequency (N/2
) since the second half of the output is a mirror image of the first half.
gistlibby LogSnag