0
votes

I'm trying to find the maximum frequency of a periodic signal in Matlab and as i know when you convert a periodic signal to the frequency spectrum you get only delta functions however i get a few curves between the produced delta functions. Here is the code :

 t=[-0.02:10^-3:0.02];
 s=5.*(1+cos(2*pi*10*t)).*cos(2*pi*100*t);
 figure, subplot(211),    plot(t,s);
 y=fft(s);
 subplot(212), plot(t,y);

enter image description here

1
Shouldn't you use plot(t, abs(y))? also, your time vector is too short to see the effect of the smaller frequency. Try for example t=[-0.02:10^-3:0.2];Luis Mendo
I did but i still get small curves not delta functions exclusivelyuser9994319
@bill because A) the transform is discrete, and B) you're connecting the points with lines. Mostly A though.Mad Physicist
Also, t is not really the axis you want to be plotting y against.Mad Physicist
@LuisMendo, you meant t=[-0.2:10^-3:0.2]; ? Right? Because with t=[-0.02:10^-3:0.2];, we have shifted the whole plot.Duck Dodgers

1 Answers

1
votes

Here is a code-snippet to help you understand how to get the frequency-spectrum using fft in matlab.

Things to remember are:

  1. You need to decide on a sampling frequency, which should be high enough, as per the Nyquist Criterion (You need the number of samples, at least more than twice the highest frequency or else we will have aliasing). That means, fs in this example cannot be below 2 * 110. Better to have it even higher to see a have a better appearance of the signal.

  2. For a real signal, what you want is the power-spectrum obtained as the square of the absolute of the output of the fft() function. The imaginary part, which contains the phase should contain nothing but noise. (I didn't plot the phase here, but you can do this to check for yourself.)

  3. Finally, we need to use fftshift to shift the signal such that we get the mirrored spectrum around the zero-frequency.
  4. The peaks would be at the correct frequencies. Now considering only the positive frequencies, as you can see, we have the largest peak at 100Hz and two further lobs around 100Hz +- 10Hz i.e. 90Hz and 110Hz.

Apparently, 110Hz is the highest frequency, in your example.

The code:

fs = 500; % sampling frequency - Should be high enough! Remember Nyquist!

 t=[-.2:1/fs:.2];
 s= 5.*(1+cos(2*pi*10*t)).*cos(2*pi*100*t);
 figure, subplot(311),    plot(t,s);

 n = length(s);
 y=fft(s);


 f = (0:n-1)*(fs/n); % frequency range
 power = abs(y).^2/n;
 subplot(312), plot(f, power);

 Y = fftshift(y);
 fshift = (-n/2:n/2-1)*(fs/n); % zero-centered frequency range

 powershift = abs(Y).^2/n;
 subplot(313), plot(fshift, powershift);

The output plots:

  1. The first plot is the signal in the time domain
  2. The signal in the frequency domain
  3. The shifted fft signal enter image description here