You are on page 1of 56

B M S College of Engineering

Basavangudi, Bangalore
(Autonomous Institution under VTU, Belgaum)

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGG.

SEMESTER: V COURSE CODE: 11EC5GCDSP

PREPARED BY: NARAYAN. T. DESHPANDE


ASSOC. PROF, DEPT. OF EC ENGG.
BMSCE, BANGALORE-19
CONTENTS

1. Familiarization with MATLAB.


2. List of programs.
3. Programs with algorithms and procedure.
4. Viva questions.
FAMILIARISATION WITH MATLAB
To familiarize with MATLAB software, general functions and signal processing toolbox functions .
The name MATLAB stands for MATrix LABoratory produced by Mathworks Inc., USA. It is a matrix
-based powerful software package for scientific and engineering computation and visualization.
Complex numerical problem can be solved in a fraction of the time that required with other high level
languages. It provides an interactive environment with hundreds of built -in –functions for technical
computation, graphics and animation. In addition to built-in-functions, user can create his own
functions.

MATLAB offers several optional toolboxes, such as signal processing, control systems, neural
networks etc. It is command driven software and has online help facility. MATLAB has three basic
windows normally; command window, graphics window and edit window.

 Command window is characterized by the prompt ‘>>’. All commands and the ready to
run program filename can be typed here.
 Graphic window gives the display of the figures as the result of the program.
 Edit window is to create program files with an extension .m.

Some important commands in MATLAB:


Help List topics on which help is available
Help command name Provides help on the topic selected
Demo Runs the demo program
Who Lists variables currently in the workspace
Whos Lists variables currently in the workspace with their size
Clear Clears the workspace, all the variables are removed
Clear x,y,z Clears only variables x,y,z
Quit Quits MATLAB

Some of the frequently used built-in-functions in Signal Processing Toolbox:

filter(b,a,x) Syntax of this function is Y = filter(b.a.x). It filters the data in vector x with
the filter described by vectors a and b to create the filtered data y.

fft (x) It is the DFT of vector x

ifft (x) It is the DFT of vector x

conv (a,b) Syntax of this function is C = conv (a,b).It convolves vectors a and b. The
resulting vector is of Length, Length (a) + Length (b)-1

deconv(b,a) Syntax of this function is [q,r] = deconv(b,a). It deconvolves vector q and


the remainder in vector r such that b = conv(a,q)+r

butter(N,Wn) designs an Nth order lowpass digital Butterworth filter and returns the
filter coefficients in length N+1 vectors B (numerator) and A
(denominator). The coefficients are listed in descending powers of z.
The cutoff frequency Wn must be 0.0 < Wn < 1.0, with 1.0 corresponding
to half the sample rate.
buttord(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital Butterworth filter that
loses no more than Rp dB in the passband and has at least Rs dB of
attenuation in the stopband. Wp and Ws are the passband and stopband
edge frequencies, Normalized from 0 to 1 ,(where 1 corresponds to pi
rad/sec)

Cheby1(N,R,Wn) designs an Nth order lowpass digital Chebyshev filter with R decibels of
peak-to-peak ripple in the passband. CHEBY1 returns the filter coefficients
in length N+1 vectors B (numerator) and A (denominator). The cutoff
frequency Wn must be 0.0 < Wn < 1.0, with 1.0 corresponding to half the
sample rate.

Cheby1(N,R,Wn,'high') designs a highpass filter.

Cheb1ord(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital Chebyshev Type I filter that
loses no more than Rp Db in the passband and has at least Rs dB of
attenuation in the stopband. Wp and Ws are the passband and stopband
edge frequencies, normalized from 0 to 1 (where 1 corresponds to pi
radians/sample).

cheby2(N,R,Wn) designs an Nth order lowpass digital Chebyshev filter with the stopband
ripple R decibels down and stopband edge frequency Wn. CHEBY2 returns
the filter coefficients in length N+1 vectors B (numerator) and A .
The cutoff frequency Wn must be 0.0 < Wn < 1.0, with 1.0 corresponding
to half the sample rate.

cheb2ord(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital Chebyshev Type II filter
that loses no more than Rp dB in the passband and has at least Rs dB of
attenuation in the stopband. Wp and Ws are the passband and stopband
edge frequencies.

abs(x) It gives the absolute value of the elements of x. When x is

complex, abs(x) is the complex modulus (magnitude)of the elements of x.

angle(H) It returns the phase angles of a matrix with complex elements in radians.

freqz(b,a,N) Syntax of this function is [h,w] = freqz(b,a,N) returns the N-point


frequency vector w in radians and the N-point complex frequency
response vector h of the filter b/a.
stem(y) It plots the data sequence y aa stems from the x axis terminated with
circles for the data value.

stem(x,y) It plots the data sequence y at the values specified in x.

plot(x,y) It plots vector y versus vector x. If x or y is a matrix, then the vector is

plotted versus the rows or columns of the matrix, whichever line up.

title(‘text’) It adds text at the top of the current axis.

xlabel(‘text’) It adds text beside the x-axis on the current axis.

ylabel(‘text’) It adds text beside the y-axis on the current axis.

*******
Sl.No LIST OF PROGRAMS
a Program to generate basic continuous signals.
1
b Program to generate basic discrete signals.
2 Program to verify sampling theorem
3 Program to find linear convolution of two sequences.

CYCLE NO-1
4 Program to find DFT of a given sequence.
5 Program to find IDFT of a given sequence.
6 Program to find DFT of an n-point sequence using FFT.
7 Program to find circular convolution of two sequences using DFT.
8 Program to find linear convolution of two sequences using DFT.
9 Program to find the Impulse response of an LTI-system
10 Program to solve the linear constant coefficient difference equation.

11 Program to find the Auto-correlation of a given sequence and verification


of its properties.
12 Program to find the Cross-correlation of a given sequence and verification
of its properties.

CYCLE NO-2
13 Program to plot magnitude response and phase response of digital
Butter worth Low pass filter.
Program to plot magnitude response and phase response of digital
14
Butter worth High pass filter.
Program to plot magnitude response and phase response of digital
15
Butter worth Band pass filter.
Program to plot magnitude response and phase response of digital
16
Butter worth Band stop filter.
17 Program to plot magnitude response and phase response of digital
Chebyshev type-1 Low pass filter.
18 Program to plot magnitude response and phase response of digital
Chebyshev type-1 High pass filter.
19 Program to plot magnitude response and phase response of digital
Chebyshev type-1 Band pass filter.
20 Program to plot magnitude response and phase response of digital
CYCLE NO-3

Chebyshev type-1 Band stop filter.


21 Program to plot magnitude response and phase response of digital FIR LP
filter using Hanning window.
22 Program to plot magnitude response and phase response of digital FIR HP
filter using Hanning window.
23 Program to plot magnitude response and phase response of digital FIR LP
filter using Hamming window.
24 Program to plot magnitude response and phase response of digital FIR HP
filter using Hamming window.
Program: 01(a) GENERATION OF BASIC CONTINUOUS SIGNALS.

AIM: To write a MATLAB program to generate some basic continuous time signals.

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Get the amplitude and frequency of the signal
Use ‘sin’, ’cos’ ,’square’ matlab built-in-functions
Using ‘plot’ function to plot the signal

MATLAB CODE:
clc;
clear all;
close all;
t=0:.001:1;
f=input('Enter the value of frequency');
a=input('Enter the value of amplitude');
subplot(3,3,1);
y=a*sin(2*pi*f*t);
plot(t,y,'r');
xlabel('time');
ylabel('amplitude');
title('sine wave')
grid on;
subplot(3,3,2);
z=a*cos(2*pi*f*t);
plot(t,z);
xlabel('time');
ylabel('amplitude');
title('cosine wave')
grid on;
subplot(3,3,3);
s=a*square(2*pi*f*t);
plot(t,s);
xlabel('time');
ylabel('amplitude');
title('square wave')
grid on;
subplot(3,3,4);
plot(t,t);
xlabel('time');
ylabel('amplitude');
title('ramp wave')

grid on;
subplot(3,3,5);
plot(t,a,'r');
xlabel('time');
ylabel('amplitude');
title('unit step wave')
grid on;

SAMPLE INPUT:
Enter the value of frequency 2
Enter the value of amplitude 1

RESULT: Thus the generation of basic continuous time signals using MATLAB is verified.
Program: 01(b) GENERATION OF BASIC DISCRETE SIGNALS.

AIM: To write a MATLAB program to generate common discrete time signals.

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Get the amplitude and frequency of the signal
Use ‘sin’, ’cos’ ,’square’ matlab built in functions
Using ‘stem’ function plot the signal

MATLAB CODE:

clc;
clear all;
close all;
n=0:1:50;
f=input('Enter the value of frequency');
a=input('Enter the value of amplitude');
N=input('Enter the length of unit step');
subplot(3,3,1);
y=a*sin(2*pi*f*n);
stem(n,y,'r');
xlabel('time');
ylabel('amplitude');
title('sine wave')
grid on;
subplot(3,3,2);
z=a*cos(2*pi*f*n);
stem(n,z);
xlabel('time');
ylabel('amplitude');
title('cosine wave')
grid on;
subplot(3,3,3);
s=a*square(2*pi*f*n);
stem(n,s);
xlabel('time');
ylabel('amplitude');
title('square wave')
grid on;
subplot(3,3,4);
stem(n,n);
xlabel('time');
ylabel('amplitude');
title('ramp wave')
grid on;
x=0:N-1;
d=ones(1,N);
subplot(3,3,5);
stem(x,d,'r');
xlabel('time');
ylabel('amplitude');
title('unit step wave')
grid on;

SAMPLE INPUT:
Enter the value of frequency 0.03
Enter the value of amplitude 1
Enter the length of unit step 9

RESULT: Thus the generation of discrete time signals using MATLAB is verified.
Program: 02 VERIFICATION OF SAMPLING THEOREM

Aim: To verify Sampling theorem for a signal of given frequency.


THEORY:

Sampling is a process of converting a continuous time signal (analog signal) x(t) into a discrete time signal
x[n], which is represented as a sequence of numbers. (A/D converter)

Converting back x[n] into analog (resulting in x (t ) ) is the process of reconstruction. (D/A converter)
Techniques for reconstruction-(i) ZOH (zero order hold) interpolation results in a staircase waveform,
is implemented by MATLAB plotting function stairs(n,x), (ii) FOH (first order hold) where the adjacent samples
are joined by straight lines is implemented by MATLAB plotting function plot(n,x),(iii) spline interpolation, etc.

For x (t ) to be exactly the same as x(t), sampling theorem in the generation of x(n) from x(t) is used.
The sampling frequency fs determine the spacing between samples.
Aliasing-A high frequency signal is converted to a lower frequency, results due to under sampling.
Though it is undesirable in ADCs, it finds practical applications in stroboscope and samp ling oscilloscopes.

ALGORITHM:

1. Input the desired frequency fd (for which sampling theorem is to be verified).


2. Generate an analog signal xt of frequency fd for comparison.
3. Generate oversampled, nyquist & under sampled discrete time signals.
4. Plot the waveforms and hence prove sampling theorem.

MATLAB IMPLEMENTATION:

Step 1: MATLAB can generate only discrete time signals. For an approximate analog signal xt, choose the spacing
between the samples to be very small (≈0), say 50µs = 0.00005. Next choose the time duration, say xt exists for
0.05seconds.(tfinal in program) (for low frequency say <1000 Hz choose 0.05 secs, for higher choose 0.01 secs or
lesser as appropriate).Now begin with the vector that represents the time base-
t = 0:0.00005:0.05;
The colon (:) operator in MATLAB creates a vector, in the above case a time vector running from 0 to 0.05 in
steps of 0.00005. The semicolon (;) tells MATLAB not to display the result.
Given t, the analog signal xt of frequency fd is generated as (sin(ωt)=sin(2πft))-
xt=sin(2*pi*fd*t);
*pi is recognized as 3.14 by MATLAB*

Step 2: To illustrate oversampling condition, choose sampling frequency fs0=2.2*fd. For this sampling rate T0=1/fs0,
generate the time vector as n1 = 0:T0:0.05; & over sampled discrete time signal

x1=sin(2*pi*fd*n1);
[Alternately let n1 be a fixed number of samples, say n=0:10; & x1=sin(2*pi*n*fd/fs0);]

The discrete signal is converted back to analog signal (reconstructed) using the MATLAB plot function (FOH).
In one plot both the analog signal (approximate one in blue color) and the reconstructed signal (in red) are plotted for
comparison.

Step 3: Repeat step 2 for different sampling frequencies, i.e., fs=1.3*fd & fs=2*fd for undersampling and Nyquist
sampling conditions.

MATLAB Program:
tfinal=0.05;
t=0:0.00005:tfinal;
fd=input('Enter analog freuency');
%define analog signal for comparison
xt=sin(2*pi*fd*t);
%simulate condition for undersampling i.e., fs1<2*fd
fs1=1.3*fd;
%define the time vector
n1=0:1/fs1:tfinal;

%Generate the undersampled signal


xn=sin(2*pi*n1*fd);

%plot the analog & sampled signals


subplot(3,1,1);
plot(t,xt,'b',n1,xn,'r*-');
title('undersampling plot');

%condition for Nyquist plot

fs2=2*fd;
n2=0:1/fs2:tfinal;
xn=sin(2*pi*fd*n2);
subplot(3,1,2);
plot(t,xt,'b',n2,xn,'r*-');
title('Nyquist plot');

%condition for oversampling

fs3=5*fd;
n3=0:1/fs3:tfinal;
xn=sin(2*pi*fd*n3);
subplot(3,1,3);
plot(t,xt,'b',n3,xn,'r*-');
title('Oversampling plot');
xlabel('time');
ylabel('amplitude');
legend('analog','discrete')
SAMPLE INPUT:
Enter analog frequency 200

The plots are shown in Fig.1.1

Inference:

1. From the under sampling plot observe the aliasing effect. The analog signal is of 200Hz (T=0.005s). The
reconstructed (from under sampled plot) is of a lower frequency. The alias frequency is computed as
fd-fs1 = 200-1.3*200 = 200-260= -60Hz

This is verified from the plot. The minus sign results in a 180˚ phase shift.

(For example: 3kHz & 6kHz sampled at 5kHz result in aliases of -2kHz (3k-5k) & 1kHz (6k-5k) respectively)

2. Sampling at the Nyquist rate results in samples sin(πn) which are identically zero, i.e., we are sampling at the
zero crossing points and hence the signal component is completely missed. This can be avoided by adding a
small phase shift to the sinusoid. The above problem is not seen in cosine waveforms (except cos(90n)). A simple
remedy is to sample the analog signal at a rate higher than the Nyquist rate. The Fig1.2 shows the result due to a
cosine signal ( x1=cos(2*pi*fd*n1);
3. The over sampled plot shows a reconstructed signal almost similar to that of the analog signal. Using low pass
filtering the wave form can be further smoothened.
Fig.1.1 Plots of a sampled sine wave of 200Hz

Fig.1.2 Plots of a sampled cosine wave of 200Hz

OR
Program 2: Verification of Sampling Theorem

f1 = input ('Enter the highest Frequency of the Signal F1 in Hz = ');


f2 = input ('Enter the highest Frequency of the Signal F2 in Hz = ');
freq = 2 * max(f1,f2);

% Under Sampling
fs = freq/2;
t = [0:1/fs:0.1];
x = cos(2*pi*f1*t) + cos(2*pi*f2*t);
XK = fft(x);
f = [0:length(XK)-1]*fs/length(XK);
figure(1);
plot(f,abs(XK));
xlabel('frequency');
ylabel('amplitude');
grid zoom on;
title('UNDER SAMPLING');
%Critical Sampling
fs = freq;
t = [0:1/fs:0.1];
x = cos(2*pi*f1*t) + cos(2*pi*f2*t);
XK = fft(x);

f = [0:length(XK)-1]*fs/length(XK);
figure(2);
plot(f,abs(XK));
xlabel('frequency');
ylabel('amplitude');
grid zoom on;
title('CRITICAL SAMPLING');

%Over Sampling
fs = 2*freq;
t = [0:1/fs:0.1];
x = cos(2*pi*f1*t) + cos(2*pi*f2*t);
XK = fft(x);
f = [0:length(XK)-1]*fs/length(XK);
figure(3);
plot(f,abs(XK));
xlabel('frequency');
ylabel('amplitude');
grid zoom on;
title('OVER SAMPLING');

SAMPLE INPUT:
Enter the highest Frequency of the Signal F1 in Hz = 1000
Enter the highest Frequency of the Signal F2 in Hz = 2000
Result: Thus the SAMPLING THEOREM using MATLAB is verified.
Program: 03 LINEAR CONVOLUTION USING MATLAB

AIM: TO write a MATLAB program to compute linear convolution of two given sequences

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Read the input sequence x[n] ,and plot
Read the impulse sequence h[n] , and plot
Use the matlab function ‘conv’
Convolve the two sequence and plot the result

MATLAB CODE:

clc;
clear all;
close all;
a=input('Enter the starting point of x[n]=');
b=input('Enter the starting point of h[n]=');
x=input('Enter the co-efficients of x[n]=');
h=input('Enter the co-efficients of h[n]=');
y=conv(x,h);
subplot(3,1,1);
p=a:(a+length(x)-1);
stem(p,x);
grid on;
xlabel('Time');
ylabel('Amplitude');
title('INPUT x(n)');
subplot(3,1,2);
q=b:(b+length(h)-1);
stem(q,h);
grid on;
xlabel('Time');
ylabel('Amplitude');
title('IMPULSE RESPONSE h(n)');
subplot(3,1,3);
n=a+b:length(y)+a+b-1;
stem(n,y);
grid on;
disp(y)
xlabel('Time');
ylabel('Amplitude');
title('LINEAR CONVOLUTION');

SAMPLE INPUT:

Enter the starting point of x(n)=0


Enter the starting point of h(n)=-1
Enter the co-efficient of x(n)=[1 2 3]
Enter the co-efficient of h(n)=[1 1 1]
1 3 6 5 3

RESULT: Thus the program for linear convolution using MATLAB is verified.
Program: 04 DISCRETE FOURIER TRANSFORM

AIM: TO write a MATLAB program to find the DFT of a sequence

PROCEDURE:

Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:

Enter the input sequence x[n]


Enter the length of sequence,N
Use the matlab function ‘fft’
Plot the input and output sequence

MATLAB CODE:

clc;
clear all;
close all;
N=input('Enter the value of N');
x=input('Enter the input sequence X(n):');
t=0:N-1;
subplot(2,1,1);
stem(t,x);
xlabel('TIME');
ylabel('AMPLITUDE');
title('INPUT SIGNAL');
grid on;
y=fft(x,N)
subplot(2,1,2);
stem(t,y);
xlabel('TIME');
ylabel('AMPLITUDE');
title('OUTPUT SIGNAL');
grid on;
FIGURE:

SAMPLE INPUT:

Enter the value of N 4


Enter the input sequence X(n):[1 2 3 4]
y = 10.0000 -2.0000 + 2.0000i -2.0000 -2.0000 - 2.0000i

RESULT: Thus the program for DFT is written using MATLAB and verified.
Program: 05 INVERSE DISCRETE FOURIER TRANSFORM

AIM: TO write a MATLAB program to find the IDFT of a sequence

PROCEDURE:

Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:

Enter the output sequence y[n]


Enter the length of sequence,N
Use the matlab function ‘ifft’
Plot the input and output sequence

MATLAB CODE:

clc;
clear all;
close all;
N=input('Enter the value of N=');
y=input('Enter the sequence y[n]=');
t=0:N-1;
subplot(2,1,1);
stem(t,y);
xlabel('TIME');
ylabel('AMPLITUDE');
title('INPUT SIGNAL');
grid on;
x=ifft(y,N)
subplot(2,1,2);
stem(t,x);
xlabel('TIME');
ylabel('AMPLITUDE');
title('OUTPUT SIGNAL');
FIGURE:

SAMPLE INPUT:

Enter the value of N=4


Enter the sequence y[n]=[10 -2+2i -2 -2-2i]
x = 1 2 3 4

RESULTS: Thus the program for IDFT is written using MATLAB and verified.
Program: 06 DFT OF AN N-POINT SEQUENCE USING FFT

Aim: To find DFT of an N-Point sequence using the function FFT .

MATLAB CODE:
clc;
x=input('enter the sequence whose dft is to be found=');
N=input('enter the length of DFT=');
y=fft(x,N);
mag=abs(y);
ph=angle(y);
subplot(221);
stem(x);
grid;
title('input sequence');
xlabel('n');
subplot(223);
stem(mag);
grid;
title('magnitude');
xlabel('k');
subplot(224);
stem(ph*180/pi);
grid;
title('phase');
xlabel('k');
disp('dft of the input sequence is');y

SAMPLE INPUT:
Enter the sequence whose DFT is to be found = [1 2 3 4]
Enter the length of DFT = 4
DFT of the input sequence is y = 10.0000 -2.0000 + 2.0000i -2.0000 -2.0000 - 2.0000i

FIGURE:

RESULT: Thus the DFT of an n-point sequence is verified using MATLAB.


Program: 07 CIRCULAR CONVOLUTION USING DFT
AIM: - TO write a MATLAB program to find the circular convolution of two sequence using DFT method

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Find the length of first sequence x[n]
Find the length of second sequence h[n]
Estimate the number of samples in the result of circular convolution
Take the dft for x[n]and h[n]
Compute Y[K]=X[k].*H[k]
Compute y[n]=idft(Y[k])
Plot input and output sequences

MATLAB CODE:

clc;
clear all;
close all;
x=input('Enter the sequence x(n):');
h=input('Enter the sequence h(n):');
l1=length(x);
l2=length(h);
a=0:l1-1;
b=0:l2-1;
l3=max(l1,l2);
c=0:l3-1;
subplot(3,1,1);
stem(a,x);
grid on;
xlabel('TIME');
ylabel('AMPLITUDE');
title('X(n)');
subplot(3,1,2);
stem(b,h);
xlabel('TIME');
ylabel('AMPLITUDE');
grid on;
title('h(n)');
q=fft(x,l3);
r=fft(h,l3);
s=(r).*q;
t=ifft(s,l3)
subplot(3,1,3);
stem(c,t);
xlabel('TIME');
ylabel('AMPLITUDE');
grid on;
title('y(n)');

SAMPLE INPUT:
Enter the sequence x(n): [1 2 3]
Enter the sequence h(n): [1 2 1 2]
t = 8 10 8 10

RESULT: Thus the program for circular convolution using DFT using MATLAB is verified.
Program: 08 LINEAR CONVOLUTION USING DFT

AIM: - TO write a MATLAB program to find the linear convolution of two sequence using DFT

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:-
Find the length of first sequence x[n]
Find the length of second sequence h[n]
Estimate the number of samples in the result of linear convolution
If l1 <l2 pad enough no: of zeros to x[n]
If l2 <l1 pad enough no: of zeros to h[n]
Take the dft for modified x[n]and h[n]
Compute Y[K]=X[k].*H[k]
Compute y[n]=idft(Y[k])
Plot input and output sequences

MATLAB CODE:
clc;
clear all;
close all;
x=input('Enter the sequence x(n):');
h=input('Enter the sequence h(n):');
l1=length(x);
l2=length(h);
x=[x,zeros(1,l2-1)];
h=[h,zeros(1,l1-1)];
l3=length(x);
c=0:l3-1;
subplot(3,1,1);
stem(c,x);
grid on;
xlabel('TIME');
ylabel('AMPLITUDE');
title('X(n)');
subplot(3,1,2);
stem(c,h);
xlabel('TIME');
ylabel('AMPLITUDE');
grid on;
title('h(n)');
q=fft(x,l3);
r=fft(h,l3);
s=(r).*q;
y=ifft(s,l3)
subplot(3,1,3);
stem(c,y);
xlabel('TIME');
ylabel('AMPLITUDE');
grid on;
title('y(n)');

SAMPLE INPUT:
Enter the sequence x(n):[1 2 3]
Enter the sequence h(n):[1 1 1]
t =1.0000 3.0000 6.0000 5.0000 3.0000

FIGURE:

RESULT: Thus the program for linear convolution using DFT using MATLAB is verified.
Program: 09 IMPULSE RESPONSE OF AN LTI-SYSTEM

AIM: TO write a MATLAB program to find the impulse response of a system defined by a difference equation

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Create a matrix a for the coefficient of y[n]
Create a matrix b for the coefficient of x[n]
Generate an impulse signal
Find the response h[n] of the system defined by a and b coefficient
To the impulse signal using ‘filter ‘command

MATLAB CODE:
clc;
clear all;
close all;
N=input('Enter the required length of impulse response N=');
n=0:N-1;
b=input('Enter the co-efficients of x(n),b=');
a=input('Enter the co=efficients of y(n),a=');
x=[1,zeros(1,N-1)];
y=filter(b,a,x);
stem(n,y);
xlabel('time');
ylabel('amplitude');
title('IMPULSE RESPONSE');
grid on;

SAMPLE INPUT:

Y[n]+0.7y[n-1]-0.45y[n-2]-0.6y[n-3]=0.8x[n]-0.44x[n-1]+0.36x[n-2]+0.2x[n-3]

Enter the required length of impulse response N = 40


Enter the co-efficients of x(n),b = [0.8 -0.44 0.36 0.02]
Enter the co=efficients of y(n),a = [1 0.7 -0.45 -0.6]
FIGURE:

RESULT: Thus the program for impulse response of an LTI system using MATLAB is
verified.
Program:10 LINEAR CONSTANT COEFFICIENT DIFFERENCE EQUATION

AIM: To write a MATLAB program to solve the linear constant co-efficient difference
Equation.

MATLAB CODE:
clc;
a=input('enter the co-efficient of x');
b=input('enter the co-efficient of y');
% to find h(z)
[r,p,k]=residuez(a,b)
disp('residues are=');r
disp('poles of h(z) are=');p
disp('constant of h(z) are=');k
rr=roots(a);
disp('zeros of h(z) are=');rr
% to find the impulse response
n=input('enter the length of the response');
x=[1,zeros(1,n-1)];
h=filter(a,b,x);
disp('impulse response is');h
subplot(3,1,1);
grid;
stem(h);
title('impulse response');
% to find system response
x=input('enter the signal');
y=conv(x,h);
disp('output of the system is');y
subplot(3,1,2);
grid;
stem(x);
title('system i/p');
subplot(3,1,3);
grid;
stem(y);
title('system o/p');

SAMPLE INPUT:
Enter the co-efficient of x [2]
Enter the co-efficient of y [1 -0.5]
r=2
p = 0.5000
k =[]
residues r =2
poles of h(z) p =0.5000
constant of h(z) k = []
zeros of h(z) rr =

Empty matrix: 0-by-1

Enter the length of the response 10

Impulse response is
h =Columns 1 through 8
2.0000 1.0000 0.5000 0.2500 0.1250 0.0625 0.0313 0.0156
Columns 9 through 10
0.0078 0.0039

Enter the signal [1 2 3 4]

Output of the system is

y = Columns 1 through 8
2.0000 5.0000 8.5000 12.2500 6.1250 3.0625 1.5313 0.7656
Columns 9 through 13
0.3828 0.1914 0.0938 0.0430 0.0156
FIGURE:

RESULT: Thus the program to solve the linear constant co-efficient difference
equation using MATLAB is verified.
Program 11: AUTOCORRELATION OF A GIVEN SEQUENCE AND VERIFICATION
OF ITS PROPERTIES.

Aim: To obtain autocorrelation of the given sequence and verify its properties.

Theory:
Correlation is mathematical technique which indicates whether 2 signals are related and in a precise
quantitative way how much they are related. A measure of similarity between a pair of energy signals x[n]
and y[n] is given by the cross correlation sequence rxy[l] defined by rxy [l ] x[n] y[n l ]; l 0, 1, 2,... .
n
The parameter ‘l’ called ‘lag’ indicates the time shift between the pair.
Autocorrelation sequence of x[n] is given by rxx [l ] x[n]x[n l ]; l 0, 1, 2,...
n
Some of the properties of autocorrelation are enumerated below
The autocorrelation sequence is an even function i.e., rxx [l ] rxx [ l ]
At zero lag, i.e., at l=0, the sample value of the autocorrelation sequence has its maximum value (equal to
the total energy of the signal x) i.e., rxx [l ] rxx [0] x x 2 [ n] .
n
This is verified in Fig. 5.1, where the autocorrelation of the rectangular pulse (square) has a maximum value
at l=0. All other samples are of lower value. Also the maximum value = 11 = energy of the pulse [1 2+12+12..].
A time shift of a signal does not change its autocorrelation sequence. For example, let y[n]=x[n-k]; then
ryy[l] = rxx[l] i.e., the autocorrelation of x[n] and y[n] are the same regardless of the value of the time shift k.
This can be verified with a sine and cosine sequences of same amplitude and frequency will have identical
autocorrelation functions.
For power signals the autocorrelation sequence is given by
1 N1
and for periodic signals with period N it is rxx [l ] x[n]x[n l ]; l 0, 1, 2,... and this rxx[l] is also
Nn0
periodic with N. This is verified in Fig. 5.3 where we use the periodicity property of the autocorrelation
sequence to determine the period of the periodic signal y[n] which is x[n] (=cos(0.25*pi*n)) corrupted by an
additive uniformly distributed random noise of amplitude in the range [-0.5 0.5]

ALGORITHM:

 Input the sequence as x.


 Use the conv and fliplr function to get cross correlated output rxx.
 Plot the output sequence.
MATLAB PROGRAM:

x=input ('Enter sequence x(n)=');


rxx= conv(x,fliplr(x));
disp('rxx=');
disp(rxx);
figure(1);
stem(rxx,'filled');
title('Autocorrelation output');
xlabel('lag index');
ylabel('amplitude');

SAMPLE INPUT: Enter sequence x(n)=[1 2 3 4]

OUTPUT: Rxx = 4 11 20 30 20 11 4

FIGURE:

RESULT: Thus the auto correlation and its properties are verified using MATLAB.
Program 12: CROSS CORRELATION OF A GIVEN SEQUENCE AND VERIFICATION
OF ITS PROPERTIES.
Aim: To obtain cross correlation of the given sequence and verify its properties.

Theory:
Cross Correlation has been introduced in the last experiment. Comparing the equations for the linear
convolution and cross correlation we find that rxy [l ] x[n] y[n l ] x[n] y[ (l n)] x[l ] y[ l ] . i.e.,
n n
convolving the reference signal with a folded version of sequence to be shifted (y[n]) results in cross
correlation output. (Use ‘fliplr’ function for folding the sequence for correlation).
The properties of cross correlation are 1) the cross correlation sequence sample values are upper bounded
by the inequality rxx [l ] rxx [0]ryy [0] x y

2) The cross correlation of two sequences x[n] and y[n]=x[n-k] shows a peak at the value of k. Hence cross
correlation is employed to compute the exact value of the delay k between the 2 signals. Used in radar and
sonar applications, where the received signal reflected from the target is the delayed version of the
transmitted signal (measure delay to determine the distance of the target).
3) The ordering of the subscripts xy specifies that x[n] is the reference sequence that remains fixed in time,
whereas the sequence y[n] is shifted w.r.t x[n]. If y[n] is the reference sequence then ryx [l ] rxy [ l ] . Hence
ryx[l] is obtained by time reversing the sequence rxy[l].

ALGORITHM:
 Input the sequence as x and y.
 Use the conv and fliplr function to get cross correlated output rxy.
 Plot the output sequence

MATLAB CODE:
x=input ('Enter sequence x(n)=');
y=input ('Enter sequence y(n)=');
rxy= conv(x,fliplr(y));
disp('rxy=');
disp(rxy);
figure(1);
stem(rxy,'filled');
title('cross correlation output');
xlabel('lag index');
ylabel('amplitude');

SAMPLE INPUIT:
Enter sequence x(n) = [1 2 3 4]
Enter sequence y(n) = [4 3 2 1]
Rxy = 1 4 10 20 25 24 16
FIGURE:

RESULT: Thus the CROSS CORRELATION and its properties are verified using MATLAB.
Program: 13 DIGITAL BUTTERWORTH LOW PASS FILTER
AIM: TO write a MATLAB program to plot magnitude response and phase response of digital
Butter worth Low pass filter.

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Get the passband and stopband ripples
Get the passband and stopband edge frequencies
Calculate the order of the filter using ‘ buttord ’ function
Find the filter coefficients, using ‘butter’ function
Draw the magnitude and phase response

MATLAB CODE:
clc;
clear all;
close all;
rp=input('enter the passband attenuation:');
rs=input('enter the stop band attenuation:');
wp=input('enter the pass band frequency:');
ws=input('enter the stop band frequency:');
[N,wn]=buttord(wp/pi,ws/pi,rp,rs);
[b,a]=butter(N,wn);
freqz(b,a);

SAMPLE INPUT:
Enter the pass band attenuation:0.4
Enter the stop band attenuation:30
Enter the pass band frequency:0.2*pi
Enter the stop band frequency:0.4*pi
RESULT: Thus the magnitude response and phase response of Digital Butter worth
Low pass filter is verified.
Program: 14 DIGITAL BUTTERWORTH HIGH PASS FILTER

AIM: - To write a MATLAB program to plot magnitude response and phase response of digital
Butter worth High pass filter.

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:-
Get the passband and stopband ripples
Get the passband and stopband edge frequencies
Calculate the order of the filter using ‘ buttord ’ function
Find the filter coefficients, using ‘butter’ function
Draw the magnitude and phase response

MATLAB CODE:
clc;
clear all;
close all;
rp=input ('Enter the pass band attenuation:');
rs=input ('Enter the stop band attenuation:');
wp=input ('Enter the pass band frequency:');
ws=input ('Enter the stop band frequency:');
[N,wn]=buttord(wp/pi,ws/pi,rp,rs);
[b,a]=butter(N,wn,'high');
freqz(b,a);

SAMPLE INPUT:

Enter the pass band attenuation: 0.4


Enter the stop band attenuation: 30
Enter the pass band frequency: 0.6*pi
Enter the stop band frequency: 0.2*pi
FIGURE:

RESULT: Thus the magnitude response and phase response of Digital


Butter worth High pass filter is verified.
Program: 15 DIGITAL BUTTERWORTH BAND-PASS FILTER
AIM: - TO write a MATLAB program to plot magnitude response and phase response of digital
Butter worth Band pass filter

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Get the passband and stopband ripples
Get the passband and stopband edge frequencies
Calculate the order of the filter using ‘ buttord ’ function
Find the filter coefficients, using ‘butter’ function
Draw the magnitude and phase response

MATLAB CODE:
clc;
clear all;
close all;
rp=input('enter the passband attenuation:');
rs=input('enter the stop band attenuation:');
wp=input('enter the pass band frequency:');
ws=input('enter the stop band frequency:');
[N,wn]=buttord(wp/pi,ws/pi,rp,rs);
[b,a]=butter(N,wn);
freqz(b,a);

SAMPLE INPUT:

Enter the passband attenuation:0.2


Enter the stop band attenuation:20
Enter the pass band frequency:[0.2*pi,0.4*pi]
Enter the stop band frequency: [0.1*pi,0.5*pi]
RESULT: Thus the Amplitude response and phase response of Butter worth
band pass filter is verified.
Program: 16 DIGITAL BUTTERWORTH BAND-STOP FILTER
AIM: TO write a MATLAB program to plot magnitude response and phase response of
Digital Butter worth band stop filter.
PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Get the passband and stopband ripples
Get the passband and stopband edge frequencies
Calculate the order of the filter using ‘ buttord ’ function
Find the filter coefficients, using ‘butter’ function
Draw the magnitude and phase response

MATLAB CODE:
clc;
clear all;
close all;
rp=input('enter the passband attenuation:');
rs=input('enter the stop band attenuation:');
wp=input('enter the pass band frequency:');
ws=input('enter the stop band frequency:');
[N,wn]=buttord(wp/pi,ws/pi,rp,rs);
[b,a]=butter(N,wn,’stop’);
freqz(b,a);

SAMPLE INPUT:-
Enter the passband attenuation:0.2
Enter the stop band attenuation:20
Enter the pass band frequency:[0.1*pi,0.5*pi]
Enter the stop band frequency:[0.2*pi,0.4*pi]
RESULT: Thus the Amplitude response and phase response of Butter worth
band stop filter is verified.
Program: 17 DIGITAL CHEBYSHEV (TYPE-1) LOW PASS FILTER

AIM: - TO write a MATLAB program to plot magnitude response and phase response of digital
Chebyshev type-1 Low pass filter

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Get the passband and stopband ripples
Get the passband and stopband edge frequencies
Calculate the order of the filter using ‘ cheb1ord ’ function
Find the filter coefficients, using ‘cheby1’ function
Draw the magnitude and phase response

MATLAB CODE:
clc;
clear all;
close all;
rp=input ('Enter the pass band attenuation:');
rs=input ('Enter the stop band attenuation:');
wp=input ('Enter the pass band frequency:');
ws=input ('Enter the stop band frequency:');
[N,wn]=cheb1ord(wp/pi,ws/pi,rp,rs);
[b,a]=cheby1(N,rp,wn);
freqz(b,a);

SAMPLE INPUT:-
Enter the pass band attenuation:20
Enter the stop band attenuation:50
Enter the pass band frequency:0.3*pi
Enter the stop band frequency:0.4*pi
RESULT: Thus the Amplitude response and phase response of Chebyshev type -1
Low pass filter is verified.
PROGRAM: 18 DIGITAL CHEBYSHEV (TYPE-1) HIGH PASS FILTER
AIM: - To write a MATLAB program to plot magnitude response and phase response of digital
Chebyshev type-1 high pass filter

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Get the passband and stopband ripples
Get the passband and stopband edge frequencies
Calculate the order of the filter using ‘ cheb1ord ’ function
Find the filter coefficients, using ‘cheby1’ function
Draw the magnitude and phase response

MATLAB CODE:
clc;
clear all;
close all;
rp=input ('Enter the pass band attenuation:');
rs=input ('Enter the stop band attenuation:');
wp=input ('Enter the pass band frequency:');
ws=input ('Enter the stop band frequency:');
[N,wn]=cheb1ord(wp/pi,ws/pi,rp,rs);
[b,a]=cheby1(N,rp,wn,'high');
freqz(b,a);

SAMPLE INPUT:-
Enter the pass band attenuation:20
Enter the stop band attenuation:50
Enter the pass band frequency:0.4*pi
Enter the stop band frequency:0.3*pi
FIGURE:

RESULT: Thus the Amplitude response and phase response of Chebyshev


type-1 high pass filter was verified.
Program: 19 DIGITAL CHEBYSHEV (TYPE-1) BAND PASS FILTER
AIM: To write a MATLAB program to plot magnitude response and phase response of digital
Chebyshev type-1 Band pass filter

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Get the passband and stopband ripples
Get the passband and stopband edge frequencies
Calculate the order of the filter using ‘ cheb1ord ’ function
Find the filter coefficients, using ‘cheby1’ function
Draw the magnitude and phase response

MATLAB CODE:
clc;
clear all;
close all;
rp=input ('Enter the pass band attenuation:');
rs=input ('Enter the stop band attenuation:');
wp=input ('Enter the pass band frequency:');
ws=input ('Enter the stop band frequency:');
[N,wn]=cheb1ord(wp/pi,ws/pi,rp,rs);
[b,a]=cheby1(N,rp,wn);
freqz(b,a);

SAMPLE INPUT:-
Enter the pass band attenuation:20
Enter the stop band attenuation:98
Enter the pass band frequency:[0.3*pi,0.5*pi]
Enter the stop band frequency:[0.1*pi,0.8*pi]
RESULT: Thus the Amplitude response and phase response of Chebyshev
type-1 band pass filter is verified.
Program: 20 DIGITAL CHEBYSHEV (TYPE-1) BAND STOP FILTER
AIM: To write a MATLAB program to plot magnitude response and phase response of digital
Chebyshev type-1 band stop filter

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Get the passband and stopband ripples
Get the passband and stopband edge frequencies
Calculate the order of the filter using ‘ cheb1ord ’ function
Find the filter coefficients, using ‘cheby1’ function
Draw the magnitude and phase response

MATLAB CODE:
clc;
clear all;
close all;
rp=input('Enter the pass band attenuation:');
rs=input('Enter the stop band attenuation:');
wp=input('Enter the pass band frequency:');
ws=input('Enter the stop band frequency:');
[N,wn]=cheb1ord(wp/pi,ws/pi,rp,rs);
[b,a]=cheby1(N,rp,wn,'stop');
freqz(b,a);

SAMPLE INPUT:-
Enter the pass band attenuation:20
Enter the stop band attenuation:98
Enter the pass band frequency:[0.1*pi,0.8*pi]
Enter the stop band frequency:[0.3*pi,0.5*pi]
RESULT: Thus the Amplitude response and phase response of Chebyshev
type-1 band stop pass filter is verified.
Program: 21 FIR LOW-PASS FILTER USING HANNING WINDOW
AIM: TO write a MATLAB program to plot magnitude response and phase response of digital FIR LP filter
Using Hanning window.

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Get the order of the filter
Get the cut off frequency
use ‘ fir1 ’& ‘hanning’ function to compute the filter coefficient
Draw the magnitude and phase response

MATLAB CODE:
clc;
clear all;
close all;
N=input('Enter the value of N:');
wc=input('Enter cutoff frequency:'); SAMPLE INPUT:
h=fir1(N,wc/pi,hanning(N+1)); Enter the value of N:28
freqz(h); Enter cutoff frequency:0.5*pi
FIGURE:

RESULT: Thus the magnitude response and phase response of FIR Low pass filter using
Hanning window is verified.
Program: 22 FIR HIGH-PASS FILTER USING HANNING WINDOW

AIM: To write a MATLAB program to plot magnitude response and phase response of digital FIR HP filter
using Hanning window

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:
Get the order of the filter
Get the cut off frequency
use ‘ fir1 ’& ‘hanning’ function to compute the filter coefficient
Draw the magnitude and phase response

MATLAB CODE:
clc;
clear all;
close all;
N=input('Enter the value of N:'); SAMPLE INPUT:
wc=input('Enter cutoff frequency:'); Enter the value of N:28
h=fir1(N,wc/pi,'high',hanning(N+1)); Enter cutoff frequency:0.5*pi
freqz(h);

FIGURE:

RESULT: Thus the magnitude response and phase response of fir High pass filter using
Hanning window is verified.
Program: 23 FIR LOW PASS FILTER USING HAMMING WINDOW
AIM: TO write a MATLAB program to plot magnitude response and phase response of digital FIR LP filter
using Hamming window

PROCEDURE:
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
ALGORITHM:
Get the order of the filter
Get the cut off frequency
use ‘ fir1 ’& ‘hamming’ function to compute the filter coefficient
Draw the magnitude and phase response

MATLAB CODE:
clc;
clear all;
close all;
N=input('Enter the value of N:');
wc=input('Enter cutoff frequency:');
h=fir1(N,wc/pi,hamming(N+1));
freqz(h); SAMPLE INPUT:
Enter the value of N: 28
Enter cutoff frequency: 0.5*pi

RESULTS: Thus the magnitude response and phase response of fir Low pass filter using
hamming window was verified.
Program: 24 FIR HIGHPASS FILTER USING HAMMING WINDOW

AIM:TO write a MATLAB program to plot magnitude response and phase response of
digital FIR HP filter using Hanning window
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window

ALGORITHM:-
Get the order of the filter
Get the cut off frequency
use ‘ fir1 ’& ‘hamming’ function to compute the filter coefficient
Draw the magnitude and phase response

MATLAB CODE:-
clc;
clear all;
close all;
N=input('Enter the value of N:'); SAMPLE INPUT:
wc=input('Enter cutoff frequency:'); Enter the value of N:28
h=fir1(N,wc/pi,'high',hamming(N+1)); Enter cutoff frequency:0.5*pi
freqz(h);

RESULT: Thus the magnitude response and phase response of FIR High pass filter
Using hamming window was verified.
VIVA QUESTIONS

1. Classify signals.
2. Classify Systems.
3. What do you mean by aliasing in digital signal processing? How it can be avoided?
4. What are the differences between a microprocessor and a DSP processor?
5. What is the convolution?
6. What is the advantage of a Direct form II FIR over fom I?
7. Difference between DFT and DTFT.
8. What do you mean by BIBO stable?
9. What are elementary signals?
10. What are the basic time domain operations of discrete time signal?
11. List out the properties of convolution.
12. What is linear time invariant system?
13. Differentiate with one dimensional and two dimensional signal with an example each.
14. What is sampling?
15. Satae sampling theorem and what is Nyquist frequency?
16. Differentiate between anti aliasing and anti imaging filters.
17. What is an anti aliadsing filter? What is the need for it?
18. What is sample and hold circuit?
19. What are the advantages and disadvantages of DSP compared to analog signal
processing?
20. Define Z-transform.
21. Define ROC in Z-transform.
22. What are the properties of ROC in Z-transform?
23. What is the condition for stability in Z-domain?
24. State the initial value theorem of Z-transforms.
25. State the final value theorem of Z-transforms.

**********************************************************