1

Assume I have a smooth function (represented as a vector):

x=0:0.1:1000; 
y=sin(2*x); 

and I want to find its periodicity - pi (or even its frequency -2 ) . I have tried the following:

nfft=1024;
Y=fft(y,nfft);
Y=abs(Y(1:nfft/2));
plot(Y);

but obviously it doesn't work (the plot does not give me a peak at "2" ).

Will you please help me find a way to find the value "2"?

Thanks in advance

Dr. John
  • 273
  • 3
  • 13
  • See [this question](http://stackoverflow.com/a/4371627/253056) for an explanation of how FFT bin index is related to frequency. – Paul R Apr 02 '17 at 16:10
  • Are you looking for the period of Harmonic Signals of unknown frequency or any signal in general? – Royi Apr 02 '17 at 20:40
  • I have $f(x)$ as a ( periodic ) numerical solution for some pde, and I want to find its periodicity using FFT. So I guess the second option you mentioned is correct. Thanks ! – Dr. John Apr 03 '17 at 08:42

2 Answers2

2

You have several issues here:

  1. You are computing the fft of x when your actual signal is y

  2. x should be in radians

  3. You need to define a sampling rate and use that to determine the frequency values along the x axis

So once we correct all of these things, we get:

samplingRate = 1000;   % Samples per period
nPeriods = 10;
nSamples = samplingRate * nPeriods;

x = linspace(0, 2*pi*nPeriods, nSamples);
y = sin(2*x);

F = fft(y);

amplitude = abs(F / nSamples);

f = samplingRate / nSamples*[0:(nSamples/2-1),-nSamples/2:-1];

plot(f, amplitude)

enter image description here

Suever
  • 64,497
  • 14
  • 82
  • 101
  • you are right, i meant fft(y) of course, and fixed it. I think I understand your answer, but assume now, that I have no control of the value of x. i.e.- I am given a function (as a vector) that is defined over another vector - x (and I want to find its frequency). How can I determine the sampling rate and use the code you just wrote? Thank you very much ! – Dr. John Apr 02 '17 at 16:17
  • 1
    @Dr.John To determine the frequency you *must* know the sampling rate of the input. – Suever Apr 02 '17 at 16:18
  • So I can't use fft to find the period, when I obtain the function ("input") as a numerical solution to a pde ? (i.e.- in my case, I want to use fourier transform to find the period of a function that is a solution to a very messy system of pdes. After numerically solving the problem, I obtain a vector x (which is my domain) and another vector f (the numerical solution)... How can I obtain the sampling rate in such a case? ) – Dr. John Apr 02 '17 at 16:22
  • @Dr.John In your case is the sampling rate simply `nSamples`? – Suever Apr 02 '17 at 18:03
  • I am not sure. Let's return to my first formulation and assume that the only thing I have is x=linspace(0,100,1000); y=sin(2*x); from this data I want to obtain a peak at "2". Is it possible to get it without changing "x" ? (i.e. writing something like fft(y), and getting "2" )? What is the sampling rate for such a case ? (when I don't have control of the x values) – Dr. John Apr 03 '17 at 08:07
  • @Dr.John I don't know what your sampling rate is. It *must* be known though. You cannot determine the period without it. If you don't have it you can only determine *relative* frequencies of two signals you receive. – Suever Apr 03 '17 at 13:08
2

In general, you can't use an FFT alone to find the period of a periodic signal. That's because an FFT does sinusoidal basis decomposition (or basis transform), and lots of non-sinusoidal waveforms (signals that look absolutely nothing like a sinewave or single sinusoidal basis vector) can be repeated to form a periodic function, waveform, or signal. Thus, it's quite possible for the frequency of a periodic function or waveform to not show up at all in an FFT result (it's called the missing fundamental problem).

Only in the case of a close or near sinusoidal signal will an FFT reliably report the reciprocal of the period of that periodic function.

There are lots of pitch detection/estimation algorithms. You can use an FFT as a sub-component of some composite methods, including cepstrums or cepstral analysis, and Harmonic Product Spectrum pitch detection methods.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153