Upload
jay-kothari
View
219
Download
0
Embed Size (px)
Citation preview
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
1/67
Jay Kothari Page 1 of 67
Date: 23-09-2013
M.E. Electronics & Communication Engineering
(VLSI &Embedded Systems Design)
Semester: III
Subject Name: Digital Signal Processing
Subject Code: 735206
Lab-Assignment:
Problem 1
Design and Implement a Digital Butterworth Low Pass System with the following
specifications:
o Passband Frequency: 1000Hz
o Passband Attenuation: 1db
o Stopband Frequency: 12000Hz
o Stopband Attenuation: 80db
o Sampling Rate: 16000Hz
Obtain and Plot the impulse response of the given digital low pass filter.
Obtain and Plot the Magnitude and Phase responses of the given digital low pass filter.
Verify the cut-off frequency of the filter from its Magnitude Response.
Implement the Difference Equation by passing the samples of a sinusoidal signal of
amplitude 0.9, frequency 1000Hz and number of cycles equal to 1000.
Verify the output signal amplitude with the theoretical and expected amplitude.
MATLAB Code:-
clc; clear all; close all;
fs=16000;
ts=1/fs;
fp=1000;
fstop=12000;
rp=1;
rs=80;
ap=10^(-rp/20);
as=10^(-rs/20);
ap_lin=(1/(ap*ap))-1;
as_lin=(1/(as*as))-1;
Wp=2*pi*fp;
Ws=2*pi*fstop;
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
2/67
Jay Kothari Page 2 of 67
N=0.5*(log(ap_lin)-log(as_lin))/(log(Wp)-log(Ws));
N=ceil(N);
disp(N);
Wc=Wp/((ap_lin)^(1/(2*N)));
k=0:1:N-1;
sk=Wc*exp(j*(((pi*(2*k+1)/(2*N))) +(pi/2)));
num=Wc^N;
den=poly(sk);
%%Frequency Transformation
[bm,ak]=bilinear(num,den,fs);
figure
impz(bm,ak);
figure
freqz(bm,ak);
title('Frequency Response')
A=0.9;
f0=1000;
ts=1/fs;
cycle=1000;
D=cycle/f0;
t=0:ts:D-ts
%%Sinusoidal Signal Passing through Filter
x=A*sin(2*pi*f0*t);
m=990*(fs/f0);
y=filter(bm,ak,x);
figure('Name','Sin() signal response of Butterworth Filter','NumberTitle','off')
subplot(2,1,1)
plot(t(m:end),x(m:end));
title('Sine Signal')
ylabel('Magnitude');
xlabel('Time');
subplot(2,1,2)
plot(t(m:end),y(m:end))
ylabel('Magnitude'); xlabel('Time');
title('Sine Signal Response')
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
3/67
Jay Kothari Page 3 of 67
OUTPUT:-
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
4/67
Jay Kothari Page 4 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
5/67
Jay Kothari Page 5 of 67
Problem 2
Design and Implement a Digital Chebyshev Low Pass System with the following
specifications:
o Passband Frequency: 1000Hz
o Passband Attenuation: 1db
o Stopband Frequency: 12000Hz
o Stopband Attenuation: 80db
o Sampling Rate: 16000Hz
Obtain and Plot the impulse response of the given digital low pass filter.
Obtain and Plot the Magnitude and Phase responses of the given digital low pass filter.
Verify the cut-off frequency of the filter from its Magnitude Response.
Implement the Difference Equation by passing the samples of a sinusoidal signal of
amplitude 0.9, frequency 1000Hz and number of cycles equal to 1000.
Verify the output signal amplitude with the theoretical and expected amplitude.
MATLAB Code:-
clc
clear all
close all
fpass = 1000;
fstop = 12000;fs = 16000;
nf = fs/2;
ts = 1/fs;
Wpass = 2*pi*fpass;
Wstop = 2*pi*fstop;
rp = 2;
rs = 20;
ap = 10^(-rp/20);
as = 10^(-rs/20);
epsilon = sqrt((1/(ap*ap))-1);
alp = sqrt((1/(as*as))-1);
N = acosh(alp/epsilon)/acosh(Wstop/Wpass);
N = ceil(N);
k = 0:1:2*N-1;
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
6/67
Jay Kothari Page 6 of 67
uk = ((2*k+1)*pi)/(2*N);
v = asinh(1/epsilon)/N;
sk = Wpass*(sin(uk)*sinh(v)+j*cos(uk)*cosh(v));
sk = sk(N+1:end);
disp(sk);
%% Frequency Transformation
zk = exp(sk*ts);
ak = poly(zk);
if(mod(N,2)==0)
kd = sum(ak)/sqrt(1+(epsilon^2));
else
kd = sum(ak);
end
bm= kd;
figure
impz(bm, ak);
figure
freqz(bm, ak);
title('Frequency Response')
%%Sinusoidal Signal
A=0.9;
f0=1000;
ts=1/fs;
cycle=1000;
D=cycle/f0;
t=0:ts:D-ts;
x=A*sin(2*pi*f0*t);
m=990*(fs/f0);
y=filter(bm,ak,x);
figure
subplot(2,1,1)
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
7/67
Jay Kothari Page 7 of 67
plot(t(m:end),x(m:end));
title('Sine Signal')
ylabel('Magnitude');
xlabel('Time');
subplot(2,1,2)
plot(t(m:end),y(m:end));
title('Sine Signal Response')
ylabel('Magnitude');
xlabel('Time');
OUTPUT:-
Command Window Output:-
1.0e+003 *
-2.5253 - 5.1104i -2.5253 + 5.1104i
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
8/67
Jay Kothari Page 8 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
9/67
Jay Kothari Page 9 of 67
Problem 3
Design and Implement an FIR system of order 127 and using Hamming Window samples
and cut-off frequency 2000Hz and sampling rate fs = 32000.
Obtain and Plot the impulse response of the given digital low pass filter.
Obtain and Plot the Magnitude and Phase responses of the given digital low pass filter.
Verify the cut-off frequency of the filter from its Magnitude Response.
Implement the Difference Equation by passing the samples of a sinusoidal signal of
amplitude 0.9, frequency 1000Hz and number of cycles equal to 1000.
Verify the output signal amplitude with the theoretical and expected amplitude.
MATLAB Code:-
clc;
clear all;
close all;
cf=2000;
order = 127;
win = window(@hamming,order);
fs=32000;
nf=fs/2;
ts=1/fs;
ncf=cf/nf;
bm=fir1(order-1,ncf,'low',win);
ak=1;
figure
impz(bm,ak);
figure
freqz(bm,ak);
title('Frequency Response');
A=0.9;
f0=1000;
cycles=1000;
D=cycles/f0;
t=0:ts:D-ts;
x=A*sin(2*pi*f0*t);
y=filter(bm , ak, x);
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
10/67
Jay Kothari Page 10 of 67
m=fix(990*(fs/f0));
figure
subplot(2,1,1);
plot(t(m+1:end),x(m+1:end));
title('Sine Signal')
ylabel('Magnitude');
xlabel('Time');
subplot(2,1,2);
plot(t(m+1:end),y(m+1:end));
title('Sine Signal Response')
ylabel('Magnitude');
xlabel('Time');
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
11/67
Jay Kothari Page 11 of 67
OUTPUT:-
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
12/67
Jay Kothari Page 12 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
13/67
Jay Kothari Page 13 of 67
Problem 4
Calculate and plot Cn = (2*A*tau/T)*sinc(2*n*f0*tau) with A = 10, T = 1 milli sec and tau
= 0.1 milli sec on n*f0 axis to depict 5 zero crossing points on both the negative and positive
frequency axes.
Repeat the above with T = 10000 milli sec and compare the following obtained in both the
cases:
o First Zero Crossing Frequency
o Total Average Power, Partial and Percentage average powers contributed by the
complex exponentials distributed between positive and negative first zero
crossing frequencies.
o Inter Sample Distance in the frequency domain.
MATLAB Code:-
clcclear all
close all
msec=10^(-3);
A = 10;
%% For T=0.1 msec
T=1*msec;
f0 = 1/T;
tau = 0.1*T;
fz1 = 1/(2*tau);
disp(['First zero crossing frequency : ',num2str(fz1)])
N1 = fz1/f0;
N5 = 5*N1;
n = -N5: 1: N5;
cn = (2*A*tau/T)*sinc(2*n*f0*tau);
figure
subplot(2,1,1)
stem(n*f0,cn);
title('Sinc Function(T=0.1*msec)')
ylabel('Magnitude');
xlabel('n*fs');
TAP = 2*A*A*tau/T;
N5 = 5*N1;
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
14/67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
15/67
Jay Kothari Page 15 of 67
disp(['Ineter Sample Distance:',num2str(ISD)]);
OUTPUT:-
Command Window:-
First zero crossing frequency : 5000
Total Average Power:20
Partial Average Power:18.0576
Percentage Average Power:90.2878
Ineter Sample Distance:1000
First zero crossing frequency : 5000
Total Average Power:0.002Partial Average Power:0.0018056
Percentage Average Power:90.2823
Ineter Sample Distance:0.1
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
16/67
Jay Kothari Page 16 of 67
Problem 5
Implement DFT and IDFT functions using the related mathematical relations as the basis.
Find the DFT samples of X(k) of a rectangular pulse of amplitude 10 and having its pulse
width equal to 2 msecs.
Plot the magnitude and phase parts of X(k) along the frequency axis f.
Find the average power of the above signal using time domain and frequency domain
signal samples
Find the DFT samples of X(k) of an exponentially decaying pulse given by alpha*exp(-
alpha*t)*u(t).
Plot the magnitude and phase parts of X(k) along the frequency axis f.
Find the average power of the above signal using time domain and frequency domain
signal samples
Find the DFT samples of X(k) of a triangular pulse of maximum amplitude 100 and pulse
width 4 msec and centered around origin.
Plot the magnitude and phase parts of X(k) along the frequency axis f.
Find the average power of the above signal using time domain and frequency domain
signal samples
MATLAB Code:-clc
clear all
close all
A = 10;
ms = 10^-3;
tau = 1*ms;
fs = 32000;
ts = 1/fs;
nq = fs/2;
% Rectangular PulseL = tau/ts;
M = 2*L + 1
N = input('Enter the value of N>M : ');
x = [ones(1,1) ones(1,L) zeros(1,N-M) ones(1,L)];
n = 0:1:N-1;
figure('Name','Rectangular pulse','NumberTitle','off')
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
17/67
Jay Kothari Page 17 of 67
plot(n,x);
title('Rectangular Pulse')
X = dft(x,N);
k = 0:1:N-1;
f = k*fs/N;
figure('Name','DFT version of rectangular pulse','NumberTitle','off')
subplot(2,1,1);
stem(f, abs(X));
title('Frequency Response');
subplot(2,1,2);
stem(f, angle(X));
title('Phase Response');
%Exponentially Decaying Function
alp = 10000;
fc = alp/(2*pi);
fm = 100*fc;
fs = 2*fm;
ts = 1/fs;
T = 100/alp;
f0 = 1/T;
N = T/ts;
n = 0:1:N-1;
x = ts*alp*exp(-alp*n*ts);
figure
plot(n,x);
title('Exponential decaying signal')
X = dft(x,N);
k = 0:1:N-1;
f = k*fs/N;
figure('Name','DFT version of exponential decaying signal','NumberTitle','off')
subplot(2,1,1);
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
18/67
Jay Kothari Page 18 of 67
stem(f, abs(X));
title('Frequncy Response');
subplot(2,1,2);
stem(f, angle(X));
title('Phase Response');
disp(['fc = ',num2str(fc)]);
TAP = (alp * (1 - exp(-2*alp*T)))/(2*T);
N1 = fix(fc/f0);
n = -N1:1:N1;
cn = (alp*f0)./(alp+j*2*pi*n*f0);
PAP = sum(abs(cn).*abs(cn));
PCAP = PAP * 100 / TAP;
disp(['Partial Average Power of the exponentially decaying curve :',num2str(PAP)]);
disp(['Total Average Power of the exponentially decaying curve : ',num2str(TAP)]);
disp(['Percentage Average Power of the exponentially decaying curve : ',num2str(PCAP)]);
% Triangular Pulse
tau = 2*ms;
fs = 32000;
ts = 1/fs;
nq = fs/2;
t = -tau:ts:-ts;
x1 = 50*t + 100;
t = 0:ts:tau;
x2 = -50*t + 100;
T = 4*tau;
f0 = 1/T;
L = tau/ts;
M = 2*L + 1
N = input('Enter the value of N>M : ');
x = [x2 zeros(1,N-M) x1];
X = dft(x,N);
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
19/67
Jay Kothari Page 19 of 67
k = 0:1:N-1;
f = k*fs/N;
figure
subplot(2,1,1);
stem(f, abs(X));
subplot(2,1,2);
stem(f, angle(X));
TAP = 1/T*sum(x.^2)
N1 = T/(2*tau);
fz1 = 1/(2*tau);
k1 = fz1*N/fs;
X1 = [X(1:k1+1) X(N-k1:end)];
PAP = 1/(2*k1)*sum(abs(X1).^2)
PCAP = 100* PAP/TAP
OUTPUT:-
Command Window:-
M =
65
Enter the value of N>M : 128fc = 1591.5494
Partial Average Power of the exponentially decaying curve :245817.2134
Total Average Power of the exponentially decaying curve : 500000
Percentage Average Power of the exponentially decaying curve : 49.1634
M =
129
Enter the value of N>M : 256
TAP =
1.6109e+008
PAP =
7.6577e+007
PCAP = 47.5376
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
20/67
Jay Kothari Page 20 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
21/67
Jay Kothari Page 21 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
22/67
Jay Kothari Page 22 of 67
Problem 6
Calculate and plot Cn = (*f0)/(+j*2**n*f0) with = 10000, f0 = 100Hz on n*f0 axis to
depict 100 frequency components on both the negative and positive frequency axes.
Identify the first Half Power frequency.
Calculate and display the Total Average Power of a of the signal xp(t). Calculate the partial amount of average power contributed by the complex exponentials
between the Half-Power Frequency points on the frequency axis.
Find the percentage of average power contributed by the same complex exponentials.
o Repeat the above exercise with = 10000.
MATLAB Code:-
clc
clear all
close all
f0 = 100;
%% For alp=10000
alp = 10000;
T = 1/f0;
N = 100;
n = -N:1:N;
cn = (alp*f0)./(alp+j*2*pi*n*f0);
figure
stem(n*f0, abs(cn));
title('Frequency Response');
xlabel('Frequency');
ylabel('Magnitude');
figure
stem(n*f0, angle(cn));
title('Phase Response');
xlabel('Frequency');
ylabel('Magnitude');
TAP = (alp/(2*T))*(1-exp(-2*alp*T));
fc = alp/(2*pi);
disp(['Half Power Frequency:',num2str(fc)]);
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
23/67
Jay Kothari Page 23 of 67
N1 = fix(fc/f0);
n = -N1:1:N1;
cn = (alp*f0)./(alp+j*2*pi*n*f0);
PAP = sum(abs(cn).*abs(cn));
PCAP = PAP*100/TAP;
disp(['Partial Average Power :',num2str(PAP)]);
disp(['Total Average Power : ',num2str(TAP)]);
disp(['Percentage Average Power : ',num2str(PCAP)]);
OUTPUT:-
Command Window:-
Half Power Frequency:1591.5494
Partial Average Power :245817.2134
Total Average Power : 500000
Percentage Average Power : 49.1634
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
24/67
Jay Kothari Page 24 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
25/67
Jay Kothari Page 25 of 67
Problem 7
Find and plot the impulse response of the discrete time system equivalent to RC system.
Apply DFT/FFT for the obtained impulse response to get H(k) and plot the magnitude and
Phase responses of the system across frequency values.
Identify the half-power frequencies from the obtained plot.
Implement the difference equation of the system by passing a sinusoidal signal of frequency
f0 and amplitude 10.
MATLAB Code:-
clc
clear all
close all
kohms = 10^3;
mfarads = 10^(-6);R = 10*kohms;
C = 0.01*mfarads;
alp = 1/(R*C);
fc = alp/(2*pi);
fm = 100*fc;
fs = 2*fm;
ts = 1/fs;
a = exp(-alp*ts);
T = 100/alp;
N = T/ts;
n = 0:1:N-1;
h = alp*ts*a.^n;
impz(h,1);
title('Impulse Response in Time Domain');
H = dft(h, N);
k = 0:1:N-1;
f = k*fs/N;
figure
subplot(2,1,1);
plot(f, abs(H));
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
26/67
Jay Kothari Page 26 of 67
title('Frequency Response');
xlabel('Frequency');
ylabel('Magnitude');subplot(2,1,2);
plot(f, angle(H));
title('Phase Response');
xlabel('Frequency');
ylabel('Magnitude');
bm = alp*ts;
ak = [1, -a];
figure
impz(bm, ak, 512);
title('Impulse Response ')
figure
freqz(bm, ak)
title('Frequency Response')
A = 10;
f0 = 1000;
cycles = 1000;
D = cycles/f0;
t = 0:ts:D-ts;
x = A*sin(2*pi*f0*t);
y = filter(bm, ak, x);
m = fix(990*fs/f0);
figure
subplot(2,1,1)
plot(t(m+1:end), x(m+1:end));
title('Sine Signal ')
ylabel('Magnitude');
xlabel('Time');
subplot(2,1,2)
plot(t(m+1:end), y(m+1:end));
title('Sine Signal Response')
ylabel('Magnitude');
xlabel('Time');
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
27/67
Jay Kothari Page 27 of 67
OUTPUT:-
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
28/67
Jay Kothari Page 28 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
29/67
Jay Kothari Page 29 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
30/67
Jay Kothari Page 30 of 67
Problem 8
Implement Goertzel Function on MATLAB and using the function implement a DTMF
Signal Detection System on MATLAB.
Matlab Code:
close all;clear allfigure(1)
% DTMF tone generator
fs=8000;
t=[0:1:204]/fs;
x=zeros(1,length(t));
x(1)=1;
y852=filter([0 sin(2*pi*852/fs) ],[1 -2*cos(2*pi*852/fs) 1],x);
y1209=filter([0 sin(2*pi*1209/fs) ],[1 -2*cos(2*pi*1209/fs) 1],x);
y7=y852+y1209;
subplot(2,1,1);plot(t,y7);grid
ylabel('y(n) DTMF: number 7');
xlabel('time (second)')Ak=2*abs(fft(y7))/length(y7);Ak(1)=Ak(1)/2;
f=[0:1:(length(y7)-1)/2]*fs/length(y7);
subplot(2,1,2);plot(f,Ak(1:(length(y7)+1)/2));grid
ylabel('Spectrum for y7(n)');
xlabel('frequency (Hz)');
figure(2)
% DTMF detector (use Goertzel algorithm)
b697=[1];
a697=[1 -2*cos(2*pi*18/205) 1];
b770=[1];
a770=[1 -2*cos(2*pi*20/205) 1];b852=[1];
a852=[1 -2*cos(2*pi*22/205) 1];
b941=[1];
a941=[1 -2*cos(2*pi*24/205) 1];
b1209=[1];
a1209=[1 -2*cos(2*pi*31/205) 1];
b1336=[1];
a1336=[1 -2*cos(2*pi*34/205) 1];
b1477=[1];
a1477=[1 -2*cos(2*pi*38/205) 1]
[w1, f]=freqz([1 -exp(-2*pi*18/205)],a697,512,8000);
[w2, f]=freqz([1 -exp(-2*pi*20/205)],a770,512,8000);
[w3, f]=freqz([1 -exp(-2*pi*22/205)],a852,512,8000);
[w4, f]=freqz([1 -exp(-2*pi*24/205)],a941,512,8000);
[w5, f]=freqz([1 -exp(-2*pi*31/205)],a1209,512,8000);
[w6, f]=freqz([1 -exp(-2*pi*34/205)],a1336,512,8000);
[w7, f]=freqz([1 -exp(-2*pi*38/205)],a1477,512,8000);
subplot(2,1,1);plot(f,abs(w1),f,abs(w2),f,abs(w3), ...
f,abs(w4),f,abs(w5),f,abs(w6),f,abs(w7));grid
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
31/67
Jay Kothari Page 31 of 67
xlabel('Frequency (Hz)');
ylabel('BPF frequency responses');
yDTMF=[y7 0];
y697=filter(1,a697,yDTMF);
y770=filter(1,a770,yDTMF);
y852=filter(1,a852,yDTMF);y941=filter(1,a941,yDTMF);
y1209=filter(1,a1209,yDTMF);
y1336=filter(1,a1336,yDTMF);
y1477=filter(1,a1477,yDTMF);
m(1)=sqrt(y697(206) 2+y697(205)^2- ...
2*cos(2*pi*18/205)*y697(206)*y697(205));
m(2)=sqrt(y770(206) 2+y770(205)^2- ...
2*cos(2*pi*20/205)*y770(206)*y770(205));
m(3)=sqrt(y852(206) 2+y852(205)^2- ...
2*cos(2*pi*22/205)*y852(206)*y852(205));
m(4)=sqrt(y941(206) 2+y941(205)^2- ...2*cos(2*pi*24/205)*y941(206)*y941(205));
m(5)=sqrt(y1209(206)^2+y1209(205)^2- ...
2*cos(2*pi*31/205)*y1209(206)*y1209(205));
m(6)=sqrt(y1336(206)^2+y1336(205)^2- ...
2*cos(2*pi*34/205)*y1336(206)*y1336(205));
m(7)=sqrt(y1477(206)^2+y1477(205)^2- ...
2*cos(2*pi*38/205)*y1477(206)*y1477(205));
m=2*m/205;
th=sum(m)/4; %based on empirical measurement
f=[ 697 770 852 941 1209 1336 1477];
f1=[0 4000];
th=[ th th];x
subplot(2,1,2);stem(f,m);grid
hold; plot(f1,th);
xlabel('Frequency (Hz)');
ylabel('Absolute output values');
Output:
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
32/67
Jay Kothari Page 32 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
33/67
Jay Kothari Page 33 of 67
Problem 9
Assuming an ideal and symmetrical frequency response of a low pass filter with the following
specifications given by
o H(w) = 1; for 0 w /2 and
o
H(w) = 0; for
/2 < w
Design a FIR system with the order given by 128 (Even number) using Frequency Sampling
Method.
MATLAB Code:
clc;
clear all;
close all;
f = [0 0.25 0.25 1]; m = [1 1 0 0];
b = fir2(128,f,m);
[h,w] = freqz(b,1,128);
plot(f,m,w/pi,abs(h))legend('Ideal','fir2 Designed')
title('Comparison of Frequency Response Magnitudes'
Output:
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
34/67
Jay Kothari Page 34 of 67
Problem 10
Calculate and plot Cn = (2*A*tau/T)*sinc(2*n*f0*tau) with A = 10, f0 = 1000Hz and tau =
0.1* T on n*f0 axis to depict 5 zero crossing points on both the negative and positive
frequency axes.
Identify the first zero crossing frequency.
Calculate and display the Total Average Power of a Rectangular Pulse Train.
Calculate the partial amount of average power contributed by the complex exponentials
between the first zero crossing points on the frequency axis.
Find the percentage of average power contributed by the same complex exponentials.
Repeat the above exercise with tau = 0.05*T.
MATLAB Code:
clc
clearall
close all
%Case A
A = 10;
mili = 10^-3;
T = 1*mili;
f0 = 1/T;
tau = 0.1*mili;
fz1 = 1/2/tau;
disp(['First zero crossing frequency for T = 1ms: ',num2str(fz1)]);f5 = 5*fz1;
n = -f5/f0:1:f5/f0;
cn = (2*A*tau/T)*sinc(2*n*f0*tau);
figure('Name','plot of Cn upto 5zero crossing frequency','NumberTitle','off')
subplot(2,1,1);
stem(n*f0,cn);
title('n*f0 vs Cn plot for 5 zero crossing frequency for T=1ms');
grid on;
% Total Average Power
TAP = 2*A^2 * tau/T;
N1 = T/(2*tau);n = -N1:1:N1;
% Partial Average Power
PAP = sum(cn.*cn);
% Partial Average Power Contributed by exponential
PACP = 100*PAP/TAP;
disp(['Total Average Power for Tau = 0.1ms: ',num2str(TAP)]);
disp(['Partial Average Power for Tau = 0.1ms: ',num2str(PAP)]);
disp(['Percentage Average Power Contributed by Exponential for Tau = 0.1ms: ',num2str(PACP)]);
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
35/67
Jay Kothari Page 35 of 67
disp(['Intersample Distance In Frequency Domain for Tau = 0.1ms: ',num2str(f0)]);
%Case B
A = 10;
mili = 10^-3;
T = 1*mili;
f0 = 1/T;tau = 0.05*mili;
fz1 = 1/2/tau;
disp(['First zero crossing frequency for Tau = 0.05ms: ',num2str(fz1)]);
f5 = 5*fz1;
n = -f5/f0:1:f5/f0;
cn = (2*A*tau/T)*sinc(2*n*f0*tau);
subplot(2,1,2);
stem(n*f0,cn);
title('n*f0 vs Cn plot for 5 zero crossing frequency for T=0.05ms');
grid on;
% Total Average PowerTAP = 2*A^2 * tau/T;
N1 = T/(2*tau);
n = -N1:1:N1;
% Partial Average Power
PAP = sum(cn.*cn);
% Partial Average Power Contributed by exponential
PACP = 100*PAP/TAP;
disp(['Total Average Power for Tau = 0.05ms: ',num2str(TAP)]);
disp(['Partial Average Power for Tau = 0.05ms: ',num2str(PAP)]);
disp(['Percentage Average Power Contributed by Exponentioal for Tau = 0.05ms:
',num2str(PACP)]);
disp(['Intersample Distance In Frequency Domain for Tau = 0.05ms: ',num2str(f0)]);
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
36/67
Jay Kothari Page 36 of 67
OUTPUT:-
Command Window:-
First zero crossing frequency : 5000
Total Average Power:20
Partial Average Power:18.0576
Percentage Average Power:90.2878
First zero crossing frequency : 10000
Total Average Power:10
Partial Average Power:9.0283
Percentage Average Power:90.2827
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
37/67
Jay Kothari Page 37 of 67
Problem 11
Given three sinusoids with the following amplitudes and phases
5.018002cos525.012002cos5
5002cos5
3
2
1
ttx
ttx
ttx
Create a MATLAB program to sample each sinusoid and generate a sum of three
sinusoids, that is x(n) = x1(n)+x2(n)+x3(n) using a sampling rate of 8000Hz and plot the sum
x(n) over a range of time that will exhibit approximately 0.1 second.
Use the user defined dft() function and MATLAB built-in function fft() to compute the
DFT coefficients and plot and examine the examine the spectrum of the signal.
MATLAB Code:-
clc;
clear all;
close all;
fs=8000;
ts=1/fs;
n=0:(0.1*fs)-1;
x1=5*cos(2*pi*500*ts*n);
x2=5*cos(2*pi*1200*ts*n+0.25*pi);
x3=5*cos(2*pi*1800*ts*n+0.5*pi);
x=x1+x2+x3;
stem(n,x)
title('Sine signals Summation (x signal)')
xlabel('n(samples)')
ylabel('Magnitude')
M=length(x);
%DFT
X=dft(x,M);
k=0:M-1;
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
38/67
Jay Kothari Page 38 of 67
f=k*fs/M;
figure
subplot(2,1,1)
plot(f,abs(X));
title('Mangnitude Vs Frequency(DFT)');
xlabel('Freq.')
ylabel('Magnitude')
subplot(2,1,2)
plot(f,angle(X))
title('Phase Vs Frequency(DFT)');
xlabel('Freq.')
ylabel('Magnitude')
%FFT
X1=fft(x,M);
figure
subplot(2,1,1)
plot(f,abs(X1));
title('Magnitude Vs Frequency(FFT)');
xlabel('Freq.')
ylabel('Magnitude')
subplot(2,1,2)
plot(f,angle(X1))
title('Phase Vs Frequency(FFT)');
xlabel('Freq.')
ylabel('Magnitude')
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
39/67
Jay Kothari Page 39 of 67
OUTPUT:-_
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
40/67
Jay Kothari Page 40 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
41/67
Jay Kothari Page 41 of 67
Problem 12
Using the user defined dft() function obtain the spectral coefficients of
o Rectangular Window
o Hamming Window
o Hanning Window
o Blackman Window
Assume window length of 255 and plot the magnitude and phase spectra of all the windows
individually and compare them
MATLAB Code:-
clc;
close all;
clear all;
win=window(@rectwin,255);
figure
subplot(2,4,1)
plot(win);
bm=dft(win,255)
ak=1;
subplot(2,4,5);
freqz(bm, ak);
title('Magnitude & Phase Spectra of Rectangular Window');
win=window(@hann,255);
figure
subplot(2,4,2);
plot(win);
bm=dft(win,255);
ak=1;
subplot(2,4,6);
freqz(bm, ak);
title('Magnitude & Phase Spectra of Hanning Window');
win=window(@hamming,255);
figure
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
42/67
Jay Kothari Page 42 of 67
subplot(2,4,3);
plot(win);
bm=dft(win,255);
ak=1;
subplot(2,4,7);
freqz(bm, ak);
title('Magnitude & Phase Spectra of Hamming Window');
win=window(@blackman,255);
figure
subplot(2,4,4);
plot(win);
bm=dft(win,255);
ak=1;
subplot(2,4,8);
freqz(bm, ak);
title('Magnitude & Phase Spectra of Blackman Window');
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
43/67
Jay Kothari Page 43 of 67
OUTPUT:-
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
44/67
Jay Kothari Page 44 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
45/67
Jay Kothari Page 45 of 67
Problem 13
Using MATLAB, design a fourth-order digital lowpass Chebyshev filter with cut-off
frequency of 1.5kHz and a 0.5dB ripple at a sampling rate of 8,000Hz.
Determine the transf er function and difference equation
Plot the Magnitude and Phase Response of the filter.
MATLAB Code:-
clc;
close all;
clear all;
N=4;
fpass = 1500;
fs = 8000;
nf = fs/2;
ts = 1/fs;
Wpass = 2*pi*fpass;
rp = 0.5;
ap = 10^(-rp/20);
epsilon = sqrt((1/(ap*ap))-1);
k = 0:1:2*N-1;
uk = ((2*k+1)*pi)/(2*N);
v = asinh(1/epsilon)/N;
sk = Wpass*(sin(uk)*sinh(v)+j*cos(uk)*cosh(v));
sk = sk(N+1:end);
disp(sk);
zk = exp(sk*ts);ak = poly(zk);
if(mod(N,2)==0)
kd = sum(ak)/sqrt(1+(epsilon^2));
else
kd = sum(ak);
end
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
46/67
Jay Kothari Page 46 of 67
bm= kd ;
figure
freqz(bm,ak);
title('Magnitude and Phase Response');
TF=tf(bm,ak)
OUTPUT:-
Command Window:-
1.0e+003 *
-1.6527 - 9.5780i -3.9899 - 3.9673i -3.9899 + 3.9673i -1.6527 + 9.5780i
Transfer function:
(0.303-1.31e-017i)
---------------------------------------------------------s^4 - (1.662-7.772e-016i) s^3 + (1.665-1.332e-015i)
s^2 (0.9257-7.772e-016i)s
+(0.244-2.359e-016i)
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
47/67
Jay Kothari Page 47 of 67
Problem 14
Design a second order digital bandstop Butterworth filter with a centre frequency of
1.8kHz and a bandwidth of 200Hz and a passband ripple of 3dB at a sampling rate of
8,000Hz.
Determine the transfer function and difference equation of the system.
Use MATLAB to plot the magnitude and phase frequency responses.
MATLAB Code:-
clc; clear all; close all;
N=2;
fs=8000;
nf=fs/2;
ts=1/fs;
fp=1902.77;
fstop=1702.77;
fmp=fp/nf;
fms=fstop/nf;
rp=3;
ap=10^(-rp/20);
ap_lin=(1/(ap*ap))-1;
Wp=2*pi*fp;
Ws=2*pi*fstop;
Wc=Wp/((ap_lin)^(1/(2*N)));
fc=Wc/(2*pi);
fmc=fc/nf;
k=0:1:N-1;
sk=Wc*exp(j*(((pi*(2*k+1)/(2*N))) +(pi/2)));
disp(sk);
num=Wc^N;
den=poly(sk);
[bm,ak]=bilinear(num,den,fs);
[num1,den1] = iirlp2bs(bm, ak, fmc, [fms, fmp]);
tf(num1,den1)
figure
freqz(num1,den1);
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
48/67
Jay Kothari Page 48 of 67
title('Frequency Response');
OUTPUT:-
Command Window:-
1.0e+003 *
-8.4638 + 8.4638i -8.4638 - 8.4638i
Transfer function:
(0.8714+2.512e-016i) s^4 - (0.5394+1.55e-016i) s^3 + (1.826+5.229e-016i) s^2 - (0.5394
+1.533e-016i) s + (0.8714+2.458e-016i)
---------------------------------------------------------
s^4 - (0.5767-2.176e-018i) s^3 + (1.81-1.452e-017i) s^2 - (0.5022-3.828e-018i) s+ (0.7594-5.339e-
018i)
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
49/67
Jay Kothari Page 49 of 67
Problem 15
Given a DSP system with a sampling rate set up to be 8,000Hz, develop a 800Hz single-tone
generator using a digital IIR filter by completing the following steps
o Determine the digital IIR filter transfer function
o Determine the DSP equation
o Plot the tone thus generated for a duration of 0.01 sec
MATLAB Code:-
clc;
close all;
clear all;
fs=8000;
ts=1/fs;
t=0:ts:0.01;
x=zeros(1,length(t));
x(1)=1;
y=filter([0 sin(2*pi*800/fs) ],[1 -2*cos(2*pi*800/fs) 1],x);
plot(t,y);grid
ylabel('y(n)');
xlabel('time (second)');
TF=tf([0 sin(2*pi*800/fs) ],[1 -2*cos(2*pi*800/fs) 1])
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
50/67
Jay Kothari Page 50 of 67
OUTPUT:-
Command Window:-
Transfer function:
0.5878
-----------------
s^2 - 1.618 s + 1
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
51/67
Jay Kothari Page 51 of 67
Problem 16
Given x(0) = 1, x(1) = 1, x(2) = 0, x(3) = -1, use the Goertzel algorithm to compute the
following DFT coefficients and their amplitude spectra
o X(0)
o X(1)
o |X(0)|2
o |X(1)|2
MATLAB Code:-
clc;
close all;
clear all;
x(1) = 1;
x(2) = 2;
x(3) = 3;
x(4) = 4;
N=4;
X1=goertzel(x,1);
subplot(4,1,1);
stem(abs(X1));
title('Amplitude Spectra of|X(0)|')
xlabel('N(Samples)')
ylabel('Amplitude')
X2=goertzel(x,2);
subplot(4,1,2);
stem(abs(X2));
xlabel('N(Samples)')
ylabel('Amplitude')
title('Amplitude Spectra of|X(1)|')
x1=abs(X1)*abs(X1);
subplot(4,1,3);
stem(abs(x1));
xlabel('N(Samples)')
ylabel('Amplitude')
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
52/67
Jay Kothari Page 52 of 67
title('Amplitude Spectra of|X(0)|^(2)')
x2=abs(X2)*abs(X2);
subplot(4,1,4);
stem(abs(x2));
xlabel('N(Samples)')
ylabel('Amplitude')
title('Amplitude Spectra of|X(1)|^(2)')
disp(['X(0):',num2str(X1)])
disp(['X(1):',num2str(X2)])
disp(['|X(0)|^(2):',num2str(x1)]);
disp(['|X(1)|^(2):',num2str(x2)]);
OUTPUT:-
Command Window:-
X(0):10
X(1):-2+2i
|X(0)|^(2):100
|X(1)|^(2):8
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
53/67
Jay Kothari Page 53 of 67
Problem 17
Design a 31-tap lowpass & highpass FIR filters whose cut-off frequency is 2,500Hz using the
following window functions. Assume that the sampling rate is 8kHz.
o Hamming Window
o Hanning Window
o Blackman Window
MATLAB Code:-
n=31;
fs=8000;
nf=fs/2;
wn=2500;
Wn=wn/nf;
%Low Pass Filter
lphamm = fir1(n,Wn,hamming(n+1));
figure('Name','Lowpass Filter using Hamming Window','NumberTitle','off')
freqz(lphamm,1,fs);
title('Lowpass Filter using Hamming Window')
lphann = fir1(n,Wn,hanning(n+1));
figure('Name','Lowpass Filter using Hanning Window','NumberTitle','off')
freqz(lphann,1,fs);
title('Lowpass Filter using Hanning Window')
lpblac = fir1(n,Wn,blackman(n+1));
figure('Name','Lowpass Filter using Blackman Window','NumberTitle','off')
freqz(lpblac,1,fs);
title('')
%High Pass Filter
if mod(n,2)==1
n=n+1;
end
lhhamm = fir1(n,Wn,'high',hamming(n+1));
figure('Name','Highpass Filter using Hamming Window','NumberTitle','off')
freqz(lhhamm,1,fs);
title('Highpass Filter using Hamming Window')
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
54/67
Jay Kothari Page 54 of 67
lhhann = fir1(n,Wn,'high',hanning(n+1));
figure('Name','Highpass Filter using Hanning Window','NumberTitle','off')
freqz(lhhann,1,fs);
title('Highpass Filter using Hanning Window')
lhblac = fir1(n,Wn,'high',blackman(n+1));
figure('Name','Highpass Filter using Blackman Window','NumberTitle','off')
freqz(lhblac,1,fs);
title('Highpass Filter using Blackman Window')
OUTPUT:-
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
55/67
Jay Kothari Page 55 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
56/67
Jay Kothari Page 56 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
57/67
Jay Kothari Page 57 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
58/67
Jay Kothari Page 58 of 67
Problem 18
Design a 41-tap bandpass FIR filter with the lower and upper cot-off frequencies being
2,500Hz and 3,000Hz respectively, using the following window functions. Assume a sampling
rate of 8kHz.
o Hanning Window
o Hamming Window
o Blackman Window
List the FIR filter coefficients and plot the frequency responses for each design.
MATLAB Code:-
clc;
close all;
clearall;
N=41;fs=8000;
nf=fs/2;
wp=2500;
ws=3000;
wp1=wp/nf;
ws1=ws/nf;
wn=[wp1 ws1];
w = hamming(N+1);
bphamm = fir1(41,wn,w);
figure('Name','Bandpass Filter using Hamming Window','NumberTitle','off')
freqz(bphamm,1,fs)title('Bandpass Filter using Hamming Window')
w=hanning(N+1);
bphann = fir1(41,wn,w);
figure('Name','Bandpass Filter using Hanning Window','NumberTitle','off')
freqz(bphann,1,fs)
title('Bandpass Filter using Hanning Window')
w=blackman(N+1);
bpblac= fir1(41,wn,w);
figure('Name','Bandpass Filter using Blackman Window','NumberTitle','off')freqz(bpblac,1,fs)
title('Bandpass Filter using Blackman Window')
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
59/67
Jay Kothari Page 59 of 67
OUTPUT:-_
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
60/67
Jay Kothari Page 60 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
61/67
Jay Kothari Page 61 of 67
Problem 19
Given the difference equation with the input-output relationship of a certain initially relaxed
DSP system (all initial conditions are zero)
y(n)-0.4 y(n-1)+0.29 y(n-2) = x(n) + 0.5 x(n-1)
Find the impulse response h(n) and frequency response H(w) of the above filter (using
MATLAB)
Find the step response of the system (using MATLAB)
MATLAB Code:-
clc;
clear all;
close all;
num=[1 0.5];den=[1 -0.4 0.29];
sys=tf(num,den,-1);
z=tf('z',-1)
impz(num,den);
figure
freqz(num,den);
title('Frequency Response')
figure
step(sys)
title('Step Response of System')
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
62/67
Jay Kothari Page 62 of 67
OUTPUT:-
Command Window:-
Transfer function:
z + 0.5
------------------
z^2 - 0.4 z + 0.29
Sampling time: unspecified
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
63/67
Jay Kothari Page 63 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
64/67
Jay Kothari Page 64 of 67
Problem 20
Write a MATLAB program to read speech data from a PCM formatted speech signal and for
passing it through Pre-Emphasis and De-Emphasis systems.
Also write the output signal from the de-Emphasis System into an output PCM formatted
signal.
MATLAB Code:-
close all;
clearall
fs = 8000; % Sampling rate
%Pre-Emphasis
alpha =0.9; % Degree of pre-emphasis
figure(1);
freqz([1 (-alpha)],1,512,fs); % Calculate and display
frequency responses
t=0:(1/fs):1
x1=sin(2*pi*100*t)+sin(2*pi*1000*t)+sin(2*pi*10000*t);%test signal on behalf of speech signal
figure(2);
y = filter([1 -alpha],1,x1); % Filtering test signal
subplot(2,1,1),
plot(x1,'k')
ylabel('test signal')
title('test signal')
subplot(2,1,2),plot(y,'k');gridylabel('Filtered samples')
xlabel('Number of samples');
title('Pre-emphasized test signal')
figure(3);
N = length(x1); % Length of test signal
Axk =(abs(fft(x1.*hamming(N)')))/ N; % Two-sided spectrum of test signal
Ayk = (abs(fft(y.*hamming(N)')))/ N; % Two-sided spectrumof pre-emphasized test signal
f=[0:N/2]*fs/N;
Axk(2:N)=2*Axk(2:N); % Get one-sided spectrum of test signal
Ayk(2:N)= 2*Ayk(2:N); % Get one-sided spectrum of filtered test signalsubplot(2,1,1),
plot(f,Axk(1:N/2 + 1),'k');grid
ylabel('Amplitude spectrum Ak')
title('Original test signal');
subplot(2,1,2),plot(f,Ayk(1:N/2 + 1),'k');grid
ylabel('Amplitude spectrum Ak')
xlabel('Frequency (Hz)');
title('Preemphasized test signal');
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
65/67
Jay Kothari Page 65 of 67
%De-Emphasis
y1=filter(1,[1 (-alpha)],y);
figure(4)
title('Original test signal');
subplot(2,1,2)
subplot(2,1,1)
plot(x1,'k')plot(y1,'k')
xlabel('Frequency (Hz)');
title('De-emphasized test signal')
OUTPUT:-_
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
66/67
Jay Kothari Page 66 of 67
7/27/2019 Vlsies Lab Assignment i Sem Dsp_cdac-2
67/67