FFT of a signal based on simulation data
7 次查看(过去 30 天)
显示 更早的评论
Hi all,
I'm a matlab newbie, and have been searching for an answer to this for a few days with no success. I've got some data from another program (CST microwave) and I need to FFT it. Here's the portion of my code that should be giving me a result:
fs = length(tr)/tr(end);
N = 1024;
Y = fft(xr,N);
k = [-N/2:N/2-1];
plot(k*fs/N,abs(fftshift(Y)))
However, the result I get seems wrong. Are there any obvious mistakes in this code? I'd be grateful for any insight anyone might have. Fs is the sampling frequency, and I'm trying to get it by using the simulation data's time vector (i.e: the signal I need to FFT lasts 40ns, and there are 4001 points).
Thanks for your time.
0 个评论
采纳的回答
Dr. Seis
2012-1-17
I usually do:
df = fs / N;
Nyq = fs / 2;
f = -Nyq : df : Nyq-df;
plot(f , abs(fftshift(Y)));
But that looks like the same thing you are doing. If you apply the same thing to an odd number of traces (i.e., N = 4001), then that flow will have to change a bit.
f = -Nyq : df : Nyq; % for odd N
Also, if tr(1) = 0, then:
fs = (length(tr)-1)/tr(end);
And if tr(1) ~= 0, then:
fs = (length(tr)-1)/(tr(end)-tr(1));
0 个评论
更多回答(2 个)
Dr. Seis
2012-1-17
A couple of helpful tips:
ti = 0 : 0.01 : 39;
Will do the same thing as:
ti = [];
j=1;
for i=1:0.01:40;
ti(j) = i-1;
j=j+1;
end
In order to get a closer look at the frequencies that contribute the most to your timeseries, as well as taking into consideration that the magnitude of your amplitude spectrum is symmetric, you could follow your "plot" with:
xlim([0 10]);
To only look at that portion.
Also, I should mention that your amplitudes are off by a factor of 1/fs. Your "plot" function should be:
plot(k*fs/N,abs(fftshift(Y))/fs);
This is because the FFT assumes your sample rate is unity (1 sample per second) when it is calculating the discrete integral. Let me know if you want more information about why this is. It is necessary if you plan to use the actual value of the amplitude for further calculations, but if you plan on only looking at the shape of your frequency spectrum it is not necessary.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!