Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (2024)

Key focus: Learn how to plot FFT of sine wave and cosine wave using Matlab. Understand FFTshift. Plot one-sided, double-sided and normalized spectrum.

Introduction

Numerous texts are available to explain the basics of Discrete Fourier Transform and its very efficient implementation – Fast Fourier Transform (FFT). Often we are confronted with the need to generate simple, standard signals (sine, cosine, Gaussian pulse, squarewave, isolated rectangular pulse, exponential decay, chirp signal) for simulation purpose. I intend to show (in a series of articles) how these basic signals can be generated in Matlab and how torepresent them in frequency domain using FFT. If you are inclined towards Python programming, visit here.

This article is part of the following books
Digital Modulations using Matlab : Build Simulation Models from Scratch, ISBN: 978-1521493885
Wireless communication systems in Matlab ISBN: 979-8648350779
All books available in ebook (PDF) and Paperback formats

Sine Wave

In order to generate a sine wave in Matlab, the first step is to fix the frequency Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (1) of the sine wave. For example, I intend to generate a f=10Hz sine wave whose minimum and maximum amplitudes are Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (2) and Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (3) respectively. Now that you have determined the frequency of the sinewave, the next step is to determine the sampling rate. Matlab is a software that processes everything in digital. In order to generate/plot a smooth sine wave, the sampling rate must be far higher than the prescribed minimum required sampling rate which is at least twice the frequency Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (4) – as per Nyquist Shannon Theorem. A oversampling factorof Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (5) is chosen here – this is to plot a smooth continuous-like sine wave (If this is not the requirement, reduce the oversampling factor to desired level). Thus the sampling rate becomes Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (6). If a phase shift is desired for the sine wave, specify it too.

f=10; %frequency of sine waveoverSampRate=30; %oversampling ratefs=overSampRate*f; %sampling frequencyphase = 1/3*pi; %desired phase shift in radiansnCyl = 5; %to generate five cycles of sine wavet=0:1/fs:nCyl*1/f; %time basex=sin(2*pi*f*t+phase); %replace with cos if a cosine wave is desiredplot(t,x);title(['Sine Wave f=', num2str(f), 'Hz']);xlabel('Time(s)');ylabel('Amplitude');

Representing in Frequency Domain

Representing the given signal in frequency domain is done via Fast Fourier Transform (FFT) which implements Discrete Fourier Transform (DFT) in an efficient manner. Usually, power spectrum is desired for analysis in frequency domain. In a power spectrum, power of each frequency component of the given signal is plotted against their respective frequency. The command Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (8) computes the Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (9)-point DFT. The number of points – Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (10) – in the DFT computation is taken as power of (2) for facilitating efficient computation with FFT.A value of Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (11) is chosen here. It can also be chosen as next power of 2 of the length of the signal.

Different representations of FFT:

Since FFT is just a numeric computation of Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (12)-point DFT, there are many ways to plot the result.

1. Plotting raw values of DFT:

The x-axis runs from Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (13) to Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (14) – representing Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (15) sample values. Since the DFT values are complex, the magnitude of the DFT Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (16) is plotted on the y-axis. From this plot we cannot identify the frequency of the sinusoid that was generated.

NFFT=1024; %NFFT-point DFT X=fft(x,NFFT); %compute DFT using FFT nVals=0:NFFT-1; %DFT Sample points plot(nVals,abs(X)); title('Double Sided FFT - without FFTShift'); xlabel('Sample points (N-point DFT)') ylabel('DFT Values');

2. FFT plot – plotting raw values against Normalized Frequency axis:

In the next version of plot, the frequency axis (x-axis) is normalized to unity. Just divide the sample index on the x-axis by the length Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (18) of the FFT. This normalizes the x-axis with respect to the sampling rate Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (19). Still, we cannot figure out the frequency of the sinusoid from the plot.

NFFT=1024; %NFFT-point DFT X=fft(x,NFFT); %compute DFT using FFT nVals=(0:NFFT-1)/NFFT; %Normalized DFT Sample points plot(nVals,abs(X)); title('Double Sided FFT - without FFTShift'); xlabel('Normalized Frequency') ylabel('DFT Values');

3. FFT plot – plotting raw values against normalized frequency (positive & negative frequencies):

As you know, in the frequency domain, the values take up both positive and negative frequency axis. In order to plot the DFT values on a frequency axis with both positive and negative values, the DFT value at sample index Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (21) has to be centered at the middle of the array. This is done by using Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (22) function in Matlab. The x-axis runs from Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (23) to Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (24) where the end points are the normalized ‘folding frequencies’ with respect to the sampling rate Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (25).

NFFT=1024; %NFFT-point DFT X=fftshift(fft(x,NFFT)); %compute DFT using FFT fVals=(-NFFT/2:NFFT/2-1)/NFFT; %DFT Sample points plot(fVals,abs(X)); title('Double Sided FFT - with FFTShift'); xlabel('Normalized Frequency') ylabel('DFT Values');

4. FFT plot – Absolute frequency on the x-axis Vs Magnitude on Y-axis:

Here, the normalized frequency axis is just multiplied by the sampling rate. From the plot below we can ascertain that the absolute value of FFT peaks at Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (27) and Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (28) . Thus the frequency of the generated sinusoid is Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (29). The small side-lobes next to the peak values at Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (30) and Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (31) are due to spectral leakage.

NFFT=1024; X=fftshift(fft(x,NFFT)); fVals=fs*(-NFFT/2:NFFT/2-1)/NFFT; plot(fVals,abs(X),'b'); title('Double Sided FFT - with FFTShift'); xlabel('Frequency (Hz)') ylabel('|DFT Values|');

5. Power Spectrum – Absolute frequency on the x-axis Vs Power on Y-axis:

The following is the most important representation of FFT. It plots the power of each frequency component on the y-axis and the frequency on the x-axis. The power can be plotted in linear scale or in log scale. The power of each frequency component is calculated as
Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (33)
Where Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (34) is the frequency domain representation of the signal Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (35). In Matlab, the power has to be calculated with proper scaling terms (since the length of the signal and transform length of FFT may differ from case to case).

NFFT=1024;L=length(x); X=fftshift(fft(x,NFFT)); Px=X.*conj(X)/(NFFT*L); %Power of each freq components fVals=fs*(-NFFT/2:NFFT/2-1)/NFFT; plot(fVals,Px,'b'); title('Power Spectral Density'); xlabel('Frequency (Hz)') ylabel('Power');

If you wish to verify the total power of the signal from time domain and frequency domain plots, follow this link.
Plotting the power spectral density (PSD) plot with y-axis on log scale, produces the most encountered type of PSD plot in signal processing.

NFFT=1024; L=length(x); X=fftshift(fft(x,NFFT)); Px=X.*conj(X)/(NFFT*L); %Power of each freq components fVals=fs*(-NFFT/2:NFFT/2-1)/NFFT; plot(fVals,10*log10(Px),'b'); title('Power Spectral Density'); xlabel('Frequency (Hz)') ylabel('Power');

6. Power Spectrum – One-Sided frequencies

In this type of plot, the negative frequency part of x-axis is omitted. Only the FFT values corresponding to Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (37) to Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (38) sample points of Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (39)-point DFT are plotted. Correspondingly, the normalized frequency axis runs between Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (40) to Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (41). The absolute frequency (x-axis) runs from Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (42) to Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (43).

L=length(x); NFFT=1024; X=fft(x,NFFT); Px=X.*conj(X)/(NFFT*L); %Power of each freq components fVals=fs*(0:NFFT/2-1)/NFFT; plot(fVals,Px(1:NFFT/2),'b','LineSmoothing','on','LineWidth',1); title('One Sided Power Spectral Density'); xlabel('Frequency (Hz)') ylabel('PSD');

Rate this article: Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (45)Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (46)Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (47)Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (48)Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (49) (124 votes, average: 4.64 out of 5)

For further reading

[1] Power spectral density – MIT opencourse ware↗

Topics in this chapter

Essentials of Signal Processing
● Generating standard test signals
Sinusoidal signals
Square wave
Rectangular pulse
Gaussian pulse
Chirp signal
Interpreting FFT results - complex DFT, frequency bins and FFTShift
Real and complex DFT
Fast Fourier Transform (FFT)
Interpreting the FFT results
FFTShift
IFFTShift
Obtaining magnitude and phase information from FFT
Discrete-time domain representation
Representing the signal in frequency domain using FFT
Reconstructing the time domain signal from the frequency domain samples
● Power spectral density
Power and energy of a signal
Energy of a signal
Power of a signal
Classification of signals
Computation of power of a signal - simulation and verification
Polynomials, convolution and Toeplitz matrices
Polynomial functions
Representing single variable polynomial functions
Multiplication of polynomials and linear convolution
Toeplitz matrix and convolution
Methods to compute convolution
Method 1: Brute-force method
Method 2: Using Toeplitz matrix
Method 3: Using FFT to compute convolution
Miscellaneous methods
Analytic signal and its applications
Analytic signal and Fourier transform
Extracting instantaneous amplitude, phase, frequency
Phase demodulation using Hilbert transform
Choosing a filter : FIR or IIR : understanding the design perspective
Design specification
General considerations in design

Books by the author


Wireless Communication Systems in Matlab Second Edition(PDF)
Note: There is a rating embedded within this post, please visit this post to rate it.

Digital Modulations using Python (PDF ebook)
Note: There is a rating embedded within this post, please visit this post to rate it.

Digital Modulations using Matlab (PDF ebook)
Note: There is a rating embedded within this post, please visit this post to rate it.
Hand-picked Best books on Communication Engineering
Best books on Signal Processing
Plot FFT using Matlab - FFT of sine wave & cosine wave - GaussianWaves (2024)

FAQs

How to plot Fourier transform of sine wave in Matlab? ›

y = fft(x); fs = 1/Ts; f = (0:length(y)-1)*fs/length(y); Plot the magnitude of the transformed signal as a function of frequency. The plot shows four frequency peaks, although the signal is expected to have two frequency peaks at 15 Hz and 20 Hz.

How to plot FFT using Matlab? ›

I usually use s version of this to plot a one-sided fft:
  1. t = ...; ...
  2. s = ...; ...
  3. L = numel(t); % Signal Length.
  4. Ts = mean(diff(t)); % Sampling Interval.
  5. Fs = 1/Ts; % Sampling Frequency.
  6. Fn = Fs/2; % Nyquist Frequency.
  7. FTs = fft(s)/L; % Normalised Fourier Transform.
  8. Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector.
Jan 3, 2021

What is the FFT of a sine wave plot? ›

Each coefficient shows contribution of that frequency in that signal if its large then it means that frequency constitute major part of signal. so for plotting fft with respect to frequency replace sample axis with frequency axis having points 2*pi*k/N where k=0 to 65535.

What does Matlab FFT do? ›

A fast Fourier transform (FFT) is a highly optimized implementation of the discrete Fourier transform (DFT), which convert discrete signals from the time domain to the frequency domain. FFT computations provide information about the frequency content, phase, and other properties of the signal.

How to plot two waveforms in Matlab? ›

Direct link to this answer
  1. legend('x-waveform','y-waveform');
  2. plot(t,x); hold on;
  3. plot(t,y,'r');
Oct 28, 2011

How to plot 50hz sine wave in matlab? ›

In MATLAB, it looks like this:
  1. t = 0:0.0001:0.1;
  2. freq = 50; % Frequency.
  3. amp = 20; % Amplitude.
  4. x = amp*sin(2*pi*freq*t);
Jul 28, 2022

What is the FFT of a matrix in MATLAB? ›

If X is a matrix, then fft(X) treats the columns of X as vectors and returns the Fourier transform of each column. If X is a multidimensional array, then fft(X) treats the values along the first array dimension whose size does not equal 1 as vectors and returns the Fourier transform of each vector.

What is the formula to plot a sine wave? ›

An investigation of y = a sin (bx+c) for different values of a, b, and c. The graphs of functions defined by y = sin x are called sine waves or sinusoidal waves. Notice that the graph repeats itself as it moves along the x-axis. The cycles of this regular repeating are called periods.

What are the disadvantages of FFT? ›

However, it does have some drawbacks and limitations, which include: Limited Frequency Resolution: The FFT assumes that the signal is periodic, which limits its frequency resolution. It means that the FFT may not accurately distinguish closely spaced frequency components.

What is the Fourier analysis of sine waves? ›

Fourier analysis is a mathematical method of analysing a complex periodic waveform to find its constituent frequencies (as sine waves). Complex waveforms can be analysed, with very simple results. Usually, few sine and cosine waves combine to create reasonably accurate representations of most waves.

How to plot the phase of FFT in MATLAB? ›

phase = angle(fft(x)) * 180/pi; figure; subplot(311); plot(x); subplot(312); plot(y);

Why is FFT so useful? ›

It converts a signal into individual spectral components and thereby provides frequency information about the signal. FFTs are used for fault analysis, quality control, and condition monitoring of machines or systems.

How to plot sine functions in matlab? ›

In order to make it occur as a repetitive signal when plotting, a sampling rate of fs=500KHz is used.
  1. fs= 500e3;
  2. f= 1000;
  3. nCyl=5;
  4. t=0:1/fs:nCyl*1/f;
  5. x=sin(2*pi*f*t);
  6. plot(t,x)
  7. title ('Continuous sinusoidal signal')
  8. xlabel('Time(s)');

How do you plot a sine wave graph? ›

To graph a sine graph by hand, plot key points. The y-value of sine is zero at the angles of 0, π, and 2π. Sine equals 1 at π/2 and −1 at 3π/2. Plot those points on a coordinate plane and draw the wave.

What is the Fourier transform of sine? ›

the answer is: πi[δ(ω−2πA)−δ(ω+2πA)].

How to plot sine square wave in matlab? ›

To change a sine wave to a square wave, use the sign function:
  1. t = linspace(0, 5, 250);
  2. f = 2;
  3. sinwav = sin(2*pi*t*f);
  4. sqrwav = sign(sin(2*pi*t*f));
  5. figure.
  6. plot(t, sinwav)
  7. hold on.
  8. plot(t, sqrwav)
Nov 10, 2020

Top Articles
OM System OM-1 Review
Chi-Med Initiates an International Phase I/Ib Trial of HMPL-689 in Patients with Advanced Relapsed or Refractory Lymphoma
Fan Van Ari Alectra
Best Big Jumpshot 2K23
Team 1 Elite Club Invite
Ixl Elmoreco.com
Flixtor The Meg
Google Jobs Denver
Crossed Eyes (Strabismus): Symptoms, Causes, and Diagnosis
Best Transmission Service Margate
Calamity Hallowed Ore
Puretalkusa.com/Amac
Tv Schedule Today No Cable
Best Restaurants In Seaside Heights Nj
Campaign Homecoming Queen Posters
Max 80 Orl
Jscc Jweb
Oxford House Peoria Il
Hood County Buy Sell And Trade
Connect U Of M Dearborn
Justified Official Series Trailer
Used Sawmill For Sale - Craigslist Near Tennessee
3S Bivy Cover 2D Gen
3476405416
Gia_Divine
Costco Great Oaks Gas Price
Belledelphine Telegram
Xxn Abbreviation List 2017 Pdf
Cona Physical Therapy
Usa Massage Reviews
Annapolis Md Craigslist
Hwy 57 Nursery Michie Tn
Turns As A Jetliner Crossword Clue
The Goonies Showtimes Near Marcus Rosemount Cinema
Vadoc Gtlvisitme App
Mbi Auto Discount Code
First Light Tomorrow Morning
P3P Orthrus With Dodge Slash
Solve 100000div3= | Microsoft Math Solver
Worlds Hardest Game Tyrone
Diana Lolalytics
Navigating change - the workplace of tomorrow - key takeaways
Dreammarriage.com Login
Chs.mywork
Saybyebugs At Walmart
Anhedönia Last Name Origin
Samantha Lyne Wikipedia
How to Print Tables in R with Examples Using table()
Cuckold Gonewildaudio
Wordle Feb 27 Mashable
Bumgarner Funeral Home Troy Nc Obituaries
Ravenna Greataxe
Latest Posts
Article information

Author: Jerrold Considine

Last Updated:

Views: 5701

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.