Need help with an FFT function
1 view (last 30 days)
Show older comments
Hello everyone.
My problem is such- i have 2x1 matrix with a column of times and another of function which is time dependent. How do i correctly convert the time dependent function to the frequency domain using FFT? What i've done is to convert the function and than, using the fact that the sampeling frequency is given to me to construct a frequncy vector matching the length of the function column. I get a fairly reasonable plot, however i am not entierly sure my answer is correct, especially because i haven't used the time column, which is given to me
3 Comments
John D'Errico
on 16 Mar 2017
As Adam said, trying to guess what you did wrong is impossible. That means we would need to instead guess exactly what you want to do, and then write it completely for you.
By showing what you have done, it is far easier to correct what you did. This helps those who might be willing and able to help you.
Answers (1)
Star Strider
on 16 Mar 2017
See if this provides a better result:
Fs = 2000; % 1/sec
Fn = Fs/2; % Nyquist Frequency
L=length(Q2(:,2)); %length of the function column
f = linspace(0, fix(L/2)+1)*Fn; % frequency vector
Iv = 1:length(f); % Index Vector
y1=fft(Q2(:,2))/L; % fft on the function
Y1=abs(y1);
figure(1)
plot(f,Y1(Iv)*2)
grid
If it is difficult to see frequencies other than the 0 Hz d-c- offset value, subtract the mean of your signal first:
Fs = 2000; % 1/sec
Fn = Fs/2; % Nyquist Frequency
Q2(:,2) = Q(:,2)-mean(Q(:,2);
L=length(Q2(:,2)); %length of the function column
. . . THE REST OF YOUR CODE IS UNCHANGED . . .
2 Comments
Star Strider
on 16 Mar 2017
My pleasure.
The Fourier transform is calculated as the sum of sine and cosine functions over the lenght of the time-domain vector, so the amplitude of the individual frequencies are the sum of these. Normalising by the length of the vector corrects for this. (Any textbook on digital signal processing discusses this in detail.)
In addition, the Fourier transform calculates a ‘double-sided’ (positive and negative frequencies) transform, with the energies of the individual frequencies divided equally between each positive and negative frequency. Because of this, in plotting a one-sided Fourier transform, it is necessary to multiply the amplitudes by 2 to give an accurate estimate of the amplitudes of the constituent frequencies in the signal, as I did in the code I posted.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!