DSP Journal
description
Transcript of DSP Journal
Digital Signal Processing
1
Practical No. 1
Topic: Basic Signals
Source Code:
clc;
clear all;
% Unit Impulse Signal
t=-2:1:2;
y=[zeros(1,2),ones(1,1),zeros(1,2)];
subplot(2,3,1);
stem(t,y);
title('Unit Impulse Signal');
ylabel('Amplitude');
xlabel('n');
%Unit Step Sequence [u(n)-u(n-N)]
n=5;
t=0:1:n-1;
y=ones(1,n);
subplot(2,3,2);
stem(t,y);
title('Unit Step Signal');
ylabel('Amplitude');
xlabel('n');
%Ramp Sequence
n=5;
t=0:n;
y=t;
subplot(2,3,3);
stem(t,y);
title('Ramp Signal');
ylabel('Amplitude');
xlabel('n');
%Exponential Sequence
n=5; %Length of Exponential
t=0:n;
a=1;
y=exp(a*t);
subplot(2,3,4);
stem(t,y);
title('Exponential Signal');
ylabel('Amplitude');
xlabel('n');
%Sine Sequence
t=0:0.01:pi;
y=sin(2*pi*t);
subplot(2,3,5);
Digital Signal Processing
2
plot(t,y);
title('Sine Signal');
ylabel('Amplitude');
xlabel('n');
grid;
%Sine Sequence
t=0:0.01:pi;
y=cos(2*pi*t);
subplot(2,3,6);
plot(t,y);
title('Cos Signal');
ylabel('Amplitude');
xlabel('n');
grid;
Output:
-2 0 20
0.2
0.4
0.6
0.8
1Unit Impulse Signal
Am
plit
ude
n
0 2 40
0.2
0.4
0.6
0.8
1Unit Step Signal
Am
plit
ude
n
0 2 4 60
1
2
3
4
5Ramp Signal
Am
plit
ude
n
0 2 4 60
50
100
150Exponential Signal
Am
plit
ude
n
0 2 4-1
-0.5
0
0.5
1Sine Signal
Am
plit
ude
n
0 2 4-1
-0.5
0
0.5
1Cos Signal
Am
plit
ude
n
Date: Sign:
________________
Digital Signal Processing
3
Practical No. 2
Topic: Frequency, Magnitude and Phase response
Source Code: clf; clc; close all; clear all; for n=1:256 sn1(n)=3.5*sin(2*pi*(0.15)*n)+sin(2*pi*(0.4)*n) sn2(n)=3+5.657*cos(2*pi*(0.1)*n) end subplot(2,1,1) plot(sn1) grid axis([0 100 -10 10]) title('sn1(n)=3.5*sin(2*pi*(0.15)*n)+sin(2*pi*(0.4)*n)'); subplot(2,1,2) plot(sn2) grid axis([0 100 -10 10]) title('sn2(n)=3+5.657*cos(2*pi*(0.1)*n)'); %Phase & Magnitude Response figure; freqz(sn1,256,512,2) title('magnitude & phase response of sn1'); figure freqz(sn2,256,512,2) title('magnitude & phase response of sn2'); Output :
Digital Signal Processing
4
Date: Sign:
___________
Digital Signal Processing
5
Practical No. 3
Topic: Z-transform
Source Code:
clc ; clear all ; close all ; % POLE ZERO PLOT FOR X(Z) = (Z-1) / (Z-0.7071)^2 figure ; b = [-1, 1] ; a = [1, -2*0.7071, (0.7071^2)] ; [r, p, k] = residuez(b, a) ; % r=RESIDUE, p=POLE, k=DIRECT TERMS r = roots(b) ; zplane(r, p) ; axis([1.5, 1.5, 1.5, 1.5]) ; title('Pole zero plot for X(z) = (z-1) / pow((z-0.7071), 2)') ; % POLE ZERO PLOT FOR X(Z) = (Z^4) - 1 / (Z^4) + 1 figure ; b = [1, 0, 0, 0, -1] ; a = [1, 0, 0, 0, 1] ; [r, p, k,] = residuez(b, a) ; r = roots(b) ; zplane(r, p) ; axis([-1.5, 1.5, -1.5, 1.5]) ; title('Pole zero plot for X(z) = (pow(z,4) - 1) / (pow(z,4) + 1)') ; % POLE ZERO PLOT FOR X(z) = (Z^3 - Z^2 + Z - 1) / ((Z + 0.9)^3) figure ; b = [1, -1, 1, -1] ; a = [1, 3*(0.9^2), 3*0.9, 0.9^3] ; [r, p, k] = residuez(b, a) ; r = roots(b) ; zplane(r, p) ; axis([1.5, 1.5, 1.5, 1.5]) ; title('Pole zero ploe for X(z) = (pow(z,3) - pow(z, 2) + z - 1) / (pow((z - 0.9), 3))') ;
Digital Signal Processing
6
Output:
-1.5 -1 -0.5 0 0.5 1 1.5-1.5
-1
-0.5
0
0.5
1
1.5
2
Real Part
Imagin
ary
Part
Pole-Zero Plot of X(z) = (z - 1)/(z - 0.7071)2
-1.5 -1 -0.5 0 0.5 1 1.5-1.5
-1
-0.5
0
0.5
1
1.5
Real Part
Imagin
ary
Part
Pole-Zero Plot of X(z) = (z4 - 1)/(z4 + 1)
Digital Signal Processing
7
-1.5 -1 -0.5 0 0.5 1 1.5-1.5
-1
-0.5
0
0.5
1
1.5
3
Real Part
Imagin
ary
Part
Pole-Zero Plot of X(z) = (z3 - z2 + z - 1) / (z + 0.9)3
Date: Sign:
___________
Digital Signal Processing
8
Practical No. 4
Topic: N-DFT
Source Code: % COMPUTE THE N-DFT FOR N = 8, 16, 24, 64, 128, 256 clc; clear all; close all; e=2.72; N = input('Please enter the length of the sequence : '); t = 0:1:N-1; for i = 1:N sig1(i) = 3 * e^(-0.1*i); sig2(i) = 2 * cos(2 * pi * 0.15 * i) + sin(2 * pi * 0.4 *i); sig3(i) = 4 * sin(4 * pi * 0.4 * i) + sin(2 * pi * 0.2 *i); end % PLOTTING FIRST SEQUENCE & ITS DFT figure ; subplot(2,1,1) ; plot(t, sig1) ; title(['Plot of sig(n) = 3 * pow(e,-0.1 * n) & N = ', int2str(N)]) ; subplot(2,1,2) ; stem(t, fft(sig1, N)) ; title(['FFT of sig(n) = 3 * pow(e,-0.1 * n) & N = ', int2str(N)]) ; % PLOTTING SECOND SEQUENCE & ITS DFT figure ; subplot(2,1,1) ; plot(t, sig2) ; title(['Plot of sig(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n) & N = ', int2str(N)]) ; subplot(2,1,2) ; stem(t, fft(sig2, N)) ; title(['FFT of sig(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n) & N = ', int2str(N)]) ; % PLOTTING THIRD SEQUENCE & ITS DFT figure ; subplot(2,1,1) ; plot(t, sig3) ; title(['Plot of sig(n) = 4 * sin(4 * pi * (0.4) * n) + sin(2 * pi * (0.2) * n) & N = ', int2str(N)]) ; subplot(2,1,2) ; stem(t, fft(sig3, N)) ; title(['FFT of sig(n) = 4 * sin(4 * pi * (0.4) * n) + sin(2 * pi * (0.2) * n) & N = ', int2str(N)])
Digital Signal Processing
9
Output:
Digital Signal Processing
10
Date: Sign:
___________
Digital Signal Processing
11
Practical No. 5
Topic: N-DFT using Twiddle Matrix
Source Code:
%N-DFT using Twiddle Matrix
clc;
clf;
clear all;
x=input('Enter x[n] ');
n=input('length of sequence ');
y=fft(x,n);
disp('y[n]');
disp(y);
subplot(2,1,1);
stem(y);
xlabel('DFT coefficients');
ylabel('Amplitude');
x=ifft(y,n);
disp('x[n]:');
disp(x);
subplot(2,1,2);
stem(x);
xlabel('n-->');
ylabel('amplitude');
Output:
Enter x[n] [1 2 3 4]
length of sequence 4
y[n]
10.0000
-2.0000 + 2.0000i -2.0000 -2.0000 - 2.0000i
Digital Signal Processing
12
Digital Signal Processing
13
Practical No. 6
Topic: Linear Convolution
Source Code: clc; clear all; close all; %Linear convolution of sequence x = [1 1 1 1] and h = [1 2 3 4 5 6 7 8] %x = input ('Enter 1st sequence: '); %h = input ('Enter 2nd sequence: '); x = [1 1 1 1]; h = [1 2 3 4 5 6 7 8]; y = conv(x,h); subplot(3,1,1); stem(x);grid ylabel('Amplitude'); title('1st sequence'); subplot(3,1,2); stem(h);grid ylabel('Amplitude'); title('2nd st sequence'); subplot(3,1,3); stem(y);grid xlabel('n --->'); ylabel('Amplitude'); title('The resultant signal');
Digital Signal Processing
14
Output:
1 1.5 2 2.5 3 3.5 40
0.5
1A
mplit
ude
1st sequence
1 2 3 4 5 6 7 80
2
4
6
8
Am
plit
ude
2nd st sequence
1 2 3 4 5 6 7 8 9 10 110
10
20
30
n --->
Am
plit
ude
The resultant signal
Date: Sign:
___________
Digital Signal Processing
15
Practical No. 7
Topic: Circular Convolution
Source Code:
clc; clear all; close all; %Circular convolution of sequence x = [1 2 4] and h = [1 2] %x = input('Enter 1st sequence: '); %h = input('Enter 2nd sequence: '); x = [1 2 4]; h = [1 2]; N1 = length(x); N2 = length(h); N3 = N1 + N2 -1 for n=1:N3-N1, x(n+N1) = 0; end for n=1:N3-N2; h(n+N2) = 0; end y = conv(x,h); subplot(3,1,1); stem(x);grid ylabel('Amplitude'); title('x(n)'); subplot(3,1,2); stem(h);grid ylabel('Amplitude'); title('h(n)'); subplot(3,1,3); stem(y);grid xlabel('n'); ylabel('Amplitude'); title('y(n)=x(n)*h(n)');
Digital Signal Processing
16
Output:
1 1.5 2 2.5 3 3.5 40
1
2
3
4
Am
plit
ude
x(n)
1 1.5 2 2.5 3 3.5 40
0.5
1
1.5
2
Am
plit
ude
h(n)
1 2 3 4 5 6 70
2
4
6
8
n
Am
plit
ude
y(n)=x(n)*h(n)
Date: Sign:
___________
Digital Signal Processing
17
Practical No. 8
Topic: Low-pass Filter
Source Code:
% LOW-PASS FIR FILTER clc ; clear all ; close all ; N = input('Please enter the number of samples : ') ; Wn = 0.2 * pi ; N1 = 1024 ; beta = 5.8 ; % RECTANGULAR WINDOW y = rectwin(N) ; figure ; % PLOTTING THE RECTANGULAR WINDOW subplot(2,2,1) ; plot(y) ; title(['Rectangular Window N = ', int2str(N)]) ; grid ; % DESIGNING THE FILTER b = fir1(N-1, Wn, 'low', y) ; % DESIGNING WINDOW-BASED FIR FILTER [H,F] = freqz(b,1,N) ; % RETURNS FREQ. RESPONSE VECTOR 'H' & FREQ. VECTOR 'F' m = 20 * log10(abs(H)) ; % CALCULATING THE MAGNITUDE RESPONSE a = rad2deg(unwrap(angle(H))) ; % CALCULATING THE PHASE RESPONSE % PLOTTING THE MAGNITUDE RESPONSE subplot(2,2,2) ; plot(F/pi, m) ; title('Magnitude Response') ; grid ; % PLOTTING THE PHASE RESPONSE subplot(2,2,3) ; plot(F/pi, a) ; title('Phase Response') ; grid ; % PLOTTING THE RECTANGULAR WINDOW FOR N = 1024 y1 = rectwin(N1) ; subplot(2,2,4) ; plot(y1) ; title('Rectangular Window for N = 1024') ;
Digital Signal Processing
18
grid ; % HAMMING WINDOW y = hamming(N) ; figure ; % PLOTTING THE HAMMING WINDOW subplot(2,2,1) ; plot(y) ; title(['Hamming window for N = ', int2str(N)]) ; grid ; % DESIGNING THE FILTER b = fir1(N-1, Wn, 'low', y) ; [H,F] = freqz(b,1,N) ; m = 20*log10(abs(H)) ; a = rad2deg(unwrap(angle(H))) ; % PLOTTING THE MAGNITUDE RESPONSE subplot(2,2,2) ; plot(F/pi, m) ; title('Magnitude Response') ; grid ; % PLOTTING THE PHASE RESPONSE subplot(2,2,3) ; plot(F/pi, a) ; title('Phase Response') ; grid ; % PLOTTING THE HAMMING WINDOW FOR N = 1024 y1 = hamming(N1) ; subplot(2,2,4) ; plot(y1) ; title('Hamming Window for N = 1024') ; grid ; % KAISER WINDOW y = kaiser(N, beta) ; figure ; % PLOTTING THE KAISER WINDOW subplot(2,2,1) ; plot(y) ; title(['Kaiser Window for N = ', int2str(N)]) ; grid ; % DESIGNING THE FILTER b = fir1(N-1, Wn, 'low', y) ; [H,F] = freqz(b,1,N) ; m = 10*log10(abs(H)) ;
Digital Signal Processing
19
a = rad2deg(unwrap(angle(H))) ; % PLOTTING THE MAGNITUDE RESPONSE subplot(2,2,2) ; plot(F/pi, m) ; title('Magnitude Response') ; grid ; % PLOTTING THE PHASE RESPONSE subplot(2,2,3) ; plot(F/pi, a) ; title('Phase Response') ; grid ; % PLOTTING THE KAISER WINDOW FOR N = 1024 y1 = kaiser(N1, beta) ; subplot(2,2,4) ; plot(y1) ; title('Kaiser Window for N = 1024') ; grid ; % FOURIER SERIES f = 0.2:0.2:0.8 ; t = 0:0.1:10 ; for i = 1:length(f) for j = 1:length(t) x(j) = 4 * sin(2*pi*f(i)*t(j)) + (4/3) * sin(2*pi*f(i)*t(j)) + (4/5) * sin(2*pi*f(i)*t(j)) + (4/7) * sin(2*pi*f(i)*t(j)) ; end end % PLOTTING THE FOURIER SERIES figure ; plot(t,x) ; title('Fourier Series') ; grid ; % DESIGNING THE FIR FILTERS b = fir1(length(x)-1, Wn, 'low', x) ; [H,F] = freqz(b,1,N) ; m = 20*log10(abs(H)) ; a = rad2deg(unwrap(angle(H))) ; figure ; % PLOTTING THE MAGNITUDE RESPONSE subplot(2,1,1) ; plot(F/pi, m) ; title('Magnitude Response') ; grid ; % PLOTTING THE PHASE RESPONSE
Digital Signal Processing
20
subplot(2,1,2) ; plot(F/pi, a) ; title('Phase Response') ; grid ; Output:
Digital Signal Processing
21
Date: Sign:
___________
Digital Signal Processing
22
Practical No. 9
Topic: High-pass FIR filter
Source Code:
%To design High PASS FIR filter using
%Blackman,Hamming and kaiser window
clc;
clear all;
clf;
close all;
sn=0;
f=input('Enter the sampling frequency where f= 0.8 or 0.6
or 0.4 or 0.2 := ');
n=input('Enter the order of filter Where n= 16 or 32 or 64
or 128 or 256 := ');
for i=1:256
sn(i)=(4*sin(2*pi*f*i)) + ((4/3)*sin(2*pi*3*f*i)) +
((4/5)*sin(2*pi*5*f*i)) + ((4/7)*sin(2*pi*7*f*i));
end
%********For Blackman Window***********
subplot(3,2,1)
plot(sn);
grid;
axis([0 150 -10 10]);
%create filter and use sn as input
wn=0.2*pi; %cuttoff frequency
win=blackman(n+1);%For Rectangular Window
b=fir1(n,wn,'high',win);
%Creating FIR LP filter with Rectangular window, returns
the filter coefficients in length N+1 vector B.
%Plot the output
hs=filter(b,1,sn);
subplot(3,2,2);
plot(hs);
grid;
axis([0 150 -10 10]);
title('output of the fir low pass filter');
%plot the magnitude response of the filter
subplot(3,2,3);
[H,F]=freqz(b,1,256);
plot(F,20*log10(abs(H)));
grid;
xlabel('Normlized f(rad/sample)');
ylabel('magnitude (dB)');
title('magnitude response');
%Plot the phase response of the filter
Digital Signal Processing
23
subplot(3,2,4);
plot(F,unwrap(angle(H)*180/pi));
grid;
xlabel('Normlized f(rad/sample)');
ylabel('phase (degree)');
title('phase response');
%Plot the filter
subplot(3,2,5);
plot(hs);grid
xlabel('Filter using Rectangular window');
fvtool(b);
%****************for Hamming window*********
figure;
subplot(3,2,1);
plot(sn);
axis([0 150 -10 10]);
win=hamming(n+1);
%fir1(n,Wn,'ftype',window) accepts both 'ftype' and window
parameters.'high' for a highpass filter
b=fir1(n,wn,'high',win);%Creating FIR LP filter with
hamminng window
hs=filter(b,1,sn);
grid;
%plot the output
subplot(3,2,2);
plot(hs);
grid;
axis([0 150 -10 10]);
title('ouput of the FIR Low pass filter');
%plot the magnitude response
subplot(3,2,3);
[H,F]=freqz(b,1,256);
plot(F,20*log10(abs(H)));
xlabel('Normlized f(rad/sample)');
ylabel('magnitude (dB)');
title('magnitude response');
grid;
%Plot the phase response of the filter
subplot(3,2,4);
plot(F,unwrap(angle(H)*180/pi));
grid;
xlabel('Normlized f(rad/sample)');
ylabel('phase (degree)');
title('phase response');
%Plot the filter
subplot(3,2,5);
plot(hs);
grid;
xlabel('Filter using hamming window');
fvtool(b);
Digital Signal Processing
24
%*************** kaiser window*********
figure;
win=kaiser(n+1,2.5);%Create a n-point Kaiser window with a
beta of 2.5 and display the result using WVTool
b=fir1(n,wn,'high',win);
yn=filter(b,1,sn);
subplot(2,1,1);
plot(yn);
grid;
axis([0 100 -6 6]);
title('output of the filter');
subplot(2,1,2);
plot(b);
grid;
xlabel('low pass filter using kaiser window');
fvtool(b);
Output:
Creating FIR LP filter with Blackman window
Where f = 0.8 or 0.6 or 0.4 or 0.2 := 0.4
Enter The Order Of Filter Where n = 16 or 32 or 64 or 128
or 256 := 16
Digital Signal Processing
25
Creating FIR LP filter with Hamming window
Where f = 0.8 or 0.6 or 0.4 or 0.2 := 0.4
Enter The Order Of Filter Where n = 16 or 32 or 64 or 128
or 256 := 16
Digital Signal Processing
26
Creating FIR LP filter with Kaiser window
Where f = 0.8 or 0.6 or 0.4 or 0.2 := 0.4
Enter The Order Of Filter Where n = 16 or 32 or 64 or 128
or 256 := 16
Digital Signal Processing
27
Date: Sign: ___________
Digital Signal Processing
28
Practical No. 10
Topic: High-pass and Low-pass FIR filters on various inputs
Source Code:
clc; clear all;close all; f=0.2:0.2:0.8; t=0:0.1:10; wn = 3/5; %wn is the cut-off frequency for k=1:length(f), for n=1:length(t), x1(n) = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n); x2(n) = 3 * exp(-0.1 * n); x3(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n); end end %Designing a low-pass filter for a x1(n) figure; b = fir1(length(x1)-1,wn,'low'); grid; freqz(b,1,256) title('Low Pass Filter for x1(n) = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)'); %Designing a high-pass filter for a x1(n) figure; b = fir1(length(x1)-1,wn,'high'); grid; freqz(b,1,256) title('High Pass Filter for x1(n) = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)'); %Designing a low-pass filter for a x2(n) figure; b = fir1(length(x2)-1,wn,'low'); grid; freqz(b,1,256) title('Low Pass Filter for x2(n) = 3 * exp(-0.1 * n)'); %Designing a high-pass filter for a x2(n) figure; b = fir1(length(x2)-1,wn,'high');
Digital Signal Processing
29
grid; freqz(b,1,256) title('High Pass Filter for x2(n) = 3 * exp(-0.1 * n)'); %Designing a low-pass filter for a x3(n) figure; b = fir1(length(x3)-1,wn,'low'); grid; freqz(b,1,256) title('Low Pass Filter for x3(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)'); %Designing a high-pass filter for a x3(n) figure; b = fir1(length(x3)-1,wn,'high'); grid; freqz(b,1,256) title('High Pass Filter for x3(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)'); Output:
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-6000
-4000
-2000
0
Normalized Frequency ( rad/sample)
Phase (
degre
es)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-100
-50
0
50
Normalized Frequency ( rad/sample)
Magnitude (
dB
)
Low Pass Filter for x1(n) = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
Digital Signal Processing
30
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-4000
-3000
-2000
-1000
0
1000
Normalized Frequency ( rad/sample)
Phase (
degre
es)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-150
-100
-50
0
50
Normalized Frequency ( rad/sample)
Magnitude (
dB
)
High Pass Filter for x1(n) = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-6000
-4000
-2000
0
Normalized Frequency ( rad/sample)
Phase (
degre
es)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-100
-50
0
50
Normalized Frequency ( rad/sample)
Magnitude (
dB
)
Low Pass Filter for x2(n) = 3 * exp(-0.1 * n)
Digital Signal Processing
31
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-4000
-3000
-2000
-1000
0
1000
Normalized Frequency ( rad/sample)
Phase (
degre
es)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-150
-100
-50
0
50
Normalized Frequency ( rad/sample)
Magnitude (
dB
)
High Pass Filter for x2(n) = 3 * exp(-0.1 * n)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-6000
-4000
-2000
0
Normalized Frequency ( rad/sample)
Phase (
degre
es)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-100
-50
0
50
Normalized Frequency ( rad/sample)
Magnitude (
dB
)
Low Pass Filter for x3(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
Digital Signal Processing
32
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-4000
-3000
-2000
-1000
0
1000
Normalized Frequency ( rad/sample)
Phase (
degre
es)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-150
-100
-50
0
50
Normalized Frequency ( rad/sample)
Magnitude (
dB
)
High Pass Filter for x3(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
Date: Sign:
___________
Digital Signal Processing
33
Practical No. 11
Topic: Band-pass and Band-stop FIR filters
Source Code:
clc; clear all; close all; % rp = input('enter passband ripple = '); % rs = input('enter stopband ripple = '); % fp = input('enter passband frequency = '); % fs = input('enter stopband frequency = '); % f = input('enter sampling frequency = '); % R = input('enter sidelob attenuation in dBs = '); rp = 0.03; rs = 0.01; fp = 2000; fs = 2500; f = 7000; R = 40; wp = 2*fp/f; ws = 2*fs/f; wn = [wp ws]; num = -20*log10(sqrt(rp*rs))-13; den = 14.6*(fs-fp)/f; n = ceil(num/den); n1 = n + 1; if (rem(n,2)~=0) n1 = n; n = n - 1; end N = 256; %Chebyshev Window --> y = chebwin(n1,R); plot(y); grid; title('Chebyshev Window'); figure; b = fir1(n1-1,wn,'bandpass',y); grid;
Digital Signal Processing
34
freqz(b,1,N); title('Bandpass Filter for Chebyshev Window'); b = fir1(n1-1,wn,'stop',y); grid; freqz(b,1,N); title('Bandstop Filter for Chebyshev Window'); Output:
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-1000
-500
0
500
Normalized Frequency ( rad/sample)
Phase (
degre
es)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-150
-100
-50
0
Normalized Frequency ( rad/sample)
Magnitude (
dB
)
Bandpass Filter for Chebyshev Window
Digital Signal Processing
35
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-2000
-1500
-1000
-500
0
Normalized Frequency ( rad/sample)
Phase (
degre
es)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-15
-10
-5
0
Normalized Frequency ( rad/sample)
Magnitude (
dB
)
Bandstop Filter for Chebyshev Window
Date: Sign:
___________
Digital Signal Processing
36
Practical No. 12
Topic: Digital IIR Filters
Source Code:
clc; clear all; close all; wp = 300; ws = 500; wn = wp/ws; [b,a] = butter (9, wn,'low'); freqz(b,a,128,1000) title('Low Pass Buttorworth Filter'); figure; [b,a] = butter(9,300/500,'high'); freqz(b,a,128,1000) title('High Pass Buttorworth Filter'); n = 5; wn = [wp ws]/600; figure; [b,a] = butter(n,wn,'bandpass') freqz(b,a,128,1000) title('Band Pass Buttorworth Filter'); figure; [b,a] = butter(n,wn,'stop') freqz(b,a,128,1000) title('Band Stop Buttorworth Filter');
Digital Signal Processing
37
Output:
0 50 100 150 200 250 300 350 400 450 500-800
-600
-400
-200
0
Frequency (Hz)
Phase (
degre
es)
0 50 100 150 200 250 300 350 400 450 500-300
-200
-100
0
Frequency (Hz)
Magnitude (
dB
)
Low Pass Buttorworth Filter
0 50 100 150 200 250 300 350 400 450 500-800
-600
-400
-200
0
200
Frequency (Hz)
Phase (
degre
es)
0 50 100 150 200 250 300 350 400 450 500-400
-300
-200
-100
0
Frequency (Hz)
Magnitude (
dB
)
High Pass Buttorworth Filter
Digital Signal Processing
38
0 50 100 150 200 250 300 350 400 450 500-1000
-500
0
500
Frequency (Hz)
Pha
se (
degr
ees)
0 50 100 150 200 250 300 350 400 450 500-300
-200
-100
0
100
Frequency (Hz)
Mag
nitu
de (
dB)
Band Pass Buttorworth Filter
0 50 100 150 200 250 300 350 400 450 500-800
-600
-400
-200
0
Frequency (Hz)
Pha
se (
degr
ees)
0 50 100 150 200 250 300 350 400 450 500-250
-200
-150
-100
-50
0
Frequency (Hz)
Mag
nitu
de (
dB)
Band Stop Buttorworth Filter
Date: Sign:
___________
Digital Signal Processing
39
Practical No. 13 Topic: Power Spectral Density
Source Code:
% POWER SPECTAL DENSITY clc ; clear all ; close all ; t = 0:0.001:0.3 ; % TRY FOR THE SAMPLING FREQUENCIES F = 200, 2000, 20000 F = input('Please enter the sampling frequency : ') ; x = cos(2*pi*F*t) + randn(size(t)) ; [Pxx,w] = periodogram(x,[],'two-sided',512,F) ; psdplot(Pxx,w,'','',['Power Spectral Density for F = ', int2str(F)]) ; %title(['Power Spectral Density for F = ', int2str(F)]) ;
Output:
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8-30
-25
-20
-15
-10
-5
0
5
10
Normalized Frequency ( rad/sample)
Pow
er S
pect
ral D
ensi
ty (
dB/
rad/
sam
ple)
Sample PSD Plot
Date: Sign:
___________
Digital Signal Processing
40
Practical No. 14
Topic: Remez Exchange Algorithm
Source Code:
clc; clear all; close all; f = [0 0.3 0.4 0.6 0.7 1]; % Frequency band edges a = [0 0 1 1 0 0]; % Desired amplitudes n = 17; % Filter order b = remez(n,f,a); [h,w] = freqz(b,1,512); figure; plot(f,a,w/pi,abs(h)) legend('Ideal','remez Design') title('Remez Design'); xlabel('Frequency'); ylabel('Magnitude'); n = 20; f = [0 0.4 0.5 1]; a = [1 1 0 0]; w = [1 10]; % Weight vector b = remez(n,f,a,w); [h w] = freqz(b,1,512); figure; plot(f,a,w/pi,abs(h)) legend('Ideal','remez Design') title('Remez Design'); xlabel('Frequency'); ylabel('Magnitude'); f = [0.05 1] a = [1 1] n = 21; b = remez(n,f,a,'h'); % Highpass Hilbert figure; [h,w] = freqz(b,1,512); plot(f,a,w/pi,abs(h)); legend('Ideal','remez Design') title('Remez Design'); xlabel('Frequency'); ylabel('Magnitude');
Digital Signal Processing
41
Output:
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.2
0.4
0.6
0.8
1
1.2
1.4Remez Design
Frequency
Magnitude
Ideal
remez Design
Date: Sign:
___________
Digital Signal Processing
42
Practical No. 15 Topic: Two-Dimensional Linear Convolution
Source Code: clc; clear all; close all; x = rand(3); h = rand(4); y = conv2(x,h); % y is 6-by-6 subplot(2,2,1); stem(x); title('Sequence --> x(n1,n2)'); subplot(2,2,2); stem(h); title('Sequence --> h(n1,n2)'); subplot(2,2,3); stem(y); title('Sequence --> y(n1,n2)');
Digital Signal Processing
43
Output:
1 1.5 2 2.5 30
0.2
0.4
0.6
0.8
1Sequence --> x(n1,n2)
1 2 3 40
0.2
0.4
0.6
0.8
1Sequence --> h(n1,n2)
0 2 4 60
1
2
3
4Sequence --> y(n1,n2)
Date: Sign:
___________
Digital Signal Processing
44
Practical No. 16
Topic: Two-Dimensional Cross-correlation and auto-correlation
Source Code:
clc; clear all; close all; x = [1 2 3 4]; y = xcorr(x); %Correlation of 1-D sequence subplot(2,2,1); stem(x); title('x(n)'); subplot(2,2,2); stem(y); title('y(n)'); x = [8 7 6 5;0 1 2 3;1 1 1 1;2 3 4 1]; y = xcorr2(x); %Correlation of 2-D sequence subplot(2,2,3); stem(x); title('x(n1,n2)'); subplot(2,2,4); stem(y); title('y(n1,n2)');
Digital Signal Processing
45
Output:
1 2 3 40
1
2
3
4x(n)
0 2 4 6 80
10
20
30y(n)
1 2 3 40
2
4
6
8x(n1,n2)
0 2 4 6 80
50
100
150
200
250y(n1,n2)
Date: Sign:
___________
Digital Signal Processing
46
Practical No. 17 Topic: Stability Check
Source Code: clc; clear all; a = [1.0000 -0.6149 0.9899 0.0000 0.0031 -0.0082] %a = [1 1 1 0 0 0.2]; %poly2rc(a) converts a prediction filter polynomial to reflection coefficients stable = all(abs(poly2rc(a))<1) if stable ~= 1 disp('The system is stable'); else disp('The system is not stable'); end a1 = [1.0000 1 0 0 0 0] stable = all(abs(poly2rc(a1))<1) if stable ~= 1 disp('The system is stable'); else disp('The system is not stable'); end Output: a =1.0000 -0.6149 0.9899 0 0.0031 -0.0082 stable = 1 The system is not stable a1 = 1 1 0 0 0 0 stable = 0 The system is stable
Date: Sign:
___________
Digital Signal Processing
47
Practical No. 18 Topic: Bit-reversal algorithm
Source Code:
#include<stdio.h> #include<iostream.h> #include<conio.h> #include<math.h> int getDecimal(int*); void main() { int arr[8] = {0,1,2,3,4,5,6,7}; int binary[8][3]; clrscr(); for(int c=0;c<8;c++) { for(int d=0;d<3;d++) { binary [c] [d] = 0; } } for(int i=0;i<8;i++) { int n = arr[i]; int j=0; while(n != 0) { int r = n%2; binary[i][3-j-1] = r; n = n/2; j++; } } cout << "Index\tBinary\t\tReversed\tReversed" << endl; cout << "\tRepresentation\tRepresentation\tIndex" << endl; cout << "------------------------------------------------" << endl; for(c=0;c<8;c++) { cout << arr[c] << "\t"; for(int d=0;d<3;d++) {
Digital Signal Processing
48
cout << binary[c][d] ; } cout << "\t\t"; for(d=0;d<3;d++) { cout << binary[c][3-d-1]; } cout << "\t\t" << getDecimal(binary[c]) << endl; } getch(); } int getDecimal(int* b) { int n=0; for(int i=0;i<3;i++) { n=n + b[i]*pow(2,i); } return n; } Output: Index Binary Reversed Reversed Representation Representation Index ------------------------------------------------ 0 000 000 0 1 001 100 4 2 010 010 2 3 011 110 6 4 100 001 1 5 101 101 5 6 110 011 3 7 111 111 7
Date: Sign:
___________