DSP File Alk-2
DSP File Alk-2
ECECCI1S
SUBMITTED BY:
Keshav Yadav
ECE-1
2020UEC2520
2
INDEX
Exp Title Date
No.
12. To write MATLAB code for zero input limit cycle operation.
EXPERIMENT-1
Aim:-
An LTI system is specified by the difference equation
MATLAB Code:-
x=[1];
y=[1,-0.9]7
subplot(4,1,1);
zplane (X,y) %plotted pole zero plot
h=freqz(x,y); %transfer function
subplot(4,1,2)
plot(abs (h))
title ('Magnitude response');
subplot(4,1,3)
plot(angle (h))
title ('Phase response');
n=1:100
z2=zeros (length(n));
z2(1)=0;
for 1i=2:length(n)
z2(1)=0.9%2z2 (i-1) +cos (0.05*pi*i);
end
subplot(4,1,4);
stem(n,z2);
title("Steady state response");
Output:-
1:5
-]
>
go @%
5,
g i
E
Real Part
o Magnitude response
sk ]
o n
0
o Phase response
05p ]
TO0
Result-
The transfer function of the given difference equation was found and its
frequency response and pole-zero plot was plotted.
EXPERIMENT-2
1 0<n<3
z(n) =
0 otherwise
(i) Compute DTFT X (¢’“) and plot its magnitude and phase
(i) Compute 4-point. 8-point. and 16-point DFT of z(n).
MATLAB Code:-
2a)clc;
close all;
clear all;
x= [1,1,1,11;
wkl=linspace(0,4*pi,16);
X3=zeros (length (wkl)) ;
for i=1l:length(wkl)
sum=0;
for n=1:4
A=-j*wkl(1)*n;
Z=exp (BA);
y=x(n)*z;
sum=sum+y;
end
X3 (1)=sum;
end
subplot (5,1,4)
stem(wkl,abs(X3));
title ("Magnitude response of 8 point DFT");
% Finding x<n+3>9
outZifin:zeros(length(a));
for i=l:length(out2_fin)
j=1-3;
if (§<=0)
j=j+length(a);
out2_fin(j)=a(i);
else
out2 fin(j)=a(i);
end
end
% x<-n>
out3_ fin=zeros(length(a))
for i=1l:length(a)
j=length(a)-i+1
out3 fin(j)=a(i)
end
figure
subplot(2,2,1)
stem(a)
subtitle ("Original sequence");
subplot(2,2,2)
stem(angle (2))
subtitle ("Phase of the sequence");
subplot(2,2,3)
stem(out3_fin)
subtitle ("reverse sequence");
£3=fft (out37fin, length(a))
subplot(2,2,4)
stem(angle (£3))
subtitle ("Phase of reversed sequence");
Output:-
¢ d
o2 0
f 5 10
). 0 5
&
)
o reverse sequence Phase of reversed sequence
P ® i [}
05 ‘
@ @
5 0 o
° j
P -0.5} o)
) At -
0 'l' N &
0 5 10 0 5
Result-
DTET for the given sequence was found and its frequency response was plotted.
Timeshifting and reversal property was verified.
1C
- EXPERIMENT-3
Aim:-
Let zy(n) = {1.2.2,1} and z(n) = {1, ~1,~1,1}.
(a) Write a MATLAB program to compute the 4-point circular convolution x;(n) ® x,(n)
(b) Write a MATLAB program to compute their linear convolution using circular convolution
MATLAB Code:-
2a)
clc;
close all;
clear all;
% Circular convolution
x1=[1,2,2,1]
x2=[1,-1,-1,1]
N=4
y=zeros (N) ;
for n=0:N-1
for m=0:N-1
nl=mod (n-m, N)
y(n+l)=y (n+1) +(x1(m+1) *x2 (nl+1))
end
end
subplot(2,2,1) %2 rows 2 columns 1lst value
stem(x1)
subtitle ("Sequencel™);
subplot(2,2,2)
stem(x2)
subtitle ("Sequence 2");
subplot(2,2,3)
stem(y)
subtitle ("Circularly convolved sequence");
2b)
N=4
a=[12 2 1];
Nl=length(a);
b=[1 -1 -1 1];
N2=length(b);
Ny=N1+N2-1;
az=[a zeros(l,Ny-N1)];
bz=[b =zeros(l,Ny-N2)];
y=zeros(1,Ny);
%circular convolution
10
for n=0:Ny-1
m=0:Ny-1
nl=mod(n-m,Ny)
bs=bz (nl1+1)
ab=az.*bs;
y(n+l)=sum(ab);
end
n=0:Ny-1
subplot(1,3,1)
stem(n,az, 'filled");
xlabel('n'");
title('xl(n)");
subplot(1,3,2)
stem(n,bz, "filled");
xlabel('n'")
title('x2(n)");
subplot(1,3,3);
stem(b) ;
xlabel('n");
'
title('y(n)");
Output:-
Sequence1 Sequence 2
2 & 1 q
15 05
1 0
0s 0.5
0 -1 € S
1 2 3 a4 1 2 3
il
11
2—g—oXii) * LU 1 yin)
18 08 08
16 06 06
14 04 04
12 02 02
i) 0 oo 9 O
08 02 02
06 04 04
04 08 06
02 08 08
2 4o e o 4 o 2 3 4
n n n
Result-
Circular convolution and linear convolution using circular convolution were
found andplotted.
12
EXPERIMENT-4
Aim:-
The impulse response of LTI system is i(n) = {1, 0, —1}. Write a MATLAB program to compute
the response of the system if the input is z(n) = (n+1). 0 < n <9,
(a) Use overlap-save method. Let the length of each section is N = 6
(b) Use overlap-add method. Let the length of each section is N =4
MATLAB Code:-
2a)
n=0:9;
N=6;
x=n+1;
Lenx= length(x);
h=[1 0 -1];
M=length (h);
M1=M-1;
L=N-M1;
hz=[h zeros(l,N-M)]; S%Appending N-M Zeros
nhz=0:1length(hz)-1;
xz=[zeros(1,M1l) x zeros(l,N-1)]; %Preappending M-1 Zeros
nxz=0:1length(xz)-1;
K=ceil ((Lenx+M1-1) /L) ;
y=zeros (K,N) ;
for i=0:K-1
X1=xz (1*L+1:1*L+N)
for j=0:N-1
m=0:N-1;
nl=mod (j-m,N);
hs=hz (nl+1);
xh=xi.*hs;
vy (i+1,J+1)=sum(xh);
end
end
y=y(:,M:N)'; %DiscaRding First M-1 Samples
y=[y(:)]1'; %Concatening The Output
ny=0:1length(y)-1;
subplot(1,3,1);
stem(nxz,xz,"'filled");
xlabel('n");
ylabel ("x(n)");
subtitle('x(n)");
title ("Using Overlap-save method");
subplot(1,3,2);
13
stem(nhz,hz,'filled");
xlabel('n'");
ylabel('h(n)")
subtitle ('h(n) )7
subplot(1,3,3)
stem(ny,y, 'filled');
xlabel('n'");
ylabel ("y(n)');
subtitle('y(n)"' )7
2b)
clear all;
close all;
clc;
n=0:9;
N=4;
x=n+1;
Lenx=length (x);
h=[1 0 -1];
M=length (h);
M1=M-1;
L=N+M-1;
hz=[h zeros(l,L-M)]; %Appending N-M Zeros
nhz=0:1length(hz)-1;
K=ceil (Lenx/N) ;
xx=[x zeros(l,N*K-Lenx)]; %Preappending M-1 Zeros
nxx=0:length(xx)-1;
y=zeros (K,N) ;
for i=0:K-1
X1=xxX (1*N+1:N* (1+1));
xr=[x1 zeros(1l,M1)1;
for j=0:L-1
m=0:L-1;
nl=mod (j-m, L) ;
hs=hz (nl+1);
xh=xr.*hs;
vy (i+1,J+1)=sum(xh);
end
end
yy=[1; %Adding Last M-1 Samples
for i=1:K-1;
y(i,:)=[y(i,1:N) y(i,N+1:L)+y(i+1,1:M1)];
end
yy=ly(1,1:L) y(2,M:L) y(3,M:L-Ml)];
ny=0:1length (yy)-1;
subplot(1,3,1);
stem(nxx,xx, "'filled");
xlabel('n");
ylabel ("x(n)");
subtitle('x(n)");
title ("Using overlap add method");
subplot(1,3,2);
stem(nhz,hz,'filled");
xlabel('n");
ylabel ('h(n)");
subtitle('h(n)");
subplot(1,3,3);
stem(ny, yy, 'filled');
xlabel('n");
ylabel ('y(n)");
subtitle('y(n)");
Output-
Using Overlap-save method
© X » h(n) 5
9 o 0.8
0
8 * 06
7 L 04
2
6 0.2
s
% ZE= oo oo 9l
ES
4 02
K
3 04
2 08
&
1 T 08 °
oo po—— 1 10
0 10 20 0 5 0 10
15
Using overlap add method
x(n) h(n) n
0 * * 2
9 L 0.8
0
8 * 0.6
7 L] 04
2
6 0.2
Ts
% E= oo o0 9% = 4
4 02
5
3 04
2 06
-8
Y 08 .
0 e - 10 -
0 5 0 0 5 5 0
n n n
Result-
Response of the LTI system was found using overlap save and overlap add method.
16
EXPERIMENT-5
Aim:-
Consider the sequence
z(n) = cos(0.487n) + cos(0.52wn)
(1) Determine and plot the 100-point DFT of x(n). 0 < n < 10 (pad 90 zeros).
(ii) Determine and plot the 100-point DFT of z(n), 0 < n < 100.
Comment on the results obtained in above two parts.
MATLAB Code-
:199;
k=1:100;
x=cos (0.48*pi*n)+cos (0.52*pi*n);
xpad=[x(1:10),zeros(1,90)]; %$first input signal with
90padded zeros
N=100;
w=2*pi/N;
DFTpadded=zeros (1,100);
for 1=1:100 %kth sample of DFT is 1 here
for i=1:100
a=xpad (1) *exp (-j*w* (1-1)*1);
DFTpadded
(1) =DFTpadded (1) +a;
end
end
xoriginal=x(1:100);
DFToriginal=zeros(1,100);
for 1=1:100 %kth sample of DFT is 1 here
for i=1:100
a=xoriginal
(1) *exp (-J*w* (i-1)*1);
DFToriginal (1)=DFToriginal(1l)+a;
end
end
subplot(2,2,1);
stem(x);
title("Original signal ");
subplot(2,2,2);
stem(DFToriginal);
title ("DFT of original signal");
subplot(2,2,3);
stem(xpad) ;
title("Zero padded input signal");
17
subplot(2,2,4);
stem(DFTpadded) ;
title ("DFT of zero padded signal");
Output:-
Original signal
Result-
100 point DFT using zero padding and without zero padding was found,both
wereperiodic with n=100.
18
EXPERIMENT-6
Aim:-
Let 2(t) = sin(27697t) + sin(271209¢), sampled at the rate of f, = 8000 Hz to get a discrete-time
signal z(n). Compute the DFT coefficient X (k) at the frequency bin k = 18,20,22,24,31,34,38
using Goertzel algorithm and detect which key of touch-tone pad is pressed
MATLAB Code-
clear all;
close all;
clc;
N=205; %DFT length
n=0:N-1;
x=s1n (2*pi*n*697/8000)+sin (2*pi*n*1209/8000);
if length (x)<N
xz=[x zeros(l,N-length(x))];
else
XZ=X
end
xl=[xz 0]
k=[18 20 22 24 31 34 38];
for i=1:7
W(i) =exp(-j*2*pi*k(i)/N);
den (i, :)=[1 -2*cos(2*pi*k(i)/N) 11;
vk(i,:) =filter(l, den(i, :), x1);
Xk(1)=vk(1,N+1)-W(i)*vk (i, N);
end
stem(k,abs
(Xk), 'filled");
xlabel ('k");
ylabel(' [X (k) |");
title ('DFT at k=18,20,22,24,31,34,38");
19
Output-
60
IX()I
40
201 4
=3
=3
=)
N
M
NB
N
S
No
©
R
©
£
©
&
©
Result- &
20
EXPERIMENT-7
Aim:-
Design an FIR filter to meet the following specifications: passband edge F, = 2 KHz. stopband edge
F, = 5 KHz. Passband attenuation A, = 2 dB. Stopband attenuation A, = 42 dB, and Sampling
frequency Fr = 20 KHz. Determine the impulse response and provide a plot of the frequency
response of the designed filter
MATLAB Code-
clear all;
close all;
clc;
Fp=2000; $%Fp=2KHz
Fs=5000; %Fs=5KHz
Ft=20000; %Ft=20KHz
wp= (2*pi*Fp)
/Ft;
ws=(2*pi*Fs)/Ft;
trwidth=ws-wp; 3%Transition Width
subplot(2,2,1);
stem(n,hd, "filled");
axis([-1 M -0.15 0.51);
xlabel('n");
ylabel ('hd(n)");
title('Ideal Impulse Response');
subplot(2,2,2);
stem(n,whan, 'filled'");
axis([-1 M -0.1 1.2]);
21
xlabel('n");
ylabel ('w(n)");
title('Hann Window');
subplot(2,2,3);
stem(n,h, 'filled");
axis([-1 M -0.15 0.51);
xlabel('n'");
ylabel ("w(n)');
title('Practical Impulse Response');
subplot(2,2,4);
plot (w/pi, HwdB) ;
axis ([0 1 -100 10]);
xlabel ('Frequency in pi ');
ylabel ('dB");
title ('Magnitude Response');
Output-
04 1
Zo2 G \
2 308
0
hd o
0 5 10 15 20 0 5 10 15 20
n B n
Practical Impulse Response Magnitude Response
0
04
5To2 T T % &0
0 .n‘o‘ . ™ ‘.m
-100
0 5 10 15 20 0 05 1
n Frequency in pi
Result-
Impulse response of the FIR filter was found and its frequency responsewas
plotted.
22
EXPERIMENT-8
Aim:-
Design a length-21 digital differentiator using a Hamming window.
MATLAB Code
clear all;
close all;
clc;
M=21; S$Hamming Window Length=21
tau=(M-1)/2;
n=0:M-1;
hd=((cos (pi* (n-tau))) ./ (n-tau))-((sin(pi*(n-
tau))) ./ (pi*(n-tau).~2)); %manually calculated impulse
response
d(tau+l)=0; gmanually calculated using 1l'hopital's
whamm=hamming (M) '; %converted to column matrix for
multiplication
h=hd. *whamm;
w=0:0.01:pi;
Hw=freqz (h,1,w);
Hrw=exp (-j* (pi/2-10*w))
. *Hw; %amplitude plotted using the
function H(e” (jw))=Hr(e"jw) *exp(J (pi/2-tau*w))
subplot(2,2,1);
stem(n,hd, "filled");
axis([-1 M -1.2 1.21);
xlabel('n'");
ylabel ('hd(n)");
title('Ideal Impulse Response');
subplot(2,2,2);
stem(n,whamm, 'filled"');
axis([-1 M -0.2 1.21);
xlabel('n");
ylabel ("w(n)');
title ('Hamming Window');
subplot(2,2,3);
stem(n,h, 'filled");
axis([-1 M -1.2 1.2]);
23
xlabel('n");
ylabel ('h (n)");
title('Practical Impulse Response');
subplot(2,2,4);
plot (w,Hrw) ;
axis([0 pi 0 pil);
xlabel ('Frequency');
ylabel ('Amplitude');
title ('Amplitude Response');
Output-
w(n)
e
o
o
o
. wTfl TTT?»
0 5 10 15 20 0 5 10 18 20
n n
Practical Impulse Response Amplitude Response
0.5
Amplitude
n
-0.5
0 1 2
Frequency
Result-
24
EXPERIMENT-9
Aim:-
Design a length-25 digital Hilbert transformer using a Hanning window.
MATLAB Code-
clear all;
close all;
clc;
M=25; %Hanning Window of Length=25
tau=(M-1)/2;
n=0:M-1;
hd=(2./(pi* (n-tau))).* (sin(pi* (n-tau)/2).72);
d(tau+1)=0;
whamm=hamming(M) ';
h=hd. *whamm;
w=-pi:0.01l:pi; Shere we delay positive and negative
frequency phases by pi/2 so range is +ve and -ve bothS
Hw=freqz (h,1,w);
Hrw=exp (-j* ((pl/2)-12*w)) .*Hw; %amplitude plotted using
the function H(e” (jw))=Hr (e”jw)*exp (J (pi/2-tau*w))
%Note: This is type 3 or 4 type filter used as this is
antisymmetric
subplot(2,2,1);
stem(n,hd, "filled");
axis([-1 M -0.8 11);
xlabel('n'");
ylabel ('hd(n)");
title('Ideal Impulse Response');
subplot(2,2,2);
stem(n,whamm, 'filled"');
axis([-1 M -0.2 1.21);
xlabel('n'");
ylabel ("w(n)');
title ('Hanning Window');
subplot(2,2,3);
stem(n,h, 'filled");
axis([-1 M -0.8 11);
xlabel('n'");
25
ylabel ( hin)");
title('Practical Impulse Response');
subplot(2,2,4);
plot (w/pi, Hrw);
axis([-1 1 -1.2 1.2]);
xlabel ('Frequency in pi Units');
ylabel ( 'ABmplitude');
title ('Amplitude Response');
Output-
05
hd(n)
<o
o
n=
N
&
n
4 Practical Impulse Response Amplitude Response
0.5
Amplitude
-05 0 05
n=}
il
o
Frequency in pi Units
Result-
26
EXPERIMENT-10
Aim:-
Write a MATLAB program to design the digital Butterworth filter that meets the following speci-
fications:
(i) 1-dB ripple in the passband 0 < |w| < 0.37
(ii) At least 40 dB attenuation in the stopband Sr<jw| <
Use bilinear transformation method. Assume 7" = 1.
MATLAB Code-
T=1;
wp=0.3*pi;
ws=0.8%pi;
Ap=1;
As=40;
Ohmp= (2/T) *tan(wp/2) ;
Ohms=(2/T) *tan(ws/2) ;
X=(10"~(0.1*Ap)-1)/ (10~ (0.1*As)-1);
N=ceil (0.5* (1ogl0(X)/ (logl0 (Ohmp/Ohms)))) ;
Ohmc=0Ohmp/ ( (10~ (0.1*Ap) -1) ~(1/(2*N)));
[b,al=butter(N,Ohmc, 'low','s"); %specified the
order,cutoff and low pass type butterworth
Hs=tf (b, a)
[numdig, dendig]=bilinear(b,a,1/T);
Hz=tf (numdig, dendig, T);
Hz
w=0:0.01:pi;
Hw=freqz (numdig, dendig, w) ;
subplot(1,2,1);
plot(w,abs (Hw)) ;
xlabel ("Frequency");
ylabel ("Magnitude") ;
subplot(1,2,2)
plot(w,20*10ogl0 (abs (Hw))) ;
xlabel ("Frequency");
ylabel ("Magnitued in dB");
27
Output-
//
-
Magnitued in dB
Magnitude
—
o
>
3
xR
o
=
-160
0.2
-180
0 -200
0 1 2 3 0 1 2 3
Frequency Frequency
Result-
Desired Butterworth filter was designed and its frequency response was plotted.
28
EXPERIMENT-11
Aim:-
Consider a number r = ,—f = 0.375. Write a MATLAB program to find Q[z] by applying (i)truncation.
and (ii) rounding in signed magnitude representation. Assume (B + 1) = 3 bit register (including
sign bit).
MATLAB Code-
x=3/8;
B=2;
xl=abs (x);
Qx=0;
for k=1:B
Qxbeq
(k) =fix (x1*2) ;
Qx=fix (x1*2)/ (2°k) +Qx;
x1=(x1*2)-fix (x1*2);
end
sg=sign(x);
if sg>0
Qxbeq=[0 Qxbeq];
else
Qxbeg=[1 Qxbeq];
end
disp('Truncated Number');
disp(Qx);
disp('Binary equivalent of truncated number is:');
disp (Qxbeq) ;
Output-
>> expll
Truncated Number
0.2500
fe>> |
Result-
29
EXPERIMENT-12
Aim:-
Write a MATLAB program for the zero-input limit cycle operation. Assume input z(n) = Z(n),
the impulse response of the system h(n) = ( %]“ u(n). (B + 1) = 5. and rounding is used for
quantization.
MATLAB Code-
a=-1/2;B=4;
x=[7/8 zeros(1,20)];ycap=0;
for n=1:21
ay=abs (a*ycap)
ay=ay+(1/2) *27 (-B) 7
Qy=07
for k= :B
Qy=fix(ay*2)/(27k)+Qy;
ay=(ay*2) -fix(ay*2) ;
end Qy=sign (a*ycap) *Qy;
y(n)=Qy+x(n); ycap=y(n);
end
k=0:20
stem(k,y, 'filled');
ylabel ("Amplitude');
xlabel('Time index n');
Output-
I
os
Pl o
L
¥
-
Ey
-
v
®
B
-
§
*
@
-
G
EY
Gy
EY
g
o5 o 2 = - s 10 12 1 1 18 20
Time index n
Result-
The amplitude and magnitude response were plotted for zero input limit cycle operation.
30