0% found this document useful (0 votes)
623 views34 pages

Digital Signal Processing Matlab Programs

Q1. Plot u(n) CODE: >> clear all; >> t=0:1:6; >> y=ones(1,7); >> plot(t,y);xlabel('n-->');ylabel('Amplitude');title('plot of u(n)') FIGURE: plot of u(n) 2 1.8 1.6 1.4 1.2 1 0.8 0.6 0.4 0.2 0 Amplitude 0 1 2 3 n--> 4 5 6 Q2. Plot nu(n) CODE: >> t=0:1:6; >> n=input('enter value of n '); enter value of n 4 >> y=ones(1,7); >> y1=y*n; >> plot(t,y1);x

Uploaded by

Anuja Bhakuni
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
623 views34 pages

Digital Signal Processing Matlab Programs

Q1. Plot u(n) CODE: >> clear all; >> t=0:1:6; >> y=ones(1,7); >> plot(t,y);xlabel('n-->');ylabel('Amplitude');title('plot of u(n)') FIGURE: plot of u(n) 2 1.8 1.6 1.4 1.2 1 0.8 0.6 0.4 0.2 0 Amplitude 0 1 2 3 n--> 4 5 6 Q2. Plot nu(n) CODE: >> t=0:1:6; >> n=input('enter value of n '); enter value of n 4 >> y=ones(1,7); >> y1=y*n; >> plot(t,y1);x

Uploaded by

Anuja Bhakuni
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

2012

MATLAB
DIGITAL SIGNAL PROCESSING LAB
BY: CHHAVI TRIVEDI , ROLL NO : 07213502709, [Link](CSE), VI SEMESTER

IGIT
KASHMERE GATE
1/1/2012

Q1. Plot u(n)


CODE:
>> clear all;
>> t=[Link];
>> y=ones(1,7);
>> plot(t,y);xlabel('n-->');ylabel('Amplitude');title('plot of u(n)')
FIGURE:
plot of u(n)
2
1.8
1.6

Amplitude

1.4
1.2
1
0.8
0.6
0.4
0.2
0

3
n-->

Q2. Plot nu(n)


CODE:
>> t=[Link];
>> n=input('enter value of n ');
enter value of n 4
>> y=ones(1,7);
>> y1=y*n;
>> plot(t,y1);xlabel('n-->');ylabel('Amplitude');title('plot of 4u(n)')
FIGURE:
plot of 4u(n)
5
4.8
4.6

Amplitude

4.4
4.2
4
3.8
3.6
3.4
3.2
3

3
n-->

Q3. Plot u(-n)


CODE:
t=-[Link];
>> y=ones(1,7);
>> plot(t,y);xlabel('n-->');ylabel('Amplitude');title('plot of u(-n)')
FIGURE:
plot of u(-n)
2
1.8
1.6

Amplitude

1.4
1.2
1
0.8
0.6
0.4
0.2
0
-6

-5

-4

-3
n-->

-2

-1

Q4. Plot u(n)-u(N-1)


CODE:
>> n=input('enter the N value ');
enter the N value 1
>> t=[Link]n-1;
>> y1=ones(1,n);
>> stem(t,y1);ylabel('Amplitude');xlabel('n-->');title('plot of u(n)-u(N-1)')
FIGURE:
plot of u(n)-u(N-1)
1
0.9
0.8

Amplitude

0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-1

-0.8

-0.6

-0.4

-0.2

0
n-->

0.2

0.4

0.6

0.8

Q5. Plot the linear convolution of sequences of length 4.


CODE:
x=input('enter the 1st sequence ');
enter the 1st sequence
>> x=input('enter the 1st sequence ');
enter the 1st sequence [2 1 0 0.5]
>> h=input('enter the 2nd sequence ');
enter the 2nd sequence [2 2 1 1]
y=conv(x,h);
m=length(y)-1;
>> n=[Link]m;
subplot(2,1,1);stem(x);xlabel('n-->');ylabel('Amplitude');subplot(2,1,2);stem(h);xlabel('n->');ylabel('Amplitude')
stem(n,y);xlabel('n-->');ylabel('Amplitude')
FIGURE:
x(n)

Amplitude

2
1.5
1
0.5
0

1.5

2.5
n-h(n)

3.5

1.5

2.5
n-->

3.5

Amplitude

2
1.5
1
0.5
0

Amplitude

3
n-->

Q6. Plot the circular convolution of Q5.


CODE:
y=cconv(x,h);
>> m=length(y)-1;
>> n=[Link]m;
>> disp('output sequence is =');disp(y)
output sequence is =
4.0000 6.0000 4.0000 4.0000 2.0000 0.5000 0.5000
stem(n,y);xlabel('n-->');ylabel('Amplitude')
FIGURE:
6

Amplitude

3
n-->

Q7. Plot the cross-correlation of sequences of length 4.


CODE:
x=input('enter the 1st sequence ');
enter the 1st sequence [1 2 3 4]
>> h=input('enter the 2nd sequence ');
enter the 2nd sequence [4 3 2 1]
>> y=xcorr(x,h);
subplot(3,1,3);stem(fliplr(y));xlabel('n-->');ylabel('Amplitude');title('crosscorrelation');subplot(3,1,1);stem(x);xlabel('n->');ylabel('Amplitude');title('x(n)');;subplot(3,1,2);stem(h);xlabel('n-->');ylabel('Amplitude');title('y(n)')
FIGURE:

Amplitude

Amplitude

Amplitude

x(n)
4
2
0

1.5

2.5
n-->
y(n)

3.5

1.5

2.5
n-->
cross-correlation

3.5

4
n-->

4
2
0

40
20
0

Q8. Plot zeros and poles of u(n).


CODE:
num=[1 0];
>> den=[1 -1];
>> zplane(num,den)
>> [z p k]=tf2zp(num,den);
>> m=abs(p);
>> disp('zeros are at ');disp(z);
zeros are at
0
>> disp('poles are at ');disp(p);
poles are at
1
disp('Gain constant ');disp(k);
Gain constant
1
>> disp('Radius of poles ' );disp(m);
Radius of poles
1
FIGURE:

1
0.8
0.6

Imaginary Part

0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-1

-0.5

0
Real Part

0.5

Q9. Compute the z-transform of anu(n) and plot its zeros and poles.
CODE:
syms a n;
>> g=a^n;
>> ztrans(g)
ans =
-z/(a - z)
a=input('Enter the value of a ');
Enter the value of a 2
>> num=[1 0];
>> den=[1 -a];
>> zplane(num,den)
>> [z p k]=tf2zp(num,den);
>> m=abs(p);
>> disp('zeros are at ');disp(z);
zeros are at
0
>> disp('poles are at ');disp(p);
poles are at
2
disp('Gain constant ');disp(k);
Gain constant
1
>> disp('Radius of poles ' );disp(m);
Radius of poles
2

FIGURE:

Imaginary Part

0.5

-0.5

-1

-1

-0.5

0.5
Real Part

1.5

Q10. Plot u(n)coswon z-transform with its poles and zeros.


CODE:
w=input('enter the value of Wo ');
enter the value of Wo 1
num=[0 1 -cos(w)];
den=[1 -2*cos(w) 1];
>> zplane(num,den)
>> [z p k]=tf2zp(num,den);
>> m=abs(p);
>> disp('zeros are at ');disp(z);
zeros are at
0.5403
>> disp('poles are at ');disp(p);
poles are at
0.5403 + 0.8415i
0.5403 - 0.8415i
disp('Gain constant ');disp(k);
Gain constant
1
>> disp('Radius of poles ' );disp(m);
Radius of poles
1
1
FIGURE:
1
0.8
0.6

Imaginary Part

0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-1

-0.5

0
Real Part

0.5

Q11. Find and plot the dft of [1,2,3,4] and hence by the idft of the result obtained.
CODE(I):
x=input('enter the sequene ')
enter the sequene [1 2 3 4]
x=
1

>> n=input('enter the length of sequence ');


enter the length of sequence 4
y=fft(x,n);
stem(y);ylabel('Imaginary Axis --> ');xlabel('Real axis -->')
y=
10.0000

-2.0000 + 2.0000i -2.0000

-2.0000 - 2.0000i

FIGURE(I):

2
1.5

Imaginary Axis -->

1
0.5
0
-0.5
-1
-1.5
-2

1.5

2.5
Real axis -->

3.5

CODE(II):
x=input('enter the sequene ')
enter the sequene [1 2 3 4]
x=
1

>> y=fft(x,4);
>> n=input('enter the length of idft ');
enter the length of idft 4
>> k=ifft(y,n)
k=
1 2 3 4
stem(k);xlabel('n --> ');ylabel('Amplitude --> ')
FIGURE(II):
4
3.5

Amplitude -->

3
2.5
2
1.5
1
0.5
0

1.5

2.5
n -->

3.5

Q12. Perform dft of sequence of length 7 using overlap add method.


CODE:
b=input('Enter input signal (of length 7) ');
Enter input signal (of length 7) [1 2 3 4 5 6 7]
>> x=input('Enter impulse signal ');
Enter impulse signal [1 2 5 6 2 3 9]
>> n=input('Enter block size ');
Enter block size 3
>> y=fftfilt(x,b,n)
y=
1

4 12 26 42 61 89

>> stem(y)
FIGURE:
90
80
70
60
50
40
30
20
10
0

Q13. Design low pass butterworth filter.


CODE:
format long;
>> rp=input('Enter the passband ripple ');
Enter the passband ripple 0.5
>> rs=input('Enter the stopband ripple ');
Enter the stopband ripple 50
>> wp=input('Enter the passband freq ');
Enter the passband freq 1200
>> ws=input('Enter the stopband freq ');
Enter the stopband freq 2400
>> fs=input('Enter the sampling freq ');
Enter the sampling freq 10000
>> w1=2*wp/fs;w2=2*ws/fs;
>> [n wn]=buttord(w1,w2,rp,rs);
>> [b a]=butter(n,wn);
>> w=0:0.01:pi;
>> [h om]=freqz(b,a,w);
>> m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(om/pi,m);ylabel('Gain in db --> ');xlabel('(a) Normalised frequency ->');subplot(2,1,2);plot(om/pi,an);xlabel(' (b) Normalised frequency-->');ylabel('Phase in radians -->')

FIGURE:

-100
-200
-300
-400

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a) Normalised frequency -->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised frequency-->

0.8

0.9

4
Phase in radians -->

Gain in db -->

2
0
-2
-4

Q14. Design low pass chebychev filter.


CODE:
format long;
>> rp=input('Enter the passband ripple ');
Enter the passband ripple 0.2
>> rs=input('Enter the stopband ripple ');
Enter the stopband ripple
>> rs=input('Enter the stopband ripple ');
Enter the stopband ripple 45
>> wp=input('Enter the passband freq ');
Enter the passband freq 1300
>> ws=input('Enter the stopband freq ');
Enter the stopband freq 1500
>> fs=input('Enter the sampling freq ');
Enter the sampling freq 10000
>> w1=2*wp/fs;w2=2*ws/fs;
[n wn]=cheb1ord(w1,w2,rp,rs);
>> [b a]=cheby1(n,rp,wn);
>> w=0:0.01:pi;
>> [h om]=freqz(b,a,w);
>> m=20*log10(abs(h));
an=angle(h);
>> subplot(2,1,1);plot(om/pi,m);ylabel('Gain in db --> ');xlabel('(a) Normalised frequency ->');subplot(2,1,2);plot(om/pi,an);xlabel(' (b) Normalised frequency-->');ylabel('Phase in radians -->')

FIGURE:

0
-200
-400
-600

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a) Normalised frequency -->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised frequency-->

0.8

0.9

4
Phase in radians -->

Gain in db -->

200

2
0
-2
-4

Q15. Design low pass filter using rectangular window.


CODE:
rp=input('Enter the passband ripple ');
Enter the passband ripple 0.05
>> rs=input('Enter the stopband ripple ');
Enter the stopband ripple 0.04
>> wp=input('Enter the passband freq ');
Enter the passband freq 1500
>> ws=input('Enter the stopband freq ');
Enter the stopband freq 2000
>> fs=input('Enter the sampling freq ');
Enter the sampling freq 9000
>> w1=2*wp/fs;w2=2*ws/fs;
>> num=-20*log10(sqrt(rp*rs))-13;
>> den=14.6*(ws-wp)/fs;
>> n=ceil(num/den);
n1=n+1;
>> if(rem(n,2)~=0)
n1=n;
n=n-1;
end
>> y=boxcar(n1);
>> b=fir1(n,w1,y);
>> [h o]=freqz(b,1,256);
>> m=20*log10(abs(h));
subplot;plot(o/pi,m);ylabel('Gain in db --> ');xlabel('Normalised frequency -->')

FIGURE:
10
0
-10

Gain in db -->

-20
-30
-40
-50
-60
-70
-80

0.1

0.2

0.3

0.4
0.5
0.6
0.7
Normalised frequency -->

0.8

0.9

Q16. Design low pass filter using hamming window.


CODE:
clear all;
>> rp=input('Enter the passband ripple ');
Enter the passband ripple 0.02
>> rs=input('Enter the stopband ripple ');
Enter the stopband ripple 0.01
>> fp=input('Enter the passband freq ');
Enter the passband freq 1200
>> fs=input('Enter the stopband freq ');
Enter the stopband freq 1700
>> f=input('Enter the sampling freq ');
Enter the sampling freq 9000
>> wp=2*fp/f;ws=2*fs/f;
>> 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;
>> y=hamming(n1);
>> b=fir1(n,wp,y);
>> [h o]=freqz(b,1,256);
>> m=20*log10(abs(h));
>> subplot;plot(o/pi,m);ylabel('Gain in db --> ');xlabel('Normalised frequency -->')

FIGURE:
20

Gain in db -->

-20

-40

-60

-80

-100

-120

0.1

0.2

0.3

0.4
0.5
0.6
0.7
Normalised frequency -->

0.8

0.9

Q17. Design low pass filter using hanning window.


CODE:
>> clear all;
>> rp=input('Enter the passband ripple ');
Enter the passband ripple 0.02
>> rs=input('Enter the stopband ripple ');
Enter the stopband ripple 0.01
>> fp=input('Enter the passband freq ');
Enter the passband freq 1200
>> fs=input('Enter the stopband freq ');
Enter the stopband freq 1700
>> f=input('Enter the sampling freq ');
Enter the sampling freq 9000
>> wp=2*fp/f;ws=2*fs/f;
>> 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;
y=hanning(n1);
>> b=fir1(n,wp,y);
>> [h o]=freqz(b,1,256);
>> m=20*log10(abs(h));
>> subplot;plot(o/pi,m);ylabel('Gain in db --> ');xlabel('Normalised frequency -->')

FIGURE:
20
0

Gain in db -->

-20
-40
-60
-80
-100
-120
-140

0.1

0.2

0.3

0.4
0.5
0.6
0.7
Normalised frequency -->

0.8

0.9

[Link] low pass filter using Bartlett window.


CODE:
>> clear all;
>> rp=input('Enter the passband ripple ');
Enter the passband ripple 0.02
>> rs=input('Enter the stopband ripple ');
Enter the stopband ripple 0.01
>> fp=input('Enter the passband freq ');
Enter the passband freq 1200
>> fs=input('Enter the stopband freq ');
Enter the stopband freq 1700
>> f=input('Enter the sampling freq ');
Enter the sampling freq 9000
>> wp=2*fp/f;ws=2*fs/f;
>> 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;
y=bartlett(n1);
>> b=fir1(n,wp,y);
>> [h o]=freqz(b,1,256);
>> m=20*log10(abs(h));
>> subplot;plot(o/pi,m);ylabel('Gain in db --> ');xlabel('Normalised frequency -->')

FIGURE:
0
-5
-10

Gain in db -->

-15
-20
-25
-30
-35
-40
-45

0.1

0.2

0.3

0.4
0.5
0.6
0.7
Normalised frequency -->

0.8

0.9

Q19. Design an all pass filter and locate its poles and zeros.
CODE:
c=[1.5 0.7];
>> hd=[Link](c)
hd =
FilterStructure: 'Minimum-Multiplier Allpass'
AllpassCoefficients: [1.5 0.7]
PersistentMemory: false
>> zplane(hd)
FIGURES:
Pole/Zero Plot
1
0.8

Imaginary Part

0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-2

-1.5

-1

-0.5

Real Part

0.5

1.5

Magnitude Response (dB)


0.5
0.4
0.3

Magnitude (dB)

0.2
0.1
0
-0.1
-0.2
-0.3
-0.4
-0.5

0.1

0.2

0.3

0.4
0.5
0.6
Normalized Frequency ( rad/sample)

0.7

0.8

0.9

0.7

0.8

0.9

0.7

0.8

0.9

Phase Response
0

Phase (radians)

-1

-2

-3

-4

-5

-6
0

0.1

0.2

0.3

0.4
0.5
0.6
Normalized Frequency ( rad/sample)
Group Delay

12

Group delay (in samples)

10

0
0

0.1

0.2

0.3

0.4
0.5
0.6
Normalized Frequency ( rad/sample)

Phase Delay
2
1.8

Phase Delay (samples)

1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0

0.1

0.2

0.3

0.4
0.5
0.6
Normalized Frequency ( rad/sample)

0.7

0.8

0.9

Impulse Response
0.7
0.6
0.5

Amplitude

0.4
0.3
0.2
0.1
0
-0.1
-0.2
-0.3
0

10

15

20

25
30
Samples

35

40

45

50

Q20. Design a minimum phase filter and locate its poles and zeros.
CODE:
Fs = 96000;
Fn = Fs/2;
f = [0 17000 20000 28000 31000 Fn]/Fn;
a = [0 0 1 1 0 0];
w = [5 1 5];
b = firgr(44, f, a, w, 'minphase');
legend(fvtool(b),'Min Phase')
FIGURES:
Pole/Zero Plot
1
0.8
0.6

Imaginary Part

0.4
0.2

44

0
-0.2
-0.4
-0.6
-0.8

Min Phase: Zero

-1

Min Phase: Pole


-2

-1.5

-1

-0.5

0
Real Part

0.5

1.5

Group Delay

20
Min Phase
Group delay (in samples)

18
16
14
12
10
8
6
0

0.1

0.2

0.3

0.4
0.5
0.6
Normalized Frequency ( rad/sample)

0.7

0.8

0.9

Magnitude Response (dB)


0
Min Phase

-5

Magnitude (dB)

-10
-15
-20
-25
-30
-35
-40
0

0.1

0.2

0.3

0.4
0.5
0.6
Normalized Frequency ( rad/sample)

0.7

0.8

0.9

Impulse Response
0.2
0.15

0.05
0
-0.05
-0.1
-0.15

Min Phase

-0.2
0

10

15

20
Samples

25

30

35

40

Phase Response
6

Min Phase

Phase (radians)

Amplitude

0.1

-2

-4

-6
0

0.1

0.2

0.3

0.4
0.5
0.6
Normalized Frequency ( rad/sample)

0.7

0.8

0.9

Phase Delay
21
20.5

Phase Delay (samples)

20

Min Phase

19.5
19
18.5
18
17.5
17
16.5
0

0.1

0.2

0.3

0.4
0.5
0.6
Normalized Frequency ( rad/sample)

0.7

0.8

0.9

You might also like