Generating Basic signals – Rectangular Pulse and Power Spectral Density using FFT

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, cosineGaussian pulsesquare waveisolated 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 to represent them in frequency domain using FFT.

This article is part of the book Digital Modulations using Matlab : Build Simulation Models from Scratch, ISBN: 978-1521493885 available in ebook (PDF) format (click here) and Paperback (hardcopy) format (click here)
Wireless Communication Systems in Matlab, ISBN: 978-1720114352 available in ebook (PDF) format (click here) and Paperback (hardcopy) format (click here).

Rectangular pulse: mathematical description

An isolated rectangular pulse of amplitude A and duration T is represented mathematically as

g(t) = A \cdot rect \left( \frac{t}{T} \right)

where

rect(t) = \begin{cases}  1,  & when\; \frac{-1}{2}<t<\frac{1}{2}\\ 0, & otherwise \end{cases} 

The Fourier transform of isolated rectangular pulse g(t) is

\displaystyle{G(f)=\int_{-T/2}^{T/2}A e^{-j2 \pi f t} dt = A \cdot \frac{sin(\pi f t)}{\pi f} = AT \cdot sinc(fT)}

where, the sinc function is given by

sinc(x)=\frac{sin(\pi x)}{\pi x}

Thus, the Fourier Transform pairs are

 A \cdot  rect( \frac{t}{T} ) \leftrightharpoons AT \cdot sinc(fT)

The Fourier Transform describes the spectral content of the signal at various frequencies. For a given signal g(t), the Fourier Transform is given by

G(f) = \left |  G(f)  \right | e^{j \theta (f) }

where, the absolute value \left |  G(f)  \right | gives the magnitude of the frequency components (amplitude spectrum) and \theta (f) are their corresponding phase (phase spectrum) . For the rectangular pulse, the amplitude spectrum is given as

\left |  G(f)  \right | = AT \left | sinc(fT)\right |  

The amplitude spectrum peaks at f=0 with value equal to AT. The nulls of the spectrum occur at integral multiples of 1/T, i.e, ( \pm 1/T, \pm 2/T, \pm 3/T,...  )

Generating an isolated rectangular pulse in Matlab:

An isolated rectangular pulse of unit amplitude and width w (the factor T in equations above ) can be generated easily with the help of in-built function – rectpuls(t,w) command in Matlab. As an example, a unit amplitude rectangular pulse of duration T=0.2 s is generated.

fs=500; %sampling frequency
T=0.2; %width of the rectangule pulse in seconds

t=-0.5:1/fs:0.5; %time base

x=rectpuls(t,T); %generating the square wave

plot(t,x,'k');
title(['Rectangular Pulse width=', num2str(T),'s']);
xlabel('Time(s)');
ylabel('Amplitude');
Rectangule Pulse how to plot FFT in Matlab

Amplitude spectrum using FFT:

Matlab’s FFT function is utilized for computing the Discrete Fourier Transform (DFT). The magnitude of FFT is plotted. From the following plot, it can be noted that the amplitude of the peak occurs at f=0 with peak value  AT=1 \times 0.2=0.2. The nulls in the spectrum are located at  (\pm 1/T = \pm 5 Hz, \pm 2/T = \pm 10 Hz, \pm 3/T = \pm 15 Hz, \cdots ).

L=length(x);
NFFT = 1024;
X = fftshift(fft(x,NFFT)); %FFT with FFTshift for both negative & positive frequencies
f = fs*(-NFFT/2:NFFT/2-1)/NFFT; %Frequency Vector

figure;
plot(f,abs(X)/(L),'r');
title('Magnitude of FFT');
xlabel('Frequency (Hz)')
ylabel('Magnitude |X(f)|');
Rectangule Pulse Magnitude of FFT

Power spectral density (PSD) using FFT:

The distribution of power among various frequency components is plotted next. The first plot shows the double-side Power Spectral Density which includes both positive and negative frequency axis. The second plot describes the PSD only for positive frequency axis (as the response is just the mirror image of negative frequency axis).

figure;
Pxx=X.*conj(X)/(L*L); %computing power with proper scaling
plot(f,10*log10(Pxx),'r');
title('Double Sided - Power Spectral Density');
xlabel('Frequency (Hz)')
ylabel('Power Spectral Density- P_{xx} dB/Hz');
Rectangule Pulse Double Sided Power Spectral Density
X = fft(x,NFFT);
X = X(1:NFFT/2+1);%Throw the samples after NFFT/2 for single sided plot
Pxx=X.*conj(X)/(L*L);
f = fs*(0:NFFT/2)/NFFT; %Frequency Vector
plot(f,10*log10(Pxx),'r');
title('Single Sided - Power Spectral Density');
xlabel('Frequency (Hz)')
ylabel('Power Spectral Density- P_{xx} dB/Hz');
Rectangule Pulse Single Sided Power Spectral Density

Magnitude and phase spectrum:

The phase spectrum of the rectangular pulse manifests as series of pulse trains bounded between 0 and \pi \; radians, provided the rectangular pulse is symmetrically centered around sample zero. This is explained in the reference here and the demo below.

clearvars;
x = [ones(1,7) zeros(1,127-13) ones(1,6)];
subplot(3,1,1); plot(x,'k');
title('Rectangular Pulse'); xlabel('Sample#'); ylabel('Amplitude');

NFFT = 127;
X = fftshift(fft(x,NFFT)); %FFT with FFTshift for both negative & positive frequencies
f = (-NFFT/2:NFFT/2-1)/NFFT; %Frequency Vector

subplot(3,1,2); plot(f,abs(X),'r');
title('Magnitude Spectrum'); xlabel('Frequency (Hz)'); ylabel('|X(f)|');

subplot(3,1,3); plot(f,atan2(imag(X),real(X)),'r');
title('Phase Spectrum'); xlabel('Frequency (Hz)'); ylabel('\angle X(f)');
Magnitude and phase spectrum of rect pulse
Magnitude and phase spectrum of rectangular pulse

Rate this article: PoorBelow averageAverageGoodExcellent (21 votes, average: 4.67 out of 5)

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
Wireless Communication Systems in Matlab
Second Edition(PDF)

PoorBelow averageAverageGoodExcellent (180 votes, average: 3.62 out of 5)

Digital modulations using Python
Digital Modulations using Python
(PDF ebook)

PoorBelow averageAverageGoodExcellent (134 votes, average: 3.56 out of 5)

digital_modulations_using_matlab_book_cover
Digital Modulations using Matlab
(PDF ebook)

PoorBelow averageAverageGoodExcellent (136 votes, average: 3.63 out of 5)

Hand-picked Best books on Communication Engineering
Best books on Signal Processing

7 thoughts on “Generating Basic signals – Rectangular Pulse and Power Spectral Density using FFT”

  1. Hello,
    It would be great help for us(learners from your material) if you can explain in detail why you choose the frequency f in this manner. What is the NFFT? and why and how you made f.
    L=length(x);
    NFFT = 1024;
    X = fftshift(fft(x,NFFT)); %FFT with FFTshift for both negative & positive frequencies
    f = fs*(-NFFT/2:NFFT/2-1)/NFFT; %Frequency Vector
    Thank you very much in advance.
    regards
    tilkesh

    Reply
  2. Hi, thank for your posts in this website.
    I have a question… Why the magnitude of the spectrum does not touch the zero in the points in which the sinc function (that is the fourier transform of a rect pulse) should be null?
    Is that due to the fact that we are performing a DFT?
    And if we plot the phase spectrum, that of a sinc should be a train of rect pulses between 0 and -pi when the sinc is negative, what information do we have instead?
    Thanks.

    Reply
    • The magnitude spectrum does not touch zero due to the relationship between the FFT length that controls the bin centers and the points where the sinc function supposed to touch zero. If the FFT length is adjusted appropriately according to the width of the rect pulse, the magnitude spectrum will touch zero at expected null places.

      Phase spectrum depends on how the input pulse is presented to the FFT. It is explained in the reference here : http://www.dspguide.com/ch11/2.htm

      Check how to get phase spectrum as you have mentioned in your question. I have provided this additional information in the post above.

      Reply
  3. Hi!
    First of all, thanks a lot for your explanation of FFT using MATLAB. It has been quite helpful for me. Anyway, there is a little question i would appreciate your help.
    I am using the Rect function just for training since I have to use Fourier transforms to analize a diffraction pattern problem. In this case, it is important for me to recover the sign of the sinc function (i.e. those frequencies where it is negative) and plotting the ABS value makes me to loose this information. Is there any way to obtain a SINC fuction and not just a |sinc|?

    Thank you

    Reply
    • FFT encodes information on exponential basis functions, so both real and imaginary part of the FFT output contains all valuable information. FFT gives result in complex format.

      Information is presented in polar form : magnitude and the phase.

      Magnitudes represent intensity deviation from zero and hence can have only positive values. If you would like to have negative values in magnitude (its against formal definition of magnitude to carry negative values), further processing needs to be done.

      These pages may help in better understanding
      http://www.dspguide.com/ch11/2.htm

      Reply

Post your valuable comments !!!