DSP Laboratory Eng. Abdullah Abdo
Experiment 4
IMPULSE RESPONSE OF A GIVEN SY:
‘Aim: To write the MATLAB code to find the impulse response of a given second-order
system whose difference equation representation is given.
Assume a second-order system represented by the following difference equation:
y(n) = box(n) + bix(n — 1) + baox(n - 2) + aiy(n — 1) + azy(n — 2)
Theory:
Impulse response of a system is defined as the output of a given system, when the
input applied to the system, is in the form of an unit impulse, or a Dirac delta function.
The impulse response completely characterizes the behavior of any LTI system. The
impulse response is often determined from knowledge of the system configuration and
dynamics, or can be measured by applying and approximate impulse to the system input.
Discrete-time LTI systems can also be described using Difference Equations. A linear
constant-coefficient difference equation can be of the form:
EhLoaryln — k] = ZkLo yin — ¥]
‘Where the integer N is termed the order of the difference equation, and corresponds to the
maximum memory involving the system output. The order generally represents the
number of energy storage devices in a physical system.
We can calculate the impulse response of the system using Z-transforms as shown in
the following example:
Consider a difference equation:
y(n) = x(n) + 3 x(n — 1) - 0.12 x(n — 2) — 0.2 y(n —1) + 15 y(n— 2)
This can be rewritten in the standard form as:
y(n) + 0.2 y(n = 1) - 15 y(n = 2) = x(n) + 3 x(n - 1) - 0.12 x(n = 2)
Finding the Z-transform of the equation:
¥(Z) + 0.2 2-4¥ (Z) - 1.5 2-°¥(Z) = X(Z) + 3 Z*X(Z) - 0.12 Z-X(Z)
Transfer Function of the system can be obtained as :
Y¥@) _1+3274-01227
X@) 1+02z t= 15z2%DSP Laboratory
By long division, we get
H(Z)=1-2.827'+ 7.022 ~*— 21.4 2-3 + 65.03 2-*
By taking Inverse-Z transform, we can obtain the Impulse Response as:
h[n] = [1 -28 7.02 21.4 65.03]
MATLAB COD!
= Experiment 4 : Impulse Response of a
Eng. Abdullah Abdo
clear all; close all; cles
% Accept Input and Output signal Coefficients:
b = input (‘Enter the coefficients of x(n) in 1-D Matrix Form:
a = input (‘Enter the cocfficients of y(n) in 1-D Matrix Form: ');
N = input (‘Enter the number of samples of impulse response ds MG
% Calculate Impulse Response using IMP2 funetio:
& [4-1] = IMPZ(B,A,N) computes N samples of the impulse response, using
§ coefficients B and A from difference equation representation.
thy t] = impz (by a/¥) +
SPlot and Display impulse res;
stem(t,a);
title (tr
ylabel (*h(n) "}7 xlabel('n");
ulse Res!
disp (‘Impulse Resp:
disp(h);
OUTPU’
Enter the coefficients of x(n) in 1-D Matrix For
10.2 -15]
Enter the coefficients of y(n) in 1-D Matrix Form: [1 3 -0.12]
Enter the number of samples of impulse response desired: 5
Impulse Response Coefficients:
1.0000
-2.8000
7.0200
-21,3960
65.0304DSP Laboratory Eng. Abdullah Abdo
ene
” T T T
A J
4 T
A 1 L 1 L ! L tDSP Laboratory Eng. Abdullah Abdo
Experiment 5
LINEAR CONVOLUTION OF TWO GIVEN SEQUENCES
To write the MATLAB code to perform Linear Convolution upon two given
discrete time signals.
Theory:
Convolution is the process used to find the response of a Linear Time Invariant system
to a given input, assuming we already know the impulse response of that system. In case
of continuous
ime signals, we can find the system response using the Convolution
Integral, while in case of discrete-time systems, the response can be calculated using the
Convolution Sum.
Let x1(n) and x2(n) be two discrete-time signals. The convolution sum of the two
signals can be calculated using the formula:
y(n) = x1 (n) * x20) = Dw X1(K) x2(n — KY
If-x1(n) is a M- point sequence and x2(n) is an N — point sequence, then the
convolved sequence, y(n) is a (MN-1) — point sequence.
We can perform the convolution by different methods
1. Using MATLAB’s “CONV” function
MATLAB has a built-in function called “conv” function, which basically performs a
linear convolution of any two given sequences.
2. Using the Linear Convolution Sum formula :
Here, we use the convolution sum formula and substitute values of n and k in the
expression, and calculate the values of the convolved signal, Alternatively, we can
perform the signal inversion-time shift-superposition method, by which we can
calculate the resultant signal values.
Assume two discrete-time sequences x1 and x2 in a Linear Time Invariant System, given
by:
xA(n) = (1, 2, -1, 3} and x2(n) = (2,3, -2}
We see that length of sequence x1 is (M = 4) and that of sequence x2 is (N = 3).
Therefore, the length of the convolved sequence will be (M+N-1 = 6)DSP Laboratory Eng. Abdullah Abdo
Using any of the above given methods, we see that the resultant convolved sequence can
be given by:
y(n) = x1(n) * x2(n) =(2 7 2-111 6}
MATLAB COD!
1. Using “conv” function:
8% Linear Convolution using CONV command,
clear all; close all; cle;
% Accept input signal sequences
x1 = input ('Enter Input Sequence for Signal xl(n): "7
x2 = input (‘Enter Input Sequence for Signal x2 (2): ');
Perform Linear Convolution using CONV command
yecony (x1,%2) 7
“Plot Input and Convelved Signals
subplot (3,1,1)7
stem (x1);
title('Input Signal x1(n) "ys
xlabel('n'); ylabel ("x1 (0) ');
subplot (3,1,2)7
stem (x2) 7
title (‘Input Signal x2(n)")¢
xlabel('n'}; ylabel ("x2 (n)');
subplot (3,1, 3);
stem(y) 7
title('Convolved Signal y(n) = x1 (n)*x2(n)');
xlabel('n'); ylabel("y(a) "Vz
% Display the convolved Sequence in Conmand Window
disp('Convolved sequence: ");
aisp ly):
2, Using Conyolution Sum formula:
8 Linear Convolution without using CONV command
clear sll; close all; cle;
x1 = input (‘Enter Input Seguenc
nl = Length (x2);
x2 = input (‘Enter
n2=Length (x2);
N= nl+n2-1; tLengch of Convolved Sequence
T= L:N; & Create Time Index
Szero padding to make sequences of length N
xl=[x1 zeros (1,N-n1) 1;
x2=(x2 zeros (1,N-n2)]7
SInitializing Output sequence of zeros
y = zeros (1,N);
‘Performing Linear Convolution:
for a = iN
for k= lin
y(n) =y (n) 4301 (c) #2 (nk)
gaal xl(ay: 05
sequen Signal x2(a): VeDSP Laboratory
and Output
subplot (3,1,1)+
stem(T, x1);
title('Input Signal x1(n)")z
xlabel('n'}; ylabel (‘x (n) ')y
subplot (3,1,2) +
stem(T, x2);
tle(in gnal x2(n)");
xlabel('n'); ylabel ("x2 (n)');
subplot (3,1,3);
stem(T, y)7
tle (*Convolved
xlabel('n'); yla\
ignal y(n)
Leryin) Ye
x1 (n) "x2 (0) "D5
Eng. Abdullah Abdo
% Display the convolved Sequence in Command Window
disp('Convolved sequence: ");
aisp(y)
OUTPI
Enter Input Sequence for Signal x1(n): [1 2 -1 3]
Enter Input Sequence for Signal x2(n): [2 3 -2]
Convolved sequence:
272-111-6
reas
4 T T T T T
L L L L
esis
4 T T T T T T T
d L 1 L L L L 1
Coch ints
" T T T T T T T T
BY t
al 1 L L L L L L LDSP Laboratory Eng. Abdullah Abdo
Experiment 6
CIRCULAR CONVOLUTION OF TWO GIVEN SEQUENCES
Aim: To write the MATLAB code to perform Circular Convolution upon two given
discrete time signals.
Theory:
The Circular convolution, also known as cyclic convolution, of two aperiodic functions
occurs when one of them is convolved in the normal way with a periodic summation of
the other function. Circular convolution is only defined for finite length functions (usually
equal in length), continuous or discrete in time. In circular convolution, itis as ifthe finite
length functions repeat in time, periodically. Because the input functions are now
periodic, the convolved output is also periodic.
Circular convolution sum can be calculated using the formula:
y(n) = x1(n) * x2(n) = LNB x1(n) x2((m— n))y
Form =0,10.,N—1
Circular convolution can be performed in different ways :
1. Using the expression for linear convolution sum, but assuming the signal repeats
periodically. This can be done by changing the negative indices of (n-k) to repetitions
of the latter portions of the original aperiodic signal.
2. Convolution in time domain corresponds to multiplication in frequency domain. To
make use of this property, we can calculate the DTFT of each of the aperiodic signals,
multiply these in the frequency domain, and find the IDFT of the product, to get the
periodic convolved signal in time domain.
Let us take the case of two discrete-time aperiodic signals given by:
x1(n) = {2,1,2,1} and x2(n) = {1,2,3,4}
Using the formula with N = 4.
Form =0:
YO) =E3-o x1 (n) x2((-n)), = 14DSP Laboratory Eng. Abdullah Abdo
Form
y(1) = D3.9x1(n) x2((1 —n))4 = 16
Form =2:
¥(2) = Diao ¥1 (n) x2((2 — n))q = 14
Form=3:
¥(3) = Deo ¥1(n) x2((3 — n)), = 16
So, we get the circular convolution sum as: y(n) = (14,16,14,16}
MATLAB CODE;
1. Using Convolution Sum Formula:
sec ar Convelutien using
Selear all; close all
x1 = input ('Enter Inpu
nl = Length (x1);
x2.
Signal xlin}: "3
input ("Enter Signal x2(n): "Ns
adeLength (x2);
N= max(nt,n2); 8
T= 1:N; § Create Time Index
S2ero padding to make sequences of length N
xis [x1 zeros(1,N-nl) 1s
x2=[x2 zeros (1,N-n2) 1;
8
alizing outp
sequence of zeros
y = zeros (1,N)¢
forming Linear Convolution:
iemntl; #(m-ntl) since we're taking index from 2
if (ico)
ientir
end
y(m)=y (m) 441 (n) *42 (i); SConvolution Sum Formula
end
end
% Plot Input and out
subplot (3,1,1)¢
stem(T, x1);
title(' Input Signal xin} ')s
SequencesDSP Laboratory Eng. Abdullah Abdo
xlabel ('n')7 ylabel (‘x1 (n)"
subplot (3,1,2)¢
stem(T, x2);
title('Input Signal x2in}')+
xlabel('n!); ylabel ("x2 (0)
subplot (3,1,3) 5
stem(T,y);
title('Convolved Signal y(n) = x1(n)*x2(n)")s
xlabel('n'); ylabel(*y(n)')7
& Display the convolved Sequence in Command Window
disp('Convolved sequence:');
disply);
2. Using “cconv” function.
S$ Cizeular Convolution using CCONV
Sclear all; close all; cles
% Accept input signal sequences
xi = input (‘Enter Inputs
x2 = input ("Enter Input Sequence for Signal x2(n):
quence for Signal x1 (n):
max (Length (x1), length (x2) )
SPerform Linear Convolution using CONV command
conv (x1, 2,2) 5
SPlot Input and Convolved Signals
subplot (3,1,1)+
stem(x1) 7
title('input Signal xl (o)")
xlabel ('2'); yLabel ("x1 (9) ");
subplot (3, 1,2);
stem(x2) 7
title('Iaput Signal x2(a)")7
xlabel('n'); ylabel('x2(n)");
subplot (3,1,3)¢
stem(y) 7
title('Convolved Signal y(n) = x(n) *x2(n)")s
xlabel('n'); ylabel("y(n)")
§ Display the convolved Sequence in Command Window
disp('Convolved sequence");
disp(y)sDSP Laboratory
ouTPU
Enter Input Sequence for Signal x1(n): [2 12.1]
Enter Input Sequence for Signal x2(n): [12 3 4]
Convolved sequence:
x4(n)
x2{n)
14 16 14 16
Input Signal x1(n)
Eng. Abdullah Abdo
16 2 25 3 35 4
n
Input Signal x2(n)
18 2 25 3 35 4
n
Convolved Signal yin) = x1(n)*x2(n)
18 2 25 3 38 4DSP Laboratory Eng. Abdullah Abdo
Aim: Computation of N point DFT of a given sequence and to plot magnitude and phase
spectrum.
hheory:
DF
stands for Discrete Fourier Transform. It is used to find the amplitude and phase
spectrum of a discrete time sequence.
The N-point DFT ofa finite-length sequence x(n) of length N, is given by X(k) as
w-oFr
x(n) <> X(k)
Basic equation to find the DFT of a sequence is given below.
X00 = BAB x(nye IO
For example: Find the DFT of the sequence x(n) = {0,1,2,3}
In this case N=4.
For k=0,
X(0) = SAC} x(n)e® = x(0) + x(1) +. x(2) +.x(3) =0F14243=6
For k=l,
X(1) = EAS xno MP" (0) € MF 4 x(1) E+ 202) OE? +
x(3) eG)
= -j-243j)=-2+j2
Similarly,
For k-2,
X(2) = SAB x(nyeMP?"= (0) €-F$ x(1) CH x(2) OI? +
x(3) e73
=0-142-3 =-2
For k=3,
X(3)
Hence, X(k)
Experiment 6 :
Sclear all; close all; cle;
pt Input sequence from user
input (‘Enter the sequence x(n) : "Jy
”DSP Laboratory Eng. Abdullah Abdo
N = Length (xn);
Xk = zeros(N, 1); SInitialize zero matrix for DFT sequence
Scalculate DFT using formula
n= O:N-l;
for k = 0:N-1
Xk(KHL) = exp (-J*2*pi¥ktn/N) *xn7
end
SDisplay DFT Sequenc
disp ("DPT Sequence : X() 24;
Asp (XK) +
wPlot Signals
n= O:N-1; $1ime base
% Input Sequence
subplot (2,2, {1:21};
stem(ny xn);
title(*tnput Sequence x(n) ');
xlabel ('n') sylabel ("x (0) "0
% Output Magnitude Plot
subplot (2/2,317
stem(n, abs (Xk)) +
oxida
title (Mm
xlabel('n');
ude Plot of DET + |X(K)|")i
abel ("|X (k) |"
$ output Phase Plot
subplot (2,2,4) +
stem(n, angle (Xk) ")s
grid ons
title('Phase Plot of DET
xlabel('n') ylabel ('Ancle");
Le (KD) "D5
OUTPUT:
Enter the sequence x(n) : {0 1 23]
DFT Sequence : X(k)
6.0000
-2,0000 + 2,00001
-2.0000 - 0.00001
-2.0000 - 2.00001
YYDSP Laboratory Eng. Abdullah Abdo
Input Sequence x(n)
3
2
1
0 L L L
0 05 1 15 2 25 3
n
Magnitude Plot of DFT : (X{k)| Phase Plot of DFT: angle(X{k))
6 4
2
2
> 0
<
2
wvDSP Laboratory Eng. Abdullah Abdo
Experiment 8
LINEAR CONVOLUTION U!
iG DFT ANI
IDFT
Aim: To calculate the Linear Convolution of two sequences using DFT and IDFT
Theory:
An interesting property of the Discrete Fourier Transforms, is the effect it has on
convol
mn. Convolution of two signals in the time domain translates to a multiplication of
their Fourier transforms in the frequency domain. In this procedure, we find the discrete
Fourier transforms of the individual signals, multiply them, and apply an Inverse Fourier
Transform upon the product, to get the convolved signal in the time domain,
If x(n) and h(n) are the two sequences of length ‘I’ and ‘m’ respectively. then X(k) and
H(k) their DFT’s of length N=L+M-1
Y¢k)=x(Kh(k)
Therefore the linear convolution of two sequence is the N point IDFT of Y(k)..
Example: Find the linear convolution of x(n)={1,2} and h(n)={1,2,3} using DFT and
IDFT method.
Solution: Given, x(n)={1,2}
hin)=(1,2,3}
here L=2,M=3
‘Therefore, N=4
x(n)={1,2,0,0} and h(n)={1,2,3,0)
Finding X(k) using FFT algorithm:
XK) = (3, 1-25, -1, 112)}
Finding H(k) using FFT algorithm
HK) = (6, -2-2) 2-242) }
Product Y(k) is calculated as :
Y(k) = XH)
MeDSP Laboratory Eng. Abdullah Abdo
¥(K) = (18, -6 + 2j,-2,6-2)}
Finding y(n) using FFT algorithm: y(n) = {1,4,7,6}
MATLAB CODE:
% Experiment 7 : Linear Convolution using Fourier Transforms
clear sll; close sil; cle;
BAccept input sequences
x1 = input (‘Ester Input Sequence for Signal x1 (2): ');
nl = Length (x2);
x2
input (‘Enter Input Sequence for Signal x2(a): "De
ength (x2);
N
nl4n2-1; % Length of convolved sequence
T= LN;
Szero padding to make sequences of length N
x1 zeros (2,N-nl) 17
x2=[x2 zeros (2,Nen2) 1:
Scalculate N-point DFT and IDFT.
=£ft (x1,N); § N-point DFT of xi
£0 (x2,N) 7% N-point DPT of x2
y
y3eyl.*y2; $ Multiplication in time domain
yriftt(y3,N); % N-point IDFT of y to rec:
% Plot Input and output Sequence:
subplot (3,1, 1)
stem (T, x1);
x result
title(‘Input Signal x1(n)")s
xlabel ('n'); ylabel ("x1 (0) ")
subplot (3,1,2)7
stem (T, x2);
title('Input Signal x2(n)")s
xlabel('n'); ylabel ('x2 (2) ")7
subplot (3,1, 317
stem(T,y);
title(*Convolved Signal y(n) = x1 (n)*x2(a)")7
xlabel ('n'); ylabel('y(a) Ve
% Display the convolved Sequence in Command Window
v
disp('Convolved sequence
disp (yl;DSP Laboratory
ourPU
x1(n)
x2{n)
yin)
Enter Input Sequence for Signal x1(n): (1 2]
Enter Input Sequence for Signal x2(n): [1 2 3]
Convolved sequence:
7
Input Signal x1(n)
2
1
o
1 15 2 25 3 35
n
Input Signal x2(n)
4
2
0
1 15 2 25 3 35
n
Convolved Signal y(n) = x1(n)*x2(n)
10
5
0
15 2 25 3 35
Eng. Abdullah AbdoDSP Laboratory Eng. Abdullah Abdo
IDFT
Aim: To calculate the Circular Convolution of two sequences using DFT and IDFT
‘Theory:
Convolution in time domain corresponds to multiplication in frequency domain, To
make use of this property, we can calculate the DTFT of each of the aperiodic signals,
multiply these in the frequency domain, and find the IDFT of the product, to get the
periodic convolved signal in time domain,
Example: Find the circular convolution of x(n)={1,2,3,4) and h(n)={4,3,2} using DFT
and IDFT method.
Solution: Given two signals, x(n)=(1,2,3.4} and hin)=(4,3,2)
Finding X(k) using FFT algorithm
X(k) = (10,
-2j ,-2 ,-2-2) }
Finding H(k) using FFT algorithm
Hk) = (9, 2-3), 3,243} })
Y(k) = XH)
Y(K) ={ 90, 2410}, 6, 2-10) }
algorithm
y(n) = { 22,19, 20,29}
Finding y(n) using
|ATLAB CODE
& Experime:
on using Fourier Tran
clear all; close all; cle
input seque:
input ("Ester Input Secue: Signal xl(a): ")¢
= Length (2);
x2. = input (‘Enter Input Secuence for Signal x2(n)
n2=Length (x2) 5
Nemax(nl,n2)7 % Length of convolved sequence
xi=[x1 zeros (1,N-nl) 1;
x2=[x2 zeros (7,N-n2)];
wDSP Laboratory Eng. Abdullah Abdo
‘alculate N-point DPT and IDFT
(x1,N); % N-point DET
(x2,N);
N-point DET of x2
§ Multiplication @ domai
3,N); % N-point IDF? o
ver result
Input and output Sequen
subplot (3,1,1)+
stem(T, x1);
tle (' Inp
Signal x1(n)")s
xlabel('n'); ylabel ("x1 (0) ")z
subplot (3,1,2);
stem (T, x2);
tle ("input Signal x2 (a)
xlabel('n'); ylabel('x2(n)');
©(3/1,3)7
stem (Ty y)7
tut
e('Convelved Signal y(n) = xl (n)*x2(n)");
xlabel('n'); ylabel('y(n)") grid on;
& Display the convolved
and Window
disp('Convolved sequence
disply):
oureu
Enter Input Sequence for Signal x1(n): [1 2 3 4
Enter Input Sequence Signal x2(n): [4 3 2]
Convolved sequence:
22 19 20 29
Input Signal x1(n)
15 2 25 3 35 4
Input Signal x2(0)
°
; 15 as 4
40
Ea
°
; 75 2 38 3 3s 4
ADSP Laboratory Eng. Abdullah Abdo
Experiment 10
DESIGN AND IMPLEMENTATION OF FIR FILTE!
To design and implement a FIR Filter for the given specifications
Theory
A linear-phase is required throughout the passband of the filter to preserve the shape of
the given signal in the passband. A causal IIR filter cannot give linear-phase
characteristics and only special types of FIR filters that exhibit center symmetry in its
impulse response give the linear-space, An FIR filter with impulse response h(n) can be
obtained as follows:
hin) O