I am trying to plot the fft of a set of data I have. These data form a nearly perfect sinc function. Here is the data of which I am trying to plot the fft: .
I know the fft of a sinc function should look like kind of a step function. However, the results I get are nowhere near that. Finding the fft in itself is super easy, but I think my mistake is when I try to compute the frequency axis. I have found several methods online, but so far I have not been able to make it work. Here is my code:
sampleRate = (max(xdata) - min(xdata))/length(xdata);
sampleN = length(xdata);
y = fft(ydata, sampleN);
Y = y.*conj(y)/sampleN;
freq = (0:1:length(Y)-1)*sampleRate/sampleNumber;
plot(freq, Y)
I have found pretty much all of that online and I understand pretty much none of it (which might be why it's not working...)
Zoom on what I get using that code:
It now seems to be working! This is what I get when I subtract the mean:
答案 0 :(得分:1)
你在这里看到的是零频率远远超过其他所有东西。使用plot(freq,Y,'o-')
绘图以证明您看到的形状只是两个样本之间的线性插值。
零频率是所有样本的总和。因为信号的平均值比幅度大很多,零频率使其他所有信息相形见绌。而且由于你正在绘制功率(DFT的绝对平方),这种差异会进一步增强。
有两个简单的解决方案:
使用对数y轴进行绘图:
plot(freq, Y)
set(gca,'yscale','log')
从信号中减去均值,删除零频率或缩放y轴(这些都或多或少等价):
y = fft(ydata-mean(ydata), sampleN);
或
y(1) = 0;
或
plot(freq, Y)
set(gca,'ylim',[0,max(Y(2:end))]);