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.

## 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 (2 \pi) for the given time base. The command behaves similar to “sin” command (used for generating sine waves), but in this case it generates a square wave instead of a sine wave. The argument – (dutyCycle) is optional and it defines the desired duty cycle of the square wave. By default (when the (dutyCycle) argument is not supplied) the square wave is generated with (50%) duty cycle.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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

1 2 3 4 5 6 7 8 9 10 |
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

1 2 3 4 5 6 7 8 9 10 |
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'); |

**Rate this article:**

**Articles in this series:**

How to Interpret FFT results – complex DFT, frequency bins and FFTShift

How to Interpret FFT results – obtaining Magnitude and Phase information

FFT and Spectral Leakage

How to plot FFT using Matlab – FFT of basic signals : Sine and Cosine waves

Generating Basic signals – Square Wave and Power Spectral Density using FFT (this article)

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

Generating Basic Signals – Gaussian Pulse and Power Spectral Density using FFT

Chirp Signal – Frequency Sweeping – FFT and power spectral density

Constructing the Auto Correlation Matrix using FFT

For more such examples check this ebook : Digital Modulations using Matlab: build simulation models from scratch – by Mathuranathan Viswanathan