matlab fair record

Upload: fawaz-saeed

Post on 14-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 MATLAB Fair Record

    1/46

    EC 707 DIGITAL SIGNAL PROCESSING LABORATORY

    RECORD OF EXPERIMENTS

    Submitted by

    fawaz

    In partial fulfillment for the award of the degree of

    BACHELOR OF TECHNOLOGY

    IN

    ELECTRONICS AND COMMUNICATION ENGINEERING

    MODEL ENGINEERING COLLEGE

    COCHIN UNIVERSITY OF SCIENCE & TECHNOLOGY

    COCHIN 682 021

    NOVEMBER 2011

    Contents

    Familiarisation of MATLAB ..................................................................................................... 3

    Generation of Test Signals ......................................................................................................... 9

    Convolution of Two Signals .................................................................................................... 16

  • 7/31/2019 MATLAB Fair Record

    2/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 2

    Circular Convolution of Two Signals ...................................................................................... 18

    Cross Correlation of Two Signals ............................................................................................ 22

    Block Convolution of Two Signals.......................................................................................... 24

    Discrete Fourier Transform of a Signal and its Properties ...................................................... 27

    Inverse Discrete Fourier Transform of a Signal ...................................................................... 32

    Decimation in Time Fast Fourier Transform of a Signal ......................................................... 34

    Decimation in Frequency Fast Fourier Transform of a Signal ................................................ 37

    Finite Impulse Response Filters ............................................................................................... 40

    Infinite Impulse Response Filters ............................................................................................ 43

  • 7/31/2019 MATLAB Fair Record

    3/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 3

    Experiment No: 1

    Date: 8 July 2011

    Familiarisation of MATLAB

    Aim:

    To familiarise with the MATLAB desktop environment and commonly used MATLAB

    commands

    Theory:

    MATLAB is a high-level technical computing language and interactive environment for

    algorithm development, data visualization, data analysis, and numeric computation. The

    MATLAB product can solve technical computing problems faster than traditional

    programming languages, such as C, C++, and Fortran.

    MATLAB can be used in a wide range of applications, including signal and image

    processing, communications, control design, test and measurement, financial modelling and

    analysis, and computational biology. Add-on toolboxes (collections of special-purpose

    MATLAB functions, available separately) extend the MATLAB environment to solve

    particular classes of problems in these application areas. Features include:

    High-level language for technical computing Development environment for managing code, files, and data Interactive tools for iterative exploration, design, and problem solving Mathematical functions for linear algebra, statistics, Fourier analysis, filtering,

    optimization, and numerical integration

    2-D and 3-D graphics functions for visualizing data Tools for building custom graphical user interfaces Functions for integrating MATLAB based algorithms with external applications and

    languages, such as C, C++, Fortran, Java, COM, and Microsoft

    Excel

    Mathematical Function Library

    This library is a vast collection of computational algorithms ranging from elementary

    functions, like sum, sine, cosine, and complex arithmetic, to more sophisticated functions like

    matrix inverse, matrix eigen values, Bessel functions, and fast Fourier transforms.

    The Language

    The MATLAB language is a high-level matrix/array language with control flow statements,

    functions, data structures, input/output, and object-oriented programming features. It allows

    both "programming in the small" to rapidly create quick programs you do not intend to reuse.

  • 7/31/2019 MATLAB Fair Record

    4/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 4

    Commonly used MATLAB Commands:

    1. clc: Clear Command WindowSyntax:

    clc

    Description:

    clc clears all input and output from the Command Window display, giving you a "clean

    screen."After using clc, you cannot use the scroll bar to see the history of functions, but

    you still can use the up arrow to recall statements from the command history.

    2. which: Locate functions and filesSyntax:

    which fun

    Description:

    which fun displays the full pathname for the argument fun. If fun is a MATLAB function

    or Simulink model in an M, P, or MDL file on the MATLAB path, then which displays the

    full pathname for the corresponding file. If a workspace variable, then which displays a

    message identifying fun as a variable

    3. help: Help for functions in Command WindowSyntax:

    help

    help functionname

    help toolboxname

    Description:

    help lists all primary help topics in the Command Window. Each main help topic

    corresponds to a directory name on the search path the MATLAB software uses. help

    functionname displays M-file help, which is a brief description and the syntax for

    functionname, in the Command Window. The output includes a link to doc functionname,

    which displays the reference page in the Help browser, often providing additional

    information. help toolboxname displays the Contents.m file for the specified directory

    named toolboxname, where Contents.m contains a list and corresponding description of

    M-files in toolboxname.

    4. ones: Create array of all onesSyntax:

    Y = ones(n)

    Y = ones(m,n)

    Description:

  • 7/31/2019 MATLAB Fair Record

    5/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 5

    Y = ones(n) returns an n-by-n matrix of 1s. An error message appears if n is not a scalar. Y

    = ones(m,n) or Y = ones([m n]) returns an m-by-n matrix of ones. Y = ones(m,n,p,...) or Y

    = ones([m n p ...]) returns an m-by-n-by-p-by-... array of 1s. The size inputs m, n, p, ...

    should be nonnegative integers. Negative integers are treated as 0.

    5. zeros: Create array of all zerosSyntax

    B = zeros(n)

    B = zeros(m,n)

    Description:

    B = zeros(n) returns an n-by-n matrix of zeros. An error message appears if n is not a

    scalar. B = zeros(m,n) or B = zeros([m n]) returns an m-by-n matrix of zeros. B =

    zeros(m,n,p,...) or B = zeros([m n p ...]) returns an m-by-n-by-p-by-... array of zeros. The

    size inputs m, n, p, ... should be nonnegative integers. Negative integers are treated as 0. B

    = zeros(size(A)) returns an array the same size as A consisting of all zeros.

    6. linspace: Generate linearly spaced vectorsSyntax:

    y = linspace(a,b)

    y = linspace(a,b,n)

    Description:

    The linspace function generates linearly spaced vectors. It is similar to the colon operator":", but gives direct control over the number of points. y = linspace(a,b) generates a row

    vector y of 100 points linearly spaced between and including a and b. y = linspace(a,b,n)

    generates a row vector y of n points linearly spaced between and including a and b

    7. size: Array dimensionsSyntax:

    d = size(X)

    [m,n] = size(X)

    m = size(X,dim)

    Description:

    d = size(X) returns the sizes of each dimension of array X in a vector d with ndims(X)

    elements. If X is a scalar, which MATLAB software regards as a 1-by-1 array, size(X)

    returns the vector [1 1]. [m,n] = size(X) returns the size of matrix X in separate variables

    m and n. m = size(X,dim) returns the size of the dimension of X specified by scalar dim.

    8. Diag: Diagonal matrices and diagonals of matrixSyntax:

    X = diag(v,k)

  • 7/31/2019 MATLAB Fair Record

    6/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 6

    X = diag(v)

    v = diag(X,k)

    v = diag(X)

    Description:

    X = diag(v,k) when v is a vector of n components, returns a square matrix X of order

    n+abs(k), with the elements of v on the kth diagonal. k = 0 represents the main diagonal, k

    > 0 above the main diagonal, and k < 0 below the main diagonal. X = diag(v) puts v on the

    main diagonal, same as above with k = 0. v = diag(X,k) for matrix X, returns a column

    vector v formed from the elements of the kth diagonal of X. v = diag(X) returns the main

    diagonal of X, same as above with k = 0.

    9. plot: 2-D line plotSyntax:

    plot(Y)

    plot(X1,Y1,...)

    plot(X1,Y1,LineSpec,...)

    Description:

    plot(Y) plots the columns of Y versus their index if Y is a real number. If Y is complex,

    plot(Y) is equivalent to plot(real(Y),imag(Y)). In all other uses of plot, the imaginary

    component is ignored. plot(X1,Y1,...) plots all lines defined by Xn versus Yn pairs. If only

    one of Xn or Yn is a matrix, the vector is plotted versus the rows or columns of the matrix,

    depending on whether the vector's row or column dimension matches the matrix. If Xn is a

    scalar and Yn is a vector, disconnected line objects are created and plotted as discrete

    points vertically at Xn. plot(X1,Y1,LineSpec,...) plots all lines defined by the

    Xn,Yn,LineSpec triples, where LineSpec is a line specification that determines line type,

    marker symbol, and color of the plotted lines.

    10.ceil: Round toward positive infinitySyntax:

    B = ceil(A)

    Description:

    B = ceil(A) rounds the elements of A to the nearest integers greater than or equal to A. For

    complex A, the imaginary and real parts are rounded independently.

    11.floor: Round toward negative infinitySyntax:

    B = floor(A)

    Description:

  • 7/31/2019 MATLAB Fair Record

    7/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 7

    B = floor(A) rounds the elements of A to the nearest integers less than or equal to A. For

    complex A, the imaginary and real parts are rounded independently.

    12.eye: Identity matrixSyntax:

    Y = eye(n)

    Y = eye(m,n)

    Description

    Y = eye(n) returns the n-by-n identity matrix. Y = eye(m,n) or eye([m n]) returns an m-by-

    n matrix with 1's on the diagonal and 0's elsewhere.

    13.inv: Matrix inverseSyntax:

    Y = inv(X)

    Description:

    Y = inv(X) returns the inverse of the square matrix X. A warning message is printed if X

    is badly scaled or nearly singular.

    14.subplot: Create axes in tiled positionsSyntax:

    subplot(m,n,P)

    Description:

    subplot divides the current figure into rectangular panes that are numbered rowwise. Each

    pane contains an axes object. Subsequent plots are output to the current pane. h =

    subplot(m,n,p) or subplot(mnp) breaks the figure window into an m-by-n matrix of small

    axes, selects the pth axes object for the current plot, and returns the axes handle. The axes

    are counted along the top row of the figure window.

    15.stem: Plot discrete sequence dataSyntax:

    stem(Y)

    stem(X,Y)

    Description:

    A two-dimensional stem plot displays data as lines extending from a baseline along the x-

    axis. A circle (the default) or other marker whose y-position represents the data value

    terminates each stem. stem(Y) plots the data sequence Y as stems that extend from equally

    spaced and automatically generated values along the x-axis. When Y is a matrix, stem

    plots all elements in a row against the same x value. stem(X,Y) plots X versus the columns

  • 7/31/2019 MATLAB Fair Record

    8/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 8

    of Y. X and Y must be vectors or matrices of the same size. Additionally, X can be a row

    or a column vector and Y a matrix with length(X) rows.

    xlabel : Label x-axis

    Syntax:

    xlabel('string')

    xlabel(fname)

    Description:

    Each axes graphics object can have one label for the x-, y-, and z-axis. The label appears

    beneath its respective axis in a two-dimensional plot and to the side or beneath the axis in

    a three-dimensional plot. xlabel('string') labels the x-axis of the current axes.

    xlabel(fname) evaluates the function fname, which must return a string, then displays the

    string beside the x-axis.

    ylabel: Label y-axis

    Syntax:

    ylabel(...)

    ylabel(axes_handle,...)

    Description:

    ylabel(...) labels the y-axis of the current axes in a fashion similar to xlabel.

    Result:

    The MATALB development environment and commonly used MATLAB commands were

    familiarised with

  • 7/31/2019 MATLAB Fair Record

    9/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 9

    Experiment No: 2

    Date: 15 July 2011

    Generation of Test Signals

    Aim:

    To generate standard signals used commonly in Digital Signal Processing applications

    namely:

    Impulse signal Step signal Ramp signal Sine signal Cosine signal Exponential signal Square signal Sawtooth signal Sinc signal

    Theory:

    In the fields of communications, signal processing, and in electrical engineering more

    generally, a signal is any time-varying or spatial-varying quantity. In the physical world, any

    quantity measurable through time or over space can be taken as a signal. Despite the

    complexity of such systems, their outputs and inputs can often be represented as simple

    quantities measurable through time or across space.

    Frequency domain techniques are applicable to all signals, both continuous-time and discrete-

    time. If a signal is passed through an LTI system, the frequency spectrum of the resulting

    output signal is the product of the frequency spectrum of the original input signal and the

    frequency response of the system.

    The different waves used are:

    A square wave is a kind of non-sinusoidal waveform, most typically encountered inelectronics and signal processing. An ideal square wave alternates regularly and

    instantaneously between two levels. Its stochastic counterpart is a two-state trajectory.

    The Heaviside step function, or the unit step function, usually denoted by H (butsometimes u or ), is a discontinuous function whose value is zero for negative

    argument and one for positive argument. It seldom matters what value is used for

    H(0), since H is mostly used as a distribution

    The sine wave or sinusoid is a mathematical function that describes a smoothrepetitive oscillation. It occurs often in pure mathematics, as well as physics, signalprocessing, electrical engineering and many other fields.

  • 7/31/2019 MATLAB Fair Record

    10/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 10

    A triangle wave is a non-sinusoidal waveform named for its triangular shape. Like asquare wave, the triangle wave contains only odd harmonics. However, the higher

    harmonics roll off much faster than in a square wave (proportional to the inverse

    square of the harmonic number as opposed to just the inverse).

    The sawtooth wave (or saw wave) is a kind of non-sinusoidal waveform. It is named asawtooth based on its resemblance to the teeth on the blade of a saw.The convention

    is that a sawtooth wave ramps upward and then sharply drops.

    The exponential function is used to model a relationship in which a constant change inthe independent variable gives the same proportional change (i.e. percentage increase

    or decrease) in the dependent variable. The function is often written as exp(x),

    especially when it is impractical to write the independent variable as a superscript.

    Program:

    function [p] = testsignalgenerator(type,L)%% function p = testsignalgenerator(type,L) generates the required

    %% test signal. Duration of the signal is to specified in L, while signal %%type is to be

    stored in type. p returns the elements of the signal

    %% Signal Type Numeral Example Declartion

    %% Impulse 1 testsignalgenerator(1, 1000)

    %% Step 2 testsignalgenerator(2, 1000)

    %% Ramp 3 testsignalgenerator(3, 1000)

    %% Sine 4 testsignalgenerator(4, 1000)

    %% Cosine 5 testsignalgenerator(5, 1000)

    %% Exponential 6 testsignalgenerator(6, 1000)

    %% Square 7 testsignalgenerator(7, 1000)

    %% Triangular 8 testsignalgenerator(8, 1000)

    %% Sawtooth 9 testsignalgenerator(9, 1000)

    %% Sinc 10 testsignalgenerator(10, 1000)

    clc;

    close all;

    p=zeros(1,L);

    if type== 1

    p(L/2)=1;

    m='Impulse Function';

    elseif type==2

    width=input('Enter width of Step: ');

    j=1;

    i=1;

    while (i

  • 7/31/2019 MATLAB Fair Record

    11/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 11

    j=j+1;

    i=i+width;

    end

    m='Step Function';

    elseif type==3p=0:L;

    m='Ramp Function';

    elseif type==4

    f=input('Enter signal frequrncy: ');

    Fs=input('Enter sampling frequency: ');

    p=sin(2*pi*f/Fs*(1:L));

    m='Sine Function';

    elseif type==5

    f=input('Enter signal frequrncy: ');

    Fs=input('Enter sampling frequency: ');

    p=cos(2*pi*f/Fs*(1:L));

    m='Cosine Function';

    elseif type==6

    p=exp(1:L);

    m='Exponent Function';

    elseif type==7

    width=input('Enter width:');

    o=zeros(1,width);

    l=ones(1,width);t=[l,o];

    l=length(t);

    index=L/l;

    p=t;

    for i=1:index

    p=[p,t];

    end

    m='Square Function';

    elseif type==8

    o=linspace(-1,1);

    l=linspace(1,-1);

    t=[l,o];

    l=length(t);

    index=L/l;

    p=t;

    for i=1:index

    p=[p,t];

    end

    m='Triangular Function';

    elseif type==9

  • 7/31/2019 MATLAB Fair Record

    12/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 12

    p=sawtooth(n);

    m='Sawtooth Function';

    elseif type==10

    t=linspace(-L/2,L/2);

    p=sinc(t);m='Sinc Function';

    else

    error('Invalid Choice');

    end

    stem(p);

    title(m);

    grid on;

    xlabel('N');

    ylabel('Magnitude');

    Sample Output:

    0 100 200 300 400 500 600 700 800 900 10000

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

    0.8

    0.9

    1Impulse Funct ion

    N

    Magnitude

    0 200 400 600 800 1000 12000

    2

    4

    6

    8

    10

    12

    14

    16

    18

    20Step Function

    N

    Magnitude

  • 7/31/2019 MATLAB Fair Record

    13/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 13

    -100 -80 -60 -40 -20 0 20 40 60 80 1000

    0.2

    0.4

    0.6

    0.8

    1

    N

    Amplitude

    Unit Step Signal

    0 200 400 600 800 1000 12000

    100

    200

    300

    400

    500

    600

    700

    800

    900

    1000Ramp Function

    N

    Magnitude

    0 100 200 300 400 500 600 700 800 900 1000-1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1Sine Function

    N

    Magnitude

  • 7/31/2019 MATLAB Fair Record

    14/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 14

    0 100 200 300 400 500 600 700 800 900 1000-1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1Cosine Function

    N

    Magnitude

    0 10 20 30 40 50 60 70 80 90 1000

    1

    2

    x 1043 Exponent Function

    N

    Magnitude

    0 200 400 600 800 1000 12000

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

    0.8

    0.9

    1Square Function

    N

    Ma

    gnitude

  • 7/31/2019 MATLAB Fair Record

    15/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 15

    0 200 400 600 800 1000 1200-1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1Triangular Function

    N

    Magnitude

    0 100 200 300 400 500 600 700-1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1Sawtooth Function

    N

    Magnitude

  • 7/31/2019 MATLAB Fair Record

    16/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 16

    Result:

    The standard test signals were generated using MATLAB

    Experiment No: 3

    Date: 22 July 2011

    Convolution of Two Signals

    Aim:

    To generate the convolution output of two inputted signals

    Theory:

    In mathematics and, in particular, functional analysis, convolution is a mathematical

    operation on two functions f and g, producing a third function that is typically viewed as a

    modified version of one of the original functions.

    0 10 20 30 40 50 60 70 80 90 100-4

    -2

    0

    2

    4

    6

    8

    10x 10

    -3 Sinc Function

    N

    Magnitude

  • 7/31/2019 MATLAB Fair Record

    17/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 17

    Convolution is similar to cross-correlation. It has applications that include probability,

    statistics, computer vision, image and signal processing, electrical engineering, and

    differential equations.

    The convolution of and g is written g, using an asterisk or star. It is defined as the

    integral of the product of the two functions after one is reversed and shifted. the convolution

    formula can be described as a weighted average of the function () at the moment t where

    the weighting is given by g() simply shifted by amount t. As t changes, the weighting

    function emphasizes different parts of the input function.

    Program:

    clc;

    clear,closeall;

    x=input('Enter the first matrix: ');

    m=length(x);

    y=input('Enter the second matrix: ');

    n=length(y);

    t=num2str(m+n-1);

    enter='The convolution product has length: ';

    l=[enter,t];

    display(l);

    display('MATLAB Convolution Output: ');

    w=conv(x,y)subplot(2,1,1);

    stem(w);

    gridon;

    xlabel('N');

    ylabel('Magnitude');

    title('MATLAB Convolution Output');

    display('Manual Convolution Output: ');

    g=zeros(1,m+n-1);

    for k=1:m+n-1

    for j=max(1,k+1-n):min(k,m)

    g(k)=g(k)+x(j)*y(k+1-j);

    end

    end

    display (g);

    subplot(2,1,2);

    stem(g);

    gridon;

    xlabel('N');

    ylabel('Magnitude');

    title('Manual Convolution Output');

  • 7/31/2019 MATLAB Fair Record

    18/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 18

    Sample Output:

    Enter the first matrix: [ 1 2 3 4]

    Enter the second matrix: [ 5 6 7 8]

    The convolution product has length: 7

    MATLAB Convolution Output: 5 16 34 60 61 52 32

    Manual Convolution Output: 5 16 34 60 61 52 32

    Result:

    MATLAB code to perform convolution of two given signals was generated and output wasobtained

    Experiment No: 4

    Date: 29 July 2011

    Circular Convolution of Two Signals

    Aim:

    To generate the circular convolution output of two inputted signals

    1 2 3 4 5 6 70

    20

    40

    60

    80

    N

    Magnitude

    MATLAB Convolution Output

    1 2 3 4 5 6 70

    20

    40

    60

    80

    N

    Magnitude

    Manual Convolution Output

  • 7/31/2019 MATLAB Fair Record

    19/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 19

    Theory:

    The circular convolution, also known as cyclic convolution, of two aperiodic functions occurs

    when one of them is convolved in the normal way with a periodic summation of the other

    function. That situation arises in the context of the Circular convolution theorem. The

    identical operation can also be expressed in terms of the periodic summations of both

    functions, if the infinite integration interval is reduced to just one period. That situation

    arises in the context of the discrete-time Fourier transform (DTFT) and is also called periodic

    convolution. In particular, the transform (DTFT) of the product of two discrete sequences is

    the periodic convolution of the transforms of the individual sequences.

    This operation is a periodic convolution of functions xT and hT. When xT is expressed as the

    periodic summation of another function, x, the same operation may also be referred to as a

    circular convolution of functions h and x.

    Program:

    clc;

    clear ,close all;

    disp('Circular Convolution Program');

    l=input('Enter length of convolution output, n: ');

    x=input('Enter i/p sequence x(n): ');

    n=length(x);

    x=[x,zeros(1,l-n)];h=input('Enter i/p sequence h(n): ');

    n=length(h);

    h=[h,zeros(1,l-n)];

    n=l;

    subplot(2,2,1), stem(x);

    title('x(n)');

    xlabel('n');ylabel('x(n)');

    grid on;

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

    title('h(n)');

    xlabel('n');ylabel('x(n)');

    grid on;

    y=zeros(1,n);

    y(1)=0;

    a(1)=h(1);

    for j=2:n

    a(j)=h(n-j+2);

    end

  • 7/31/2019 MATLAB Fair Record

    20/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 20

    for i=1:n

    y(1)=y(1)+x(i)*a(i);

    end

    for k=2:n

    y(k)=0;for j=2:n

    x2(j)=a(j-1);

    end

    x2(1)=a(n);

    for i=1:n

    if(i+1

  • 7/31/2019 MATLAB Fair Record

    21/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 21

  • 7/31/2019 MATLAB Fair Record

    22/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 22

    Experiment No: 5

    Date: 29 July 2011

    Cross Correlation of Two Signals

    Aim:

    To generate the cross correlation output of two inputted signals

    Theory:

    In signal processing, cross-correlation is a measure of similarity of two waveforms as a

    function of a time-lag applied to one of them. This is also known as a sliding dot product or

    sliding inner-product. It is commonly used for searching a long-duration signal for a shorter,

    known feature. It also has applications in pattern recognition, single particle analysis, electron

    tomographic averaging, cryptanalysis, and neurophysiology.

    The cross-correlation is similar in nature to the convolution of two functions. Whereas

    convolution involves reversing a signal, then shifting it and multiplying by another signal,

    correlation only involves shifting it and multiplying (no reversing).

    In an autocorrelation, which is the cross-correlation of a signal with itself, there will always

    be a peak at a lag of zero unless the signal is a trivial zero signal.

    In probability theory and statistics, correlation is always used to include a standardising factor

    in such a way that correlations have values between 1 and +1, and the term cross-correlation

    is used for referring to the correlation corr(X, Y) between two random variables X and Y,

    while the "correlation" of a random vector X is considered to be the correlation matrix

    (matrix of correlations) between the scalar elements of X.

    Program:

    function [Rxx]=crosscorr(x,y)

    % function [Rxx]=crosscorr(x,y)

    % This function Estimates the cross correlation of the sequences of

    % random variables given in x, y as: Rxx(1), Rxx(2),,Rxx(N), where N is

    % Number of samples in x or y whichever is greater. For autocorrelation

    % call should be crosscorr(x,x)

    if (length(x)>length(y))

    y=[y,zeros(1,length(x)-length(y))];

    elseif (length(x)

  • 7/31/2019 MATLAB Fair Record

    23/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 23

    for m=1: N+1

    for n=1: N-m+1

    Rxx(m)=Rxx(m)+x(n)*y(n+m-1);

    end;

    end;

    y1=Rxx(1, 2:length(y));

    for m=1:length(y1)/2

    temp=y1(m);

    y1(m)=y1(length(y1)-m+1);

    y1(length(y1)-m+1)=temp;

    end

    Rxx=[y1,Rxx];

    Sample Output:

    crosscorr([ 1 1 1 1 ], [ 2 2 2 2])

    ans = 2 4 6 8 6 4 2

    xcorr([ 1 1 1 1 ], [ 2 2 2 2])

    ans = 2 4 6 8 6 4 2

    Result:

    MATLAB code to perform correlation of two given signals was generated and output was

    obtained

  • 7/31/2019 MATLAB Fair Record

    24/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 24

    Experiment No: 6

    Date: 12 August 2011

    Block Convolution of Two Signals

    Aim:

    To write a program to perform block convolution using overlap-save and overlap-add

    method.

    Theory:

    Segment the infinite-length input sequence into smaller sections (or blocks), process each

    section using the DFT, and finally assemble the output sequence from the outputs of each

    section. This procedure is called a block convolution operation.

    While short signals provide useful illustrations of convolution, they are not typical of

    practical filtering problems. In many applications the input x[n] is very long with respect to

    the filter impulse response h[n]. It is often impractical to process the entire input signal at

    once. An alternative solution is to break the input into blocks, filter each block separately and

    then combine the blocks appropriately to obtain the output. There are two basic strategies for

    block processing, which are known as overlap-add and overlap-save, respectively.

    Overlap-Add Implementation:-

    The overlap-add method works by breaking the long input signal into small non-overlapping

    sections. If the length of these sections is L and the length of the impulse response is P, then

    an N = L + P1 point DFT is used to implement the filter for each section. (The L + P - 1-

    point DFT avoids problems with time-aliasing inherent in the circular convolution.) The

    result for each block overlaps with the following block. These overlapping sections must be

    added together to compute the output.

    Overlap-Save Implementation:-

    The overlap-save method uses a different strategy to break up the input signal. If the length of

    the circular convolution (i.e., the DFT length) is chosen to be N = L + P - 1, overlap-save

    uses input segments of length N. The starting location of each input segment is skipped by an

    amount L, so there is an overlap of P - 1 points with the previous section. Thus this method

    could also be called the overlapped inputs method. The L good points resulting from the N-

    point circular convolution of each section with the filter are retained. No additions are needed

    to create the output.

    Program:

    %%Program to perform Overlap-add.

    clc;

  • 7/31/2019 MATLAB Fair Record

    25/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 25

    clear,close all;

    h= input('Transfer function: ');

    x= input(' Data Series: ');

    l= input(' Enter Block Length: ');

    sum= zeros(1, length(x));

    h=fft(h);

    n= length(h);

    for i = 1: length(x)/l

    temp = x((i-1)*l+1:l*i);

    temp= [ temp, zeros(1, n-l)];

    temp= fft(temp);

    temp= temp .* h;

    temp= ifft(temp);

    sum((i-1)*n+1:n*i)=temp;

    end

    h = ifft(h);

    % Overlap-Save method of block convolution

    % ----------------------------------------% [y] = ovrlpsav(x,h,N)

    % y = output sequence

    % x = input sequence

    % h = impulse response

    % N = block length

    clc;

    clear,close all;

    h= input('Transfer function: ');

    x= input(' Data Series: ');

    N= input(' Enter Block Length: ');

    Lenx = length(x); M = length(h);

    M1 = M-1; L = N-M1;

    h = [h zeros(1,N-M)];

    %

    x = [zeros(1,M1), x, zeros(1,N-1)]; % preappend (M-1) zeros

    K = floor((Lenx+M1-1)/(L)); % # of blocks

    Y = zeros(K+1,N);

    % convolution with succesive blocks

    for k=0:K

  • 7/31/2019 MATLAB Fair Record

    26/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 26

    xk = x(k*L+1:k*L+N);

    Y(k+1,:) = cconv(xk,h,N);

    end

    Y = Y(:,M:N)'; % discard the first (M-1) samples

    y = (Y(:))'; % assemble outputdisplay('Convolution Output:');

    display(y);

    Sample Output:

    Overlap Add Method

    Transfer function: [ 1 1 1 1 ]

    Data Series: [ 1 2 3 4 5]

    Enter Block Length: 4

    Convolution Output: 1 3 6 10 14 12 9 5

    Overlap Save Method

    Transfer function: [ 1 1 1 1 ]

    Data Series: [ 1 2 3 4 5]

    Enter Block Length: 4

    Convolution Output: 1 3 6 10 14 12 9 5

    conv([ 1 1 1 1], [ 1 2 3 4 5])

    ans = 1 3 6 10 14 12 9 5

    Result:

    The MATLAB code for block convolution was generated and desired output was obtained

  • 7/31/2019 MATLAB Fair Record

    27/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 27

    Experiment No: 7

    Date: 12 August 2011

    Discrete Fourier Transform of a Signal and its Properties

    Aim:

    To write a program to find DFT without using MATLAB functions and also to prove its

    properties.

    Theory:

    The discrete Fourier transform (DFT) is a specific kind ofdiscrete transform, used

    in Fourier analysis. DFT is a transform for Fourier analysis of finite-domain discrete-time

    functions.It transforms a time domain signal into the frequency domain representation.

    The sequence of N complex numbers x0... xN1 is transformed into another sequence

    of N complex numbers according to the DFT formula

    The inverse discrete Fourier transform (IDFT) is given by

    Properties:

    1. Circular Shift property of DFT:

    If X(k) = DFT {x(n)}, then X(k)ej2km/N = DFT {x((n m) modN)}

    2. Linearity Property:

    X(k) = DFT {x(n)}, Y(k) = DFT {y(n)}, then if z(n)=x(n)+y(n),

    Z(k) = DFT {x(n)+y(n)}

    3. Convolution in time domain is multiplication in frequency domain

    The DFT is widely employed in signal processing and related fields to analyze the

    frequencies contained in a sampled signal, to solve partial differential equations, and to

    perform other operations such as convolutions or multiplying large integers.

    Program:

    %DFT using matrix multiplication

    http://en.wikipedia.org/wiki/Discrete_transformhttp://en.wikipedia.org/wiki/Fourier_analysishttp://en.wikipedia.org/wiki/Time_domainhttp://en.wikipedia.org/wiki/Frequency_domainhttp://en.wikipedia.org/wiki/Sequencehttp://en.wikipedia.org/wiki/Complex_numberhttp://en.wikipedia.org/wiki/Complex_numberhttp://en.wikipedia.org/wiki/Sequencehttp://en.wikipedia.org/wiki/Frequency_domainhttp://en.wikipedia.org/wiki/Time_domainhttp://en.wikipedia.org/wiki/Fourier_analysishttp://en.wikipedia.org/wiki/Discrete_transform
  • 7/31/2019 MATLAB Fair Record

    28/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 28

    clc

    clear, close all

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

    N=input('enter the length of DFT'); % Length of DFTlen=length(x); % Length of sequence

    if(N>len)

    x=[x zeros(1,N-len)]; % Adding zeros to x

    else if(N

  • 7/31/2019 MATLAB Fair Record

    29/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 29

    display('Linearity property');

    display('fft of x');

    display(fft(x));

    display('fft of y');

    display(fft(y));display('fft of x+fft of y');

    display(fft(x)+fft(y));

    display('fft of x+y');

    display(fft(x+y));

    display('Convolution Property');

    display(fft(conv(x,y)));

    display(' Frequency domain multiplication');

    display(fft(y).*fft(x));

  • 7/31/2019 MATLAB Fair Record

    30/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 30

    Sample Output:

    enter the sequence[ 1 2 3 4 5]

    enter the length of DFT: 8

    Columns 1 through 5

    15.0000 -5.4142 - 7.2426i 3.0000 + 2.0000i -2.5858 - 1.2426i 3.0000 + 0.0000i

    Columns 6 through 8

    -2.5858 + 1.2426i 3.0000 - 2.0000i -5.4142 + 7.2426i

    >> fft([ 1 2 3 4 5],8)

    ans =

    Columns 1 through 5

    15.0000 -5.4142 - 7.2426i 3.0000 + 2.0000i -2.5858 - 1.2426i 3.0000

    Columns 6 through 8

    -2.5858 + 1.2426i 3.0000 - 2.0000i -5.4142 + 7.2426i

    Output for Circular Shift:

    Input sequence 1: [ 1 2 3 4 5]

    Input sequence 2: [ 1 2 2 3 4]

    0 1 2 3 4 5 6 70

    5

    10

    15magnitude spectrum of x(n)

    0 1 2 3 4 5 6 7-3

    -2

    -1

    0

    1

    2

    3phase spectrum of x(n)

    0 1 2 3 4 5 6 70

    5

    10

    15

    magnitude spectrum of x (n-n0)

    0 1 2 3 4 5 6 7-3

    -2

    -1

    0

    1

    2

    3

    4

    phase spectrum of x (n-n0)

  • 7/31/2019 MATLAB Fair Record

    31/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 31

    Linearity property

    fft of x= 15.0000 -2.5000 + 3.4410i -2.5000 + 0.8123i -2.5000 - 0.8123i -2.5000 -

    3.4410i

    fft of y= 12.0000 -1.1910 + 2.4899i -2.3090 + 0.2245i -2.3090 - 0.2245i -1.1910 -2.4899i

    fft of x+fft of y =

    27.0000 -3.6910 + 5.9309i -4.8090 + 1.0368i -4.8090 - 1.0368i -3.6910 - 5.9309i

    fft of x+y =

    27.0000 -3.6910 + 5.9309i -4.8090 + 1.0368i -4.8090 - 1.0368i -3.6910 - 5.9309i

    Convolution Property

    Ans=

    1.0e+002 *

    1.0e+002 *

    1.8000 -0.0559 - 0.1032i 0.0559 - 0.0244i 0.0559 + 0.0244i -0.0559 + 0.1032i

    Frequency domain multiplication

    ans =

    1.0e+002 *

    1.8000 -0.0559 - 0.1032i 0.0559 - 0.0244i 0.0559 + 0.0244i -0.0559 + 0.1032i

    Result:

    To familiarise with the MATLAB desktop environment and commonly used MATLAB

    commands

  • 7/31/2019 MATLAB Fair Record

    32/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 32

    Experiment No: 8

    Date: 23 August 2011

    Inverse Discrete Fourier Transform of a Signal

    Aim:

    To generate the inverse Discrete Fourier Transform of given signal.

    Theory:

    The inverse DFT (IDFT) transforms Ndiscrete-frequency samples to the same number of

    discrete-time samples. The IDFT has a form very similar to the DFT,

    and can thus also be computed efficiently using FFTs.

    Program:

    %Program to find inverse DFT

    clc

    clear ,close allx= input('enter the sequence: '); % Enter the sequence

    N=input('enter the length of DFT: '); % Length of DFT

    len=length(x);

    if(N>len)

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

    else if(N

  • 7/31/2019 MATLAB Fair Record

    33/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 33

    grid on;

    Sample Output:

    enter the sequence: [ 1 2 3 4 5]

    enter the length of DFT: 8

    X =

    Columns 1 through 5

    1.8750 -0.6768 + 0.9053i 0.3750 - 0.2500i -0.3232 + 0.1553i 0.3750 - 0.0000i

    Columns 6 through 8

    -0.3232 - 0.1553i 0.3750 + 0.2500i -0.6768 - 0.9053i

    >> ifft([ 1 2 3 4 5], 8)

    Columns 1 through 5

    1.8750 -0.6768 + 0.9053i 0.3750 - 0.2500i -0.3232 + 0.1553i 0.3750

    Columns 6 through 8

    -0.3232 - 0.1553i 0.3750 + 0.2500i -0.6768 - 0.9053i

    Result:

    The program to find inverse DFT was implemented and the desired output was observed.

  • 7/31/2019 MATLAB Fair Record

    34/46

  • 7/31/2019 MATLAB Fair Record

    35/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 35

    A=nextpow2(length(x)) ; %no of levels

    x=[x,zeros(1,(2^A)-N)]; %appending zeros

    x = bitrevorder(x); %perform bit-reversal

    N = length(x);

    level = A;phase = cos(2*pi/N*[0:(N/2-1)])-j*sin(2*pi/N*[0:(N/2-1)]);

    for a = 1:level

    L = 2^a;

    phase_level = phase(1:N/L:(N/2));

    for k = 0:L:N-L

    for n = 0:L/2-1

    first = x(n+k+1);

    second = x(n + k + L/2 +1)*phase_level(n+1);

    x(n+k+1) = first + second;

    x(n+k + L/2+1) = first - second;

    end

    end

    end

    display(' The DIF FFT Output is:');

    display(x);

    subplot 212, plot(x);, grid on, title(' DIF FFT Output');

  • 7/31/2019 MATLAB Fair Record

    36/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 36

    Sample Output:

    my_fft([ 1 2 3 4])

    MATLAB FFT Output is 10.0000 -2.0000 + 2.0000i -2.0000 -2.0000 - 2.0000i

    The DIT FFT Output is: 10.0000 -2.0000 + 2.0000i -2.0000 -2.0000 - 2.0000i

    Result:

    To familiarise with the MATLAB desktop environment and commonly used MATLAB

    commands

    -2 0 2 4 6 8 10-2

    -1

    0

    1

    2MATLAB FFT Output

    -2 0 2 4 6 8 10-2

    -1

    0

    1

    2DIT FFT Output

  • 7/31/2019 MATLAB Fair Record

    37/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 37

    Experiment No: 10

    Date: 20 September 2011

    Decimation in Frequency Fast Fourier Transform of a Signal

    Aim:

    To generate the Discrete Fourier Transform of given signal by Decimation in Frequency Fast

    Fourier transform method.

    Theory:

    To familiarise with the MATLAB desktop environment and commonly used MATLAB

    commands

    Program:

    function radix4(x);

    display('MATLAB FFT Output is');

    display(fft(x));

    subplot 211, plot(fft(x));, grid on, title(' MATLAB FFT Output');

    temp=length(x);

    p=1;

    while (4^p)

  • 7/31/2019 MATLAB Fair Record

    38/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 38

    x(n+1+k) = a*exp( -j*((2*pi)/N)* (0) *(4^(stage-1)) ) ;

    x(M+n+1+k) = b*exp( -j*((2*pi)/N)*(n*1)*(4^(stage-1)) ) ; % In place

    Computation

    x((2*M)+n+1+k) = c*exp( -j*((2*pi)/N)*(n*2)*(4^(stage-1)) ) ;

    x((3*M)+n+1+k) = d*exp( -j*((2*pi)/N)*(n*3)*(4^(stage-1)) ) ;%*************************************************************

    end;

    end;

    M=M/4;

    end;

    x

    display(' The DIF FFT Output is:');

    display(x);

    subplot 212, plot(x);, grid on, title(' DIF FFT Output');

  • 7/31/2019 MATLAB Fair Record

    39/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 39

    Sample Output:

    radix4([ 1 2 3 4])

    MATLAB FFT Output is 10.0000 -2.0000 + 2.0000i -2.0000 -2.0000 - 2.0000i

    The DIF FFT Output is: 10.0000 -2.0000 + 2.0000i -2.0000 -2.0000 - 2.0000i

    Result:

    To familiarise with the MATLAB desktop environment and commonly used MATLAB

    commands

    -2 0 2 4 6 8 10-2

    -1.5

    -1

    -0.5

    0

    0.5

    1

    1.5

    2MATLAB FFT Output

    -2 0 2 4 6 8 10-2

    -1.5

    -1

    -0.5

    0

    0.5

    1

    1.5

    2DIF FFT Output

  • 7/31/2019 MATLAB Fair Record

    40/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 40

    Experiment No: 11

    Date: 4 October 2011

    Finite Impulse Response Filters

    Aim:

    To write a program in MATLAB to implement FIR filter .

    Theory:

    A finite impulse response (FIR) filter is a type of a signal processing filter whose impulse

    response (or response to any finite length input) is offinite duration, because it settles to zero

    in finite time. This is in contrast to infinite impulse response (IIR) filters, which have internal

    feedback and may continue to respond indefinitely (usually decaying). The impulse

    response of an Nth-order discrete-time FIR filter (i.e. with a Kronecker delta impulse input)

    lasts for N+1 samples, and then dies to zero.FIR filters can be discrete-time or continuous-

    time, and digital or analogue.

    For a discrete-time FIR filter, the output is a weighted sum of the current and a finite number

    of previous values of the input. The operation is described by the following equation, which

    defines the output sequence y[n] in terms of its input convolving its input signalx with

    its impulse response b. Sequence x[n]:

    Where:

    x[n] is the input signal, y[n] is the output signal, bi are the filter coefficients, also known as tap weights, that make up the impulse

    response,

    Nis the filter order; an Nth-order filter has (N+ 1) terms on the right-hand side.The x[ni] in these terms are commonly referred to as taps, based on the

    structure of a tapped delay line that in many implementations or block diagrams

    provides the delayed inputs to the multiplication operations. One may speak of a

    "5th order/6-tap filter", for instance.The main disadvantage of FIR filters is that considerably more computation power

    in a general purpose processor is required compared to an IIR filter with similar sharpness

    or selectivity, especially when low frequency (relative to the sample rate) cutoffs are needed.

    Program:

    %% HPF

    clc;clear,close all;

    http://en.wikipedia.org/wiki/Digital_delay_linehttp://en.wikipedia.org/wiki/Selectivity_(electronic)http://en.wikipedia.org/wiki/Selectivity_(electronic)http://en.wikipedia.org/wiki/Digital_delay_line
  • 7/31/2019 MATLAB Fair Record

    41/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 41

    Fs=10000; % Sampling frequency

    N=41; % Filter length

    WT=[10 3 10]; % Weights of the deviations in the bands

    Hd=[ 0 0 0 1 1 1]; % Desired magnitude response in the bands

    F=[0 0.1 0.2 0.3 0.4 1]; % Band edge frequenciesb = remez(N-1, F, Hd, WT); % Compute the filter coefficients

    [H, f] = freqz(b, 1, 512, Fs); % Compute the frequency response

    mag = 20*log10(abs(H)); % of filter and plot it

    plot(f, mag), grid on

    title(' FIR HPF Response');

    xlabel('Frequency (Hz)')

    ylabel('Magnitude (dB)')

    %% LPF

    clc;

    clear,close all;

    Fs=10000; % Sampling frequency

    N=41; % Filter length

    WT=[10 3 10]; % Weights of the deviations in the bands

    Hd=[1 1 1 1 0 0]; % Desired magnitude response in the bands

    F=[0 0.1 0.2 0.3 0.4 1]; % Band edge frequencies

    b = remez(N-1, F, Hd, WT); % Compute the filter coefficients

    [H, f] = freqz(b, 1, 512, Fs); % Compute the frequency response

    mag = 20*log10(abs(H)); % of filter and plot itplot(f, mag), grid on

    title(' FIR LPF Response');

    xlabel('Frequency (Hz)')

    ylabel('Magnitude (dB)')

  • 7/31/2019 MATLAB Fair Record

    42/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 42

    Sample Output:

    Result:

    To familiarise with the MATLAB desktop environment and commonly used MATLAB

    commands

    0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000-90

    -80

    -70

    -60

    -50

    -40

    -30

    -20

    -10

    0

    10FIR LPF Response

    Frequency (Hz)

    Magnitude(dB)

    0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000-80

    -70

    -60

    -50

    -40

    -30

    -20

    -10

    0

    10FIR HPF Response

    Frequency (Hz)

    Magnitude(dB)

  • 7/31/2019 MATLAB Fair Record

    43/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 43

    Experiment No: 12

    Date: 4 October 2011

    Infinite Impulse Response Filters

    Aim:

    To write a program in MATLAB to design a Butterworth filter

    Theory:

    The Butterworth filter is a type of signal processing filter designed to have as flat a frequency

    response as possible in the passband so that it is also termed a maximally flat magnitude

    filter.

    The frequency response of the Butterworth filter is maximally flat (has no ripples) in the

    passband and rolls off towards zero in the stopband. Butterworth filters have a monotonically

    changing magnitude function with time, unlike other filter types that have non-monotonic

    ripple in the passband and/or the stopband. It can be seen that as n approaches infinity, the

    gain becomes a rectangle function and frequencies below cut off frequency will be passed

    with gain G, while frequencies above it will be suppressed. For smaller values of n, the cut

    off will be less sharp.

    Program:

    % LPF

    clc;

    clear, close all;

    %LPF of cut off 500Hz

    fp=500; %pass band frequency in Hz

    fs=600; %stop band frequencyin Hz

    samp=2000; %sampling frequency in Hz

    ap=.5; %pass band attenuation

    as=40; %stop band attenuation

    wp=fp/(samp/2);ws=fs/(samp/2);

    [N,wn]=buttord(wp,ws,ap,as);%to find cut off frequency and order of filter

    [b,a]=butter(N,wn); %system function of fiter

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

    [H,W]=freqz(b,a,256);

    subplot 311, plot(W/(2*pi),20*log10(abs(H)))

    title(' IIR LPF Frequency Response');

    grid on;

    n=0:1/samp:1;

  • 7/31/2019 MATLAB Fair Record

    44/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 44

    x=cos(2*pi*200*n)+cos(2*pi*700*n);

    subplot 312,plot(n,abs(fft(x)))

    title(' Excitation Input'),

    grid on;y=filter(b,a,x);

    subplot 313, plot(n,abs(fft(y)))

    title(' IIR LPF Response'), grid on;

    % HPF

    clc;

    clear, close all;

    %HPF of cut off 500Hz

    fp=500; %pass band frequency in Hzfs=600; %stop band frequencyin Hz

    samp=2000; %sampling frequency in Hz

    ap=.5; %pass band attenuation

    as=40; %stop band attenuation

    wp=fp/(samp/2);

    ws=fs/(samp/2);

    [N,wn]=buttord(wp,ws,ap,as);%to find cut off frequency and order of filter

    [b,a]=butter(N,wn,'high'); %system function of fiter

    [H,W]=freqz(b,a,256);

    subplot 311, plot(W/(2*pi),20*log10(abs(H)))

    title(' IIR HPF Frequency Response');

    grid on;

    n=0:1/samp:1;

    x=cos(2*pi*200*n)+cos(2*pi*700*n);

    subplot 312,plot(n,abs(fft(x)))

    title(' Excitation Input'),

    grid on;

    y=filter(b,a,x);

    subplot 313, plot(n,abs(fft(y)))

    title(' IIR HPF Response'), grid on;

  • 7/31/2019 MATLAB Fair Record

    45/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Department of Electronics and Communication Engineering 45

    Sample Output:

    0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5-400

    -200

    0

    200IIR LPF Frequency Response

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    500

    1000Excitation Input

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    500

    1000IIR LPF Response

    0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5-400

    -200

    0

    200IIR HPF Frequency Response

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    500

    1000Excitation Input

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    500

    1000IIR HPF Response

  • 7/31/2019 MATLAB Fair Record

    46/46

    Model Engineering College EC 706 Digital Signal Processing Laboratory

    Result:

    Implemented a low pass and high pass Butterworth filter in MATLAB.