fft time window freqeuncy in matlab

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.m
% Generate test signal
Fs = 1000;                      % Sampling frequency
t = 0:1/Fs:1-1/Fs;              % Time vector
f = 10;                         % Signal frequency
x = sin(2*pi*f*t);              % Test signal

% Apply Hanning window
win = hann(length(x));
xw = x .* win';

% Perform FFT
N = length(xw);
X = fft(xw)/N;
f = Fs*(0:N/2-1)/N;

% Plot results
subplot(2,1,1)
plot(t, x)
xlabel('Time (s)')
ylabel('Amplitude')
title('Test Signal')

subplot(2,1,2)
plot(f, abs(X(1:N/2)))
xlabel('Frequency (Hz)')
ylabel('Magnitude')
title('FFT of Test Signal (with Hanning Window)')
584 chars
28 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