Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

find FFT of a set of data (Time, Amplitude) in MATLAB

Status
Not open for further replies.

fraizer

Newbie level 4
Joined
Apr 5, 2010
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
London
Activity points
1,356
I have tried doing the same for the set of data that I have got but with no use. I have got a data file that contains 2500 points of amplitude against time which I am trying to find the FFT of. Since its a 5MHz signal, I am expecting to see the peak magnitude at around 5MHz, but I am not! Been fiddling around for a couple of weeks now.

data=dlmread('inputm.txt','\t'); % to input the points in the file column 1 being time and column 2 being for the corresponding amplitude.
time=data:),1);
vdata=data:),2);
X=fft(vdata);
M=abs(X);
f0=5000000; % This is the fundamental frequency.
f=f0*(0:length(X)-1);
figure;
clf;
plot(f(1:(length(f))),M(1:(length(M))))

Can anyone help - Basically I have got data which when plot will give a signal - I need to find the FFT of this signal and verify that the peak magnitude is at 5MHz ( although its not expected for it to be exactly at 5 maybe 4.89 or so). Thanks.

Added after 9 minutes:

I have also tried this in order to get the amplitude spectrum:

clear;
clc;
s=importdata('echo1.txt');
z=s.data;
Y=z:),2);
y=fft(Y)
figure(1), plot(y)
figure(2),plot(y,'ro')
grid on
xlabel('Real number')
ylabel('Imaginary')
title('FFT of Signal')

n=length(y)
power=abs(y(1:floor(n/2))).^2
nyquist=1/2
freq=((1:n/2)/(n/2)*nyquist)*5000000
figure(3), plot(freq,power)

xlabel('Frequency')
ylabel('Power')
title('Power Spectrum of Signal')
grid on

amp=abs(y(1:floor(n/2)))
figure(4), plot(freq,amp)
xlabel('Frequency')
ylabel('Amplitude')
title('Amplitude Spectrum of Signal')
grid on

Added after 23 seconds:

I have also tried this in order to get the amplitude spectrum:

clear;
clc;
s=importdata('echo1.txt');
z=s.data;
Y=z:),2);
y=fft(Y)
figure(1), plot(y)
figure(2),plot(y,'ro')
grid on
xlabel('Real number')
ylabel('Imaginary')
title('FFT of Signal')

n=length(y)
power=abs(y(1:floor(n/2))).^2
nyquist=1/2
freq=((1:n/2)/(n/2)*nyquist)*5000000
figure(3), plot(freq,power)

xlabel('Frequency')
ylabel('Power')
title('Power Spectrum of Signal')
grid on

amp=abs(y(1:floor(n/2)))
figure(4), plot(freq,amp)
xlabel('Frequency')
ylabel('Amplitude')
title('Amplitude Spectrum of Signal')
grid on
 

Thank for your reply,

I have tried the above, the signal is actually at 5MHz, but I get an error message saying the lengths of the vectors are not the same. That is, Frequency and abs(X). This is because I have got 2500 data points and NFFT is 4096 which is the next power to the base 2 ...

Added after 43 seconds:

Thank for your reply,

I have tried the above, the signal is actually at 5MHz, but I get an error message saying the lengths of the vectors are not the same. That is, Frequency and abs(X). This is because I have got 2500 data points and NFFT is 4096 which is the next power to the base 2 ...
 

I have tried Zero padding the points as I have got only 2500 points and NFFT is 4096 which is the next power, I padded my single from 2,500 to 4096. However, I would still not get the right graph plotted and the frequency axes gives something 10 to the power of 16 which is crazy and it should not be. I think there is something wrong in normalizing the frequency and calculating the frequency ... etc
I am also unsure of the magnitude. I am sending you the data points file so that you could plot it normally column against the other and see how the signal should look like. I am also attaching you a piece of code that I have written, its easier and got me closer to what I expected .... maybe easier to debug !! its been 3 weeks trying to do this. It shouldn't be this hard, I know its something silly and i'll be kicking myself in the end. A second opinion always helps. Thanks.
clc;
clear;
data=dlmread('inputm.txt','\t'); % to input the points in the file column 1 being time and column 2 being for the corresponding amplitude.
time=data:),1);
vdata=data:),2);
L=length(vdata);
NFFT = 2^nextpow2(L) % Next power of 2 from length of vdata
pdata=zeros(NFFT,1);
pdata(1:length(vdata))=vdata;
X = fft(pdata,NFFT);
X = fftshift(pdata);
f = (-NFFT/2:NFFT/2-1)/NFFT;
Fs =1/(time(2)-time(1) );
f=Fs*(1:length(X))/NFFT;
frequency = f*Fs/2;
subplot(2,1,2);
plot(frequency,abs(X));

Added after 4 minutes:

this is the input data file
 

still not working, but I managed to sort it out :D Thanks for the help though much appreciated. with your help in Fs the whole thing was able to work ! Thanks once again.
 

First check: Is there a peak in whatever you are plotting. The x-axis of fft output is bins. To convert into frequency, you need to know the sampling frequency. Second, since its a 5MHz signal, have you downsampled it? All of these affect the final output. 5MHz signal requires at least 10Msps sampling, so 2500 is only 250us of data.. how many cycles can you expect in it...
 

Hi
I have the same problem. The PSD works fine but i get an error when ploting:
Error using ==> plot
Vectors must be the same lengths.
Error in ==>
plot(frequency,abs(X));
The length of frequency is 131072 but the length of X is 131036.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top