P. James
P. James

Reputation: 11

MATLAB FFT plotting

How do I plot the x-axis of the FFT plot? I have the amplitude down, but the x-axis is giving me a hard time. Also, my magnitude is mirrored. How would I prevent this from happening?

Upvotes: 1

Views: 254

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112759

An N-point DFT of a discrete-time signal with sampling frequency fs gives frequencies 0, 1/fs, ..., (N-1)/fs. To have the zero frequency in the center of the plot you can apply fftshift to the DFT result, and then define the frequencies as ( -N/2:N/2-1 )/N*fs for N even, or ( -(N-1)/2:(N-1)/2 )/N*fs for N odd (this exploits the periodicity of the DFT).

Example

Signal formed by two sinusoids of different frequencies and amplitudes.

N = 400;
fs = 1000; % Hz
fc1 = 120; % Hz
fc2 = 260; % Hz
y = sin(2*pi*fc1*(0:N-1)/fs) + .5*sin(2*pi*fc2*(0:N-1)/fs); % example signal
Y = fft(y);
if ~mod(N,2) % even
    f_axis = ( -N/2:N/2-1 )/N*fs;
else % odd
    f_axis = ( -(N-1)/2:(N-1)/2 )/N*fs;
end
plot(f_axis, fftshift(abs(Y)));
grid    

enter image description here

Upvotes: 2

Related Questions