FFT of a signal based on simulation data
7 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Dr. Seis
on 17 Jan 2012
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 Comments
More Answers (2)
Dr. Seis
on 17 Jan 2012
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!