# How to find the frequency of a signal using the FFT: Matlab Tutorial

|In signal processing, most of time we acquire a signal which frequency is unknown. For example, if we are taking EEG from an Emotiv headset, we can be unable to predict the frequency is present in our EEG waveform. However, we can take FFT to determine the frequency in our signal.

Sometimes, we may have an unknown frequency of our signal and we need to remove noise. We can’t do this unless we know the frequency of the original signal and the frequency of the noise signal. The Fast Fourier Transform is one of the tool to find the signal frequency. After this, we can apply a suitable filter which we can usem such as high pass, low pass, band pass or band stop filter.

For instance, in the following figure, the signal (below) comprises of many frequencies (above) and by using the FFT we can separate those frequencies.

## Example: find out the frequency of a signal by using Matlab

Here, we will take an example to make you understand the use of the FFT. Let’s imagine we have a signal and we don’t know its sampling frequency. For simplicity and randomly, we take the sampling frequency as 1000 (sampes/second).

According to Nyquist theorem, it should be more than twice of the signal frequency. As we don’t know signal frequency, as a guess we take 1000. Then we create a time vector *t*.

Fs=1000; t=0:1/Fs:1-1/Fs;

In the following line, we create 3 sinusoidal signals. In this case you may have import it from your circuit or device.

s1=0.3 *sin (2*pi*30*t); s2=0.7 * sin (2*pi*120*t); s3=1 * sin (2*pi*200*t); s=s1+s2+s3;

The vector *s* is actually the sum of the 3 signals with different frequency and amplitude.

If we have signals whose frequency is exact power of two, our process can be speedy. But in our case our sampling frequency is 1000 which is not exact power of 2. So by using the Matlab function, *nextpow2*, we calculate another frequency of 1024 samples/second, which is an exact power of 2.

%fft n1=nextpow2(Fs); n=2^n1 f=fft(s,n); f=f(1:n/2);

As our sine signal lies on both side of origin, we divide it into two parts and get one part, because on both sides, the signal is the same.

l=length (s); m=abs(2*f/l);

*Abs *function is used to find the magnitude/amplitude of the signal.

plot(m);

## Complete Code

Fs=1000; t=0:1/Fs:1-1/Fs; s1=0.3 *sin (2*pi*30*t); s2=0.7 * sin (2*pi*120*t); s3=1 * sin (2*pi*200*t); s=s1+s2+s3; n1=nextpow2(Fs); n=2^n1; f=fft(s,n); f=f(1:n/2); l=length (s); m=abs(2*f/l); plot(m) xlabel('frequency') % x-axis label ylabel('amlitude') % y-axis label

## Conclusion

You can view in attached picture the frequency and amplitude are the same as were in our produced signal.