Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

FFT from time domain data

Status
Not open for further replies.

NaikD

Mechanical
Nov 23, 2002
48
Hi,

I am getting wrong results and hence trying to figure out if I am computing FFT from time domain data right.
Here is the code for matlab (MATLAB 7.5.0; R2007b).
AllData.dat file contains time in first column and X moment in the 5th column.

load AllData.dat;
Time = AllData:),1);
Mx = AllData:),5);
% Define time step and sampling frequency
Ts=Time(2)-Time(1); % Time Step (sec)
fs=1/Ts; %frequency
% X Axis Moment (Mx) FFT Plots
Mx_fft = fft(Mx);
Mx_fft = abs(Mx_fft)/(length(Time)/2);
FHz_fft = [0:(length(Time)-1)]*(1/(Ts*length(Time))); % Cycles per seconds
%
figure;
subplot(1,1,1)
loglog(FHz_fft,Mx_fft,'k',[0.03906 0.159 5.30 1000],[0.003 0.003 0.1 0.1],'r');
grid on;
%Raw moment FFT plot
xlabel('Frequency (Hz)');
ylabel('Moment Mx (N-m)');
title('X Axis Moment Mx FFT');
grid on;
print -djpeg Mx-fft;

I know this is elementary for some of you. Thanks in advance.

Appreciate your help.

- Dipak
 
Replies continue below

Recommended for you

Here is one I posted in another thread:

%%%%%
clear all;

%
fs = 1000; % sampling freq.
T = 1/fs;
TM=3;
t = 0:T:TM;
x = cos(50*t)+2*sin(80*t+pi/3)+3.5*cos(155*t+pi/6)+4*cos(180*t+pi/4); % example signal
%
data=x;
N=length(data);
Fmax=fs;
delta_f=(1/(TM*Fmax))*Fmax;
f = 0:delta_f:Fmax;
F=fft(data);
figure
plot(f(1:N/10),abs(F(1:N/10))),grid
title('FFT'); xlabel('Frequency (Hz)');ylabel('Amplitude')
%%%%%

Change x to your signal.


The thread is here:



[peace]




[peace]
Fe
 
Thank you FeX32 !!

I will try this code.
Appreciate your help.

- Dipak
 
No problem. Hope it helps.
The important parameters are simply fs (sampling freq. ) and TM (total length of signal , seconds)
The rest is not signal specific.

[peace]
Fe
 
Also there is a scaling factor. Multiply by 2 to combine positive and negative frequencies. Divide by N to account for the length of the sample. Then when the peak magnitude of a sinusoid in is 1, the magnitude of the peak shown on the plot will be 1. (of course if you want rms, there is another factor sqrt2 to account for).

=====================================
Eng-tips forums: The best place on the web for engineering discussions.
 
A quick way to check whether you have the y scaling correct is to compute the RMS of both the time signal and the spectrum. They should be identical.

The amplitude scaling factors in Matlab are derivable from first principles, but they do look a bit like voodoo.

Also you should always use a test signal as Felix has done in order to make sure you have got the frequency scaling right.

Cheers

Greg Locock

SIG:please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
Felix? How did you know that's my middle name [smile] jk.

Anyways, good points about the magnitude/scaling guys.

[peace]
Fe
 
Nit picking time...

Note that the DC & nyquist terms don't need the multiplication by 2 (in Matlab anyway).

I'm with Greg though. Compare time domain and frequency domain calcs before declaring that you're happy. The next issue will probably be peak vs RMS.

- Steve
 
'e that the DC & nyquist terms don't need the multiplication by 2 (in Matlab anyway)."

Yes, that seems to be fundamental due to the folding. Not that I really worry about scaling the Nyquist term. If it isn't zero then it needs to be, for engineering.

(Sorry Fex, I got the impression you were someone I know elsewhere)


Cheers

Greg Locock

SIG:please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
No worries Greg. I don't mind, just curious. [smile]

[peace]
Fe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor