Altered DSP Complete Manual Part A

Embed Size (px)

Citation preview

  • 7/29/2019 Altered DSP Complete Manual Part A

    1/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 1

    EXPERIMENT No 1 Verification of Sampling theorem.

    Aim: To verify Sampling theorem for a signal of given frequency.

    Algorithm:

    1.

    Input the desired frequency fd (for which sampling theorem is to be verified).2. Generate an analog signal xt of frequency fd for comparison.3. Generate oversampled, nyquist & under sampled discrete time signals.4. Plot the waveforms and hence prove sampling theorem.

    MATLAB Program:

    clear;

    clc

    tfinal=0.05;

    t=0:0.00005:tfinal;

    fd=input('Enter analog freuency');xt=sin(2*pi*fd*t);

    fs1=1.3*fd; %Under sampling

    n1=0:1/fs1:tfinal;

    xn=sin(2*pi*n1*fd);

    subplot(3,1,1);

    plot(t,xt,'b',n1,xn,'r*-');

    title('undersampling plot');

    fs2=2*fd; %Critical sampling (Nyquist rate)

    n2=0:1/fs2:tfinal;

    xn=sin(2*pi*fd*n2);

    subplot(3,1,2);

    plot(t,xt,'b',n2,xn,'r*-');

    title('Nyquist plot');

    fs3=5*fd; %over sampling

    n3=0:1/fs3:tfinal;

    xn=sin(2*pi*fd*n3);subplot(3,1,3);

    plot(t,xt,'b',n3,xn,'r*-');

    title('Oversampling plot');

    xlabel('time');

    ylabel('amplitude');

    legend('analog','discrete')

    Result:Enter analog frequency 200

    The plots are shown in Fig.1.1

  • 7/29/2019 Altered DSP Complete Manual Part A

    2/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 2

    Fig.1.1 Plots of a sampled sine wave of 200Hz

    Sampling at the Nyquist rate results in samples sin(n) which are identically zero, i.e., we are

    sampling at the zero crossing points and hence the signal component is completely missed. This

    can be avoided by adding a small phase shift to the sinusoid. The above problem is not seen in

    cosine waveforms (except cos(90n)). A simple remedy is to sample the analog signal at a rate

    higher than the Nyquist rate. The Fig1.2 shows the result due to a cosine signal

    (x1=cos(2*pi*fd*n1);

    Fig.1.2 Plots of a sampled cosine wave of 200Hz

    EXPERIMENT NO 2: Impulse response of a given system

  • 7/29/2019 Altered DSP Complete Manual Part A

    3/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 3

    Aim: To find the impulse response h(n) of the given LTI system whose response y(n)

    to an input x(n) is given.

    Algorithm for Problem1:1. Input the two sequences as x and y.2. Use deconv to get impulse response h.3. Plot the sequences.4. Verify using conv(x,h) to get y back.

    MATLAB Programs:

    clear;

    clc;

    y= input('Enter the output sequence y(n) of the system=');

    x=input('Enter the input sequence of the system=');

    h=deconv(y,x);

    disp('the impulse response of the system is=');disp(h);

    N=length(h);

    n=0:1:N-1;

    stem(n,h);

    xlabel('Time index n');

    ylabel('Amplitude');

    title('impulse response of a system')

    Result:Enter the output sequence y(n) of the system=[1 4 10 16 17 12]

    Enter the input sequence of the system=[1 2 3 4]

    The impulse response of the system is= 1 2 3

    Fig. 2: Impulse Response (Program 1)

  • 7/29/2019 Altered DSP Complete Manual Part A

    4/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 4

    EXPERIMENT NO 3: Linear convolution of two given sequences.

    Aim: To obtain convolution of two finite duration sequences.

    Algorithm:

    1.

    Input the two sequences as x1, x22. Convolve both to get output y.3. Plot the sequences.

    MATLAB Program:

    Program-1 (both sequences are right sided)clear;

    clc;

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

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

    y=conv(x,h);

    disp('linear convolution of x & h is y =');

    disp(y);

    subplot (2,1,1);

    n1 = length(x)+length(h)-1;

    n = 0:n1-1;

    stem(n,y);

    xlabel('time index n');

    ylabel('amplitude ');

    title('convolution output');

    subplot(2,2,3);

    n2 = length(x);

    n = 0 : n2-1;

    stem(n,x);

    xlabel('time index n');

    ylabel('amplitude ');

    title('plot of x');

    subplot(2,2,4);

    n3 = length(h);

    n = 0 : n3-1;stem(n,h);

    xlabel('time index n');

    ylabel('amplitude ');

    title('plot of h');

    Fig.3.1 For right sided sequences

    ResultEnter the first sequence = [1 2 3 4]

    Enter the second sequence = [1 2 3]

    linear convolution of x & h is y =

    1 4 10 16 17 12

  • 7/29/2019 Altered DSP Complete Manual Part A

    5/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 5

    Program-2 (Two sided sequences)

    clear;

    clc;

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

    n1=input('Enter the first sequence time range = ');

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

    n2=input('Enter the second sequence time range = ');

    ybegin=n1(1)+n2(1);

    yend=n1(length(x))+n2(length(h));

    n =[ybegin:yend];

    y=conv(x,h);

    disp('linear convolution of x & h is y =');

    disp(y);

    subplot (2,1,1);

    stem(n,y);

    xlabel('time index n');

    ylabel('amplitude ');

    title('convolution output');

    subplot(2,2,3);

    stem(n1,x);

    xlabel('time index n');

    ylabel('amplitude ');title('plot of x');

    subplot(2,2,4);

    stem(n2,h);

    xlabel('time index n');

    ylabel('amplitude ');

    title('plot of h');

    Fig.3.2 For two sided sequences (With time information)

    ResultEnter the first sequence = [1 2 3 2 1 3 4]

    Enter the first sequence time range = -3:3

    Enter the second sequence = [2 -3 4 -1 0 1]

    Enter the second sequence time range = -1:4

    linear convolution of x & h is y =

    2 1 4 2 6 9 3 2 15 -3 3 4

  • 7/29/2019 Altered DSP Complete Manual Part A

    6/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 6

    EXPERIMENT NO 4: Circular convolution of two given sequences.

    Aim: To obtain circular convolution of two finite duration sequences.

    Algorithm:

    1.

    Input the two sequences as x and h.2. Circularly convolve both to get output y (using cconv function).3. Plot the sequences.

    MATLAB Program:

    clc;

    clear;

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

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

    xlength = length(x);

    hlength = length(h);

    N=max(xlength,hlength);

    if(xlength > hlength) %zero padding

    zp = xlength - hlength;

    h = [h, zeros(1,zp)];

    end;

    if(hlength > xlength)

    zp = hlength - xlength;

    x = [x, zeros(1,zp)];

    end;

    y = cconv(x,h,N);

    disp('circular convolution of x & h is y =');

    disp(y);

    n = 0:N-1;

    subplot (2,1,1);stem(n,y);

    xlabel('time index n');

    ylabel('amplitude ');

    title('convolution output');

    subplot(2,2,3);

    xlength = length(x);

    n1 = 0:xlength-1;

    stem(n1,x);

    xlabel('time index n');

    ylabel('amplitude ');title('plot of x');

  • 7/29/2019 Altered DSP Complete Manual Part A

    7/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 7

    subplot(2,2,4);

    hlength = length(h);

    n2 = 0:hlength-1;

    stem(n2,h);

    xlabel('time index n');

    ylabel('amplitude ');

    title('plot of h');

    Result:Run1Enter the first sequence = [1 2 3 4]

    Enter the second sequence = [1 2 3 4]

    circular convolution of x & h is y =

    26 28 26 20

    Fig. 4.1 Circular convolution output(Run-1)

    Run2

    Enter the first sequence = [1 2 3 4]Enter the second sequence = [1 2]

    circular convolution of x & h is y =

    9 4 7 10

    Fig. 4.2 Circular convolution output(Run-2)

  • 7/29/2019 Altered DSP Complete Manual Part A

    8/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 8

    EXPERIMENT NO 5: Autocorrelation of a given sequence and verification of its

    properties.

    Aim: To obtain autocorrelation of the given sequence and verify its properties.

    Algorithm:1. Input the sequence as x.2. Use the xcorr function to get auto correlated output r.3. Plot the sequences.

    MATLAB Programs

    clc;

    clear;

    x = input('Enter the input sequence = ');

    n = length(x);

    [rx lag] = xcorr(x);

    disp('auto correlated output rx = ');

    disp(rx);

    subplot(2,1,1)

    stem(0:n-1,x);title('input sequence');

    xlabel('time');

    ylabel('amplitude');

    subplot(2,1,2)

    stem(lag,rx);

    title('auto correlated output');

    xlabel('lag index');

    ylabel('amplitude');

    disp('Property -1 : ');

    disp('At lag index = 0, the correlated output has maximum value');

    m1 = max(rx);

    rxlen = length(rx);

    m2 = rx(rxlen-n+1);if(m1 == m2)

    disp('property - 1 is verified');

    else

    disp('Property - 1 is not verified');

    end ;

    disp('Property -2 : ');

    disp('correlated output is even function');

    if( rx == fliplr(rx))

    disp('property - 2 is verified');else

  • 7/29/2019 Altered DSP Complete Manual Part A

    9/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 9

    disp('Property - 2 is not verified');

    end ;

    disp('Property -3 : ');

    disp('the maximum value at lag = 0 gives the energy of the input signal');

    e = sum(x.*x);if(m2 == e)

    disp('property - 3 is verified');

    else

    disp('Property - 3 is not verified');

    end ;

    Fig.5: Autocorrelation output

    Result:

    Enter the input sequence = [1 2 3 4]

    auto correlated output rx =

    4.0000 11.0000 20.0000 30.0000 20.0000 11.0000 4.0000

    Property -1 :

    At lag index = 0, the correlated output has maximum value

    property - 1 is verified

    Property -2 :

    correlated output is even function

    property - 2 is verified

    Property -3 :

    the maximum value at lag = 0 gives the energy of the input signal

    property - 3 is verified

  • 7/29/2019 Altered DSP Complete Manual Part A

    10/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 10

    EXPERIMENT NO 6:Cross correlation of a given sequence and verification of its

    properties.

    Aim: To obtain cross correlation of the given sequence and verify its properties.

    Algorithm:1. Input the sequence as x and y.2. Use the xcorr function to get cross correlated output r.3. Plot the sequences.

    MATLAB Programs

    clear;

    clc;

    x = input('Type in the reference sequence = ');

    y = input('Type in the second sequence = ');

    n1 = length(x)-1;

    n2 = length(y)-1;

    [rxy, lag]=xcorr(x,y);

    disp(' Cross correlation output is = ' );

    disp(rxy);

    subplot(3,1,1);

    stem(0:n1,x);title('reference sequence');

    xlabel('time');

    ylabel('amplitude');

    subplot(3,1,2);

    stem(0:n2,y);

    title('reference sequence');

    xlabel('time');

    ylabel('amplitude');

    subplot(3,1,3);stem(lag,rxy);

    title('cross corrleated output');

    xlabel('Lag index');

    ylabel('Amplitude');

    disp(

  • 7/29/2019 Altered DSP Complete Manual Part A

    11/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 11

    Result:

  • 7/29/2019 Altered DSP Complete Manual Part A

    12/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 12

    EXPERIMENT NO 7 Solving a given difference equation

    Aim: To obtain the impulse response/step response of a system described by the

    given difference equation

    Algorithm:1. Input the two sequences as a and b representing the coefficients of y and x.2. If IIR response, then input the length of the response required (say 100, which can be made

    constant).

    3. Compute the output response using the filter command.4. Plot the input sequence & impulse response (and also step response, etc if required).

    Given Problem1)Difference equation y(n) - y(n-1) + 0.9y(n-2) = x(n);

    Calculate impulse response h(n) and also step response at n=0,..,100

    2)Plot the steady state response y[n] to x[n]=cos(0.05n)u(n), given y[n]-0.8y[n-1]=x[n]

    MATLAB Program:

    %To find Impulse ResponseN=input('Length of response required=');

    b=[1]; %x[n] coefficient

    a=[1,-1,0.9]; %y coefficients

    %impulse input

    x=[1,zeros(1,N-1)];

    %time vector for plotting

    n=0:1:N-1;

    %impulse response

    h=filter(b,a,x); *note

    %plot the waveforms

    subplot(2,1,1);

    stem(n,x);

    title('impulse input');

    xlabel('n');

    ylabel('(n)');

    subplot(2,1,2);stem(n,h);

    title('impulse response');

    xlabel('n');

    ylabel('h(n)');

    Result:Length=100

    Plots in Fig. 7.1

    Fig.7.1 Impulse Response

  • 7/29/2019 Altered DSP Complete Manual Part A

    13/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 13

    %To find step responseN=input('Length of response required=');

    b=[1]; %x[n] coefficient

    a=[1,-1,0.9]; %y coefficients

    x=[ones(1,N)]; %step input

    n=0:1:N-1;

    %time vector for plottingy=filter(b,a,x); %step response

    %plot the waveforms

    subplot(2,1,1);

    stem(n,x);

    title('step input');

    xlabel('n');

    ylabel('u(n)');

    subplot(2,1,2);

    stem(n,h);

    title('step response');

    xlabel('n');ylabel('y(n)');

    Result:Length=100

    Plots in Fig. 7.2Fig.7.2 Step Response

    %To find steady state response%y[n]-0.8y[n-1]=x[n]

    N=input('Length of response required=');b=[1]; %x[n] coefficient

    a=[1,-0.8]; %y coefficients

    n=0:1:N-1; %time vector

    x=cos(0.05*pi*n); % sinusoidal input

    y=filter(b,a,x); %steady state response

    Result:Length=100

    Plots in Fig. 7.3

    Fig. 7.3 Steady state Response to a cosine input

  • 7/29/2019 Altered DSP Complete Manual Part A

    14/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 14

    EXPERIMENT NO 8. Computation of N point DFT of a given sequence and to plot

    magnitude and phase spectrum.

    Aim: To compute DFT of the given sequence.

    Algorithm:1. Input the sequence for which DFT is to be computed.2. Input the length of the DFT required (say 4, 8, >length of the sequence).3. Compute the DFT using the fft command.4. Plot the magnitude & phase spectra.

    Matlab Program:

    x=[1,2,3,6]; %x[n] sequence

    N=4 %N points for DFT

    xk=fft(x,N); %computes DFT

    % compute & plot amplitude spectrum

    n=0:1:N-1;

    figure(1);

    stem(n,abs(xk));

    xlabel(' k');

    ylabel('lxkl');

    title('Magnitude spectrum');

    % phase spectrum

    figure(2);

    stem(n,angle(xk));

    xlabel(' k');ylabel('angle(xk)');

    title('phase spectrum');

    figure(3);

    n1=0:1:length(x)-1;

    stem(n1,x);

    xlabel(' n');

    ylabel('x[n]');

    title('original signal');

    x=[2 3 9 -1];

    N=4;

    for k=0:N-1xk(k+1)=0;

    for n=0:N-1

    xk(k+1)=xk(k+1)+x(n+1)*exp(-j*2*pi*n*k/N);

    end

    end

    n=0:1:N-1;

    figure(1);

    stem(n,abs(xk));

    xlabel(' k');

    ylabel('lxkl');

    title('Magnitude spectrum');% phase spectrum

    figure(2);

  • 7/29/2019 Altered DSP Complete Manual Part A

    15/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 15

    stem(n,angle(xk));

    xlabel(' k');

    ylabel('angle(xk)');

    title('phase spectrum');

    figure(3);

    n1=0:1:length(x)-1;

    stem(n1,x);xlabel(' n');

    ylabel('x[n]');

    title('original signal');

    Result:

    Run1: with N =4, plots are as shown in Fig. 8.1

    Run2: with N =8, plots are as shown in Fig. 8.2

    Run 3: with n1=0:7; x=cos(2*pi*n*250/8000); %x[n] sequence of 250Hz with fs=8kHz,

    & N=8 point DFT, Fig. 8.3 is the output, which shows a frequency of 0Hz, i.e., a peak at k=0 in the

    magnitude spectrum.Run 4: with n1=0:7; x=cos(2*pi*n*250/8000); %x[n] sequence of 250Hz with fs=8kHz,

    & N=80 point DFT, Fig. 8.4 is the output, which shows still a frequency of 0Hz, i.e., a peak at k=0

    in the magnitude spectrum, but the spectra are more smooth (=continuous). This is a high density

    spectra.

    Run 5: with n1=0:79; x=cos(2*pi*n*250/8000); %x[n] sequence of 250Hz with fs=8kHz, i.e., the

    length of x[n] has increased(implies more information from the time domain)

    & N=80 point DFT,

    Fig. 8.5 is the output, which shows two frequencies of 200 and 300Hz, i.e., 2 peaks at k=2

    and 3 in the magnitude spectrum for N=80. This is a high resolution spectra.

    Run 6: with n1=0:79; x=cos(2*pi*n*250/8000); %x[n] sequence of 250Hz with fs=8kHz, &

    N=160 point DFT, Fig. 8.6 is the output, which shows one maximum frequency of 250 Hz, i.e., apeak at k=5 in the magnitude spectrum for N=160.

    Fig.8.1 Fig.8.2

  • 7/29/2019 Altered DSP Complete Manual Part A

    16/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 16

  • 7/29/2019 Altered DSP Complete Manual Part A

    17/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 17

    Fig.8.3 Fig.8.5

    Fig.8.4 Fig 8.6

  • 7/29/2019 Altered DSP Complete Manual Part A

    18/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 18

    EXPERIMENT NO 9 Linear convolution of two given sequences using DFT and IDFT

    Aim: To perform linear convolution of two given sequences x1 & x2 using DFT & IDFT

    And also

    EXPERIMENT NO 10 Circular convolution of two given sequences using DFT and

    IDFTAim: To perform circular convolution of two given sequences x1 & x2 using DFT &

    IDFT

    Algorithm:1. Input the two sequences as x1, x22. Compute N = xlength + hlength -1

    (for circular convolution, N=max(xlength + hlength))

    3. Obtain N-point DFTs (X1, X2) of both the sequences.4. Multiply both the DFTs (Y=X1X2).5. Perform IDFT on Y to get y[n] (the linearly convolved sequence) in time domain.6. Verify using the conv command.7. Plot the sequences.

    MATLAB Program:

    %Linear Convolution Program using DFTx1=[1 2 3 4];

    x2= [1 2 3 4];

    N=length(x1)+length(x2)-1;

    %obtain DFTs

    X1= fft(x1,N);X2= fft(x2,N);

    %Perform vector multiplication

    y= X1.*X2;

    %ifft to get y[n]

    yn= ifft(y,N);

    disp('linear convolution of x1 &x2 is yn=');

    disp(yn);

    %verify

    disp('output using conv');

    yv=conv(x1,x2);

    disp(yv);%plot

    stem(yn);

    title('linear convolution output y(n)'); Fig. 9.1

    Result:linear convolution of x1 &x2 is yn=

    1.0000 4.0000 10.0000 20.0000 25.0000 24.0000 16.0000

    output using conv

    1 4 10 20 25 24 16

    The output plot is shown in Fig. 9.1

  • 7/29/2019 Altered DSP Complete Manual Part A

    19/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 19

    %Circular Convolution Program using DFTx1=[1 2 3 4];

    x2= [1 2 3 4];

    N=max(length(x1),length(x2));

    %obtain DFTs

    X1= fft(x1,N);X2= fft(x2,N);

    %Perform vector multiplication

    y= X1.*X2;

    %ifft to get y[n]

    yn= ifft(y,N);

    disp('circular convolution of x1 &x2 is yn=');

    disp(yn);

    %plot

    stem(yn);title('Circular convolution output y(n)');

    Result:circular convolution of x1 &x2 is yn=

    26 28 26 20

    The output plot is shown in Fig. 9.2

    Fig. 9.2

  • 7/29/2019 Altered DSP Complete Manual Part A

    20/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 20

    EXPERIMENT NO 11. Design and implementation of FIR filter to meet given

    specifications

    Aim: To design and implement a FIR filter for given specifications.

    DESIGNING AN FIR FILTER (using window method):

    Method I: Given the order N, cutoff frequency fc, sampling frequency fs and the window. Step 1: Compute the digital cut-off frequency Wc (in the range - < Wc < , with

    corresponding to fs/2) for fc and fs in Hz. For example let fc=400Hz, fs=8000Hz

    Wc = 2** fc / fs = 2* * 400/8000 = 0.1* radians

    For MATLAB the Normalized cut-off frequency is in the range 0 and 1, where 1

    corresponds to fs/2 (i.e.,fmax)). Hence to use the MATLAB commands

    wc = fc / (fs/2) = 400/(8000/2) = 0.1

    Note: if the cut off frequency is in radians then the normalized frequency is computed as

    wc = Wc /

    Step 2: Compute the Impulse Response h(n) of the required FIR filter using the givenWindow type and the response type (lowpass, bandpass, etc). For example given arectangular window, order N=20, and a high pass response, the coefficients (i.e., h[n]

    samples) of the filter are computed using the MATLAB inbuilt command fir1 as

    h =fir1(N, wc , 'high', boxcar(N+1));

    Note: In theory we would have calculated h[n]=hd[n]w[n], where hd[n] is the desired

    impulse response (low pass/ high pass,etc given by the sinc function) and w[n] is the

    window coefficients. We can also plot the window shape as stem(boxcar(N)).

    Plot the frequency response of the designed filter h(n) using the freqz function and observe the

    type of response (lowpass / highpass /bandpass).

    Method 2:Given the pass band (wp in radians) and Stop band edge (ws in radians) frequencies, Pass band

    ripple Rp and stopband attenuation As.

    Step 1: Select the window depending on the stopband attenuation required. Generally ifAs>40 dB, choose Hamming window. (Refer table )

    Step 2: Compute the order N based on the edge frequencies asTransition bandwidth = tb=ws-wp;

    N=ceil (6.6*pi/tb);

    Step 3: Compute the digital cut-off frequency Wc asWc=(wp+ws)/2

    Now compute the normalized frequency in the range 0 to 1 for MATLAB as

    wc=Wc/pi;

    Note: In step 2 if frequencies are in Hz, then obtain radian frequencies (for computation of tb and

    N) as wp=2*pi*fp/fs, ws=2*pi*fstop/fs, where fp, fstop and fs are the passband, stop band and

    sampling frequencies in Hz

    Step 4: Compute the Impulse Response h(n) of the required FIR filter using N, selectedwindow, type of response(low/high,etc) using fir1 as in step 2 of method 1.

    IMPLEMENTATION OF THE FIR FILTER1. Once the coefficients of the FIR filter h[n] are obtained, the next step is to simulate an

    input sequence x[n], say input of 100, 200 & 400 Hz (with sampling frequency of fs), each

    of 20/30 points. Choose the frequencies such that they are >, < and = to fc.2. Convolve input sequence x[n] with Impulse Response, i.e., x (n)*h (n) to obtain the output

    of the filter y[n]. We can also use the filter command.

  • 7/29/2019 Altered DSP Complete Manual Part A

    21/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 21

    3. Infer the working of the filter (low pass/ high pass, etc).MATLAB IMPLEMENTATION

    FIR1 Function

    B = FIR1(N,Wn) designs an N'th order lowpass FIR digital filter and returns the filter

    coefficients in length N+1 vector B. The cut-off frequency Wn must be between 0 < Wn < 1.0,

    with 1.0 corresponding to half the sample rate. The filter B is real and has linear phase, i.e., even

    symmetric coefficients obeying B(k) = B(N+2-k), k = 1,2,...,N+1.If Wn is a two-element vector, Wn = [W1 W2], FIR1 returns an order N bandpass filter with

    passband W1 < W < W2. B = FIR1(N,Wn,'high') designs a highpass filter. B = FIR1(N,Wn,'stop')

    is a bandstop filter if Wn = [W1 W2]. If Wn is a multi-element vector, Wn = [W1 W2 W3

    W4 W5 ... WN], FIR1 returns an order N multiband filter with bands

    0 < W < W1, W1 < W < W2, ..., WN < W < 1.

    FREQZ Digital filter frequency response. [H,W] = FREQZ(B,A,N) returns the N-point complex

    frequency response vector H and the N-point frequency vector W in radians/sample of the filter

    whose numerator and denominator coefficients are in vectors B and A. The frequency response

    is evaluated at N points equally spaced around the upper half of the unit circle. If N isn't specified,

    it defaults to 512.

    For FIR filter enter A=1 and B = h[n] coefficients. Appropriately choose N as 128, 256, etc

    Table: Commonly used window function characteristics

    Window Transition Width Min. Stop band Matlab

    Name Approximate Exact values Attenuation Command

    RectangularM

    4

    M

    8.121db B = FIR1(N,Wn,boxcar)

    BartlettM

    8

    M

    1.625db B = FIR1(N,Wn,bartlett)

    Hanning M

    8

    M

    2.6

    44db B = FIR1(N,Wn,hanning)

    HammingM

    8

    M

    6.653db B= FIR1(N,Wn,hamming)

    BlackmanM

    12

    M

    1174db B = FIR1(N,Wn,blackman)

    Matlab Program

    %Design and implementation of FIR filter Method 1

    %generate filter coefficients for the given %order & cutoff Say N=33, fc=150Hz,

    %fs=1000 Hz, Hamming window

    h=fir1(33, 150/(1000/2),hamming(34));

    %generate simulated input of 50, 300 & 200 Hz, each of 30 points

    n=1:30;

    f1=50;f2=300;f3=200;fs=1000;

    x=[];

    x1=sin(2*pi*n*f1/fs);x2=sin(2*pi*n*f2/fs);

    x3=sin(2*pi*n*f3/fs);

  • 7/29/2019 Altered DSP Complete Manual Part A

    22/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 22

    x=[x1 x2 x3];

    subplot(2,1,1);

    stem(x);

    title('input');

    %generate o/p

    %y=conv(h,x);

    y=filter(h,1,x);subplot(2,1,2);

    stem(y);

    title('output');

    Result:Plots are in Fig.10.3 & 10.4

    Notice that freqs below 150 Hz are passed & above 150 are cutoff

    %Method 2:The following program gives only the design of the FIR filter- for implementation continue with

    the next program (after h[n])

    %input data to be given: Passband & Stopband frequency

    % Data given: Passband ripple & stopband attenuation As. If As>40 dB, Choose hamming

    clear

    wpa=input('Enter passband edge frequency in Hz');

    wsa= input('Enter stopband edge frequency in Hz');

    ws1= input('Enter sampling frequency in Hz');

    %Calculate transmission BW,Transition band tb,order of the filter

    wpd=2*pi*wpa/ws1;

    wsd=2*pi*wsa/ws1;

    tb=wsd-wpd;

    N=ceil(6.6*pi/tb)

    wc=(wsd+wpd)/2;

    %compute the normalized cut off frequency

    wc=wc/pi;

    %calculate & plot the window

    hw=hamming(N+1);

    stem(hw);

    title('Fir filter window sequence- hamming window');%find h(n) using FIR

    h=fir1(N,wc,hamming(N+1));

    %plot the frequency response

    figure(2);

    [m,w]=freqz(h,1,128);

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

    plot(ws1*w/(2*pi),mag);

    title('Fir filter frequency response');

    grid;

    Result:Enter passband edge frequency in Hz100

    Enter stopband edge frequency in Hz200

  • 7/29/2019 Altered DSP Complete Manual Part A

    23/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 23

    Enter sampling frequency in Hz1000

    N = 33

    Plots are as in Fig.10.1 and 10.2

    Notice the maximum stopband attenuation of 53 dB from plot 10.2

    Fig.10.1 Fig.10.2

    Fig.10.3(using filter command) Fig.10.4(using conv command)

    Fig.10.5 Freqs f1=150;f2=300;f3=170;fs=1000;

  • 7/29/2019 Altered DSP Complete Manual Part A

    24/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 24

    EXPERIMENT NO 12. Design and implementation of IIR filter to meet given

    specifications

    Aim: To design and implement an IIR filter for given specifications.

    Method I:

    Given the order N, cutoff frequency fc, sampling frequency fs and the IIR filter type (butterworth,cheby1, cheby2).

    Step 1: Compute the digital cut-off frequency Wc (in the range - < Wc < , with corresponding to fs/2) for fc and fs in Hz. For example let fc=400Hz, fs=8000Hz

    Wc = 2** fc / fs = 2* * 400/8000 = 0.1* radians

    For MATLAB the Normalized cut-off frequency is in the range 0 and 1, where 1 corresponds

    to fs/2 (i.e.,fmax)). Hence to use the MATLAB commands

    wc = fc / (fs/2) = 400/(8000/2) = 0.1

    Note: if the cut off frequency is in radians then the normalized frequency is computed as wc =

    Wc /

    Step 2: Compute the Impulse Response [b,a] coefficients of the required IIR filter and theresponse type (lowpass, bandpass, etc) using the appropriate butter, cheby1, cheby2 command.For example given a butterworth filter, order N=2, and a high pass response, the coefficients

    [b,a] of the filter are computed using the MATLAB inbuilt command butter as [b,a]

    =butter(N, wc , 'high');

    Method 2:Given the pass band (Wp in radians) and Stop band edge (Ws in radians) frequencies, Pass

    band ripple Rp and stopband attenuation As.

    Step 1: Since the frequencies are in radians divide by to obtain normalized frequencies toget wp=Wp/pi and ws=Ws/pi

    If the frequencies are in Hz (note: in this case the sampling frequency should be given),

    then obtain normalized frequencies as wp=fp/(fs/2), ws=fstop/(fs/2), where fp, fstop and fsare the passband, stop band and sampling frequencies in Hz

    Step 2: Compute the order and cut off frequency as[N, wc] = BUTTORD(wp, ws, Rp, Rs)

    Step 3: Compute the Impulse Response [b,a] coefficients of the required IIR filter and theresponse type as [b,a] =butter(N, wc , 'high');

    IMPLEMENTATION OF THE IIR FILTER1. Once the coefficients of the IIR filter [b,a] are obtained, the next step is to simulate an

    input sequence x[n], say input of 100, 200 & 400 Hz (with sampling frequency of fs), each

    of 20/30 points. Choose the frequencies such that they are >, < and = to fc.

    2. Filter the input sequence x[n] with Impulse Response, to obtain the output of the filter y[n]using the filter command.

    3. Infer the working of the filter (low pass/ high pass, etc).MATLAB IMPLEMENTATIONBUTTORD Butterworth filter order selection.

    [N, Wn] = BUTTORD(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital

    Butterworth filter that loses no more than Rp dB in the passband and has at least Rs dB of

    attenuation in the stopband. Wp and Ws are the passband and stopband edge frequencies,

    normalized from 0 to 1 (where 1 corresponds to pi radians/sample). For example,

    Lowpass: Wp = .1, Ws = .2 Highpass: Wp = .2, Ws = .1

    Bandpass: Wp = [.2 .7], Ws = [.1 .8] Bandstop: Wp = [.1 .8], Ws = [.2 .7]BUTTORD also returns Wn, the Butterworth natural frequency (or, the "3 dB frequency") to use

    with BUTTER to achieve the specifications.

  • 7/29/2019 Altered DSP Complete Manual Part A

    25/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    Dept. of TCE, D.S.C.E, Bangalore Page No: 25

    [N, Wn] = BUTTORD(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in which

    case Wp and Ws are in radians/second.

    When Rp is chosen as 3 dB, the Wn in BUTTER is equal to Wp in BUTTORD.

    BUTTER Butterworth digital and analog filter design.

    [B,A] = BUTTER(N,Wn) designs an Nth order lowpass digital Butterworth filter and returns the

    filter coefficients in length N+1 vectors B (numerator) and A (denominator). The coefficients arelisted in descending powers of z. The cutoff frequency Wn must be 0.0 < Wn < 1.0, with 1.0

    corresponding to half the sample rate. If Wn is a two-element vector, Wn = [W1 W2], BUTTER

    returns an order 2N bandpass filter with passband W1 < W < W2. [B,A] =

    BUTTER(N,Wn,'high') designs a highpass filter. [B,A] = BUTTER(N,Wn,'stop') is a bandstop

    filter if Wn = [W1 W2].

    BUTTER(N,Wn,'s'), BUTTER(N,Wn,'high','s') and BUTTER(N,Wn,'stop','s') design analog

    Butterworth filters. In this case, Wn is in [rad/s] and it can be greater than 1.0.

    Matlab Program (Design & implementation)

    %generate filter coefficients for the given %order & cutoff Say N=2, fc=150Hz, %fs=1000 Hz,butterworth filter

    [b,a]=butter(2, 150/(1000/2));

    %generate simulated input of 100, 300 & 170 Hz, each of 30 points

    n=1:30;

    f1=100;f2=300;f3=170;fs=1000;

    x=[];

    x1=sin(2*pi*n*f1/fs);

    x2=sin(2*pi*n*f2/fs);

    x3=sin(2*pi*n*f3/fs);x=[x1 x2 x3];

    subplot(2,1,1);

    stem(x);

    title('input');

    %generate o/p

    y=filter(b,a,x);

    subplot(2,1,2);

    stem(y);

    title('output');

    Result:Plot is in Fig. 11.1, which shows that 100 Hz is passed, while 300 is cutoff and 170 has slight

    attenuation.

    Note: If fp,fstp,fs, rp,As are given then use[N,wc]=buttord(2*fp/fs,2*fstp/fs,rp,As)

    [b,a]=butter(N,wc);

    If wp & ws are in radians

    [N,wc]=buttord(wp/pi,ws/pi,rp,As)

    [b,a]=butter(N,wc);

    If wc is in radians & N is given

    [b,a]=butter(N,wc/pi);For a bandpass output

  • 7/29/2019 Altered DSP Complete Manual Part A

    26/26

    Digital Signal Processing Lab Manual -- 06ECL57 Part A

    wc=[150/(1000/2) 250/(1000/2)];

    [b,a]=butter(4, wc);

    Plot is in Fig.11.2 where only 170 is passed, while 100 & 300 are cutoff.

    Fig.11.1 Low pass IIR filter(butterworth) output

    Fig.11.2 IIR output for bandpass (150-250 Hz)