Basic signal processing Matlab codes

download Basic signal processing Matlab codes

of 13

Transcript of Basic signal processing Matlab codes

  • 7/30/2019 Basic signal processing Matlab codes

    1/13

    Digital Signal Processor Architecture Lab

    Nitish S. Prabhu

    1MS10EC071

    Table of contents

    Analysis of system form it i/o equations .................................................................................................. 1

    Impulse response ...................................................................................................................................... 2

    Sampling theorem ..................................................................................................................................... 3

    FIR filter - window method ....................................................................................................................... 5

    IIR Low pass filters .................................................................................................................................... 9

    IIR Filter - using bilenear transformation .............................................................................................. 112

    Analysis of system form its i/o equations

    Compute the magnitude and phase plots form the coefficients of the differential equation.

    clc

    clear all

    close all

    b=[1,2,3]; % Feedforward coefficients

    a=1; % Feedback coefficients

    n=100;

    % Plot the Pole-Zero diagram (Fig 1.a)

    subplot(4,1,1);

    zplane(b,a);

    % Plot the frequency response (Fig 1.b)

    subplot(4,1,2);

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

    plot(h);

    % Plot the phase response (Fig 1.c)

    subplot(4,1,3);

    phase=angle(h);plot(phase);

  • 7/30/2019 Basic signal processing Matlab codes

    2/13

    % Plot the magnitude response (Fig 1.d)

    subplot(4,1,4);

    mag=abs(h);

    plot(mag);

    Fig.1 :Plots for a given i/o differential equation

    Impulse response

    To determine the impulse response of a given system and thus model the behavior of the system

    clc

    clear all

    close all

    n=-20:100; % Discrete time variable

    b=1; % Coefficients

    a=[1 -0.1 -0.9];

    x=(n==0); % Impulse function

  • 7/30/2019 Basic signal processing Matlab codes

    3/13

    stem(n,x);

    title('Filter Output');

    figure

    y=filter(b,a,x);

    stem(n,y)

    title('Unit Impulse function');

    Sampling theorem

    The setup below explains the requirement of fast sampling rates greater than that of Nyquest rates. The

    behavior of a system to sampling rates equal to and greater than that of Nyquest frequency is tested.

    clc

    close all

    clear all

    f1= 500;

    f2= 2000;

    % Define the parameters of the signal

    N=256;

    n=0:N-1;

    fs=2*max(f1,f2);

    % Case1: Nyquist rate sampling with 'a' and 'b' as input signals

    a=sin(2*pi*(f1)*n);

  • 7/30/2019 Basic signal processing Matlab codes

    4/13

    b=sin(2*pi*(f2)*n);

    x=sin(2*pi*(f1/fs)*n)+sin(2*pi*(f2/fs)*n);

    nt=n./fs;

    % Plot of the sine wave to be sampled

    subplot(2,2,1)

    plot(nt,x)

    xlabel('Time(s)')

    ylabel('Amplitude')

    title('Sampling at nyquist rate')

    grid;

    X=fft(x);

    mag=abs(X);

    fhz=n.*(fs/N);

    % The frequecy domain representation

    subplot(2,2,3)

    plot(fhz(1:N/2),mag(1:N/2));

    xlabel('Frequency')

    ylabel('Amplitude')

    title('Sinewave in freq domain')

    grid

    % Case2: Sampling at higher rates than Nyquest rate

    fh=2*2*max(f1,f2);

    z=sin(2*pi*(f1/fh)*n)+sin(2*pi*(f2/fh)*n);

    nh=n./fh;

    % Plot of the signal to be sampled

    subplot(2,2,2)

    plot(nh,z);

    xlabel('time(seconds)');

    ylabel('amplitude');

    title('sampling at nyquist rate');grid;

    p=fft(x);

    mag1=abs(X);

    fhz1=n.*(fh/N);

  • 7/30/2019 Basic signal processing Matlab codes

    5/13

    % The frequency domain representation

    subplot(2,2,4)

    plot(fhz1(1:N/2),mag(1:N/2));

    xlabel('frequency');

    ylabel('amplitude');

    title('sinewave in freq domain');

    grid;

    FIR filter - window method

    Creating an FIR filter using window method based on the filter specifications like pass band and stop

    bang attenuation.

    clear all

    close all

    clc

    % Define filter parameters

    FS = 8000; % Max frequency

  • 7/30/2019 Basic signal processing Matlab codes

    6/13

    FN = FS/2; % Nyquest frequency

    fp = 1500; % Passband frequency

    tw = 500; % Transition Width

    fs = fp + tw;

    fc = (fp+fs)/2/FN;tw = (fs - fp)/FS;

    A = 5; % Stop band Attenuation

    for A = 15:15:60

    if A21 && A44 && A

  • 7/30/2019 Basic signal processing Matlab codes

    7/13

    ylabel('Mag resp(dB)');

    title(strcat(str,' Window'));

    end

    Rectangular window :FIR coefficients

    Columns 1 through 7

    0.0126 0.0188 -0.0066 -0.0246 -0.0026 0.0281 0.0153

    Columns 8 through 14

    -0.0281 -0.0318 0.0224 0.0537 -0.0067 -0.0878 -0.0358

    Columns 15 through 21

    0.1815 0.3916 0.3916 0.1815 -0.0358 -0.0878 -0.0067

    Columns 22 through 28

    0.0537 0.0224 -0.0318 -0.0281 0.0153 0.0281 -0.0026

    Columns 29 through 32

    -0.0246 -0.0066 0.0188 0.0126

    Fig.1 :Frequency response for rectangular window

  • 7/30/2019 Basic signal processing Matlab codes

    8/13

    Fig.2 : Frequency response for Hanning window

    Fig.3 : Frequency response for Hamming winndow

    Fig.4 : Frequency response for Blackman window

  • 7/30/2019 Basic signal processing Matlab codes

    9/13

    IIR Low pass filters

    %IIR LPF -- Butterworth characteristic, Impulse invariant transformation

    clc

    clear all

    close all

    %N=2, fs=1.28kHz, fc=150Hz

    %Filter order

    N=2;

    %Sampling frequency

    fs=1280;

    %Cut off frequency

    fc=150;

    wc=2*pi*fc;

    %Analog filter design

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

    disp('Coefficients of analog filter');

    disp(b);

    disp(a);

    [z,p,k]=butter(N,wc,'s');

    disp('zeros, poles & gain of analog filter');

    disp(z);

    disp(p);

    disp(k);

    %Convert to discrete filter

    [bz,az]=impinvar(b,a,fs);

    disp('Coefficients of the discrete filter');

    disp(bz);

    disp(az);

    %Plot magnitude response

    subplot(2,1,1);

    [H,f]=freqz(bz,az,512,fs);

    plot(f,20*log10(abs(H)));

    grid;

    xlabel('Frequency(Hz)');

    ylabel('Magnitude response(dB)');

  • 7/30/2019 Basic signal processing Matlab codes

    10/13

    %plot pole-zero diagram

    subplot(2,1,2);

    zplane(bz,az);

    %Determine poles and zeros

    zz=roots(bz);

    pz=roots(az);

    disp('Zeros and poles of the discrete filter');

    disp(zz);

    disp(pz);

    Coefficients of analog filter

    1.0e+05 *

    0 0 8.8826

    1.0e+05 *

    0.0000 0.0133 8.8826

    Zeros, poles & gain of analog filter

    1.0e+02 *

    -6.6643 + 6.6643i

    -6.6643 - 6.6643i

    8.8826e+05

    Coefficients of the discrete filter

    0 0.3078 0

    1.0000 -1.0308 0.3530

    Zeros and poles of the discrete filter

    0

    0.5154 + 0.2955i

    0.5154 - 0.2955i

  • 7/30/2019 Basic signal processing Matlab codes

    11/13

    IIR Filter - using bilinear transformation

    To Design a simple low pass digital IIR filter with Butterworth characteristics to meet the following

    specifications Cut off frequency=150Hz, sampling frequency 1.28kHz and filter order 2.

    clc

    clear all

    close all

    %IIR LPF -- Butterworth characteristic, Bilinear transformation

    %N=2, fs=1.28kHz, fc=150Hz

    N=2; %Filter order

    fs=1280; %Sampling frequency

    fc=150; %Cut off frequency

    wc=2*pi*fc;

    %Analog filter design -- Normalised Butterworth filter

    [b,a]=butter(N,1,'s');

    [z,p,k]=butter(N,1,'s');

  • 7/30/2019 Basic signal processing Matlab codes

    12/13

    %Convert to discrete filter

    wc=tan(2*pi*(fc/fs)/2); %Prewarping

    %Low pass to low pass transformation

    [b1,a1]=lp2lp(b,a,wc);

    [bz,az]=bilinear(b1,a1,0.5); %2*Fs=1, Fs=0.5

    disp('coefficients of the discrete filter ');

    disp(bz);

    disp(az);

    %Plot magnitude response

    subplot(2,1,1);

    [H,f]=freqz(bz,az,512,fs);

    plot(f,20*log10(abs(H)));

    grid;

    xlabel('Frequency(Hz)');

    ylabel('Magnitude response(dB)');

    %plot pole-zero diagram

    subplot(2,1,2);

    zplane(bz,az);

    %determine poles and zeros

    [z,p,k]=tf2zp(bz,az);

    disp('zeros, poles and gain of the discrete filter');

    disp(z);

    disp(p);

    disp(k);

    coefficients of the discrete filter

    0.0878 0.1756 0.0878

    1.0000 -1.0048 0.3561

    Zeros, poles and gain of the discrete filter

    -1

    -1

    0.5024 + 0.3220i

    0.5024 - 0.3220i

    0.0878

  • 7/30/2019 Basic signal processing Matlab codes

    13/13