To find the frequency components in the data and make a frequency plot

I have a data which is a frequency modulated signal. I want to make a matlab code to find the frequency components in it using Fourier transform method and make a frequency plot? I tried fft(data(:,2)) but most of the time it is giving me wrong answers. I am attaching the data. The first column is time and the second column is the signal.

 Accepted Answer

Use the attached file with the following code:
filename='Test_Sample_edited.txt';
A=load(filename);
time=A(:,1);
signal=A(:,2);
h1=spectrum.welch;
set(h1,'Windowname','Hann');
Fs=1000;
set(h1,'OverlapPercent',66.7);
set(h1,'SegmentLength',2048);
myPsd=psd(h1,A(:,2)-mean(A(:,2)),'Fs',Fs)
semilogx(myPsd.Frequencies,myPsd.Data);xlabel('Frequency(rad/sec)');

5 Comments

Isn't it possible to use Fourier transform method (fft)? This code is working for this specific data but when I run for other similar data I get error:
Error using welchparse>segment_info (line 220)
The length of the segments cannot be greater than the length of the input signal.
Error in welchparse (line 32)
[L,noverlap,win] = segment_info(M,win,noverlap);
Error in welch (line 42)
welchparse(x,esttype,varargin{:});
Error in pwelch (line 158)
[varargout{1:nargout}] = welch(x,esttype,varargin{:});
Error in spectrum.welch/thispsd (line 163)
[Pxx, W] = pwelch(x,...
Error in spectrum.abstractspectrum/psd (line 132)
[Pxx, W] = thispsd(this,x,opts); % Produces spectrum with pos freq only!
Error in FourierTransform (line 17)
myPsd=psd(h1,A(:,2)-mean(A(:,2)),'Fs',Fs)
If I can plot the Fourier transform of the same data, it would be easy to know the frequency components in the data. Thank you.
But this way you also obtain the frequency components of the data. Have you tried it?
Yes I tried. It work on the same data but when I try to run on another data it is giving error message (as it is in my previous comment). How can I make it to run for all data. All my data is in the same format. Only the values and size of the file changes.
It will work if you change the following line accordingly:
set(h1,'SegmentLength',2048);
For your initial data, it is 2048 but if other data is smaller than this, than you have to change the value to 1024 or 512, to a reasonable value.

Sign in to comment.

More Answers (0)

Asked:

on 4 Jan 2018

Commented:

on 5 Jan 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!