Why is the output of FFTSHIFT shifted by one when used with the PSDFREQVEC function?

7 views (last 30 days)
I am using FFTSHIFT on my frequency spectrum and plotting it over the frequency range specified in PSDFREQVEC.
When I execute the following commands:
Fs = 1024; % Sampling frequency
t = (0:1/Fs:(1-1/Fs))'; % Time vector of 1 second
x = 1 + 3*sin(2*pi*t*200); % Create a sine wave of 200 Hz with dc component
NFFT = 2^(nextpow2(length(x)));% Use next highest power of 2 greater than or equal length(x) to calculate FFT.
FFTX = fftshift(fft(x,NFFT)); % Take fft, padding with zeros so that length(FFTX) is equal to NFFT
MX = abs(FFTX); % Take the magnitude of fft of x
MX = MX/length(x); % Scale the fft so that it is not a function of the length of x
FreqVec = psdfreqvec(NFFT,Fs,'negnpos');
plot(FreqVec,MX,'ro')
grid on
xlabel('Frequency (Hz)')
ylabel('Amplitude')
The frequency spectrum is plotted incorrectly.
The zero frequency component is returned at frequency 1Hz. FFTSHIFT causes the whole frequency spectrum to be shifted by one frequency bin.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This behavior is due to the different conventions used by PSDFREQVEC to specify the frequency vector. FFTSHIFT shifts frequency components 1:512 to 513:1024 and components 513:1024 to 1:512. The zero frequency component occurs at the 513th array index. PSDFREQVEC uses the convention that the zero frequency component is at the 512th index.
To work around this issue, avoid using PSDFREQVEC with FFTSHIFT. The following commands return a frequency vector that is consistent with the FFTSHIFT convention for plotting frequency spectra:
FreqVec2 = linspace(-Fs/2,Fs/2-1,NFFT);
figure,plot(FreqVec2,MX,'ro')
grid on
xlabel('Frequency (Hz)')
ylabel('Amplitude')
You can also use FFTSHIFT itself to construct the frequency vector. The frequency vector can be constructed as follows:
dftFreqVec = (0:NFFT-1)*Fs/NFFT;
shiftedFreqVec = fftshift(dftFreqVec);

More Answers (0)

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!