DSP Final Lab
DSP Final Lab
DSP Final Lab
Q1 generate and plot each of the following seqences over the interval –n:n determined by your roll
number as shown below
1)
clc
clear all
close all
n=(58/2)+5
N=-n:1:n
x=2*(N==-2)-(N==4)
stem(N,x)
xlabel('Time')
ylabel('Amplitude')
title('Graph of x')
Q2 generate the following complex valued signal and plot its magnitudes phase and the real part and
the imajinary part in four separate subplots…
X4(n)=e^(-0.1+j0.3)n
clc
clear all
close all
n=(58/2)+5
N=-n:1:n
x= N.*((N>=0) - (N>=10)+( (10.*exp(-0.3*(N-10))).*((N>=10)-(N>=20) ) ) )
stem(N,x)
xlabel('Time')
ylabel('Amplitude')
title('Graph of x')
Q2
clc
clear all
close all
n=(58/2)+5
N=-n:2:n
x=exp((-0.1+j*0.3)*N)
subplot(2,2,1)
stem(N,abs(x))
xlabel('Time')
ylabel('Amplitude')
legend('Magnitude')
subplot(2,2,2)
stem(N,angle(x))
xlabel('Time')
ylabel('Amplitude')
legend('Phase')
subplot(2,2,3)
stem(N,imag(x))
xlabel('Time')
ylabel('Amplitude')
legend('Imaginary part')
subplot(2,2,4)
stem(N,real(x))
xlabel('Time')
ylabel('Amplitude')
legend('Real part')
Lab1
Write a matlab function for following basic seuences .function should take input variables n0,n1,n2
and returns x and n.as given by equations
task1(a)
unit sample
function [x,n] = impseq( n0,n1,n2 )
%UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
n=[n1:n2]%range
x=[(n-n0)==0]%logical operation for comparison
stem(n,x)
end
task1(b)
unit step
function [x,n] = impseq( n0,n1,n2 )
task1(c)
rise function
function [x,n] = exp1( n0,n1,n2 )
%UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
n=[n1:n2]%range
x=[exp(n-n0)]
stem(n,x)
end
task1(d)
decay funtion
function [x,n] = exp1( n0,n1,n2 )
%UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
n=[n1:n2]%range
x=[exp(-1.*(n-n0))]
stem(n,x)
end
task 2 (a)
function [y] =fact( x )
%UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
if x>1
n = 1
y=x*fact(x-1)
else
y=1
end
Plot the following two signals together in the same plot
X1(t)=cos(t) and x2(t)=sin(t+pi/2)
Where t=0 to 2sec
t=0:2
x1=3.*cos(3*pi*t+(pi/3))
plot(t,x1,'G')
title('cosine signal')
xlabel('time')
ylabel('amplitude')
legend('3.*cos(3*pi*t=(pi/3))')
grid on
Q1 generate and plot each of the following seqences over the interval –n:n determined by your roll
number as shown below
n=57
x1=2.*((n)==2)-((n)==4)
stem(n,x1,'G')
title('functi0n ')
xlabel('time')
ylabel('amplitude')
legend('2*((n-2)==0)-((n-4)==0)')
grid on
Lab 2
Task 1
Consider the following continuose time sinusoidal signal
Xa(t)=cos(2pi f t) 0<=t<=2
Plote the discrete time signal x(n),0<=n<=19 for sampling frequency Fs=100H and f=1050 and
90 Hz compare the graph by identifying similarities and differences
clc
clearall
closeall
Fs=100
t=1/Fs
n=0:t:19.*t
f=10
f1=50
f2=90
x1=cos(2.*pi.*f.*n)
x2=cos(2.*pi.*f1.*n)
x3=cos(2.*pi.*f2.*n)
subplot(3,1,1)
stem(n,x1)
xlabel('Time')
ylabel('Amplitude')
legend('x1=cos(2.*pi.*f.*n)')
title('GRAPH OF x1')
subplot(3,1,2)
stem(n,x2)
xlabel('Time')
ylabel('Amplitude')
legend('x1=cos(2.*pi.*f1.*n)')
title('GRAPH OF x2')
subplot(3,1,3)
stem(n,x3)
xlabel('Time')
ylabel('Amplitude')
legend('x1=cos(2.*pi.*f2.*n)')
title('GRAPH OF x3')
b)Quantize the signal x(n)for f=10Hz using 4 bits and plot sampled ,uantized and error signal
on same figure
clc
clearall
closeall
f=10
n=4
L=2^n
t=0:1/(2*f):2
x=cos(2*pi*f*t)
D=[(max(x)-min(x))]/L
xq=quant(x,D)
error=x-xq
figure
stairs(t,xq)
ylim([-5 5])
xlabel('Time')
ylabel('Amplitude')
legend('Quantized')
title('GRAPH OF xq')
figure
stem(t,error)
ylim([-5 5])
xlabel('Time')
ylabel('Amplitude')
legend('Error')
title('GRAPH OF ERROR')
figure
stem(t,x)
ylim([-5 5])
xlabel('Time')
ylabel('Amplitude')
legend('Sample')
title('GRAPH OF SAMPLE')
c)Quantize the sampled signal x(n) for f=100Hz using 4,5 and 6 bits respectively ,compute the
difference in the output by computing SQNRT
clc
clearall
closeall
f=100
n1=4
n2=5
n3=6
L1=2^n1
L2=2^n2
L3=2^n3
t=0:(1/(pi)):2
x=cos(2*pi*f*t)%
D1=[(max(x)-min(x))]/L1
D2=[(max(x)-min(x))]/L2
D3=[(max(x)-min(x))]/L3
xq1=quant(x,D1)%
xq2=quant(x,D2)
xq3=quant(x,D3)%
error1=x-xq1
error2=x-xq2
error3=x-xq3
SQNR1=10.*log10(sum(x.^2)/sum(error1.^2))
SQNR2=10.*log10(sum(x.^2)/sum(error2.^2))
SQNR3=10.*log10(sum(x.^2)/sum(error3.^2))
Task 2
Given the digital speech signal in the file guitartune wav write a Matlab program to
a) Read the audio file and find sampling frequency
b) Quantize x(z) using 4 bit quantizers to obtain the quantized signal xq, assuming the signal
range isfrom -1 to 1 volts
c) plot the original speech and quantization error.
d) calculate the SQNRT
clc
clearall
closeall
[x,fs]=audioread('guitartune.wav')
n=4
L=2.^n
t=0.1:length(x-1)
D=[1-(-1/L)]
xq=quant(x,D)
error=x-xq
subplot(3,1,1)
stairs(t,xq)
xlabel('Time')
ylabel('Amplitude')
legend('Quantized')
title('GRAPH OF xq')
subplot(3,1,2)
stem(t,error)
xlabel('Time')
ylabel('Amplitude')
legend('Error')
title('GRAPH OF ERROR')
subplot(3,1,3)
stem(t,x)
xlabel('Time')
ylabel('Amplitude')
legend('Sample')
title('GRAPH OF SAMPLE')
SQNR=10.*log10(sum(x.^2)/sum(error.^2))
TASK 3
1.Write your own custome function which is able to simulate a uniform quantizer .the inputs
should contain sampled signal number of bits used in quantization and return quantized and
error signal
2.write ur own custom function which able to the SQNRT due to
quantization
or
Lab 3
Task 1
Given the following two sequence determine the convolution
y[n].plot each step in the subplot.
X[n]=[3,11,7,0,-1,4,2] -3<=n<=3; h[n]=[2,0,-5,2,1] -1<=n<=4
clc
clearall
closeall
x=[3 11 7 0 -1 4 2]
h=[2 3 0 -5 2 1]
n1=[-3:3]
n2=[-1:4]
c=conv(x,h)
subplot(3,1,1)
stem(n1,x)
xlabel('Time')
ylabel('Amplitude')
legend('x')
title('GRAPH OF x')
subplot(3,1,2)
stem(n2,h)
xlabel('Time')
ylabel('Amplitude')
legend('h')
title('GRAPH OF h')
subplot(3,1,3)
stem(c)
xlabel('Time')
ylabel('Amplitude')
legend('y(n)=x(n)*h(n)')
title('GRAPH OF x(n)*h(n)')
Task-2
Write a matlab fuction to systematically develop the sequence y[n] generated by the convolution of
the two finite length sequence x[n] and h[n] program should be able to handle causal and non causal
sequences .you should verify this functionality of the program .program should call for the input
sequences and their indices vectors
function [ y,ny ] = convo_1(nx,nh,x,h )
ny1=nx(1)+nh(1)
ny2=nx(length(x))+nh(length(h))
ny=[ny1:ny2]
y=conv(x,h)
end
lab 4
clc
clear all
close all
num=[4 3 9]
den=[4 3 -4]
H=num/den
[R,p,K] = residuez(num,den) [z,p,K] = tf2zp(num,den)
zplane(z,p
[sos,g]=zp2sos(z,p,K)
TASK 1
Determine the rational Z-Transform from its poles and zero locations. The zeros are at ζ1 = 0.21, ζ2 =
3.14 , ζ3 = −0.3 + 𝑗 0.5 , ζ4= −0.3 − 𝑗0.5 And poles are at 𝜆1 = −0.45 , 𝜆2 = −0.67, 𝜆3 = 0.81 + 𝑗 0.72 , 𝜆4 =
0.81 − 𝑗0.72 and the gain constant is 2.2.
clc
clear all
close all
z=[0.21;3.14;-0.3+0.5i;-0.3-0.5i]
p=[-0.45;-0.67;0.81+0.72i;0.81-0.72j]
k=2.2
[num,den] = zp2tf(z,p,k)%
tf(num,den,-1)
task 2
Using MATLAB, determine the partial fraction expansion of the z transform (𝑧), given by
clc
clear all
close all
syms z
num=[18 0 0 0]
den=[18 3 -4 -1]
Hz=num/den
[r,p,K] = residuez(num,den)
p=[(r(1)/(1-p(1).*z.^(-1)))+(r(2)/(1-p(2).*z.^(-1)))+(r(3)/(1-p(3).*z.^(-
1)))]
pretty(p)
task 3
Determine the rational form of z transform from its partial fraction expansion of the
following z transforms and then determine their inverse z transform. Write down your
analysis.
clc
clear all
close all
num=[1 -1 0]
den=[1 1.3 0.3]
[R,p,K] = residuez(num,den)
[z,p,K] = tf2zp(num,den)
zplane(z,p)
zplane(z,p)
Lab 6
pre lab
Write a MATLAB function to compute K-point DFT and IDFT of N-point sequence. The function should
take sequence (array), integer value K and binary value switch to decide if to compute DFT or IDFT.
The function should return the sequence after computation.
function [y] = lab6(x,N,b )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
if b==1
n=[0:1:N-1]
k=[0:1:N-1]
WN=exp(-j*2*pi/N)
nk=n'*k
WNnk=WN.^nk
y=x*WNnk;
else if b==0
n=[0:1:N-1]
k=[0:1:N-1]
WN=exp(-j*2*pi/N);
nk=n'*k
WNnk=WN.^(-nk);
y=x*WNnk/N;
end
end
task-1
Compute the M-point DFT of the following N-point sequence.
u(n)={1 ,0<=n<=N-1
0 , otherwise}
task-3
Write a MATLAB program to compute the circular convolution of two length- N sequences via
DFT based approach. Using this program determine the circular convolution of the following pairs
of sequences.
a) 𝑔[𝑛] = {3 ,4 − 2 ,0 , 1 , −4} , ℎ[𝑛] = { 1,−3, 0,4 , −2 , 3}
b)x[n]=sin(pi*n)/2, y[n] =(roll number)^n 0<=n<=4
clc
close all
clear all
g=[3,4'-2,0,1,-4]
h=[1,-3,0,4,-2,3]
c=cconv(g,h)
stem(c)
question
if b==1
n=[0:1:N-1]
k=[0:1:N-1]
WN=exp(-j*2*pi)/N;
nk=n'*k
WNnk=WN.^nk;
y=x*WNnk
else if b==0
n=[0:1:N-1];
k=[0:1:N-1]
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^(-nk);
y=(x*WNnk)/N;
end
end
output:
>> lab6
>> b=1
b=
>> x=[1 2 3 4 5]
x=
1 2 3 4 5
>> N=5
N=
n=
0 1 2 3 4
k=
0 1 2 3 4
nk =
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16
y=x*WNnk
>> [y] = lab6( x,N,b )
n=
0 1 2 3 4
k=
0 1 2 3 4
nk =
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16
y=
15.0000 + 0.0000i 1.5600 + 0.0000i 1.0851 + 0.0000i 1.0162 + 0.0000i 1.0032 + 0.0000i
y=
15.0000 + 0.0000i 1.5600 + 0.0000i 1.0851 + 0.0000i 1.0162 + 0.0000i 1.0032 + 0.0000i
>>
Lab 7
TASK-1
Write MATLAB code that determines and plot the N-point Discrete Fourier Transform of
x[n]defined by the following equations:
x(n)=sin(0.5pi*n), 0<=n<=16
Compute and plot 16-point DFT using two 8-point FFTs and combining techniques.
function [y]=fft16(x)
%stage1
for n=0:1:7
twiddle=exp(-2*pi*j*n*1/16)
x1(n+1)=x(n+1)+x(n+9)
x1(n+9)=(x(n+1)-x(n+9))*twiddle;
end
%stage 2
for n=0:1:3
twiddle=exp(-2*pi*j*n*1/8)
x2(n+1)=x1(n+1)+x1(n+5)
x2(n+5)=(x1(n+1)-x(n+5))*twiddle;
x2(n+9)=x1(n+9)+x1(n+13);
x2(n+13)=x1(n+9)-x1(n+13)*twiddle;
end
%stage 3
for n=0:1:1
twiddle=exp(-2*pi*j*n*1/4)
x3(n+1)=x2(n+1)+x2(n+3)
x3(n+3)=(x2(n+1)-x2(n+3))*twiddle
x3(n+5)=x2(n+5)+x2(n+7)
x3(n+7)=(x2(n+5)-x2(n+7))*twiddle
x3(n+9)=x2(n+9)+x2(n+11)
x3(n+11)=(x2(n+9)-x2(n+11))*twiddle
x3(n+13)=x2(n+13)+x2(n+15)
x3(n+15)=(x2(n+13)-x2(n+15))*twiddle
end
%stage 4
twiddle=exp(-2*pi*j*n*1/2)
x4(1)=x3(1)+x3(2)
x4(2)=(x3(1)-x3(2))*twiddle
x4(3)=x3(3)+x3(4)
x4(4)=(x3(3)-x3(4))*twiddle
x4(5)=x3(5)+x3(6)
x4(6)=(x3(5)-x3(6))*twiddle
x4(7)=x3(7)+x3(8)
x4(8)=(x3(7)-x3(8))*twiddle
x4(9)=x3(9)+x3(10)
x4(10)=(x3(9)-x3(10))*twiddle
x4(11)=x3(11)+x3(12)
x4(12)=(x3(11)-x3(12))*twiddle
x4(13)=x3(13)+x3(14)
x4(14)=(x3(13)-x3(14))*twiddle
x4(15)=x3(15)+x3(16)
x4(16)=(x3(15)-x3(16))*twiddle
y=bitrevorder(x4);
end
****Calling Mfile****
clc
clear all
close all
a=16
n=0:15
x=sin(0.5*pi*n)
f1=fft(x)
subplot(211)
stem(abs(f1))
title('matlab fft')
f2=fft16(x)
subplot(212)
stem(abs(f2))
title('self made function fft')
TASK-2
Write MATLAB code to compare the efficiency linear convolution and the high-speed convolution
times for 5 ≤ N ≤ 150
clc
clear all
close all
conv_time = zeros(1,150); fft_time = zeros(1,150);
%
for L = 1:150
tc = 0; tf=0;
N = 2*L-1; I=1
nu = ceil(log10(N*I)/log10(2)); N = 2^nu;
for I=1:100
h = randn(1,L); x = rand(1,L);
t0 = clock; y1 = conv(h,x); t1=etime(clock,t0); tc = tc+t1;
t0 = clock; y2 = ifft(fft(h,N).*fft(x,N)); t2=etime(clock,t0);
tf = tf+t2;
end
%
conv_time(L)=tc/100; fft_time(L)=tf/100;
end
n=1:150;
subplot(1,1,1);
plot(n(25:150),conv_time(25:150),n(25:150),fft_time(25:150))
TASK-3
Write a MATLAB function to implement a block convolution algorithm called the overlap-and-save
method (and its companion the overlap-and-add method), which is used to convolve a very large
sequence with a relatively smaller sequence.
% ----------------------------------------
% [y] = ovrlpsav(x,h,N)
% y = output sequence
% x = input sequence
% h = impulse response
% N = block length
Lenx = length(x)
M = length(h)
M1 = M-1; L = N-M1
h = [h zeros(1,N-M)]
K = floor((Lenx+M1-1)/(L)) % # of blocks
Y = zeros(K+1,N)
for k=0:K
xk = x(k*L+1:k*L+N)
Y(k+1,:) = circonvt(xk,h,N)
end
TASK-4
Using the function of task 3, write a MATLAB program to implement length-N moving average filter to
filter a noise corrupted signal. Where N is class Roll Number.
clc
clear all
close all
n=1:5
s=n*pi
subplot(221)
stem(s)
title('original signal')
d=rand(1,5)
subplot(222)
stem(d)
title('noise ')
p=ovrlpsav(s,d,6),
subplot(223)
stem(p)
title('corrupted signal')
m=5
xu=0
for i=1:m
x=s+d
xu=xu+x
end
xu=xu/m
subplot(224)
stem(xu)
title('filtered signal')
lab 8
Task 1
Build a SIMULINK model for Sampling and Reconstruction of analog signal
() 3cos() 2sin(0.5) xtftftwhere f R KHz
where,
R = Your Reg. Number
i i) Sample the analog signal x(t) at 8 times the Nyquist rate.
ii ii) Non-uniformly quantize the discrete time signal using μ-law companding method, μ=255
and number of quantization levels = 16.
iii iii) Encode and Decode the signal into binary.
iv iv) Reconstruct the analog signal.
v v) Show both time and frequency domain results.
clc;
clear;
close all;
L = 2^qbits;
I = 2*A/(L-1);
% Settings for
Spectrum Scope
span = 8*F;
span1 = 8*F;
NFFT = 256;
clc
clear all
close all
t=0:0.001:1
fm=10
fs=1000
N=8
L=2.^N
%message signals
x=sin(2*pi*fm*t)
figure
subplot 211
plot(t,x)
title('message signal')
xlabel('time')
ylabel('amplitude')
%pulse traain
d=0:1/50:1
y=pulstran(t,d,'rectpuls',0.001)
subplot 212
plot(t,y)
title('pulse train')
xlabel('time')
ylabel('amplitude')
%sampling
z=x.*y
figure
subplot 211
plot(t,z)
title('sampled signal')
xlabel('time')
ylabel('amplitude')
%quantization
D=[max(x)-min(x)]/(L-1)
xq=quant(x,D)
subplot 212
plot(t,xq)
title('quantized signal')
xlabel('time')
ylabel('amplitude')
%encoder
H_e=dsp.UniformEncoder(max(xq),N)
encoder=step(H_e,xq)
figure
subplot 211
plot(t,encoder)
title('encoded signal')
xlabel('time')
ylabel('amplitude')
%decoder
H_d=dsp.UniformDecoder(max(xq),N)
decoder=step(H_d,encoder)
subplot 212
plot(t,decoder)
title('decoded sig')
xlabel('time')
ylabel('amplitude')
%interpolation
time=0:1/(2*fs):5
interpolation=interp1(t,decoder,time)
figure
subplot 211
plot(time,interpolation)
title('interpolation')
%SC interpolation
subplot 212
xx=0:0.001:1
sc=spline(t,x,xx)
plot(t,sc)
title('spline interpolation')
LAB 11
FIR
% phase responce linear hota ha
% num hota ha or den 1 hota ha
clc
clear all
close all
syms z
H1N=[2 0 2]
H1D=[1 -0.8 0.64]
H2N=[4 -2]
H2D=[1 -0.75]
H3N=[2 4 2]
H3D=[1 0 0.81]
A1=conv(H1N,H2N)
HN=conv(A1,H3N)
A2=conv(H1D,H2D)
HD=conv(A2,H3D)
num=round(HN,3)
den=round(HD,3)
H1=dfilt.dffir(num)
fvtool(H1)
H2=dfilt.dffirt(num)
fvtool(H2)
LAB 12
Task 1
Design a band pass Chebyshev Type II filter using analog prototyping. The order
of filter is 20 with a value of 60 dB stop band attenuation and 0.75dB pass band
ripple where, Pass band edge = 800Hz Stop band edge=2000Hz Sampling
frequency = 6000
%Chebyshev
clc
clear all
close all
Rp=0.75
Rs=60
fp=800
fst=2000
fs=6000
ft=fs/2
wp=fp/ft
wst=fst/ft
%chebbyshev
[n1,wn]=cheb1ord(wp,wst,Rp,Rs)
[num,den]=cheby1(n1,Rp,wn)
[H,w]=freqz(num,den,512,fs)
plot(w,20*log10(abs(H)))
title('Chebbyshev mag in dB')
task 2
Design a Digital Bandpass IIR filter using Analog filter prototype and
Frequency Transformation. The filter should have following
specifications: Minimum order of the filter Lab Experiment | 12
Muhammad Usman Iqbal |EEE324 | Digital Signal Processing Lab
Manual 83 Maximum passband = 0.5dB Minimum stopband
attenuation= 30 dB Pass band edge frequencies = (N-5) – (N+5) MHz
Stop band edge frequencies = (N-5.5) – (N+5.5) MHz Sampling rate of
8N MHz Where 𝑁 = { 10𝑅, 𝑖𝑓𝑅 < 15 𝑅, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑤𝑒 , 𝑀𝐻𝑧 𝑅 = 𝑌𝑜𝑢𝑟
𝑅𝑒𝑔. 𝑁𝑜. i) Give reason which filter type you would prefer based on
the specification ii) Compute the normalized passband and stopband
frequencies iii) Compute the order and transfer function of desired
filter iv) Properly present your results to show that your designed
filter meet the specification
%BANDPASS
clc
clear all
close all
Rp=0.5
Rs=30
fp=2000
fst=2500
N=13
fs=8*N
ft=fs/2
wp=fp/ft
wst=fst/ft
t=0:1/fs:4*(1/f1)
x=sin(2*pi*f*t)
X=fft(x)
subplot(2,2,1)
plot(t,x)
title('x(t)')
subplot(2,2,2)
plot(abs(fftshift(X)))
title('x(F)')
y=filter(num,den,x)
subplot(2,2,3)
plot(t,y)
title('y')
yf=fft(y)
subplot(2,2,4)
plot(abs(shift(yf)))
title('y(F)')
LAB 13
clc
clear all
close all
As=50
wp=0.2*pi
wst=0.3*pi
fs=16000
ft=fs/2
fp=wp*ft
fs=wst*ft
DelF=wst-wp
%filter order=M
M=ciel((As-7.95)/(14.36*DelF))+1
wn=(wp+wst)/2
win=hann(M)
num=fir1(M-1,wn,'Low',win)
[H,w]=freq2(num,1,512,fs)
subplot(1,2,1)
plot(w,20*log10(abs(H)))
subplot(1,2,2)
plot(w,abs(H))