matlabplotfftcontinuous-fourier

Matlab not plotting the exact fourier signal


I'm trying to plot a simple signal in fourier domain using Matlab. It's not plotting the correct signal. Here is my code:

clc;
clear all;
close all;
x=1:0.001:10;
f1=sin(2*pi*10*x);
f2=sin(2*pi*15*x);
f3=sin(2*pi*30*x);
f=f1+f2+f3;
plot(2*pi*x,fft(f1));
figure
plot(x,fft(f1));

I've expected a peak at 10 since the frequency is 10. But it is giving a peak at some other point

Here are the two plot images:

This is the image for plot(x,fft(f1))

This is the image for plot(x,fft(f1))

This is the image for plot(2*pi*x,fft(f1))

This is the image for plot(2*pi*x,fft(f1))

It is not showing the peak at 10.I even tried using abs(fft(f1)). No luck :/

Isn't it the correct way to plot signal in fourier domain?


Solution

  • The fft function assumes unit time step. In order to correct for non unit time step you need to define the frequency component based on the nyquist rate. The following code plots the magnitude of the fft with the correct frequency axis.

    clc;
    clear all;
    close all;
    x=1:0.001:10;
    %     ^ this is your sampling time step
    f1=sin(2*pi*10*x);
    f2=sin(2*pi*15*x);
    f3=sin(2*pi*30*x);
    
    % bounds of fourier transform based on sampling rate
    Fs = 1/0.001;
    ff = linspace(-Fs/2,Fs/2,numel(x));
    
    F1 = fftshift(fft(f1)/numel(x));
    F2 = fftshift(fft(f2)/numel(x));
    F3 = fftshift(fft(f3)/numel(x));
    
    figure();
    plot(ff,abs(F1),'-r'); hold on;
    plot(ff,abs(F2),'-b');
    plot(ff,abs(F3),'-k');
    

    enter image description here

    Edit: To answer OPs question in the comment.

    Speaking in normalized frequency units (assuming sampling rate of 1). The fft function returns the frequency response from 0 to 2*pi radians, but due to some signal processing properties and the way that discrete signals are interpreted when performing an FFT, the signal is actually periodic so the pi to 2*pi section is identical to the -pi to 0 section. To display the plot with the DC component (0 frequency) in the center we use fftshift which does a circular shift equal to 1/2 the length of the signal on the data returned by fft. Before you take the ifft make sure you use ifftshift to put it back in the right place.

    Edit2: The normalization term (/numel(x)) is necessary to estimate the continuous time fourier transform using the discrete fourier transform. I don't remember the precise mathematical reason off the top of my head but the examples in the MATLAB documentation also imply the necessity of this normalization.

    Edit 3: The original link that I had is down. I may come back to add a more detailed answer but in the mean time I definitely recommend that anyone interested in understanding the relationship between the fundamentals of the FS, FT, DTFT, and DFT watch Professor Oppenheim's hilariously old, but amazingly informative and straightforward lectures on MIT OpenCourseWare.