Sei sulla pagina 1di 57

Digital Signal Processing Lab Manual

Program No. 1
1. Verification of Sampling Theorem both in time and frequency domains.

% Verification of sampling theorem

tfinal=0.05;

t=0:0.00005:tfinal;

fd=input('enter the desired analog frequency');

xt=cos(2*pi*fd*t);

%Simulate condition for under sampling i.e.,fs1,2*fd

fs1=1.3*fd;

n1=0:1/fs1 :tfinal;

xn=cos(2*pi*n1*fd);

subplot(3,1,1);

plot(t,xt,'b',n1,xn,'r*-');

title('Undersampling Plot');

%Condition For Nyquist Sampling fs2=2*fd

fs2=2*fd;

n2=0:1/fs2 :tfinal;

xn=cos(2*pi*n2*fd);

subplot(3,1,2);

plot(t,xt,'b',n2,xn,'r*-');

title('Nyquist Plot');

GCE, Ramanagaram Dept. of E&E


1
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

%Condition For OverSampling fs2=2*fd

fs3=5*fd;

n3=0:1/fs3 :tfinal;

xn=cos(2*pi*n3*fd);

subplot(3,1,3);

plot(t,xt,'b',n3,xn,'r*-');

title('Oversampling Plot');

xlabel('time');

ylabel('amplitude');

legend('analog','discrete');

Undersampling Plot
1

-1
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05
Nyquist Plot
1

-1
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05
Oversampling Plot
1
amplitude

analog
0 discrete

-1
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05
time

GCE, Ramanagaram Dept. of E&E


2
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 2
Evaluation of impulse response of a system

Aim: Write a program to generate an impulse response of a system using MATLAB

clc;
clear all;
close all;
% Difference equation of a second order system
% y(n) = x(n)+0.5x(n-1)+0.85x(n-2)+y(n-1)+y(n-2)
b=input('enter the coefficients of x(n),x(n-1)-----');
a=input('enter the coefficients of y(n),y(n-1)----');
N=input('enter the number of samples of imp response ');
[h,n]=impz(b,a,N);
stem(n,h);
title('plot of impulse response');
ylabel('amplitude');
xlabel('time index----->N');
disp(h);
grid on;

Output
enter the coefficients of x(n),x(n-1)-----[1 -2 -3 2]
enter the coefficients of y(n),y(n-1)----[2,-1,2,2]
enter the number of samples of imp response 10
h(n)={0.5000,-0.7500,-2.3750,0.0625,3.1563,3.8906,-1.2734,-7.6836,-
6.4590,5.7275}

GCE, Ramanagaram Dept. of E&E


3
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

plot of impulse response


6

0
amplitude

-2

-4

-6

-8
0 1 2 3 4 5 6 7 8 9
time index----->N

GCE, Ramanagaram Dept. of E&E


4
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 3
Linear convolution of given sequences.

Aim: Write a program to perform linear convolution of given sequences using


MATLAB

clc;
x1=input('enter the first sequence ');
subplot(3,1,1);
stem(x1);
xlabel('n');
ylabel('x1');
title('plot of the first sequence x1');
x2=input('enter 2nd sequence ');
subplot(3,1,2);
stem(x2);
xlabel('n');
ylabel('x2');
title('plot of 2nd sequence x2');
f=conv(x1,x2);
disp('output of linear convolution of two sequences is y = ');
disp(f);
xlabel('n');
ylabel('f');
subplot(3,1,3);
stem(f);
title('linear conv of two sequences x1 & x2 ');

GCE, Ramanagaram Dept. of E&E


5
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

OUTPUT:
enter the first sequence [1,2,-1,-2]
enter 2nd sequence [1,-2,-3,2,4]
output of linear convolution of two sequences is
y = 1 0 -8 -4 15 12 -8 -8

GCE, Ramanagaram Dept. of E&E


6
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 4. a).


Circular convolution of given sequences using convolution
summation formula.
Aim: Write a program to perform Circular convolution of given sequences using
convolution summation formula.
% circular Convolution using summation formula
xn=input('Enter The Input Sequence');
subplot(3,1,1);
stem(xn);
xlabel('n');
ylabel('x1');
title('plot of the first sequence xn');
hn=input('Enter The Impulse Response');
subplot(3,1,2);
stem(hn);
xlabel('n');
ylabel('hn');
title('plot of 2nd sequence hn');
N= max(length(xn),length(hn));
for n=0:N-1
y(n+1)=0;
for k=0:N-1
i=mod((n-k),N);
if i<0
i=i+N;
end
y(n+1)=y(n+1)+hn(k+1)*xn(i+1);
end
end
disp('Circular Convolution of x(n) & h(n) is y(n)=');
disp(y);
n1=0:N-1;
xlabel('n');
ylabel('y');
subplot(3,1,3);
stem(y);
title('Circular Convolution Output y(n)');

GCE, Ramanagaram Dept. of E&E


7
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Output:
Enter The Input Sequence[1,2,3,4]
Enter The Impulse Response[4,3,2,1]
Circular Convolution of x(n) & h(n) is y(n)=
24 22 24 30

GCE, Ramanagaram Dept. of E&E


8
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 4. b).


Circular Convolution Using Matrix Method:
Aim: Write a program to perform Circular convolution of given sequences using
Matrix Method:

clc;
close all;
x1=input('Enter The First sequence x(n)');
h1=input('Enter The Second sequence h(n)');
N1=length(x1);
N2=length(h1);
N=max(N1,N2);
if(N1>N2)
h1=[h1,zeros(1,N1-N2)];
x1=[x1,zeros(1,N2-N1)];
end;
x=transpose(x1);
h=transpose(h1);
temp=h;
for i=1:N-1;
temp=circshift(temp,1);
h=horzcat(h,temp);
end;
h
x
y=h*x
disp('Circular convolved output y[n]=');y
subplot(3,1,1);
stem(x1);
xlabel('N-->');

GCE, Ramanagaram Dept. of E&E


9
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

ylabel('Amplitude-->');
title('Input Sequence x[n]');
subplot(3,1,2);
stem(h1);
xlabel('N-->');
ylabel('Amplitude-->');
title('Second Sequence h[n]');
subplot(3,1,3);
stem(y);
xlabel('N-->');
ylabel('Amplitude-->');
title('Circular convolved output y[n]');
Output:
Enter The First sequence x(n)[1 2 3 4 ]
Enter The Second sequence h(n)[4 3 2 1 ]

h=

4 1 2 3
3 4 1 2
2 3 4 1
1 2 3 4

x=

1
2
3
4

Circular convolved output y[n]=[24 22 24 30]

GCE, Ramanagaram Dept. of E&E


10
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

GCE, Ramanagaram Dept. of E&E


11
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 4. C).


Linear convolution using circular convolution with zero padding.
Aim: Write a program to perform linear convolution using circular convolution with
zero padding.

% Linear convolution using circular Convolution with zero padding


xn=input('Enter The Input Sequence');
subplot(3,1,1);
stem(xn);
xlabel('n');
ylabel('x1');
title('plot of the first sequence xn');
hn=input('Enter The Impulse Response');
subplot(3,1,2);
stem(hn);
xlabel('n');
ylabel('hn');
title('plot of 2nd sequence hn');
N= length(xn)+length(hn)-1;
xn=[xn zeros(1,N-length(xn))]
hn=[hn zeros(1,N-length(hn))]
for n=0:N-1
y(n+1)=0;
for k=0:N-1
i=mod((n-k),N);
if i<0
i=i+N;
end
y(n+1)=y(n+1)+hn(k+1)*xn(i+1);
end

GCE, Ramanagaram Dept. of E&E


12
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

end
disp('Circular Convolution of x(n) & h(n) is y(n)=');
disp(y);
n1=0:N-1;
xlabel('n1');
ylabel('y');
subplot(3,1,3);
stem(n1,y);
title('Circular Convolution Output y(n)');

output:
Enter The Input Sequence[1,2]
Enter The Impulse Response[1,3,5,7,2]
xn = 1 2 0 0 0 0
hn = 1 3 5 7 2 0
Circular Convolution of x(n) & h(n) is y(n)=
1 5 11 17 16 4

GCE, Ramanagaram Dept. of E&E


13
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 5.
Computation of N – point DFT and to plot the magnitude and phase
spectrum.
Aim: Write a program to compute N – point DFT and to plot the magnitude and phase
spectrum.

N = input('Enter the the value of N ');


x = input('Enter the input sequence for which DFT is to be calculated ');
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-1j*2*pi/N);
nk=n'*k;
WNnk=WN.^nk;
Xk=x*WNnk;
MagX=abs(Xk) % Magnitude of calculated DFT
PhaseX=angle(Xk)*180/pi % Phase of the calculated DFT
figure(1);
subplot(2,1,1);
stem(k,MagX);
xlabel('k');
ylabel('x(k)');
title('magnitude plot');
subplot(2,1,2);
stem(k,PhaseX);
xlabel('k');
ylabel('Q(k)');
title('Phase plot');

GCE, Ramanagaram Dept. of E&E


14
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

output;
Enter the the value of N 8
Enter the input sequence for which DFT is to be calculated [1,2,3,4,-1,-2,-3,-4]
MagX = Φ
Columns 1 through 5

0 14.5090 0.0000 5.4305 0.0000

Columns 6 through 8

5.4305 0.0000 14.5090

PhaseX =

Columns 1 through 5

0 -93.2732 45.0000 -27.2357 90.0000

Columns 6 through 8

27.2357 135.0000 93.2732

GCE, Ramanagaram Dept. of E&E


15
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

magnitude plot
15

10
x(n)

0
0 1 2 3 4 5 6 7
n
Phase plot
200

100
Q(n)

-100
0 1 2 3 4 5 6 7
n

GCE, Ramanagaram Dept. of E&E


16
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 6.
LINEAR CONVOLUTION USING DFT AND IDFT

Aim: Write a program to perform linear convolution using DFT and IDFT.

clc;
clear all;
close all;
x=input('Enter x[n]:');
h=input('Enter h[n]:');
nx=length(x);
nh=length(h);
n=nx+nh-1;
xnew=[x zeros(1,n-nx)];
hnew=[h zeros(1,n-nh)];
xf=fft(xnew);
hf=fft(hnew);
zf=xf.*hf;
z=ifft(zf);
lx=0:1:nx-1;
lh=0:1:nh-1;
nz=0:1:length(z)-1;
subplot(3,1,1);
stem(lx,x);
xlabel('Time');ylabel('Amplitude');
title('Input sequence x[n]');
grid;
subplot(3,1,2);
stem(lh,h);
xlabel('Time');ylabel('Amplitude');

GCE, Ramanagaram Dept. of E&E


17
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

title(‘Impulse Response h[n]’);


grid;
subplot(3,1,3);
stem(nz,z);
xlabel('Time');ylabel('Amplitude');
title('Linear Convolution ');
grid;

%Verification
z1=conv(xnew,hnew)

INPUT:
Enter x[n]:[1 2 3 4]
Enter h[n]:[4 3 2 1]

OUTPUT:
z1 = 4 11 20 30 20 11 4 0 0 0 0 0 0

GCE, Ramanagaram Dept. of E&E


18
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

GCE, Ramanagaram Dept. of E&E


19
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 7. a).


Aim: Write a program to find Impulse response for a given difference equation.

% To find Impulse response

N = input('Length Of Response required=');

b = [1];%x(n) coefficients

a = [1,-1,0.9]; %y(n) coefficients

%impulse input

x = [1,zeros(1,N-1)];

% time vector for plotting

n = 0:N-1;

% Impulse Response

h = filter(b,a,x);

subplot(2,1,1);

stem(n,x);

title('impulse input');

xlabel('n');

ylabel('?(n)');

subplot(2,1,2);

stem(n,h);

title('impulse response');

xlabel('n');

ylabel('h(n)');

GCE, Ramanagaram Dept. of E&E


20
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

GCE, Ramanagaram Dept. of E&E


21
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 7. b).


Aim: Write a program to find a Step response for a given difference equation.

% To find Step response


%y[n]-0.8y [n-1]=x[n]
N = input('Length Of response required=');
b = [1];%x(n) coefficients
a = [1,-1,1]; %y(n) coefficients
x = [ones(1,N)]; %Step input
% time vector for plotting
n = 0:1:N-1;
% Step Response
y = filter(b,a,x);
subplot(2,1,1);
stem(n,x);
title('Step input');
xlabel('n');
ylabel('u(n)');
subplot(2,1,2);
stem(n,y);
title('Step response');
xlabel('n');
ylabel('y(n)');

GCE, Ramanagaram Dept. of E&E


22
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

GCE, Ramanagaram Dept. of E&E


23
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 8.
Calculation of DFT and IDFT by FFT
Aim: Write a program to compute DFT and IDFT by FFT.

n = 0:6;
x = input('enter the i/p sequence ');
a = fft(x);
mag = abs(a);
pha = angle(a);
subplot(3,1,1);
stem(mag);
xlabel('k')
ylabel('|x(k)|');
grid on
title('Magnitude Response');
subplot(3,1,2);
stem(pha);
xlabel('k')
ylabel('?(k)');
grid on
title('phase Response');
xn= ifft(a);
subplot(3,1,3);
stem(xn);
xlabel('n')
ylabel('x(n)');
title('Input Sequence');

GCE, Ramanagaram Dept. of E&E


24
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

output:
enter the i/p sequence [1,2,3,1,-4,-5,-1,-2]

Magnitude Response
20
|x(k)|

10

0
1 2 3 4 5 6 7 8
k
phase Response
5
?(k)

-5
1 2 3 4 5 6 7 8
k
Input Sequence
5
x(n)

-5
1 2 3 4 5 6 7 8
n

GCE, Ramanagaram Dept. of E&E


25
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 9. a).


Design of IIR Filter
a) Butterworth Filter

Aim: Write a program to design digital Butterworth low pass filter for a given
specification

To design digital Butterworth low pass filter clear all;

clc;
rp = input(‘Enter the pass band ripple freq’);
rs = input(‘Enter the stop ripple freq’);
fp = input(‘Enter the pass band freq’);
fs = input(‘Enter the stop band freq’);
f = input(‘Enter the sample freq’);
w1=2*fp/f;
w2=2*fs/f;
[n,wn]= buttord(w1,w2,rp,rs);
[b,a] = butter(n,wn);
w=0:.1:pi;
[h,p] = freqz(b,a,w);
g=20*log10(abs(h));
A=angle(h);
subplot (2,2,1); plot(p/pi,g);
ylabel(‘amp’);
xlabel(‘ferq’);
title(‘amp,freq’);
subplot (2,2,2); plot(p/pi,A);
xlabel(‘normal. freq’);
ylabel(‘phase’);
title(‘normal.freq,phase’);

GCE, Ramanagaram Dept. of E&E


26
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Output:
Enter the pass band ripple freq= .5
Enter the stop band ripple freq= 40
Enter the pass band freq= 2000
Enter the stop band freq= 3000
sample freq= 10000

GCE, Ramanagaram Dept. of E&E


27
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 9. b).


Aim: Write a program to design digital Butterworth high pass filter for a given
specification

%To design digital Butterworth high pass filter


clear all;
clc;
rp = input(' Enter the pass ripple freq');
rs = input(' Enter the stop ripple freq');
fp = input(' Enter the pass band freq');
fs = input(' Enter the stop band freq');
f = input(' Enter the sample freq');
w1= 2*fp/f;
w2= 2*fs/f;
[n,wn]= buttord(w1,w2,rp,rs);
[b,a] = butter(n,wn,'high');
w=0:.1:pi;
[h,p]= freqz(b,a,w);
g=20*log10(abs(h));
A=angle(h);
subplot (2,2,1);
plot(p/pi,g);
ylabel('amp');
xlabel('ferq');
title('amp,freq');
subplot (2,2,2);
plot(p/pi,A);
xlabel('normal. freq');
ylabel('phase');
title('normal.freq,phase');

Output
Enter the pass ripple freq .5
Enter the stop ripple freq 40
Enter the pass band freq 2000
Enter the stop band freq 3000
Enter the sample freq 10000

GCE, Ramanagaram Dept. of E&E


28
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

amp,freq
0

-100
amp

-200

-300

-400
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
ferq
normal.freq,phase
4

2
phase

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normal. freq

GCE, Ramanagaram Dept. of E&E


29
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 9. c).


Aim: Write a program to design digital Butterworth band pass filter for a given
specification

%To design digital Butterworth Band pass filter


clear all;
clc;
rp = input('Enter the pass ripple freq ');
rs = input('Enter the stop ripple freq ');
fp = input('Enter the pass band freq ');
fs = input('Enter the stop band freq ');
f = input('Enter the sample freq ');
w1= 2*fp/f;
w2= 2*fs/f;
[n,wn]= buttord(w1,w2,rp,rs);
[b,a] = butter(n,wn,'high');
w=0:.1:pi;
[h,p]= freqz(b,a,w);
g=20*log10(abs(h));
A=angle(h);
subplot (2,1,1);
plot(p/pi,g);
ylabel('amp');
xlabel('ferq');
title('amp,freq');
subplot (2,1,2);
plot(p/pi,A);
xlabel('normal. freq');
ylabel('phase');
title('normal.freq,phase');

GCE, Ramanagaram Dept. of E&E


30
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Output:

Enter the pass ripple freq= .5


Enter the stop ripple freq= 40
Enter the pass band freq= 2000
Enter the stop band freq= 3000
Enter the sample freq= 10000

GCE, Ramanagaram Dept. of E&E


31
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 9. d).


Aim: Write a program to design digital Butterworth band stop filter for a given
specification

%To design digital Butterworth Band stop filter using MATLAB.


clear all;
clc;
rp = input('Enter the pass ripple freq ');
rs = input('Enter the stop ripple freq ');
fp = input('Enter the pass band freq ');
fs = input('Enter the stop band freq ');
f = input('Enter the sample freq ');
w1= 2*fp/f;
w2= 2*fs/f;
[n]= buttord(w1,w2,rp,rs);
wn = [w1,w2];
[b,a] = butter(n,wn,'stop');
w=0:.1:pi;
[h,p]= freqz(b,a,w);
g=20*log10(abs(h));
A=angle(h);
subplot(2,1,1);
plot(p/pi,g);
ylabel('amp');
xlabel('ferq');
title('amp,freq');
subplot (2,1,2);
plot(p/pi,A);
xlabel('normal. freq');
ylabel('phase');
title('normal.freq,phase');

GCE, Ramanagaram Dept. of E&E


32
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Output
Enter the pass ripple freq .3
Enter the stop ripple freq 30
Enter the pass band freq 2000
Enter the stop band freq 3000
Enter the sample freq 10000

GCE, Ramanagaram Dept. of E&E


33
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 10.a).


Aim: Write a program to design low pass FIR FILTER for given specification.

clc;
clear all;
wc=input('enter the value of cut off frequency');
N=input('enter the order of filter');
alpha=(N-1)/2;
eps=0.001;
%Rectangular Window
n=0:1:N-1;
hd=sin(wc*(n-alpha+eps))./(pi*(n-alpha+eps));
hn=hd
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h));
hold on
%Hamming Window
n=0:1:N-1;
wh=0.54-0.46*cos((2*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'ms');
hold off;
hold on
%Hanning Window
n=0:1:N-1;
wh=0.5-0.5*cos((2*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'blue');
hold off;
hold on
%Blackman Window
n=0:1:N-1;

GCE, Ramanagaram Dept. of E&E


34
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

wh=0.42-0.5*cos((2*pi*n)/(N-1))+0.08*cos((4*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'green');
hold off;

Output:
enter the value of cut off frequency2
enter the order of filter10
hn =
Columns 1 through 6
0.0293 0.0596 -0.1222 0.0304 0.5361 0.5353
Columns 7 through 10
0.0295 -0.1220 0.0599 0.0290
hn =
Columns 1 through 6
0.0023 0.0112 -0.0562 0.0234 0.5212 0.5205
Columns 7 through 10
0.0227 -0.0561 0.0112 0.0023
hn =
Columns 1 through 5
0 0.0070 -0.0505 0.0228 0.5199
Columns 6 through 10
0.5192 0.0221 -0.0504 0.0070 0
hn =
Columns 1 through 5
-0.0000 0.0030 -0.0315 0.0191 0.5099
Columns 6 through 10
0.5092 0.0186 -0.0315 0.0030 -0.0000
1.4

1.2

0.8

0.6

0.4

0.2

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

GCE, Ramanagaram Dept. of E&E


35
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 10.b).


Aim: Write a program to design high pass FIR FILTER for given specification using
window functions.

% DESIGN FOR HIGH PASS FILTER:


clc;
clear all;
wc=input('enter the value of cut off frequency');
N=input('enter the value of filter');
alpha=(N-1)/2;
eps=0.001;
%Rectangular Window
n=0:1:N-1;
hd=(sin(pi*(n-alpha+eps))-sin((n-alpha+eps)*wc))./(pi*(n-alpha+eps));
hn=hd
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h));
hold on
%Hamming Window
n=0:1:N-1;
wh=0.54-0.46*cos((2*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'ms');
hold off;
hold on
%Hanning Window
n=0:1:N-1;
wh=0.5-0.5*cos((2*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'blue');
hold off;
hold on

GCE, Ramanagaram Dept. of E&E


36
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

%Blackman Window
n=0:1:N-1;
wh=0.42-0.5*cos((2*pi*n)/(N-1))-0.08*cos((4*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'green');
hold off;

OUTPUT:
enter the value of cut off frequency2
enter the value of filter15
hn =
Columns 1 through 6
-0.0449 0.0284 0.0347 -0.0790 0.0302 0.1198
Columns 7 through 12
-0.2890 0.3634 -0.2899 0.1211 0.0291 -0.0784
Columns 13 through 15
0.0345 0.0285 -0.0452
hn =
Columns 1 through 6
-0.0036 0.0036 0.0088 -0.0346 0.0194 0.0991
Columns 7 through 12
-0.2758 0.3634 -0.2767 0.1001 0.0187 -0.0343
Columns 13 through 15
0.0087 0.0036 -0.0036
hn =
Columns 1 through 5
0 0.0014 0.0065 -0.0307 0.0185
Columns 6 through 10
0.0972 -0.2747 0.3634 -0.2755 0.0983
Columns 11 through 15
0.0178 -0.0305 0.0065 0.0014 0

GCE, Ramanagaram Dept. of E&E


37
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

hn =
Columns 1 through 5
0.0072 -0.0023 0.0044 -0.0301 0.0182
Columns 6 through 10
0.0898 -0.2371 0.3052 -0.2379 0.0908
Columns 11 through 15
0.0176 -0.0299 0.0044 -0.0023 0.0072

1.4

1.2

0.8

0.6

0.4

0.2

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

GCE, Ramanagaram Dept. of E&E


38
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 10.c).


Aim: Write a program to design band pass FIR FILTER for given specification using
window functions.

% DESIGN FOR BAND PASS FILTER:


clc;
Wc1=input('enter the value of Wc1=');
Wc2=input('enter the value of Wc2=');
N=input('enter the value of N=');
alpha=(N-1)/2;
eps=0.001;
%Rectangular Window
n=0:1:N-1;
hd=(sin(Wc1*(n-alpha+eps))-sin(Wc2*(n-alpha+eps)*pi))./((n-alpha+eps)*pi);
hn=hd
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h));
hold on;
%Hamming Window
n=0:1:N-1;
Wn=0.54-0.46*cos((2*pi*n)/(N-1));
hn=hd.*Wn
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h),'green');
hold on;
%Hanning Window
n=0:1:N-1;
Wn=0.5-0.5*cos((2*pi*n)/(N-1));

GCE, Ramanagaram Dept. of E&E


39
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

hn=hd.*Wn
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h),'red');
hold off;
%Blackman Window
n=0:1:N-1;
wh=042-0.5*cos((2*pi*n)/(N-1))-0.08*cos((4*pi*n)/(N-1));
hn=hd.*wh
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(W/pi,abs(h),'green');
hold off;

Output:
enter the value of Wc1=1
enter the value of Wc2=1.5
enter the value of N=12
hn =
Columns 1 through 6
-0.0816 -0.1194 0.0322 0.1668 0.0624 -0.1479
Columns 7 through 12
-0.1420 0.0609 0.1656 0.0326 -0.1189 -0.0819
hn =
Columns 1 through 6
-0.0065 -0.0183 0.0112 0.1010 0.0525 -0.1452
Columns 7 through 12
-0.1393 0.0512 0.1003 0.0114 -0.0182 -0.0066
hn =
Columns 1 through 6
0 -0.0095 0.0094 0.0953 0.0516 -0.1449
Columns 7 through 12
-0.1391 0.0504 0.0946 0.0095 -0.0094 0

GCE, Ramanagaram Dept. of E&E


40
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

hn =
Columns 1 through 5
-3.3807 -4.9610 1.3473 7.0313 2.6401
Columns 6 through 10
-6.2735 -6.0216 2.5781 6.9813 1.3647
Columns 11 through 12
-4.9405 -3.3920

40

35

30

25

20

15

10

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

GCE, Ramanagaram Dept. of E&E


41
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 11.a).


Aim: Write a program to design low pass FIR filter using frequency sampling
method.

%Design a lowpass filter using frequency sampling method


M=63; Wp=0.5*pi;%the number of samples and the passband cutoff frequency
m=0:(M+1)/2; Wm=2*pi*m./(M+1);%the sampling points and the stopband cutoff
frequency
mtr=floor(Wp*(M+1)/(2*pi))+2;%round to negative part,i.e.floor(3.5)=3;floor(-
3.2)=-4
Ad=[Wm<=Wp];Ad(mtr)=0.38;
Hd=Ad.*exp(-j*0.5*M*Wm);%define frequency-domain sampling vector H(k)
Hd=[Hd conj(fliplr(Hd(2:(M+1)/2)))];
%fliplr is to realize the fliplr of matrix and conj
h=real(ifft(Hd));% h(n)=IDFT[H(k)
w=linspace(0,pi,1000);%get 1000 row vectors between 0 and pi
H=freqz(h,[1],w);%the amplitude -frequency characteristic diagram of the filter
figure(1)
plot(w/pi,20*log10(abs(H)));%parameters are respectively the normalized frequency
and amplitude
xlabel('the normailzed frequency');ylabel('gian/dB');title('The gain response of
lowpass filter');
axis([0 1 -50 0.5]);
f1=100;f2=300;f3=700;fs=2000;%the frequencies of sines signal that needs filtered
and the sample frequency
subplot(211)
t=0:1/fs:0.25;%define the time domain and steplength
s=sin(2*pi*f1*t)+sin(2*pi*f2*t)+sin(2*pi*f3*t);%signal before filtering
plot(t,s);%plot the diagram before filtering
xlabel('time/s');ylabel('amplitude');title('Time-domain diagram before filtering');

GCE, Ramanagaram Dept. of E&E


42
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

subplot(212)
Fs=fft(s,512); AFs=abs(Fs);%transform to the frequency domain
f=(0:255)*fs/512;%frequency sampling
plot(f,AFs(1:256));%plot the frequency domain diagram before filtering
xlabel('frequency/Hz');ylabel('amplitude');title('Frequency-domain diagram before
filtering');
figure(3)
sf=filter(h,1,s);%use function filter
subplot(211)
plot(t,sf)%plot the diagram after filtering
xlabel('time/s');ylabel('amplitude');title('Time-domain diagram after filtering')
axis([0.2 0.25 -2 2]);%set the range of image coordinates
subplot(212)
Fsf=fft(sf,512); AFsf=abs(Fsf);%frequency-domain and the amplitude diagram
f=(0:255)*fs/512;%frequency sampling
plot(f,AFsf(1:256))%plot the frequency domain diagram before filtering
xlabel('frequency/Hz');ylabel('amplitude');title('Frequency-domain diagram after
filtering');

GCE, Ramanagaram Dept. of E&E


43
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Time-domain diagram before filtering


2

1
amplitude

-1

-2
0 0.05 0.1 0.15 0.2 0.25
time/s
Frequency-domain diagram before filtering
300

200
amplitude

100

0
0 100 200 300 400 500 600 700 800 900 1000
frequency/Hz

Time-domain diagram after filtering


2

1
amplitude

-1

-2
0.2 0.205 0.21 0.215 0.22 0.225 0.23 0.235 0.24 0.245 0.25
time/s
Frequency-domain diagram after filtering
300

200
amplitude

100

0
0 100 200 300 400 500 600 700 800 900 1000
frequency/Hz

GCE, Ramanagaram Dept. of E&E


44
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 11.b).


Aim: Write a program to design high pass FIR filter using frequency sampling
method.

M=32;%the number of samples


Wp=0.6*pi;%passband cutoff frequency
m=0:M/2;%the sampling points
Wm=2*pi*m./(M+1);%stopband cutoff frequency
mtr=ceil(Wp*(M+1)/(2*pi));%round to positive part,i.e.ceil(3.5)=4;ceil(-3.2)=-3;
Ad=[Wm>=Wp];
Ad(mtr)=0.28;
Hd=Ad.*exp(-j*0.5*M*Wm);%define frequency-domain sampling vector H(k))
Hd=[Hd conj(fliplr(Hd(2:M/2+1)))];
%fliplr is to realize the fliplr of matrix and conj is the conjugate
h=real(ifft(Hd));%h(n)=IDFT[H(k)]
w=linspace(0,pi,1000);%get 1000 row vectors between 0 and
piH=freqz(h,[1],w);%the amplitude -frequency characteristic diagram of the filter
figure(1)
plot(w/pi,20*log10(abs(H)));%parameters are respectively the noemalized frequency
and amplitude
xlabel('the normailzed frequency');ylabel('gian/dB');title('The gain response of
highpass filter');
axis([0 1 -50 0]);
f1=200;f2=700;f3=800;%the frequencies of sines signal that needs filtered
fs=2000;%the sample frequency
figure(2)
subplot(211)
t=0:1/fs:0.25;%define the time domain and steplength
s=sin(2*pi*f1*t)+sin(2*pi*f2*t)+sin(2*pi*f3*t);%signal before filtering

GCE, Ramanagaram Dept. of E&E


45
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

plot(t,s);%plot the diagram before filtering


xlabel('time/s');ylabel('amplitude');title('Time-domain diagram before filtering');
subplot(212)
Fs=fft(s,512);%transform to the frequency domain
AFs=abs(Fs);%the amplitude
f=(0:255)*fs/512;%frequency sampling
plot(f,AFs(1:256));%plot the frequency domain diagram before filtering
xlabel('frequency/Hz');ylabel('amplitude');title('Frequency-domain diagram before
filtering');
figure(3)
sf=filter(h,1,s);%use function filter
subplot(211)
plot(t,sf)%plot the diagram after filtering
xlabel('time/s');ylabel('amplitude');title('Time-domain diagram after filtering')
axis([0.2 0.25 -2 2]);%set the range of image coordinates
subplot(212)
Fsf=fft(sf,512);AFsf=abs(Fsf);
f=(0:255)*fs/512;%frequency sampling
plot(f,AFsf(1:256))%plot the frequency domain diagram before filtering
xlabel('frequency/Hz');ylabel('amplitude');title('Frequency-domain diagram after
filtering');

GCE, Ramanagaram Dept. of E&E


46
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

The gain response of highpass filter


0

-5

-10

-15

-20
gian/dB

-25

-30

-35

-40

-45

-50
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
the normailzed frequency

Time-domain diagram before filtering


4

2
amplitude

-2

-4
0 0.05 0.1 0.15 0.2 0.25
time/s
Frequency-domain diagram before filtering
300

200
amplitude

100

0
0 100 200 300 400 500 600 700 800 900 1000
frequency/Hz

GCE, Ramanagaram Dept. of E&E


47
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Time-domain diagram after filtering


2

1
amplitude

-1

-2
0.2 0.205 0.21 0.215 0.22 0.225 0.23 0.235 0.24 0.245 0.25
time/s
Frequency-domain diagram after filtering
300

200
amplitude

100

0
0 100 200 300 400 500 600 700 800 900 1000
frequency/Hz

GCE, Ramanagaram Dept. of E&E


48
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 12.a).


Aim: Write a program to Realize direct form I and II of digital filters.

%realization of Direct form one & two digital filters


function y= direct(typ,b,a,x);
x= input('enter the input sequence=');
b= input('enter the numerator polynomials=');
a= input('enter the denominator polynomials=');
typ=input('type of realization=');
p=length(a)-1;
q=length(b)-1;
pq=max(p,q);
a=a(2:p+1);
u=zeros(1,pq);
if(typ==1)
for i=1:length(x),unew=x(i)-sum(u(1:p).*a);
u=[unew,u];
y(i)=sum(u(1:q+1).*b);
u=u(1:pq);
end
if(typ==2)
fori=1:length(x),y(i)=b(1)*x(i)+u(1);
u=[u(2:pq),0];
u(1:q)=u(1:q)+b(2:q+1)*x(i);
u(1:p)=u(1:p)-a*y(i);
end
end

GCE, Ramanagaram Dept. of E&E


49
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Output:
enter the input sequence=[1 1 2 2]
enter the numerator polynomials=[1 2 3 4 ]
enter the denominator polynomials=[4 3 2 1 ]
type of realization=1
ans = 1 0 5 -3

GCE, Ramanagaram Dept. of E&E


50
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Program No. 12.b).


Aim: Write a program to Realize parallel form of digital filters.

%parallel realization of digital filters


function y= parallel(c,nsec,dsec,x);
x= input('enter the input sequence=');
b= input('enter the numerator polynomials=');
a= input('enter the denominator polynomials=');
c= input('enter the gain of the filter=');
[n,m]=size(a);a=a(:,2:3);
u=zeros(n,2);
for i=1:length(x),y(i)=c*x(i);
for k=1:n,
unew=x(i)-sum(u(k,:).*a(k,:));
u(k,:)=[unew,u(k,1)];
y(i)=y(i)+sum(u(k,:).*b(k,:));
end

GCE, Ramanagaram Dept. of E&E


51
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

DSP Lab - viva questions


1. What is MATLAB?
2. What are the applications of MATLAB?
3. State sampling theorem.
4. What is meant by Nyquist rate and Nyquist criteria?
5. Explain scaling and superposition properties of a system.
6. What is meant by linearity of a system and how it is related to scaling and
superposition?
7. What is impulse function?
8. What is meant by impulse response?
9. What is energy signal? How to calculate energy of a signal?
10. What is power signal? How to calculate power of a signal?
11. Differentiate between even and odd signals.
12. Explain time invariance property of a system with an example.
13. What is memory less system?
14. When a system is said to have memory?
15. What is meant by causality?
16. Explain linear convolution and circular convolution.
17. What is the length of linear and circular convolutions if the two sequences are having
the length n1 and n2?
18. What are Fourier series and Fourier transform?
19. What are the advantages and special applications of Fourier transform, Fourier series,
Z transform and Laplace transform?
20. Differentiate between DTFT and DFT. Why it is advantageous to use DFT in
computers rather than DTFT?
21. In DTFT, frequency appears to be continuous. But, in DFT, frequency is discrete. This
property is useful for computation in computers.

GCE, Ramanagaram Dept. of E&E


52
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

22. How to perform linear convolution using circular convolution?


If two signals x (n) and y (n) are of length n1 and n2, then the linear convoluted output
z (n) is of length n1+n2-1. Each of the input signals is padded with zeros to make it of
length n1+n2-1. Then circular convolution is done on zero padded sequences to get the
linear convolution of original input sequences x (n) and y (n).
23. What is meant by correlation?
Correlation is the measure of similarity between two signal/waveforms. It compares
the waveforms at different time instants.
24. What is auto-correlation?
It is a measure of similarity of similarity of a signal/waveform with itself.
25. What is cross-correlation?
26. What are the advantages of using autocorrelation and cross correlation properties in
signal processing fields?
27. How auto-correlation can be used to detect the presence of noise?
28. Differentiate between IIR filters and FIR filters.

Advantages
FIR IIR
 Stable
 Highly precise  cost lesser
 Finite duration impulse response  Faster computations
 Excellent phase response  Less hardware, computations
 The word-size effect such as round-  Easier to design
off noise and coefficient  Lower order required
quantization errors are much less
severe in FIR.

GCE, Ramanagaram Dept. of E&E


53
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

Disadvantages
FIR IIR
 Require higher order  Sensitive to data
 Increased hardware round off and cutoff
 More computations  Make become
 Larger input and unstable
output delays  Poor phase response
 Cost more

29. What is the procedure to design a digital Butterworth filter?


30. What is the difference between Butterworth, Chebyshev I and Chebyshev II filters?
31. What are difference equations and differential equations?
32. What is non real time processing?

Raw Refined
Collector Processor
Data Data

Signal
33. What is meant by real time processing?
 Ability to collect, analyze, and modify signals in real-time
 Real-Time: As these signals are occurring
 We can analyze and process signals while collecting them, not at a later time.

Real-Time
Signal Refined
Processor Data
Signal

34. What is a Digital Signal Processor (DSP)?


Microprocessor specifically designed to perform fast DSP operations (e.g., Fast Fourier
Transforms, inner products, Multiply & Accumulate)
 Good at arithmetic operations (multiplication/division)

GCE, Ramanagaram Dept. of E&E


54
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

 Mostly programmed with Assembly and C through Integrated Development


Environment (IDE)
35. Differentiate between RISC and CISC architectures.

RISC Emphasis Single- large Better C


on clock, code compilers
software reduced size
instruction
only

CISC Emphasis Includes Small Poor C


on multi-clock code compilers
hardware complex sizes
instructions

36. Differentiate between General purpose MPU(Micro Processor Unit) and DSP Processor
MPU are built for a range of general-purpose functions such as:
Data manipulation
Math calculations
Control systems
They run large blocks of software
They are used in real-time and in unreal-time systems
DSPs are single-minded, dedicated to:
Perform mathematical calculations
Small blocks of software
Have a predictable execution time
Real-time only
Could assist a general-purpose host MPU

Microprocessor DSP
General purpose Arithmetic
Fixed internal format Varying internal format
Single memory access Multiple memory access
General addressing mode Special addressing mode
Very large external memory Very large internal memory

GCE, Ramanagaram Dept. of E&E


55
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

37. What is pipelining?

Pipeline
Description
Stage
Generate program fetch address
PF
Read opcode
Route opcode to functional unit
D
Decode instruction

E Execute instruction

38. What is parallel processing?


39. What is MAC?
40. What is barrel shifter? Why it is advantageous to use it in DSP processor?
41. Differentiate between floating point DSP and fixed point DSP.
42. Fixed Point/Floating Point
 fixed point processor are :

i. cheaper
ii. smaller
iii. less power consuming
iv. Harder to program

1. Watch for errors: truncation, overflow, rounding

v.Limited dynamic range


vi.Used in 95% of consumer products
 floating point processors
i.have larger accuracy
ii.are much easier to program
iii.can access larger memory
iv. It is harder to create an efficient program in C on a fixed point processors than on
floating point processors

GCE, Ramanagaram Dept. of E&E


56
Digital Signal Processing Lab ManualDigital Signal Processing Lab Manual

References
Digital Signal Processing – Principles, Jhon G. Proakis 4th Edition,
Pearson
Algorithms, and Applications Dimitris G. Manolakis 2007.
st
1 Edition,
Digital Signal Processing Sanjith K Mitra McGraw Hill
2005
nd
2 Edition,
Digital Signal Processing A.NagoorKani McGraw Hill
2012
nd
2 Edition,
Digital Signal Processing Shaila D. Apte Wiley
2009
st
1 Edition,
Digital Signal Processing Ashok Amberdar Cengage
2007
st
1 Edition,
Digital Signal Processing Tarun Kumar Rawat Oxford
2015

Dr. Ganesh Rao & Vineeta P. Gejji. Texas instruments materials.

GCE, Ramanagaram Dept. of E&E


57

Potrebbero piacerti anche