dsp file main_2

Upload: kapil-singh

Post on 05-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Dsp File Main_2

    1/44

    Each student must have their own laboratory notebook. All pre-lab exercises and laboratory

    reports are to be entered into your notebook.

    Your notebook must be clearly labelled on the cover with the following information:

    Digital Signal Processing

  • 7/31/2019 Dsp File Main_2

    2/44

    There are 2 hours allocated to a laboratory session in Digital Signal Processing. It is a

    necessary part of the course at which attendance is compulsory.

    Here are some guidelines to help you perform the experiments and to submit the reports:

    1. Read all instructions carefully and carry them all out.2. Ask a demonstrator if you are unsure of anything.3. Record actual results (comment on them if they are unexpected!)4. Write up full and suitable conclusions for each experiment.5. If you have any doubt about the safety of any procedure, contact the demonstrator

    beforehand.

    6. about what you are doing!

  • 7/31/2019 Dsp File Main_2

    3/44

    1. To write a program to represent basic signal (unit step, unit impulse, ramp,exponential, Sine sequence, Cosine sequence).

    2. To perform the discrete convolution of sequences.3. To write a program for discrete correlation.

    (a)Program for computing cross correlation of the sequences

    (b) Program for computing auto correlation.

    4. To design(a) Program for low pass IIR butterworth filter(b) Program for high pass IIR butterworth filter

    5. To Design a digital filter using rectangular window method.(a)Program for low pass filter(b)Program for high pass filter(c) Program for band pass filter(d)Program for band stop filter

    6. To design(a) Program for low pass filter(b) Program for high pass filter(c) Program for band pass filter(d) Program for band stop filter

  • 7/31/2019 Dsp File Main_2

    4/44

    To write a program to represent basic signal (unit step, unit impulse, ramp,

    exponential, Sine sequence, Cosine sequence)

    MATLAB SOFTWARE, PC

    clc;

    clearall;

    close all;

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

    t=0:1:n-1;

    y1=ones(1,n);subplot(2,2,1);

    stem(t,y1); ylabel('Amplitude -->');

    xlabel('(a) n -->');

    title('unit step signal');

    t=-2:1:2;

    y=[zeros(1,2),ones(1,1),zeros(1,2)];

    subplot(2,2,2);

    stem(t,y);

    ylabel('Amplitude -->');

    xlabel('(b)n -->');

    title('unit impulse signal');

    n1=input('eneter the length of ramp sequence');

    t=0:n1;

    subplot(2,2,3);

    stem(t,t);ylabel('Amplitude -->');

  • 7/31/2019 Dsp File Main_2

    5/44

    xlabel('(c)n -->');

    title('Unit ramp sequence');

    n2=input('enter the length exponential sequence');

    t=0:n2;

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

    y2=exp(a*t);

    subplot(2,2,4);

    stem(t,y2);

    ylabel('Amplitude -->');

    xlabel('(d)n -->');

    title('exponential sequence');

    enter the N value7

    eneter the length of ramp sequence7enter the length exponential sequence7

    enter the value of a1

  • 7/31/2019 Dsp File Main_2

    6/44

    Program to represent basic signal (unit step, unit impulse, ramp, exponential, Sine

    sequence, Cosine sequence).

  • 7/31/2019 Dsp File Main_2

    7/44

    n=0:.01:pi;

    y=sin(2*pi*n);

    subplot(2,2,1);

    stem(n,y);

    xlabel('(a)n');

    ylabel('Amplitude -->');

    title('Sine Sequence');

    n=0:.01:pi;

    y1=cos(2*pi*n);

    subplot(2,2,2);

    stem(n,y1);

    xlabel('(b)n');

    ylabel('Amplitude -->');title('cosine sequence');

  • 7/31/2019 Dsp File Main_2

    8/44

    Program to represent basic signal (Sine sequence, Cosine sequence)

  • 7/31/2019 Dsp File Main_2

    9/44

    To perform the discrete convolution of sequences.

    MATLAB SOFTWARE, PC

    clc;

    clear all;

    close all;

    x=input('enter the first sequence');

    h=input('enter the second sequence');

    y=conv(x,h)

    figure;

    subplot(3,1,1);

    stem(x);

    ylabel('amplitude');

    xlabel ('(a)n');

    subplot(3,1,2);

    stem(h);

    ylabel('amplitude');

    xlabel ('(b)n');

    subplot(3,1,3);

    stem(y);

    ylabel('amplitude');

    xlabel ('(c)n');

    disp (The resultant signal is ); y

    title('convolution');

  • 7/31/2019 Dsp File Main_2

    10/44

    enter the first sequence [1 2]

    enter the second sequence [1 2 4]

    The resultant signal is

    y = 1 4 8 8

  • 7/31/2019 Dsp File Main_2

    11/44

    Program for convolution of two discrete sequences

  • 7/31/2019 Dsp File Main_2

    12/44

    To write a program for discrete correlation.

    MATLAB SOFTWARE, PC

    clc; clear all; close all;

    x=input('enter the first sequence');

    h=input('enter the second sequence');

    y=xcorr(x,h);

    figure;

    subplot(3,1,1);

    stem(x);

    xlabel('(a) n -->');

    ylabel('Amplitude -->');

    title('First Input sequence');

    subplot(3,1,2);

    stem(h);

    xlabel('(b) n -->'); ylabel('Amplitude -->');title('Second Input sequence');

    subplot(3,1,3);

    stem(y);

    xlabel('(c) n -->');

    ylabel('Amplitude -->');

    title('Output sequence');

    disp ('The resultant signal is'); y

  • 7/31/2019 Dsp File Main_2

    13/44

    enter the first sequence[1 2 3 4]

    enter the second sequence[4 3 2 1]

    The resultant signal is

    y =

    Columns 1 through 3

    1.0000 4.0000 10.0000

    Columns 4 through 6

    20.0000 25.0000 24.0000

    Column 7

    16.0000

  • 7/31/2019 Dsp File Main_2

    14/44

    ( A)Program for computing cross correlation of the sequences

  • 7/31/2019 Dsp File Main_2

    15/44

    (

    clc; clear all; close all;

    x=input('enter the sequence');

    y=xcorr(x,x);

    figure;

    subplot(2,1,1);

    stem(x);

    xlabel('(a) n -->');

    ylabel('Amplitude -->');

    subplot(2,1,2);

    stem(y);

    xlabel('(b) n -->');

    ylabel('Amplitude -->');

    disp ('The resultant signal is'); y

    enter the sequence[1 2 3 4]

    The resultant signal is

    y =

    Columns 1 through 3

    4.0000 11.0000 20.0000

    Columns 4 through 6

    30.0000 20.0000 11.0000

    Column 7

    4.0000

  • 7/31/2019 Dsp File Main_2

    16/44

    (B)Program for computing auto correlation function

  • 7/31/2019 Dsp File Main_2

    17/44

    To design IIR butterworth filter.

    MATLAB SOFTWARE, PC

    clc;

    close all;

    clear all;

    rs=input('enter the stop band ripple ');

    rp=input('enter the pass band ripple ');

    ws=input('enter the stop band frequency ');

    wp=input('enter the pass band freequency ');

    fs=input('enter the sampling frequency ');

    w1=2*wp/fs;

    w2=2*ws/fs;

    %to find the cutoff frequency and order of the filter

    [n,wn]=buttord(w1,w2,rp,rs);

    %system function of the filter

    [b,a]=butter(n,wn)

    w=0:0.01:pi;

    [h,om]=freqz(b,a,w);

    m=abs(h);

    an=angle(h);

  • 7/31/2019 Dsp File Main_2

    18/44

    subplot(2,1,1);

    plot(om/pi,20*log10(m));grid

    ylabel('gain in db ');

    xlabel('(a) normalised frequency ');

    subplot(2,1,2);

    plot(om/pi,an);grid

    ylabel('phase in radian');

    xlabel('(b) normalised frequency ');

    enter the stop band ripple 30

    enter the pass band ripple 0.40

    enter the stop band frequency 800

    enter the pass band freequency 400

    enter the sampling frequency 2000

    b =0.1518 0.6073 0.9109 0.6073 0.1518

    a = 1.0000 0.6418 0.6165 0.1449 0.025

  • 7/31/2019 Dsp File Main_2

    19/44

    (A) Program for low pass IIR butterworth filter

  • 7/31/2019 Dsp File Main_2

    20/44

    clc;

    close all;

    clear all;

    rs=input('enter the stop band ripple ');

    rp=input('enter the pass band ripple ');

    ws=input('enter the stop band frequency ');

    wp=input('enter the pass band freequency ');

    fs=input('enter the sampling frequency ');

    w1=2*wp/fs;

    w2=2*ws/fs;

    %to find the cutoff frequency and order of the filter

    [n,wn]=buttord(w1,w2,rp,rs);

    %system function of the filter

    [b,a]=butter(n,wn,'high')

    w=0:0.01:pi;

    [h,om]=freqz(b,a,w);

    m=abs(h);

    an=angle(h);

    subplot(2,1,1);

    plot(om/pi,20*log10(m));grid

    ylabel('gain in db ');

    xlabel('(a) normalised frequency ');

    subplot(2,1,2);

    plot(om/pi,an);grid

    ylabel('phase in radian');

    xlabel('(b) normalised frequency ');

  • 7/31/2019 Dsp File Main_2

    21/44

    enter the stop band ripple 30

    enter the pass band ripple .4

    enter the stop band frequency 800

    enter the pass band freequency 400

    enter the sampling frequency 2000

    b =0.0535 -0.2139 0.3209 -0.2139 0.0535

    a =1.0000 0.6418 0.6165 0.1449 0.0259

  • 7/31/2019 Dsp File Main_2

    22/44

    (B) Program for high pass IIR butterworth filter

  • 7/31/2019 Dsp File Main_2

    23/44

    To Design a digital filter using rectangular window method.

    MATLAB SOFTWARE, PC

    clc;

    close all;

    clear all;

    wc=0.5*pi;%cut off frequency

    N=25;

    alpha=(N-1)/2;eps=0.001;%to avoid indeterminate form

    n=0:1:N-1;

    hd=sin(wc*(n-alpha+eps))./(pi*(n-alpha+eps));

    wr=boxcar(N);%rectangular window sequence

    hn=hd.*wr';%filter coefficients

    w=0:0.01:pi;

    h=freqz(hn,1,w);

    %plot magnitude response

    m=abs(h);

    subplot(2,1,1);

    plot(w/pi,m);grid

    xlabel('normalized frequency');

    ylabel('magnitude');

    title('magnitude response of low pass filter using rectangular window');

    an=angle(h);

  • 7/31/2019 Dsp File Main_2

    24/44

    subplot(2,1,2);

    plot(w/pi,an);grid

    xlabel('normalized frequency');

    ylabel('phase shift');

    title('phase response of low pass filter using rectangular window');

  • 7/31/2019 Dsp File Main_2

    25/44

    (A) Program for low pass filter

  • 7/31/2019 Dsp File Main_2

    26/44

    clc;

    close all;

    clear all;

    wc=0.5*pi;%cut off frequency

    N=25;

    alpha=(N-1)/2;

    eps=0.001;%to avoid indeterminate form

    n=0:1:N-1;

    hd=sin(pi*(n-alpha+eps))-sin(wc*(n-alpha+eps))./(pi*(n-alpha+eps));

    wr=boxcar(N);%rectangular window sequence

    hn=hd.*wr';%filter coefficients

    w=0:0.01:pi;

    h=freqz(hn,1,w);

    %plot magnitude response

    m=abs(h);

    subplot(2,1,1);

    plot(w/pi,m);grid

    xlabel('normalized frequency');

    ylabel('magnitude');

    title('magnitude response of high pass filter using rectangular window');

    %plot phase response

    an=angle(h);

    subplot(2,1,2);

    plot(w/pi,an);grid

    xlabel('normalized frequency');

    ylabel('phase shift');

    title('phase response of high pass filter using rectangular window');

  • 7/31/2019 Dsp File Main_2

    27/44

    (B) program for high pass filter

  • 7/31/2019 Dsp File Main_2

    28/44

    clc;

    close all;

    clear all;

    wc1=0.25*pi;

    wc2=0.75*pi;

    N=25;

    alpha=(N-1)/2;

    eps=0.001;%to avoid indeterminate form

    n=0:1:N-1;

    hd=(sin(wc2*(n-alpha+eps))-sin(wc1*(n-alpha+eps)))./(pi*(n-alpha+eps));

    wr=boxcar(N);%rectangular window sequence

    hn=hd.*wr';%filter coefficients

    w=0:0.01:pi;

    h=freqz(hn,1,w);

    %plot magnitude response

    m=abs(h);

    subplot(2,1,1);

    plot(w/pi,m);grid

    xlabel('normalized frequency');

    ylabel('magnitude');

    title('magnitude response of band pass filter using rectangular window');

    %plot phase response

    an=angle(h);

    subplot(2,1,2);

    plot(w/pi,an);grid

    xlabel('normalized frequency');

    ylabel('phase shift');

    title('phase response of band pass filter using rectangular window');

  • 7/31/2019 Dsp File Main_2

    29/44

    (C) Program for band pass filter

  • 7/31/2019 Dsp File Main_2

    30/44

    clc;

    close all;

    clear all;

    wc1=0.25*pi;

    wc2=0.75*pi;

    N=25;

    alpha=(N-1)/2;

    eps=0.001;%to avoid indeterminate form

    n=0:1:N-1;

    hd=(sin(pi*(n-alpha+eps))+sin(wc1*(n-alpha+eps))-sin(wc2*(n-alpha+eps)))./(pi*(n-

    alpha+eps));

    wr=boxcar(N);%rectangular window sequence

    hn=hd.*wr';%filter coefficients

    w=0:0.01:pi;

    h=freqz(hn,1,w);

    %plot magnitude response

    m=abs(h);

    subplot(2,1,1);

    plot(w/pi,m);grid

    xlabel('normalized frequency');

    ylabel('magnitude');title('magnitude response of band stop filter using rectangular window');

    %plot phase response

    an=angle(h);

    subplot(2,1,2);

    plot(w/pi,an);grid

    xlabel('normalized frequency');

    ylabel('phase shift');

  • 7/31/2019 Dsp File Main_2

    31/44

    title('phase response of band stop filter using rectangular window');

  • 7/31/2019 Dsp File Main_2

    32/44

    (D) Program for band stop filter

  • 7/31/2019 Dsp File Main_2

    33/44

    To design the program for analog butterworth filter.

    MATLAB SOFTWARE, PC

    clc;

    clear all;

    close all;

    rs=input('enter the stopband ripple=');

    rp=input('enter the passband ripple=');

    ws=input('enter the stopband frequency=');

    wp=input('enter the passband frequency=');

    fs=input('enter the sampling frequency=');

    w1=2*wp/fs;

    w2=2*ws/fs;

    %find the order and cut off frequency

    [n,wn]=buttord(w1,w2,rs,rp,'s');

    %calculating system function

    [b,a]=butter(n,wn,'s');

    %plot frequency response

    w=0:0.01:pi;

    [h,w]=freqs(b,a,w);

    m=20*log10(abs(h));

    subplot(2,1,1);plot(w/pi,m);grid

  • 7/31/2019 Dsp File Main_2

    34/44

    ylabel('Gain in dB--');

    xlabel('Normalised Frequency--');

    title('Magnitude Response of low pass filter');

    an=angle(h);

    subplot(2,1,2);

    plot(w/pi,an); grid

    ylabel('Phase in radians--');

    xlabel('Normalised Frequency--');

    title('Phase Response of low pass filter');

    enter the stopband ripple=.25

    enter the passband ripple=60

    enter the stopband frequency=3000

    enter the passband frequency=2000

    enter the sampling frequency=9000

    n =21 wn =0.4798

  • 7/31/2019 Dsp File Main_2

    35/44

    (A)Program for low pass filter

  • 7/31/2019 Dsp File Main_2

    36/44

    clc;

    clear all;

    close all;

    rs=input('enter the stopband ripple=');

    rp=input('enter the passband ripple=');

    ws=input('enter the stopband frequency=');

    wp=input('enter the passband frequency=');

    fs=input('enter the sampling frequency=');

    w1=2*wp/fs;

    w2=2*ws/fs;

    %find the order and cut off frequency

    [n,wn]=buttord(w1,w2,rs,rp,'s');

    %calculating system function

    [b,a]=butter(n,wn,'high','s');

    %plot frequency response

    w=0:0.01:pi;

    [h,w]=freqs(b,a,w);

    m=20*log10(abs(h));

    subplot(2,1,1);plot(w/pi,m);grid

    ylabel('Gain in dB--');

    xlabel('Normalised Frequency--');

    title('Magnitude Response of high pass filter');

    an=angle(h);

    subplot(2,1,2);

    plot(w/pi,an); grid

    ylabel('Phase in radians--');

  • 7/31/2019 Dsp File Main_2

    37/44

    xlabel('Normalised Frequency--');

    title('Phase Response of high pass filter');

    enter the stopband ripple=.25

    enter the passband ripple=60

    enter the stopband frequency=3000

    enter the passband frequency=2000

    enter the sampling frequency=9000

    n = 21

    wn =0.4798

  • 7/31/2019 Dsp File Main_2

    38/44

    (B) Program for high pass filter

  • 7/31/2019 Dsp File Main_2

    39/44

    clc;

    clear all;

    close all;

    rs=input('enter the stopband ripple=');

    rp=input('enter the passband ripple=');

    ws=input('enter the stopband frequency=');

    wp=input('enter the passband frequency=');

    fs=input('enter the sampling frequency=');

    w1=2*wp/fs;

    w2=2*ws/fs;

    %find the order and cut off frequency

    [n]=buttord(w1,w2,rs,rp,'s')

    wn=[w1,w2]

    %calculating system function

    [b,a]=butter(n,wn,'bandpass','s');

    %plot frequency response

    w=0:0.01:pi;

    [h,w]=freqs(b,a,w);

    m=20*log10(abs(h));subplot(2,1,1);

    plot(w/pi,m);grid

    ylabel('Gain in dB--');

    xlabel('Normalised Frequency--');

    title('Magnitude Response of band pass filter');

    an=angle(h);

    subplot(2,1,2);

    plot(w/pi,an); grid

  • 7/31/2019 Dsp File Main_2

    40/44

    ylabel('Phase in radians--');

    xlabel('Normalised Frequency--');

    title('Phase Response of band pass filter');

    -

    enter the stopband ripple=50

    enter the passband ripple=60

    enter the stopband frequency=70

    enter the passband frequency=80

    enter the sampling frequency=90

    n = 9

    wn = 1.7778 1.5556

  • 7/31/2019 Dsp File Main_2

    41/44

    (C) Program for band pass filter

  • 7/31/2019 Dsp File Main_2

    42/44

    clc;

    clear all;

    close all;

    rs=input('enter the stopband ripple=');

    rp=input('enter the passband ripple=');

    ws=input('enter the stopband frequency=');

    wp=input('enter the passband frequency=');

    fs=input('enter the sampling frequency=');

    w1=2*wp/fs;

    w2=2*ws/fs;

    %find the order and cut off frequency

    [n]=buttord(w1,w2,rs,rp,'s')

    wn=[w1,w2]

    %calculating system function

    [b,a]=butter(n,wn,'stop','s');

    %plot frequency response

    w=0:0.01:pi;

    [h,w]=freqs(b,a,w);

    m=20*log10(abs(h));subplot(2,1,1);

    plot(w/pi,m);grid

    ylabel('Gain in dB--')

    xlabel('Normalised Frequency--')

    title('Magnitude Response of band stop filter');

    an=angle(h);

    subplot(2,1,2);

    plot(w/pi,an); grid

  • 7/31/2019 Dsp File Main_2

    43/44

    ylabel('Phase in radians--')

    xlabel('Normalised Frequency--')

    title('Phase Response of band stop filter');

    enter the stopband ripple=50

    enter the passband ripple=60

    enter the stopband frequency=70

    enter the passband frequency=80

    enter the sampling frequency=90

    n = 9

    wn = 1.7778 1.5556

  • 7/31/2019 Dsp File Main_2

    44/44

    (D) Program for band stop filter