Anda di halaman 1dari 48

Digital Signal Processing

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);
1

Digital Signal Processing


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:
Unit Step Signal

0.8

0.8

0.6
0.4

Amplitude

0.2

0.6
0.4
0.2

0
-2

0
n

2
n

0.5

0.5

-1

Cos Signal

-0.5

Amplitude

50

Sine Signal

Amplitude

100

Exponential Signal
150

Amplitude

Ramp Signal

Amplitude

Amplitude

Unit Impulse Signal


1

-0.5

2
n

Date:

-1

2
n

Sign:
________________

Digital Signal Processing

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

Date:

Sign:
___________

Digital Signal Processing

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


Output:
Pole-Zero Plot of X(z) = (z - 1)/(z - 0.7071)2
1.5

Imaginary Part

0.5

-0.5

-1

-1.5
-1.5

-1

-0.5

0
Real Part

0.5

1.5

Pole-Zero Plot of X(z) = (z 4 - 1)/(z 4 + 1)


1.5

Imaginary Part

0.5

-0.5

-1

-1.5
-1.5

-1

-0.5

0
Real Part

0.5

1.5

Digital Signal Processing

Pole-Zero Plot of X(z) = (z 3 - z 2 + z - 1) / (z + 0.9)3


1.5

Imaginary Part

0.5

-0.5

-1

-1.5
-1.5

-1

-0.5

0
Real Part

Date:

0.5

1.5

Sign:
___________

Digital Signal Processing

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


Output:

Digital Signal Processing

Date:

Sign:
___________

10

Digital Signal Processing

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

11

Digital Signal Processing

12

Digital Signal Processing

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');

13

Digital Signal Processing


Output:

1st sequence

Amplitude

0.5

1.5

2.5
2nd st sequence

3.5

Amplitude

8
6
4
2
0

4
5
The resultant signal

Amplitude

30
20
10
0

6
n --->

Date:

10

11

Sign:
___________

14

Digital Signal Processing

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)');

15

Digital Signal Processing


Output:
x(n)

Amplitude

4
3
2
1
0

1.5

2.5
h(n)

3.5

1.5

2.5
y(n)=x(n)*h(n)

3.5

4
n

Amplitude

2
1.5
1
0.5
0

Amplitude

8
6
4
2
0

Date:

Sign:
___________

16

Digital Signal Processing

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') ;
17

Digital Signal Processing


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)) ;
18

Digital Signal Processing


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
19

Digital Signal Processing


subplot(2,1,2) ;
plot(F/pi, a) ;
title('Phase Response') ;
grid ;
Output:

20

Digital Signal Processing

Date:

Sign:
___________

21

Digital Signal Processing

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
22

Digital Signal Processing


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);
23

Digital Signal Processing


%*************** 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

24

Digital Signal Processing

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

25

Digital Signal Processing

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

26

Digital Signal Processing

Date:

Sign: ___________

27

Digital Signal Processing

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');
28

Digital Signal Processing


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:

Magnitude (dB)

Low Pass Filter for x1(n) = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
50

-50

-100

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

Phase (degrees)

-2000

-4000

-6000

29

Digital Signal Processing

Magnitude (dB)

High Pass Filter for x1(n) = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
50
0
-50
-100
-150

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

Phase (degrees)

1000
0
-1000
-2000
-3000
-4000

Low Pass Filter for x2(n) = 3 * exp(-0.1 * n)

Magnitude (dB)

50

-50

-100

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

Phase (degrees)

-2000

-4000

-6000

30

Digital Signal Processing


High Pass Filter for x2(n) = 3 * exp(-0.1 * n)

Magnitude (dB)

50
0
-50
-100
-150

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

Phase (degrees)

1000
0
-1000
-2000
-3000
-4000

Low Pass Filter for x3(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)

Magnitude (dB)

50

-50

-100

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

Phase (degrees)

-2000

-4000

-6000

31

Digital Signal Processing


High Pass Filter for x3(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)

Magnitude (dB)

50
0
-50
-100
-150

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

Phase (degrees)

1000
0
-1000
-2000
-3000
-4000

Date:

Sign:
___________

32

Digital Signal Processing

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;
33

Digital Signal Processing


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:
Bandpass Filter for Chebyshev Window

Magnitude (dB)

-50

-100

-150

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

Phase (degrees)

500

-500

-1000

34

Digital Signal Processing


Bandstop Filter for Chebyshev Window

Magnitude (dB)

-5

-10

-15

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

Phase (degrees)

0
-500
-1000
-1500
-2000

Date:

Sign:
___________

35

Digital Signal Processing

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');

36

Digital Signal Processing


Output:
Low Pass Buttorworth Filter

Magnitude (dB)

-100

-200

-300

50

100

150

200
250
300
Frequency (Hz)

350

400

450

500

50

100

150

200
250
300
Frequency (Hz)

350

400

450

500

Phase (degrees)

0
-200
-400
-600
-800

High Pass Buttorworth Filter

Magnitude (dB)

0
-100
-200
-300
-400

50

100

150

200
250
300
Frequency (Hz)

350

400

450

500

50

100

150

200
250
300
Frequency (Hz)

350

400

450

500

Phase (degrees)

200
0
-200
-400
-600
-800

37

Digital Signal Processing


Band Pass Buttorworth Filter

Magnitude (dB)

100
0
-100
-200
-300

50

100

150

200
250
300
Frequency (Hz)

350

400

450

500

50

100

150

200
250
300
Frequency (Hz)

350

400

450

500

Phase (degrees)

500

-500

-1000

Band Stop Buttorworth Filter

Magnitude (dB)

0
-50
-100
-150
-200
-250

50

100

150

200
250
300
Frequency (Hz)

350

400

450

500

50

100

150

200
250
300
Frequency (Hz)

350

400

450

500

Phase (degrees)

0
-200
-400
-600
-800

Date:

Sign:
___________

38

Digital Signal Processing

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:
Sample PSD Plot
10

Power Spectral Density (dB/ rad/sample)

5
0
-5
-10
-15
-20
-25
-30

0.2

0.4

0.6
0.8
1
1.2
1.4
1.6
Normalized Frequency (
rad/sample)

Date:

1.8

Sign:
___________

39

Digital Signal Processing

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');

40

Digital Signal Processing


Output:

Remez Design
1.4
Ideal
remez Design
1.2

Magnitude

0.8

0.6

0.4

0.2

0.1

0.2

0.3

0.4

0.5
0.6
Frequency

Date:

0.7

0.8

0.9

Sign:
___________

41

Digital Signal Processing

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)');

42

Digital Signal Processing


Output:
Sequence --> x(n1,n2)

Sequence --> h(n1,n2)

0.8

0.8

0.6

0.6

0.4

0.4

0.2

0.2

1.5

2.5

Sequence --> y(n1,n2)


4
3
2
1
0

Date:

Sign:
___________

43

Digital Signal Processing

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);
subplot(2,2,1);
stem(x);
title('x(n)');
subplot(2,2,2);
stem(y);
title('y(n)');

%Correlation of 1-D sequence

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)');

44

Digital Signal Processing


Output:

x(n)

y(n)

30

20

2
10

1
0

x(n1,n2)

y(n1,n2)

250
200

150
4
100
2
0

50
1

Date:

Sign:
___________

45

Digital Signal Processing

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
stable = 1

0.9899

0.0031 -0.0082

The system is not stable


a1 = 1 1 0 0 0 0
stable = 0
The system is stable

Date:

Sign:
___________

46

Digital Signal Processing

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++)
{
47

Digital Signal Processing


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:
___________

48

Anda mungkin juga menyukai