digital signal processing matlab programs

34
MATLAB DIGITAL SIGNAL PROCESSING LAB BY: CHHAVI TRIVEDI , ROLL NO : 07213502709, B.TECH(CSE), VI SEMESTER 2012 IGIT KASHMERE GATE 1/1/2012

Upload: niravsinghdabhi

Post on 03-Oct-2015

58 views

Category:

Documents


3 download

DESCRIPTION

gdhagd

TRANSCRIPT

  • MATLAB DIGITAL SIGNAL PROCESSING LAB BY: CHHAVI TRIVEDI , ROLL NO : 07213502709, B.TECH(CSE), VI SEMESTER

    2012

    IGIT KASHMERE GATE

    1/1/2012

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

    0 1 2 3 4 5 60

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    n-->

    Am

    plit

    ude

    plot of u(n)

  • 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);xlabel('n-->');ylabel('Amplitude');title('plot of 4u(n)')

    FIGURE:

    0 1 2 3 4 5 63

    3.2

    3.4

    3.6

    3.8

    4

    4.2

    4.4

    4.6

    4.8

    5

    n-->

    Am

    plit

    ude

    plot of 4u(n)

  • Q3. Plot u(-n)

    CODE:

    t=-6:1:0;

    >> y=ones(1,7);

    >> plot(t,y);xlabel('n-->');ylabel('Amplitude');title('plot of u(-n)')

    FIGURE:

    -6 -5 -4 -3 -2 -1 00

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    n-->

    Am

    plitu

    de

    plot of u(-n)

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

    CODE:

    >> n=input('enter the N value ');

    enter the N value 1

    >> t=0:1:n-1;

    >> y1=ones(1,n);

    >> stem(t,y1);ylabel('Amplitude');xlabel('n-->');title('plot of u(n)-u(N-1)')

    FIGURE:

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

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

    0.8

    0.9

    1

    Am

    plit

    ude

    n-->

    plot of u(n)-u(N-1)

  • 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=0:1: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:

    1 1.5 2 2.5 3 3.5 40

    0.5

    1

    1.5

    2

    n--

    Am

    plit

    ude

    x(n)

    1 1.5 2 2.5 3 3.5 40

    0.5

    1

    1.5

    2

    n-->

    Am

    plit

    ude

    h(n)

  • 0 1 2 3 4 5 60

    1

    2

    3

    4

    5

    6

    n-->

    Am

    plit

    ude

  • Q6. Plot the circular convolution of Q5.

    CODE:

    y=cconv(x,h);

    >> m=length(y)-1;

    >> n=0:1: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:

    0 1 2 3 4 5 60

    1

    2

    3

    4

    5

    6

    n-->

    Am

    plit

    ude

  • 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('cross-

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

    1 2 3 4 5 6 70

    20

    40

    n-->

    Am

    plit

    ude

    cross-correlation

    1 1.5 2 2.5 3 3.5 40

    2

    4

    n-->

    Am

    plit

    ude

    x(n)

    1 1.5 2 2.5 3 3.5 40

    2

    4

    n-->

    Am

    plit

    ude

    y(n)

  • 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.5 0 0.5 1

    -1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    Real Part

    Imagin

    ary

    Part

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

    -1 -0.5 0 0.5 1 1.5 2

    -1

    -0.5

    0

    0.5

    1

    Real Part

    Imagin

    ary

    Part

  • 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.5 0 0.5 1

    -1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    Real Part

    Imagin

    ary

    Part

  • 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 2 3 4

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

    1 1.5 2 2.5 3 3.5 4-2

    -1.5

    -1

    -0.5

    0

    0.5

    1

    1.5

    2

    Imagin

    ary

    Axis

    -->

    Real axis -->

  • CODE(II):

    x=input('enter the sequene ')

    enter the sequene [1 2 3 4]

    x =

    1 2 3 4

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

    1 1.5 2 2.5 3 3.5 40

    0.5

    1

    1.5

    2

    2.5

    3

    3.5

    4

    n -->

    Am

    plit

    ude -

    ->

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

    1 2 3 4 5 6 70

    10

    20

    30

    40

    50

    60

    70

    80

    90

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

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

    -300

    -200

    -100

    0

    Gain

    in d

    b -

    ->

    (a) Normalised frequency -->

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

    -2

    0

    2

    4

    (b) Normalised frequency-->

    Phase in r

    adia

    ns -

    ->

  • 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 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-600

    -400

    -200

    0

    200

    Gain

    in d

    b -

    ->

    (a) Normalised frequency -->

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

    -2

    0

    2

    4

    (b) Normalised frequency-->

    Phase in r

    adia

    ns -

    ->

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

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

    -70

    -60

    -50

    -40

    -30

    -20

    -10

    0

    10

    Gain

    in d

    b -

    ->

    Normalised frequency -->

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

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

    -100

    -80

    -60

    -40

    -20

    0

    20

    Gain

    in d

    b -

    ->

    Normalised frequency -->

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

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

    -120

    -100

    -80

    -60

    -40

    -20

    0

    20

    Gain

    in d

    b -

    ->

    Normalised frequency -->

  • Q18.Design 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 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-45

    -40

    -35

    -30

    -25

    -20

    -15

    -10

    -5

    0

    Gain

    in d

    b -

    ->

    Normalised frequency -->

  • Q19. Design an all pass filter and locate its poles and zeros.

    CODE:

    c=[1.5 0.7];

    >> hd=dfilt.allpass(c)

    hd =

    FilterStructure: 'Minimum-Multiplier Allpass'

    AllpassCoefficients: [1.5 0.7]

    PersistentMemory: false

    >> zplane(hd)

    FIGURES:

    -2 -1.5 -1 -0.5 0 0.5 1 1.5 2

    -1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    Real Part

    Imagin

    ary

    Part

    Pole/Zero Plot

  • 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9-0.5

    -0.4

    -0.3

    -0.2

    -0.1

    0

    0.1

    0.2

    0.3

    0.4

    0.5

    Normalized Frequency ( rad/sample)

    Magnitu

    de (

    dB

    )

    Magnitude Response (dB)

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

    -6

    -5

    -4

    -3

    -2

    -1

    0

    Normalized Frequency ( rad/sample)

    Phase (

    radia

    ns)

    Phase Response

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

    0

    2

    4

    6

    8

    10

    12

    Normalized Frequency ( rad/sample)

    Gro

    up d

    ela

    y (

    in s

    am

    ple

    s)

    Group Delay

  • 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    Normalized Frequency ( rad/sample)

    Phase D

    ela

    y (

    sam

    ple

    s)

    Phase Delay

    0 5 10 15 20 25 30 35 40 45 50

    -0.3

    -0.2

    -0.1

    0

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

    Samples

    Impulse Response

    Am

    plit

    ude

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

    -2 -1.5 -1 -0.5 0 0.5 1 1.5 2

    -1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    Real Part

    Imagin

    ary

    Part

    44

    Pole/Zero Plot

    Min Phase: Zero

    Min Phase: Pole

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

    6

    8

    10

    12

    14

    16

    18

    20

    Normalized Frequency ( rad/sample)

    Gro

    up d

    ela

    y (

    in s

    am

    ple

    s)

    Group Delay

    Min Phase

  • 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

    -40

    -35

    -30

    -25

    -20

    -15

    -10

    -5

    0

    Normalized Frequency ( rad/sample)

    Magnitu

    de (

    dB

    )

    Magnitude Response (dB)

    Min Phase

    0 5 10 15 20 25 30 35 40

    -0.2

    -0.15

    -0.1

    -0.05

    0

    0.05

    0.1

    0.15

    0.2

    Samples

    Impulse Response

    Am

    plit

    ude

    Min Phase

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

    -6

    -4

    -2

    0

    2

    4

    6

    Normalized Frequency ( rad/sample)

    Phase (

    radia

    ns)

    Phase Response

    Min Phase

  • 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

    16.5

    17

    17.5

    18

    18.5

    19

    19.5

    20

    20.5

    21

    Normalized Frequency ( rad/sample)

    Phase D

    ela

    y (

    sam

    ple

    s)

    Phase Delay

    Min Phase