21eel66 - DSP Lab Manual
21eel66 - DSP Lab Manual
LABORATORY MANUAL
For the academic year 2024-25
Name:………………………
USN:………………………..
COURSE OUTCOMES
(i)
DIGITAL SIGNAL PROCESSING LABORATORY
SN Experiment Page
No
1 Generation of Different signals in both Continuous and discrete time domains. 7
2 Verification of Sampling Theorem both in time and Frequency Domains 14
3 To perform Basic operations on given sequences-Signal folding, evaluation of even 18
and odd
4 Evaluation of Impulse Response of a System 25
5 Evaluation of Linear and Circular Convolution of given sequences 27
6 Computation of N-Point DFT by using Definite equations. 33
7 Evaluation of Circular Convolution of two sequences using DFT and IDFT approach 35
8 Solution of a difference Equation 37
9 Computation of N-Point DFT and IDFT by FFT Method 41
10 Design and implementation of IIR filter to meet the given specification 44
11 Design and implementation of FIR filter to meet the given specification 59
(ii)
Digital Signal Processing Laboratory -21EEL66
INTRODUCTION TO MATLAB
At its core ,MATLAB is essentially a set (a “toolbox”) of routines (called “mfiles” or “mex
files”) that sit on your computer and a window that allows you to create new variables with
names (e.g. voltage and time) and process those variables with any of those routines (e.g. plot
voltage against time, find the largest voltage, etc). It also allows you to put a list of your
processing requests together in a file and save that combined list with a name so that you can run
all of those commands in the same order at some later time. Furthermore, it allows you to run
such lists of commands such that you pass in data.
MATLAB Windows:
MATLAB works with through these basic windows
COMMAND WINDOW
This is the main window ,it is characterized by MATLAB command prompt >> when you launch
the application program MATLAB puts you in this window all commands including those for
user-written programs ,are typed in this window at the MATLAB prompt.
GRAPHICS WINDOW
The output of all graphics commands typed in the command window are flushed to the graphics
or figure window, a separate gray window with white background color the user can create as
many windows as the system memory will allow.
EDIT WINDOW
This is where you write edit, create and save your own programs in files called M files.
Input-output
MATLAB supports interactive computation taking the input from the screen and flushing, the
output to the screen. In addition it can read input files and write output files.
Data Type
The fundamental data distinct data objects- integers, real numbers, matrices, character strings,
structures and cells. There is no need to declare variables as real or complex, MATLAB
automatically sets the variable to be real.
Dimensioning
Dimensioning is automatic in MAT required for vectors or arrays .we can find the dimensions of
an existing matrix or a vector with the size and length commands.
Note: Save all M files in a folder in the current directory. Otherwise you have to locate the file
during compiling.
Typing quit in the command prompt>> quit, will close MATLAB Development Environment.
For any clarification regarding plot etc, which are built in functions type help topic i.e. help plot
1. T = 0: 1:10 This instruction indicates a vector T which as initial value 0 and final value
10 with an increment of 1 in MATLAB is the array.
T = [0 1 2 3 4 5 6 7 8 9 10]
2. F= 20: 1: 100
F = [20 21 22 23 24 ……… 100]
3. T= 0:1/ pi: 1
T= [0, 0.3183, 0.6366, 0.9549]
4. zeros (1, 3)
The above instruction creates a vector of one row and three columns whose values are
zero Output= [0 0 0]
5. Transpose a vector
Suppose T= [1 2 3],
Then transpose T‟= 1
2
3
6. Matrix Operation
a) If a = [1 2 3] b = [4 5 6]
a.*b = [4 10 18]
b) If v = [0:2:8]
v = [0 2 4 6 8]
v (1:3)
ans [0 2 4]
v (1:2:4)
ans [ 0 4]
c) A = [1 2 3; 3 4 5; 6 7 8]
A=
1 23
3 45
6 78
A(2,3)
ans 5
A(1:2,2:3)
ans =
23
45
A(:,2)
ans =
2
4
7
A(3,:)
ans =
678
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power Operator
„ transpose
end
This construction is used if there is one alternative only.
Two alternatives requires the following construction
if expression
commands (evaluated if expression is true)
else
commands (evaluated if expression is false)
end
EXPERIMENT NO1
Generation Of Different Signals In Both Continuous And Discrete Time Domains
THEORY:-
One of the more useful functions in the study of linear systems is the "unit impulse function."
An ideal impulse function is a function that is zero everywhere but at the origin, where it is infinitely high.
However, the area of the impulse is finite. This is, at first hard to visualize but we can do so by using the
graphs shown below.
If b>a, then
Solution:
We now that the impulse is zero except at t=0 so
Sometimes, u(0) is given other values, usually either 0 or 1. For many applications, it is irrelevant what the
value at zero is. u(0) is generally written as undefined.
Derivative
The unit step function is level in all places except for a discontinuity at t = 0. For this reason, the derivative
of the unit step function is 0 at all points t, except where t = 0. Where t = 0, the derivative of the unit step
function is infinite. The derivative of a unit step function is called an impulse function. The impulse function
will be described in more detail next.
Integral
The integral of a unit step function is computed as such:
MATLAB Program:
ylabel('i(t)');
%Ramp function
subplot(3,4,3);
t=0:10;
y=[t];
stem(t,y)
title('ramp function');
xlabel('t(sec)');
ylabel('r(t)');
%Sine function
subplot(3,4,4);
t=linspace(-10,10);
y=sin(t);
plot(t,y);
title('sine function');
xlabel('t(sec)');
ylabel('s(t)');
%Cosine function
subplot(3,4,5);
t=linspace(-10,10);
y=1+cos(t);
plot(t,y);
title('cosine function');
xlabel('t(sec)');
ylabel('c(t)');
%Falling exponential
subplot(3,4,6);
t=linspace(0,10);
y=exp(-t);
plot(t,y);
title('falling exponential function');
xlabel('t(sec)');
ylabel('e(t)');
%Rising exponential
subplot(3,4,7);
t=linspace(-10,10);
y=exp(0.1*t);
plot(t,y);
title('rising exponential function');
xlabel('t(sec)');
ylabel('e(t)');
%Rectangular function
subplot(3,4,8);
t=-10:10;
y=[zeros(1,9),ones(1,3),zeros(1,9)];
stem(t,y);
title('rectagular function');
xlabel('t(sec)');
ylabel('r(t)');
%Sinc function
subplot(3,4,9);
t=linspace(-10,10);
y=sinc(2*3.14*0.1*t);
plot(t,y);
title('sinc function');
xlabel('t(sec)');
ylabel('sinc(t)');
%Triangular wave
subplot(3,4,10);
t=-10:10;
output=((1-2*abs(t))/20);
plot(t,output);
title('triangular wave');
xlabel('t(sec)');
ylabel('x(t)');
%Complex exponential
subplot(3,4,11);
t=-10:10;
c=complex(3,2);
d=exp(c*t);
plot(t,abs(d));
disp(angle(d));
title('complex exponential wave');
xlabel('t(sec)');
ylabel('x(t)');
Result
EXPERIMENT NO 2
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 (cos(ωt)=cos(2πft))-
Xt = cos(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 = cos(2*pi*fd*n1);
[Alternately let n1 be a fixed number of samples, say n = 0:10; & x1 = cos(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 under
sampling and Nyquist sampling conditions.
MATLAB PROGRAM:
tfinal=0.05;
t=0:0.00005:tfinal;
fd=input('Enter analog freuency');
xt=cos(2*pi*fd*t); %define analog signal for comparison
%Condition for under sampling
fs1=1.3*fd; %fs1<2*fd
n1=0:1/fs1:tfinal; %define the time vector
xn1=cos(2*pi*n1*fd); %Generate the under sampled signal
subplot (3,1,1); %plot the analog & sampled signals
plot(t,xt,'b',n1,xn1,'r*-');
title('under sampling plot');
xlabel('time');
ylabel('amplitude')
Result:
Enter analog frequency 200
The plots are shown in Fig.1.1
Output:
EXPERIMENT NO 3
Basic Operations On Given Signals & 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
MATLAB PROGRAM:
(a)Signal Folding
clc;
clear all;
close all;
t=0:0.001:2;
Dept of EEE, AMCEC Page 19
Digital Signal Processing Laboratory -21EEL66
s=sin(2*pi*5*t);
m=length(s);
n=[-m:m];
y=[0,zeros(1,m),s];
subplot(2,1,1);
plot(n,y,'g');
xlabel('time');
ylabel('amplitude');
title('original signal');
y1=[fliplr(s),0,zeros(1,m)];
subplot(2,1,2);
plot(n,y1,'r');
xlabel('time');
ylabel('amplitude');
title('folded signal');
OUTPUT:
Enter the number of samples 5
Enter the sample Values[1,2,3,4,5]
RESULT:
In this Experiment various operations on signals and evaluation of Odd and Even sequences
performed and output obtained.
EXPERIMENT NO 4
Aim: To find the impulse response h(n) of the given LTI system whose response y(n) to an
input x(n) is given.
Theory:
In signal processing, the impulse response or impulse response function (IRF), of a dynamic
system is its output when presented with a brief input signal, called an IMPULSE. More
generally, an impulse response is the reaction of any dynamic system in response to some
external change. In both cases, the impulse response describes the reaction of the system as a
function of time.
A discrete time LTI system (also called digital filters) as shown in Fig.2.1 is represented
by
o A linear constant coefficient difference equation, for example,
y[n] a1 y[n 1] a2 y[n 2] b0 x[n] b1 x[n 1] b2 x[n 2];
o A system function H(z) (obtained by applying Z transform to the difference
Y (z) b b z 1 b z 2
equation). H (z) 0 1 2
X (z) 1 a1 z 1 2a z 2
1 a1 e j a 2e2 j
Given the difference equation or H(z), the impulse response of the LTI system is found
using filter or impz MATLAB functions.
Given only the input sequence x[n] and the output sequence y[n], we can find the impulse
function h[n] by using the inverse operation deconv. (The conv operation convolves 2
sequences x[n] and h[n] to obtain the output y[n]. If both y[n] and x[n] are finite then the
impulse response is finite (FIR). The deconvolution operation is valid only if the LTI
system is „invertible‟
MATLAB Implementation:
h = deconv(y,x): y the output sequence should be of longer length than x. DECONV - Deconvolution
and polynomial division. [Q, R] = DECONV(B, A) deconvolves vector A out of vector B. The result
is returned in vector Q and the remainder in vector R such that B = conv(A,Q) + R. If A and B are
vectors of polynomial coefficients, deconvolution is equivalent to polynomial division. The result of
dividing B by A is quotient Q and remainder R. (Remember conv is polynomial multiplication).
Note: Valid y and x should be given, else the conv verification will result in a slightly different y
(because of the remainder generated by deconv).
h=impz(b,a,N), returns the impulse response h[n], where b and a are coefficient arrays obtained from
difference equation, N = length of the response required. (refer expt.7). For the given difference
equation b=[1]; a=[1 -1 0.9].
IMPZ Impulse response of digital filter. [H,T] = IMPZ(B, A) computes the impulse response of the
filter B/A choosing the number of samples for you, and returns the response in column vector H and a
vector of times (or sample intervals) in T (T = [0 1 2 ...]').
[H, T] = IMPZ(B, A, N) computes N samples of the impulse response.
MATLAB PROGRAM:
RESULT:
Length of impulse response required=5
Coefficient of x[n] = [2 -1]
Coefficient of y[n] = [1 3 2]
EXPERIMENT NO 5
Theory:
The output y[n] of a LTI (linear time invariant) system can be obtained by convolving the
input x[n] with the system‟s impulse response h[n].
The convolution sum is y[n] x[n] h[n] x[k]h[n k] x[n k]h[k]
k k
subplot(3,1,1);
stem(x1);
xlabel('time');
ylabel('amplitude');
subplot(3,1,2);
stem(x2);
xlabel('time')
ylabel('amplitude');
f=conv(x1,x2);
is'); disp(f);
xlabel('time index
n');
ylabel('amplitudef');
subplot(3,1,3);
stem(f);
RESULT:
end
end
disp('The output sequence is y(n)=');
disp(y);
subplot(3,1,1)
;
stem(a,'filled');
title('x1(n)');
xlabel('n--->');
ylabel('Amplitude');
subplot(3,1,2);
stem(b,'filled');
title('x2(n)');
xlabel('n--->');
ylabel('Amplitude');
subplot(3,1,3);
stem(y,'filled');
title('y(n)');
xlabel('n--->');
ylabel('Amplitude');
Output:
Enter the sequence x1(n)=[2 1 2 1]
Enter the sequence x2(n)=[1 2 3 4]
The output sequence is y(n)=
14 16 14 16
EXPERIMENT NO: 6
N – POINT DFT
Aim: To compute N – point DFT of a given sequence and to plot the magnitude and phase
spectrum.
Algorithm:
MATLAB PROGRAM:
Result:
Experiment No: 7
Algorithm:
2) Compute the DFT of both the sequences using the command fft.
4) Compute the IDFT of the previous result using the command ifft
MATLAB PROGRAM
yn='); disp(yn);
%plot
stem(yn);
title('Circular convolution output y(n)');
Result:
First sequence = [1 2 3 4]
Second sequence = [1 2 3 4]
26 28 26 20
Experiment No 8
Algorithm:
2) Use command filter filters the input data x using a rational transfer function defined by the
numerator and denominator coefficients b and a.
3) The command filtic assumes that the input x is 0 in the past. (only for finding forced response)
MATLAB PROGRAM:
clc;
clear all;
n=[-5:50];
x=[(n>=0)]-[(n>4)];
figure(1);
subplot(2,1,1);
stem(n,x,'filled');
xlabel('n--->');
ylabel('x');
subplot(2,1,2);
y=filter(b,a,x);
stem(n,y,'filled');
xlabel('n--->');
ylabel('y');
OUTPUT:
b. Find the output of y(n) -0.25 y(n-1) -0.125 y(n-2)= x(n) with x(n)=u(n)-u(n10) and initial
condition y(-1)=1,y(-2)= -2
clc;
clear all;
n=[-5:50];
ic=filtic(b,a,c);
x=[(n>=0)]-[(n>10)];
y=filter(b,a,x,ic);
subplot(2,1,1);
stem(n,x,'filled');
xlabel('n');
ylabel('x');
subplot(2,1,2);
stem(n,y,'filled');
xlabel('n');
ylabel('y')
OUTPUT:
Experiment No: 9
MATLAB PROGRAM:
clc;
clear all;
N=length(xn);
n=0:1:N-1;
k=0:1:N-1;
Xk=fft(xn,N);
disp(Xk)
MagX=abs(Xk)
PhaseX=angle(Xk)*180/pi
xn=ifft(Xk)
subplot(2,2,1);
stem(n,xn,'filled');
title('input sequence');
ylabel('xn');
xlabel('n');
subplot(2,2,2);
stem(n,xn,'filled');
title('IDFT Sequence');
ylabel('xn');
xlabel('n');
subplot(2,2,3);
stem(k,MagX,'filled');
title('Magnitude Spectrum');
ylabel('Mag Xk');
xlabel('K');
subplot(2,2,4);
stem(k,PhaseX,'filled');
title('Phase Spectrum');
ylabel('PhaseXk');
xlabel('K');
Output:
EXPERIMENT NO : 10
Algorithm:
1) Enter the pass band ripple (rp) and stop band ripple (rs).
2) Enter the pass band frequency (wp) and stop band frequency (ws).
4) Calculate normalized pass band frequency, and normalized stop band frequency w1 and w2
respectively.
w1 = 2 * wp /fs
w2 = 2 * ws /fs
6) Design an nth order digital lowpass Butterworth or Chebyshev filter using the
following statements.
OR
Design an nth order digital high pass Butterworth or Chebyshev filter using the following
statement.
7) Find the digital frequency response of the filter by using „freqz( )‟ function
[H,w]=freqz(b,a,512,fs)
11) Plot the phase response [phase in radians Vs normalized frequency (Hz)].
MATLAB PROGRAM
subplot(211);
plot(w,mag);
grid on;
ylabel('--> Magnitude in dB');
xlabel('--> Normalized frequency in Hz');
Output:
Fig 9.1: Magnitude and phase response of Butterworth IIR Low Pass Filter
Fig 9.2: Magnitude and phase response of Butterworth IIR High Pass Filter
phase=angle(H);
subplot(211);
plot(w,mag);grid on;
ylabel('--> Magnitude in dB');
xlabel('--> Normalized frequency in Hz');
title('Magnitude Response of the desired Butterworh BPF');
subplot(212);
plot(w,phase);grid on;
ylabel('--> Phase in radians');
xlabel('--> Normalized frequency in Hz');
title('Phase Response of the desired Butterworh BPF');
case 2
disp('Frequency response of IIR Butterworth BSF is:');
[b,a]=butter(n,wn,'stop'); % Find the filter coefficients of BSF
z=tf(b,a)
[num,den]=bilinear(b,a,fs);
t=tf(num,den,1/fs)
[H,w]=freqz(b,a,512,fs);% to get the transfer func of the filter
mag=20*log10(abs(H));
phase=angle(H);
subplot(211);
plot(w,mag);grid on;
ylabel('--> Magnitude in dB');
xlabel('--> Normalized frequency in Hz');
title('Magnitude Response of the desired Butterworh BSF');
subplot(212);
plot(w,phase);grid on;
ylabel('--> Phase in radians');
xlabel('--> Normalized frequency in Hz');
title('Phase Response of the desired Butterworh BSF');
end
Output:
enter the IIR filter design specifications
enter the passband ripple 0.3
enter the stopband ripple 40
enter the passband freq 1500
enter the stopband freq 2000
enter the sampling freq 9000
give type of filter 1:BPF,2:BSF1
Frequency response of Butterworth IIR BPF is:
Fig 9.3: Magnitude and phase response of Buttrworth IIR Band Pass Filter
Fig 9.4: Magnitude and phase response of Butterworth IIR Band Stop Filter
Note: For design of filter by impulse invariance method use the command impinvar
instead of bilinear.
switch ch
case 1
disp('Frequency response of Chebyshev IIR LPF is:');
[b,a]=cheby1(n,rp,wn); % Find the filter coefficient of LPF
z=tf(b,a)
[num,den]=bilinear(b,a,fs);
t=tf(num,den,1/fs)
[H,w]=freqz(b,a,512,fs);% to get the transfer function of the
filter
mag=20*log10(abs(H));
phase=angle(H);
subplot(211);
plot(w,mag);grid on;
ylabel('--> Magnitude in dB');
xlabel('--> Normalized frequency in Hz');
Fig 9.5: Magnitude and phase response of Chebyshev IIR Low Pass Filter
Fig 9.6: Magnitude and phase response of Chebyshev IIR High Pass Filter
case 1
disp('Frequency response of Chebyshev IIR BPF is:');
[b,a]=cheby1(n,rp,wn,'bandpass'); % Find the filter coeff of BPF
z=tf(b,a)
[num,den]=bilinear(b,a,fs);
t=tf(num,den,1/fs)
[H,w]=freqz(b,a,512,fs);% to get the transfer func of the filter
mag=20*log10(abs(H));
phase=angle(H);
subplot(211);
plot(w,mag);grid on;
ylabel('--> Magnitude in dB');
xlabel('--> Normalized frequency in Hz');
title('Magnitude Response of the desired Chebyshev BPF');
subplot(212);
plot(w,phase);grid on;
ylabel('--> Phase in radians');
xlabel('--> Normalized frequency in Hz');
title('Phase Response of the desired Chebyshev BPF');
case 2
disp('Frequency response of IIR Chebyshev BSF is:');
[b,a]=cheby1(n,rp,wn,'stop'); % Find the filter coeffici of BSF
z=tf(b,a)
[num,den]=bilinear(b,a,fs);
t=tf(num,den,1/fs)
[H,w]=freqz(b,a,512,fs);% to get the transfer func of the filter
mag=20*log10(abs(H));
phase=angle(H);
subplot(211);
plot(w,mag);grid on;
ylabel('--> Magnitude in dB');
Fig 9.7: Magnitude and phase response of Chebyshev IIR Band Pass Filter
Fig 9.8: Magnitude and phase response of Chebyshev IIR Band Stop Filter
Experiment No 11
Aim: To design and implement FIR filters using different windows to meet the given
specifications.
MATLAB PROGRAM
%FIR Low Pass/High pass filter design using Rectangular/Bartlett Window
y=bartlett(n1);
disp('bartlett window filter response');
end
ch=input('give type of filter 1:LPF,2:HPF');
switch ch
case 1
b=fir1(n,w1,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
plot(o/pi,m);
title('LPF');
xlabel('(a) Normalized frequency-->');
ylabel('Gain in dB-->');
case 2
b=fir1(n,w1,'high',y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
plot(o/pi,m);
title('HPF');
xlabel('(b) Normalized frequency-->');
ylabel('Gain in dB-->');
end
Output:
enter passband ripple 0.05
enter the stopband ripple 0.04
enter passband freq 1500
enter stopband freq 2000
enter sampling freq 9000
enter your choice of window function 1. rectangular 2. bartlett1
Rectangular window filter response
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
plot(o/pi,m);
title('BPF');
xlabel('(a) Normalized frequency-->');
ylabel('Gain in dB-->');
case 2
b=fir1(n,wn,'stop',y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
plot(o/pi,m);
title('BSF');
xlabel('(b) Normalized frequency-->');
ylabel('Gain in dB-->');
end
Output:
enter passband ripple 0.05
enter the stopband ripple 0.04
enter passband freq 1500
enter stopband freq 2000
enter sampling freq 9000
enter your choice of window function 1. rectangular 2. bartlett1
Rectangular window filter response
give type of filter 1:BPF,2:BSF1
Note: Repeat the above program for Blackman, Hammimg and Hannning Window.