Add zero padding to fft for accurate estimation
Show older comments
Hi,
I wonder why in this line of code(bolded), there is the length divided by 2+1?
Fs = 1e3;
t = 0:0.001:1-0.001;
x = cos(2*pi*100*t)+sin(2*pi*202.5*t);
xdft = fft(x);
xdft = xdft(1:length(x)/2+1);
xdft = xdft/length(x);
xdft(2:end-1) = 2*xdft(2:end-1);
freq = 0:Fs/length(x):Fs/2;
Accepted Answer
More Answers (1)
Returning the one-sided frequency spectrum instead of two-sided (the zero frequency/DC component is in the middle).
Fs = 1e3;
t = 0:0.001:1-0.001;
x = cos(2*pi*100*t)+sin(2*pi*202.5*t);
xdft = fft(x);
subplot(2,1,1)
plot(abs(xdft))
tmp=xdft;
title('Two Sided DFT Magnitude vs Index')
xdft = xdft(1:length(x)/2+1);
subplot(2,1,2)
plot(abs(xdft))
title('One Sided DFT Magnitude vs Index')
Nota Bene: the second of the two peaks is roughly half in magnitude of the first (not exact because binning isn't exact for 202.5 Hz so energy is spread over several bins as the shape shows). By happenstance of the sampling rate and length chosen, the frequency and bin number are equivalent for the first half.
You can see the symmetry also if look at the values
L=length(tmp); M=L/2+1;
fprintf('%0.8f ',real(tmp(M-4:M+4)))
You can observe the values are symmetric about the midpoint, M.
ADDENDUM:
"Y - Frequency domain representation returned as a vector, matrix, or multidimensional array.
...
If X is real, then Y is conjugate symmetric, and the number of unique points in Y is ceil((n+1)/2)"
Categories
Find more on Single-Rate Filters in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!