How to plot double sided amplitude spectrum after FFT?
11 次查看(过去 30 天)
显示 更早的评论
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]);
0 个评论
采纳的回答
David Goodmanson
2020-1-18
编辑:David Goodmanson
2020-1-18
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.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spectral Measurements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!