# How to plot double sided amplitude spectrum after FFT?

14 views (last 30 days)
Hamid Kilic on 17 Jan 2020
Edited: Hamid Kilic on 19 Jan 2020
Hey everybody. I have this piece of code and I would like to plot the amplitude spectrum symmetrically around the y axis. But I also want to show the negative values. Any help is appreciated and thanks in advance.
N = 512;
periods = 2;
dx = 2*periods*pi/N;
x = -periods*pi:dx:periods*pi-dx;
c = zeros(1,13);
y = zeros(size(x));
for n = 1:2:25
c(n) = 4/pi/n;
y = y + c(n) * sin(n*x);
end
c = fft(y);
mag = abs(c);
mag = mag/N;
del = 1/periods;
f = -N/periods/2:del:N/periods/2-del;
amp = [mag(1) mag(2:N/2)+mag(end:-1:N/2+2)];
plot(f(N/2+1:end), amp,'.b');
xlim([-30 30]);

David Goodmanson on 18 Jan 2020
Edited: David Goodmanson on 18 Jan 2020
Hi Hamid,
figure(2)
plot(f,fftshift(cmag),'o');
grid on
xlim([-30 30]);
figure(3)
covN = c/N;
plot(f,real(fftshift(covN)),f,imag(fftshift(covN)),'o');
grid on
xlim([-30 30]);
The first plot uses the entire frequency array and uses fftshift to put the cmag values at the appropriate location.
If you want to use both positive and negative frequencies, i'ts more in the spirit of the thing to plot the real and imaginary parts instead of the absolute value. When this is done you can see that the imaginary part is an odd function of frequency. The real part is an even function, but since you are transforming a bunch of sines, the real part comes out zero as shown on the plot.