0% found this document useful (0 votes)
36 views

Advanced Digital Signal Processing Lab

This MATLAB code generates and plots several signals: 1) It generates a random number sequence and plots it. 2) It generates three sine waves with different frequencies, plots each individually, and then plots their sum. 3) It plots an original sequence, folds it (reverses it), and plots the folded sequence. 4) It generates an exponential signal a^n, multiplies it by a unit step function, and plots the resulting signal.

Uploaded by

Raju Rockzz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Advanced Digital Signal Processing Lab

This MATLAB code generates and plots several signals: 1) It generates a random number sequence and plots it. 2) It generates three sine waves with different frequencies, plots each individually, and then plots their sum. 3) It plots an original sequence, folds it (reverses it), and plots the folded sequence. 4) It generates an exponential signal a^n, multiplies it by a unit step function, and plots the resulting signal.

Uploaded by

Raju Rockzz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Lab Tasks/Assignments

ADSP Lab

Submitted to: Submitted by:


Mr. Rajesh Mehra Gaurav Soni
Assistant Professor M.E.-Modular-ECE
ECE 2011 Batch

1
INDEX

S.No. Name of Experiment Page No.

To understand the basics of MATLAB


1 4

To write MATLAB code for random number generation and plot it.
2 7

To write MATLAB code for waveform addition and plot it.


3 8

To write MATLAB code for folding a sequence and plot it.


4 10

To write MATLAB code to plot x=a^n*u(n).


5 11

To write MATLAB code to generate the impulse sequence at n0 samples


6 lying between n1 and n2. 13

To write MATLAB code to generate complex exponential


7 x=exp(complex(sigma,w).*n). 14

To write MATLAB code for FIR filter design.


8 16

To write MATLAB code for IIR Filter design.


9 23

To write MATLAB code to upsample an input sequence by a factor L=3


10 with a frequency of 0.042 Hz. 29

2
To write MATLAB code to downsample an input sequence by a factor
11 M=3 with a frequency of 0.042 Hz. 30

To write MATLAB code for estimating the cost of filter.


12 31

To write MATLAB code to design an adaptive filter to extract a desired


13 signal from noise corrupted signal by cancelling the noise. 32

ADDITIONAL TASKS/PROGRAMS
14 34

3
EXPERIMENT-1

AIM: To understand the basics of MATLAB

Exercises:

>> a=[1 2 3 4 5 6]

a=

1 2 3 4 5 6

>> b=a+2

b=

3 4 5 6 7 8

>> plot(b)

7.5

6.5

5.5

4.5

3.5

3
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6

4
>> bar(b)

0
1 2 3 4 5 6

>> xlabel('sample #')


>> ylabel('pounds')

5
pounds

0
1 2 3 4 5 6
s a m p le #

5
>> plot(b,'*')

7.5

6.5

5.5

4.5

3.5

3
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6

>> axis([0 10 0 10])

10

0
0 1 2 3 4 5 6 7 8 9 10

6
EXPERIMENT-2

AIM: To write MATLAB code for random number generation and plot it.

Code:

>> n=input('enter the required number of samples:=');


enter the required number of samples:=10
>> x=rand(1,n);
>> stem(x)

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
1 2 3 4 5 6 7 8 9 10

7
EXPERIMENT-3

AIM: To write MATLAB code for waveform addition and plot it.

Code:

>> fs=100;
>> t=[1:100]/fs;
>> a1=input('enter value of amplitude a1:=');
enter value of amplitude a1:=5
>> a2=input('enter value of amplitude a2:=');
enter value of amplitude a2:=10
>> a3=input('enter value of amplitude a3:=');
enter value of amplitude a3:=15
>> s1=a1*sin(2*pi*t*5);
>> s2=a1*sin(2*pi*t*15);
>> s3=a1*sin(2*pi*t*30);
>> subplot(2,2,1);
>> plot(s1);
>> xlabel('sine waveform with frequency=5 hz');
>> ylabel('amplitude');
>> subplot(2,2,2);
>> plot(s2);
>> xlabel('sine waveform with frequency=15 hz');
>> ylabel('amplitude');
>> subplot(2,2,3);
>> plot(s3);
>> xlabel('sine waveform with frequency=30 hz');
>> ylabel('amplitude');
>> subplot(2,2,4);
>> s4=s1+s2+s3;
>> plot(s4);
>> xlabel('addition of three sine waves');
>> ylabel('amplitude');

8
5 5
amplitude

amplitude
0 0

-5 -5
0 50 100 0 50 100
sine waveform with frequency=5 hz sine waveform with frequency=15 hz

5 20

10
amplitude

amplitude

0 0

-10

-5 -20
0 50 100 0 50 100
sine waveform with frequency=30 hz addition of three sine waves

9
EXPERIMENT-4

AIM: To write MATLAB code for folding a sequence and plot it.

Code:

>> n=[-5:5];
>> x=[10:-1:0];
>> subplot(2,1,1);
>> stem(n,x);
>> title('the original sequence');
>> xlabel('samples n');
>> ylabel('x(n)');
>> y=fliplr(x);
>> subplot(2,1,2);
>> stem(n,y);
>> title('the folded sequence x(n)');
>> xlabel('samples n');
>> ylabel('x(-n)');

the original sequence


10

6
x(n)

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
sam ples n
the folded sequence x(n)
10

5
x(-n)

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
sam ples n

10
EXPERIMENT-5

AIM: To write MATLAB code to plot x=a^n*u(n).

Code:

>> ns=input('enter the ending point of the sequence if it is not from n=0:=');
enter the ending point of the sequence if it is not from n=0:=10

>> ne=input('enter the ending point of the sequence:=');


enter the ending point of the sequence:=20

>> a=input('enter the value of a:=');


enter the value of a:=1

>> n=[ns:ne];
>> u=[(n-ns)>=0];
>> subplot(2,2,1);
>> stem(n,u);
>> title('sample n');
>> ylabel('u(n)');
>> e=a*exp(n);
>> subplot(2,2,2);
>> stem(n,e);
>> title('exponential signal e=(a^n)');
>> xlabel('sample n');
>> ylabel('e=(a^n)');
>> m=u.*e;
>> subplot(2,1,2);
>> stem(n,m);
>> title('multiplied signal m=u*e');
>> xlabel('samples n');
>> ylabel('m=a*exp(n).*u');

11
n
sample n x 10exponential signal e=(a )
8

1 6

e=(an)
u(n) 0.5
2

0 0
10 15 20 10 15 20
sample n
x 10
8 multiplied signal m=u*e
5

4
m=a*exp(n).*u

0
10 11 12 13 14 15 16 17 18 19 20
samples n

12
EXPERIMENT-6

AIM: To write MATLAB code to generate the impulse sequence at n0 samples


lying between n1 and n2.

Code:

>> ns=input('enter the ending point of the sequence if it is not from n=0:=');
enter the ending point of the sequence if it is not from n=0:=5
>> ne=input('enter the ending point of the sequence:=');
enter the ending point of the sequence:=12
>> n=[ns:ne];
>> u=[(n-ns)>=0];
>> stem(n,u);

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
5 6 7 8 9 10 11 12

13
EXPERIMENT-7

AIM: To write MATLAB code to generate complex exponential


x=exp(complex(sigma,w).*n)

Code:

>> n=input('enter the value of n:=');


enter the value of n:=5

>> sigma=input('enter the real value sigma:=');


enter the real value sigma:=2

>> w=input('enter the imaginary value w:=');


enter the imaginary value w:=3

>> x=exp(complex(sigma,w).*n)

x=

-1.6733e+004 +1.4324e+004i

>> n=[2 4;1 3]

n=

2 4
1 3

>> x=exp(complex(sigma,w).*n)

x=

1.0e+003 *

0.0524 - 0.0153i 2.5155 - 1.5995i


-0.0073 + 0.0010i -0.3676 + 0.1663i

14
>> plot(x,n)
Warning: Imaginary parts of complex X and/or Y arguments ignored

3.5

2.5

1.5

1
-500 0 500 1000 1500 2000 2500 3000

15
EXPERIMENT-8

AIM: To write MATLAB code for FIR filter design

Code:

a.Design of a low pass filter with cutoff frequency 0.6*pi radians per samples
(with filter order specified)

>> fc=0.6;
>> N=20;
>> Hf=fdesign.lowpass('N,fc',N,fc);
>> hd(1)=design(Hf,'window','window',@hamming);
>> hd(2)=design(Hf,'window','window',{@chebwin,50});
>> hfvt=fvtool(hd);
>> legend(hfvt,'hamming window design','dolph chebyshev window design');

Magnitude Response (dB)

hamming window design


-10
dolph chebyshev window design

-20

-30
Magnitude (dB)

-40

-50

-60

-70

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

16
b.(Without specifying filter order)

>> fp=0.6;
>> fst=0.7725;
>> fc=(fp+fst)/2;
>> Ap=0.01;
>> Ast=90;
>> setspecs(Hf,'fp,fst,Ap,Ast',fp,fst,Ap,Ast);
>> Hd(4)=design(Hf,'Kaiserwin');
>> hfvt=fvtool(Hd(4));

Magnitude Response (dB)

-20

-40
Magnitude (dB)

-60

-80

-100

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

17
c.Optimal Filter Design

>> Hd(5)=design(Hf,'equiripple');
>> hfvt=fvtool(Hd(4:5));
>> legend(hfvt,'Kaiser Window Design,Filter order 68','Equiripple Design,Filter Order 53')
Magnitude Response (dB)

Kaiser Window Design,Filter order 68


Equiripple Design,Filter Order 53
-20

-40
Magnitude (dB)

-60

-80

-100

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

d.Controlling filter parameters

>> N=20;
>> setspecs(Hf,'N,fc,Ap,Ast',N,fc,Ap,Ast);
>> Hd(6)=design(Hf,'equiripple');
>> hfvt=fvtool(Hd(5:6));
>> legend(hfvt,'Equiripple design,53 coefficients','Equiripple design,20 coefficients')

18
Magnitude Response (dB)

-10

-20
Equiripple design,53 coefficients
Equiripple design,20 coefficients
-30

-40
Magnitude (dB)

-50

-60

-70

-80

-90

-100

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

e.Transition Width Control

>> setspecs(Hf,'N,fp,fst',N,fp,fst);
>> Hd(7)=design(Hf,'Equiripple');
>> measure(Hd(7))

ans =

Sampling Frequency : N/A (normalized frequency)


Passband Edge : 0.6
3-dB Point : 0.65698
6-dB Point : 0.68512
Stopband Edge : 0.7725
Passband Ripple : 0.33005 dB
Stopband Atten. : 34.4345 dB
Transition Width : 0.1725

>> hfvt=fvtool(Hd(5),Hd(7));
>> legend(hfvt,'Equiripple design,53 coefficients','Equiripple design,20 coefficients')

19
Magnitude Response (dB)

-10

-20
Equiripple design,53 coefficients
Equiripple design,20 coefficients
-30

-40
Magnitude (dB)

-50

-60

-70

-80

-90

-100

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

f.Stop Band Attenuation Control

>> Hd(8)=design(Hf,'Equiripple','Wstop',5);
>> measure(Hd(8))

ans =

Sampling Frequency : N/A (normalized frequency)


Passband Edge : 0.6
3-dB Point : 0.64658
6-dB Point : 0.67277
Stopband Edge : 0.7725
Passband Ripple : 0.50871 dB
Stopband Atten. : 44.628 dB
Transition Width : 0.1725

>> hfvt=fvtool(Hd(7:8));

20
>> legend(hfvt,'passband weight=1,Stopband weight=1','Passband weight=1,Stopband
weight=5')

Magnitude Response (dB)

-5 passband weight=1,Stopband weight=1


Passband weight=1,Stopband weight=5
-10

-15

-20
Magnitude (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


Normalized Frequency (×π rad/sample)

g.No control over passband ripples

>> setspecs(Hf,'N,fp,fst,Ast',N,fp,fst,Ast);
>> Hd(9)=design(Hf,'Equiripple');
>> hfvt=fvtool(Hd(8:9));
>> legend(hfvt,'Equiripple design using weights','Equripple design constraining the stopband')

21
Magnitude Response (dB)

-10

-20
Equiripple design using w eights
Equripple design constraining the stopband
-30

-40
Magnitude (dB)

-50

-60

-70

-80

-90

-100

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

22
EXPERIMENT-9

AIM: To write MATLAB code for IIR Filter design

Code:

a.Butterworth Filter with cutoff frequency 0.4Pi

>> N=8;
>> f3db=0.4;
>> d=fdesign.lowpass('N,f3db',N,f3db);
>> Hbutter=design(d,'butter');
>> hfvt=fvtool([Hbutter]);
Magnitude Response (dB)

-20

-40

-60

-80
Magnitude (dB)

-100

-120

-140

-160

-180

-200

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

b.Chebyshev type 1

>> Ap=0.5;
>> setspecs(d,'N,f3db,Ap',N,f3db,Ap);
>> Hcheby1=design(d,'cheby1');
>> hfvt=fvtool([Hbutter,Hcheby1],'color','white');
>> legend(hfvt,'Butterworth','Chebyshev Type 1');

23
Magnitude Response (dB)

-50

-100
Magnitude (dB)

-150

Butterworth
Chebyshev Type 1

-200

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

c.Chebyshev type II

>> Ast=80;
>> setspecs(d,'N,f3db,Ast',N,f3db,Ast);
>> Hcheby2=design(d,'Cheby2');
>> set(hfvt,'filters',[Hbutter;Hcheby2]);
>> legend(hfvt,'Butterworth','Chebyshev Type II');

24
Magnitude Response (dB)

-20

-40

-60
Butterworth
Chebyshev Type II
-80
Magnitude (dB)

-100

-120

-140

-160

-180

-200

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

d.Elliptic Filter

>> setspecs(d,'N,f3db,Ap,Ast',N,f3db,Ap,Ast);
>> Hellip=design(d,'Ellip');
>> set(hfvt,'Filters',[Hbutter,Hcheby1,Hcheby2,Hellip]);
>> axis([0 1 -90 2])
>> legend(hfvt,'Butterworth','Chebyshev Type I','Chebyshev Type II','Elliptic');
>>

25
Magnitude Response (dB)

-10

-20 Butterw orth


Chebyshev Type I
Chebyshev Type II
-30 Elliptic

-40
Magnitude (dB)

-50

-60

-70

-80

-90
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalized Frequency (×π rad/sample)

After Zoom

>> axis([0 0.44 -5 0])

Magnitude Response (dB)


0

-0.5

-1

Butterw orth
Chebyshev Type I
-1.5
Chebyshev Type II
Elliptic

-2
Magnitude (dB)

-2.5

-3

-3.5

-4

-4.5

-5
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4
Normalized Frequency (×π rad/sample)

26
e.Group delay analysis

>> set(hfvt,'Analysis','grpdelay');

Group Delay

45

40

35

30
Group delay (in samples)

Butterw orth
25
Chebyshev Type I
Chebyshev Type II

20 Elliptic

15

10

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

f.All IIR Filters

>> fp=0.1;fst=0.3;Ap=1;Ast=60;
>> setspecs(d,'fp,fst,Ap,Ast',fp,fst,Ap,Ast);
>> Hd=design(d,'alliir');
>> set(hfvt,'Filters',Hd,'Analysis','magnitude','Designmask','On');
>> axis([0 1 -70 2])
>> legend(hfvt,'Butterworth','Chebyshev Type I','Chebyshev Type II','Elliptic');

27
Magnitude Response (dB)

-10

-20

-30 Butterworth
Magnitude (dB)

Chebyshev Type I
Chebyshev Type II
Elliptic
-40

-50

-60

-70
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalized Frequency (×π rad/sample)

g.finding order of a filter

>> order(Hd)

ans =

7 5 5 4

28
EXPERIMENT-10

AIM: To write MATLAB code to upsample an input sequence by a factor L=3


with a frequency of 0.042 Hz.

Code:

>> N=input('Input length = ');


Input length = 5
>> L=input('Up sampling Factor = ');
Up sampling Factor = 3
>> fo=input('input signal frequency = ');
input signal frequency = 0.042
>> n=0:N-1;
>> x=sin(2*pi*fo*n);
>> subplot(2,1,1);
>> stem(x);
>> y=upsample(x,3);
>> subplot(2,1,2);
>> stem(y);

0.8

0.6

0.4

0.2

0
1 1.5 2 2.5 3 3.5 4 4.5 5

0.8

0.6

0.4

0.2

0
0 5 10 15

29
EXPERIMENT-11

AIM: To write MATLAB code to downsample an input sequence by a factor


M=3 with a frequency of 0.042 Hz.

Code:

>> N=input('Input length = ');


Input length = 20
>> M=input('Down sampling Factor = ');
Down sampling Factor = 3
>> fo=input('input signal frequency = ');
input signal frequency = 0.042
>> n=0:N-1;
>> x=sin(2*pi*fo*n);
>> subplot(2,1,1);
stem(x)
>> y=downsample(x,3);
>> subplot(2,1,2);
>> stem(y)

0.5

-0.5

-1
0 2 4 6 8 10 12 14 16 18 20

0.5

-0.5

-1
1 2 3 4 5 6 7

30
EXPERIMENT-12

AIM: To write MATLAB code for estimating the cost of filter.

Code:

1.Butterworth Filter

>> N=8;
>> f3db=0.4;
>> d=fdesign.lowpass('N,f3db',N,f3db);
>> Hbutter=design(d,'butter');
>> hfvt=fvtool([Hbutter]);
>> cost(Hbutter)

ans =

Number of Multipliers : 17
Number of Adders : 16
Number of States :8
MultPerInputSample : 17
AddPerInputSample : 16

2.Chebyshev-1

>> Ap=0.5;
>> setspecs(d,'N,f3db,Ap',N,f3db,Ap);
>> Hcheby1=design(d,'cheby1');
>> hfvt=fvtool([Hbutter,Hcheby1],'color','white');
>> cost(Hcheby1)

ans =

Number of Multipliers : 17
Number of Adders : 16
Number of States :8
MultPerInputSample : 17
AddPerInputSample : 16

31
EXPERIMENT-13

AIM: To write MATLAB code to design an adaptive filter to extract a desired


signal from noise corrupted signal by cancelling the noise.

Code:

>> n=(1:1000)';
>> s=sin(0.075*pi*n);
>> v=0.8*randn(1000,1);
>> ar=[1,0.5];
>> v1=filter(1,ar,v);
>> x=s+v1;
>> ma=[1,-0.8,0.4,-0.2];
>> v2=filter(ma,1,v);
>> L=7;
>> hlms=adaptfilt.lms(7);
>> hnlms=adaptfilt.nlms(7);
>> [mumaxlms,mumaxmselms]=maxstep(hlms,x);
Warning: Step size is not in the range 0 < mu < mumaxmse/2:
Erratic behavior might result.
> In adaptfilt.lms.maxstep at 32
>> [mumaxlms,mumaxmsenlms]=maxstep(hnlms);
>> [ylms,elms]=filter(hlms,v2,x);
>> [ynlms,enlms]=filter(hnlms,v2,x);
>> bw=firwiener(L-1,v2,x);
>> yw=filter(bw,1,v2);
>> ew=x-yw;
>> plot(n(900:end),[ew(900:end),elms(900:end),enlms(900:end)]);
>> legend('wiener filter denoised sinusoid','NLMS denoised sinusoid','LMS
denoised sinusoid');
>> xlabel('time index (n)');
>> ylabel('amplitude');
>> [bw.' hlms.coefficients.' hnlms.coefficients.']

ans =

1.0131 0.8855 1.0347


0.3293 0.2237 0.2358
0.1321 0.1693 -0.1512
0.0811 0.0463 -0.0769
0.1745 0.2385 0.2180
0.1293 0.1568 0.1796
0.0647 0.0926 0.2457

32
4
wiener filter denoised sinusoid
NLMS denoised sinusoid
LMS denoised sinusoid
3

1
amplitude

-1

-2

-3

-4
900 910 920 930 940 950 960 970 980 990 1000
time index (n)

33
ADD ON PROGRAMS

Matlab Basics

>>t=linspace(1,10,10)

t=

1 2 3 4 5 6 7 8 9 10

>> sin(t)

ans =

Columns 1 through 9

0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894


0.4121

Column 10

-0.5440

>> x=t.*sin(t)

x=

Columns 1 through 9

0.8415 1.8186 0.4234 -3.0272 -4.7946 -1.6765 4.5989 7.9149


3.7091

Column 10

-5.4402

>> t.^2

ans =

1 4 9 16 25 36 49 64 81 100

34
>> z=sin(t.^2)

z=

Columns 1 through 9

0.8415 -0.7568 0.4121 -0.2879 -0.1324 -0.9918 -0.9538 0.9200 -


0.6299

Column 10

-0.5064

>> a=[1,2;3,4]

a=

1 2
3 4

>> nthroot(a,3)

ans =

1.0000 1.2599
1.4422 1.5874

35
Program to plot sine wave

x=linspace(0,2*pi,100);
>> y=sin(x);
>> plot(x,y)
>> title('plot created by gaurav')

plot created by gaurav


1

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7

36
Generation of delayed sine wave

>> t=0:pi/100:2*pi;
>> a=1;
>> y=a*sin(t);
>> y1=a*sin(t-0.25);
>> y1=a*sin(t-0.5);
>> y1=a*sin(t-0.25);
>> y2=a*sin(t-0.5);
>> y3=a*sin(t+0.25);
>> y4=a*sin(t+0.5);
>> plot(t,y,t,y1,t,y2,t,y3,t,y4);
>> grid on;
>> xlabel('time');
>> ylabel('amplitude');

delayed sine waves


1

0.8

0.6

0.4

0.2
amplitude

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
time

37
FIR Filter

a. Design of a low pass filter with cutoff frequency 0.4*pi radians per samples
(with filter order specified)

>> fc=0.4;
>> N=25;
>> Hf=fdesign.lowpass('N,fc',N,fc);
>> hd(1)=design(Hf,'window','window',@hamming);
>> hd(2)=design(Hf,'window','window',{@chebwin,50});
>> hfvt=fvtool(hd);
>> legend(hfvt,'hamming window design','dolph chebyshev window design');

Magnitude Response (dB)

-10
hamming w indow design
dolph chebyshev w indow design
-20

-30
Magnitude (dB)

-40

-50

-60

-70

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

38
b.Without specifying filter order

>> fp=0.3;
>> fst=0.3725;
>> fc=(fp+fst)/2;
>> Ap=0.05;
>> Ast=100;
>> setspecs(Hf,'fp,fst,Ap,Ast',fp,fst,Ap,Ast);
>> Hd(4)=design(Hf,'Kaiserwin');
>> hfvt=fvtool(Hd(4));

Magnitude Response (dB)

-20

-40

-60
Magnitude (dB)

-80

-100

-120

-140

-160
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
Normalized Frequency (×π rad/sample)

39
c.Optimal Filter Design

>> Hd(5)=design(Hf,'equiripple');
>> hfvt=fvtool(Hd(4:5));
>> legend(hfvt,'Kaiser Window Design,Filter order 68','Equiripple Design,Filter
Order 53')

Magnitude Response (dB)

-20
Kaiser Window Design,Filter order 68
Equiripple Design,Filter Order 53
-40

-60
Magnitude (dB)

-80

-100

-120

-140

-160
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
Normalized Frequency (×π rad/sample)

40
d.Controlling filter parameters

>> N=10;
>> setspecs(Hf,'N,fc,Ap,Ast',N,fc,Ap,Ast);
>> Hd(6)=design(Hf,'equiripple');
>> hfvt=fvtool(Hd(5:6));
>> legend(hfvt,'Equiripple design,53 coefficients','Equiripple design,20
coefficients')

Magnitude Response (dB)

0 Equiripple design,53 coefficients


Equiripple design,20 coefficients

-20

-40
Magnitude (dB)

-60

-80

-100

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

41
e.Transition Width Control

>> setspecs(Hf,'N,fp,fst',N,fp,fst);
>> Hd(7)=design(Hf,'Equiripple');
>> measure(Hd(7))

ans =

Sampling Frequency : N/A (normalized frequency)


Passband Edge : 0.3
3-dB Point : 0.30949
6-dB Point : 0.33612
Stopband Edge : 0.3725
Passband Ripple : 3.9308 dB
Stopband Atten. : 13.0541 dB
Transition Width : 0.0725

>> hfvt=fvtool(Hd(5),Hd(7));
>> legend(hfvt,'Equiripple design,53 coefficients','Equiripple design,20
coefficients')

Magnitude Response (dB)

0 Equiripple design,53 coefficients


Equiripple design,20 coefficients

-20

-40
Magnitude (dB)

-60

-80

-100

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

42
f.Stop Band Attenuation Control

>> Hd(8)=design(Hf,'Equiripple','Wstop',50);
>> measure(Hd(8))

ans =

Sampling Frequency : N/A (normalized frequency)


Passband Edge : 0.3
3-dB Point : 0.20826
6-dB Point : 0.23965
Stopband Edge : 0.3725
Passband Ripple : 19.2864 dB
Stopband Atten. : 35.8683 dB
Transition Width : 0.0725

>> hfvt=fvtool(Hd(7:8));
>> legend(hfvt,'passband weight=1,Stopband weight=1','Passband
weight=1,Stopband weight=50')

Magnitude Response (dB)

0
passband w eight=1,Stopband w eight=1
Passband w eight=1,Stopband w eight=50

-10
Magnitude (dB)

-20

-30

-40

-50

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

43
g.No control over passband ripples

>> setspecs(Hf,'N,fp,fst,Ast',N,fp,fst,Ast);
>> Hd(9)=design(Hf,'Equiripple');
>> hfvt=fvtool(Hd(8:9));
>> legend(hfvt,'Equiripple design using weights','Equripple design constraining the
stopband')

Magnitude Response (dB)

Equiripple design using w eights


-20 Equripple design constraining the stopband

-40
Magnitude (dB)

-60

-80

-100

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

44
IIR Filters

a.Butterworth Filter with cutoff frequency 0.8Pi

>> N=80;
>> f3db=0.8;
>> d=fdesign.lowpass('N,f3db',N,f3db);
>> Hbutter=design(d,'butter');
>>hfvt=fvtool([Hbutter]);

Magnitude Response (dB)

-100

-200

-300
Magnitude (dB)

-400

-500

-600

-700

-800
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
Normalized Frequency (×π rad/sample)

45
b.Chebyshev type 1

>> Ap=50;
>> setspecs(d,'N,f3db,Ap',N,f3db,Ap);
>> Hcheby1=design(d,'cheby1');
hfvt=fvtool([Hbutter,Hcheby1],'color','white');
legend(hfvt,'Butterworth','Chebyshev Type 1');

Magnitude Response (dB)

-200

Butterw orth
-400
Chebyshev Type 1
Magnitude (dB)

-600

-800

-1000

-1200

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

46
c.Chebyshev type II

>> Ast=80;
>> setspecs(d,'N,f3db,Ast',N,f3db,Ast);
>> Hcheby2=design(d,'Cheby2');
>> set(hfvt,'filters',[Hbutter;Hcheby2]);
>> legend(hfvt,'Butterworth','Chebyshev Type II');

Magnitude Response (dB)

-100

-200
Butterw orth
Chebyshev Type II
-300
Magnitude (dB)

-400

-500

-600

-700

-800
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
Normalized Frequency (×π rad/sample)

47
d.Elliptic Filter

>> setspecs(d,'N,f3db,Ap,Ast',N,f3db,Ap,Ast);
>> Hellip=design(d,'Ellip');
??? Error using ==> feval
Error using ==> fdesign.abstracttype.ellip at 19
The filter must be stable.

>> Ast=800;
>> setspecs(d,'N,f3db,Ast',N,f3db,Ast);
>> Hcheby2=design(d,'Cheby2');
set(hfvt,'filters',[Hbutter;Hcheby2]);
legend(hfvt,'Butterworth','Chebyshev Type II');

Magnitude Response (dB)

-100

-200
Butterw orth
Chebyshev Type II
-300
Magnitude (dB)

-400

-500

-600

-700

-800

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency (×π rad/sample)

>> setspecs(d,'N,f3db,Ap,Ast',N,f3db,Ap,Ast);
>> Hellip=design(d,'Ellip');
??? Error using ==> feval
Error using ==> fdesign.abstracttype.ellip at 19
Frequency value from the prototype filter must be between 0 and 1.

48
MATLAB code to design an adaptive filter to extract a desired signal from
noise corrupted signal by cancelling the noise.

1. With ar=[1,0.8];

>> n=(1:1000)';
>> s=sin(0.075*pi*n);
>> v=0.8*randn(1000,1);
>> ar=[1,0.8];
>> v1=filter(1,ar,v);
>> x=s+v1;
>> ma=[1,-0.8,0.4,-0.2];
>> v2=filter(ma,1,v);
>> L=7;
>> hlms=adaptfilt.lms(7);
>> hnlms=adaptfilt.nlms(7);
>> [mumaxlms,mumaxmselms]=maxstep(hlms,x);

Warning: Step size is not in the range 0 < mu < mumaxmse/2:


Erratic behavior might result.
> In adaptfilt.lms.maxstep at 32

>> [mumaxlms,mumaxmsenlms]=maxstep(hnlms);
>> [ylms,elms]=filter(hlms,v2,x);
>> [ynlms,enlms]=filter(hnlms,v2,x);
>> bw=firwiener(L-1,v2,x);
>> yw=filter(bw,1,v2);
>> ew=x-yw;
>> plot(n(900:end),[ew(900:end),elms(900:end),enlms(900:end)]);
>> legend('wiener filter denoised sinusoid','NLMS denoised sinusoid','LMS
denoised sinusoid');
>> xlabel('time index (n)');
>> ylabel('amplitude');
>> [bw.' hlms.coefficients.' hnlms.coefficients.']

ans =

1.0074 0.8684 1.0036


0.0384 -0.0256 -0.0044
0.2879 0.2646 -0.0426
-0.0626 -0.0775 -0.1927
0.2824 0.3048 0.2858
0.0363 0.1388 0.2067
0.2160 0.1952 0.3369

49
3
wiener filter denoised sinusoid
NLMS denoised sinusoid
LMS denoised sinusoid

0
amplitude

-1

-2

-3

-4
900 910 920 930 940 950 960 970 980 990 1000
time index (n)

2. With ar=[1,0.2];

>> n=(1:1000)';
>> s=sin(0.075*pi*n);
>> v=0.8*randn(1000,1);
>> ar=[1,0.2];
>> v1=filter(1,ar,v);
>> x=s+v1;
>> ma=[1,-0.8,0.4,-0.2];
>> v2=filter(ma,1,v);
>> L=7;
>> hlms=adaptfilt.lms(7);
>> hnlms=adaptfilt.nlms(7);
>> [mumaxlms,mumaxmselms]=maxstep(hlms,x);
Warning: Step size is not in the range 0 < mu < mumaxmse/2:
Erratic behavior might result.
> In adaptfilt.lms.maxstep at 32
>> [mumaxlms,mumaxmsenlms]=maxstep(hnlms);
>> [ylms,elms]=filter(hlms,v2,x);
>> [ynlms,enlms]=filter(hnlms,v2,x);
>> bw=firwiener(L-1,v2,x);

50
>> yw=filter(bw,1,v2);
>> ew=x-yw;
>> plot(n(900:end),[ew(900:end),elms(900:end),enlms(900:end)]);
>> legend('wiener filter denoised sinusoid','LMS denoised sinusoid','NLMS
denoised sinusoid');
>> xlabel('time index (n)');
>> ylabel('amplitude');
>> [bw.' hlms.coefficients.' hnlms.coefficients.']

ans =

1.0020 0.7985 0.7853


0.6063 -0.9678 0.4494
0.1318 -1.9994 -0.1909
0.0638 -1.7650 -0.0177
0.1330 -0.1956 -0.1847
0.1178 0.8161 -0.3353
0.0485 0.6470 -0.0202

50
wiener filter denoised sinusoid
LMS denoised sinusoid
NLMS denoised sinusoid

40

30
amplitude

20

10

-10
900 910 920 930 940 950 960 970 980 990 1000
time index (n)

51
3. With ar=[1,0.5];

>> n=(1:1000)';
>> s=sin(0.075*pi*n);
>> v=0.8*randn(1000,1);
>> ar=[1,0.5];
>> v1=filter(1,ar,v);
>> x=s+v1;
>> ma=[1,-0.8,0.4,-0.2];
>> v2=filter(ma,1,v);
>> L=7;
>> hlms=adaptfilt.lms(7);
>> hnlms=adaptfilt.nlms(7);
>> [mumaxlms,mumaxmselms]=maxstep(hlms,x);
Warning: Step size is not in the range 0 < mu < mumaxmse/2:
Erratic behavior might result.
> In adaptfilt.lms.maxstep at 32
>> [mumaxlms,mumaxmsenlms]=maxstep(hnlms);
>> [ylms,elms]=filter(hlms,v2,x);
>> [ynlms,enlms]=filter(hnlms,v2,x);
>> bw=firwiener(L-1,v2,x);
>> yw=filter(bw,1,v2);
>> ew=x-yw;
>> plot(n(900:end),[ew(900:end),elms(900:end),enlms(900:end)]);
>> legend('wiener filter denoised sinusoid','LMS denoised sinusoid','NLMS
denoised sinusoid');
>> xlabel('time index (n)');
>> ylabel('amplitude');
>> [bw.' hlms.coefficients.' hnlms.coefficients.']

ans =

0.9975 0.7548 0.6196


0.2983 0.1452 -0.3585
0.0930 -0.2884 -0.1028
0.0354 -0.4093 -0.4651
0.1215 -0.0258 -0.1977
0.0771 0.3733 0.0438
0.0333 0.1736 0.0575

52
20
wiener filter denoised sinusoid
LMS denoised sinusoid
NLMS denoised sinusoid

10

0
amplitude

-10

-20

-30

-40
900 910 920 930 940 950 960 970 980 990 1000
time index (n)

53

You might also like