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 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).
Significance of Square Waves
The most logical way of transmitting information across a communication channel is through a stream of square pulse – a distinct pulse for ‘0‘ and another for ‘1‘. Digital signals are graphically represented as square waves with certain symbol/bit period. Square waves are also used universally in switching circuits, as clock signals synchronizing various blocks of digital circuits, as reference clock for a given system domain and so on.
Square wave manifests itself as a wide range of harmonics in frequency domain and therefore can cause electromagnetic interference. Square waves are periodic and contain odd harmonics when expanded as Fourier Series (where as signals like saw-tooth and other real word signals contain harmonics at all integer frequencies). Since a square wave literally expands to infinite number of odd harmonic terms in frequency domain, approximation of square wave is another area of interest. The number of terms of its Fourier Series expansion, taken for approximating the square wave is often seen as Gibbs Phenomenon, which manifests as ringing effect at the corners of the square wave in time domain (visual explanation here).
True Square waves are a special class of rectangular waves with 50% duty cycle. Varying the duty cycle of a rectangular wave leads to pulse width modulation, where the information is conveyed by changing the duty-cycle of each transmitted rectangular wave.
How to generate a square wave in Matlab
If you know the trick of generating a sine wave in Matlab, the task is pretty much simple. Square wave is generated using “square” function in Matlab. The command sytax – square(t,dutyCycle) – generates a square wave with period
f=10; %frequency of sine wave in Hz
overSampRate=30; %oversampling rate
fs=overSampRate*f; %sampling frequency
duty_cycle=50; % Square wave with 50% Duty cycle (default)
nCyl = 5; %to generate five cycles of sine wave
t=0:1/fs:nCyl*1/f; %time base
x=square(2*pi*f*t,duty_cycle); %generating the square wave
plot(t,x,'k');
title(['Square Wave f=', num2str(f), 'Hz']);
xlabel('Time(s)');
ylabel('Amplitude');
Power Spectral Density using FFT
Let’s check out how the generated square wave will look in frequency domain. The Fast Fourier Transform (FFT) is utilized here. As discussed in the article here, there are numerous ways to plot the response of FFT. Single Sided power spectral density is plotted first, followed by the Double-sided power spectral density.
Single 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)/(NFFT*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');
ylim([-45 -5])
Double Sided Power Spectral Density
L=length(x);
NFFT = 1024;
X = fftshift(fft(x,NFFT));
Pxx=X.*conj(X)/(NFFT*L); %computing power with proper scaling
f = fs*(-NFFT/2:NFFT/2-1)/NFFT; %Frequency Vector
plot(f,10*log10(Pxx),'r');
title('Double Sided Power Spectral Density');
xlabel('Frequency (Hz)')
ylabel('Power Spectral Density- P_{xx} dB/Hz');
Note: There is a rating embedded within this post, please visit this post to rate it.
Topics in this chapter
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 |
@humourmind:disqus can u give me good books name to understand digital communcation system and signal processing with practical knowldge and using MATLAB
@dharmanjeyaraman:disqus
Some books listed below
1) MATLAB/Simulink for Digital Communication by Won Y. Yang et al
http://www.amazon.com/gp/product/8972839965/ref=as_li_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=8972839965&linkCode=as2&tag=gaussi-20&linkId=RQXNBRVVFJAGGOJR
2) Contemporary Communication Systems Using MATLAB by John G. Proakis , Masoud Salehi , Gerhard Bauch
http://www.amazon.com/gp/product/0495082511/ref=as_li_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=0495082511&linkCode=as2&tag=gaussi-20&linkId=3RI75WCTBIZXWX6X
3) Digital Communication Systems using MATLAB and Simulink by Dennis Silage
http://www.amazon.com/gp/product/1589096215/ref=as_li_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1589096215&linkCode=as2&tag=gaussi-20&linkId=BGYKT5BIHNDU4MVZ
4) MIMO-OFDM Wireless Communications with MATLAB by Yong Soo Cho et al
http://www.amazon.com/gp/product/0470825618/ref=as_li_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=0470825618&linkCode=as2&tag=gaussi-20&linkId=TTXQ6XWMEIAQKCRU
If you are looking for basics of simulating digital communication system in Matlab, you can refer to the following resource authored by me.
https://www.gaussianwaves.com/simulation-of-digital-communication-systems-using-matlab-ebook/
In my opinion the square function does have a severe Problem. even if you use coherent sampling the PSD shows a lot of distortions which shouldnt be there. in time domain this can be seen by using stem command instead of plot. There, altough using an integer number for fs/f0, e.g. 8, the discrete points are not 4 by 4 for the positive and negative half waves, respectively. unfortunately these vary from 4/4 to 5/3 or 3/5 sometimes, generating additional noise (jitter).
by the way: FFT … Fast Fourier Transform. there is a typing error just at the start of the psd section.
Thanks for your thoughts !!!
Corrected the typo too. Thanks for spotting that.
can u give me good books name to understand digital communcation system
and signal processing with practical knowldge and using MATLAB