How to plot double sided amplitude spectrum after FFT?

10 visualizaciones (últimos 30 días)
Hamid Kilic
Hamid Kilic el 17 de En. de 2020
Editada: Hamid Kilic el 19 de En. de 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]);

Respuesta aceptada

David Goodmanson
David Goodmanson el 18 de En. de 2020
Editada: David Goodmanson el 18 de En. de 2020
Hi Hamid,
at the end of your code I added the following:
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.

Más respuestas (0)

Categorías

Más información sobre Spectral Measurements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by