Mat Lab practicals for final year

download Mat Lab practicals for final year

of 73

Transcript of Mat Lab practicals for final year

  • 7/29/2019 Mat Lab practicals for final year

    1/73

    SIES College of Arts, Commerce & Science, Nerul

    GENERAL TERMS USED IN PROGRAMS OF MATLAB:-

    1) WINDOW :-

    SYNTAX :-

    WINDOW FUNCTION GATEWAY:

    WINDOW(@WNAME,N) returns an N-point window of typespecified

    by the function handle @WNAME in a column vector.

    WINDOW(@WNAME,N,OPT) designs the window with theoptional input argument specified in OPT. To see what theoptional input arguments are, see the help for the individual windows, forexample, KAISER or CHEBWIN.

    2) FREQZ :-

    SYNTAX :-

    Digital filter frequency response :-

    [H,W] = FREQZ(B,A,N) returns the N-point complex frequencyresponse

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

    filter:

    jw -jw -jmwjw B(e) b(1) + b(2)e + .... + b(m+1)e

    H(e) = ---- = ------------------------------------

    jw -jw -jnwA(e) a(1) + a(2)e + .... + a(n+1)e

    given numerator and denominator coefficients in vectors B and A. The

    frequency response is evaluated at N points equally spaced around theupper half of the unit circle. If N isn't specified, it defaults to 512.

    [H,W] = FREQZ(B,A,N,'whole') uses N points around the whole unit

    circle.

    3) STEM :-

    SYNTAX :-

    Discrete sequence or "stem" plot :-

    STEM(Y) plots the data sequence Y as stems from the x axis

    terminated with circles for the data value.

    STEM(X,Y) plots the data sequence Y at the values specified in X.

    Digital Signal Processing 1

  • 7/29/2019 Mat Lab practicals for final year

    2/73

    SIES College of Arts, Commerce & Science, Nerul

    4) FIGURE :-

    SYNTAX :-

    Create figure window :-

    FIGURE, by itself, creates a new figure window, and returns its handle.

    FIGURE(H) makes H the current figure, forces it to becomevisible, and raises it above all other figures on the screen. If Figure H

    does not exist, and H is an integer, a new figure is created with handle H.

    5) ABS :-

    SYNTAX :-

    Absolute value :-

    ABS(X) is the absolute value of the elements of X. When X iscomplex, ABS(X) is the complex modulus (magnitude) of the elements of

    X.

    6) ANGLE :-

    SYNTAX :-

    Phase angle :-

    ANGLE(H) returns the phase angles, in radians, of a matrix withcomplex elements.

    7) SIN :-

    SYNTAX :-Sine :-

    SIN(X) is the sine of the elements of X.

    8) COS :-

    SYNTAX :-

    Cosine :-COS(X) is the cosine of the elements of X.

    9) HOLD :-

    SYNTAX :-

    Hold current graph. HOLD ON holds the current plot and all axisproperties so that subsequent graphing commands add to the existinggraph.

    Digital Signal Processing 2

  • 7/29/2019 Mat Lab practicals for final year

    3/73

    SIES College of Arts, Commerce & Science, Nerul

    10) XLABEL :-

    SYNTAX :-

    X-axis label :-XLABEL('text') adds text beside the X-axis on the current axis.

    11) YLABEL :-

    SYNTAX:Y-axis label:

    YLABEL('text') adds text beside the Y-axis on the current axis.

    12) INPUT :-

    SYNTAX :-

    Prompt for user input :-

    R = INPUT('How many apples') gives the user the prompt in the textstring and then waits for input from the keyboard.The input can be any

    MATLAB expression, which is evaluated, using the variables in the currentworkspace, and the result returned in R. If the user presses the returnkey without entering anything, INPUT returns an empty matrix.

    13) AXIS :-

    SYNTAX :-Control axis scaling and appearance :-

    AXIS([XMIN XMAX YMIN YMAX]) sets scaling for the x- and y-axes onthe current plot.AXIS([XMIN XMAX YMIN YMAX ZMIN ZMAX]) sets the scaling for thex-, y- and z-axes on the current 3-D plot.

    AXIS([XMIN XMAX YMIN YMAX ZMIN ZMAX CMIN CMAX]) sets thescaling for the x-, y-, z-axes and color scaling limits on the current axis .

    14) CONV :-

    SYNTAX :-

    Convolution and polynomial multiplication :-

    C = CONV(A, B) convolves vectors A and B. The resulting vector islength LENGTH(A)+LENGTH(B)-1.

    If A and B are vectors of polynomial coefficients, convolving them

    is equivalent to multiplying the two polynomials.

    15) SUBPLOT:-

    Digital Signal Processing 3

  • 7/29/2019 Mat Lab practicals for final year

    4/73

    SIES College of Arts, Commerce & Science, Nerul

    SYNTAX :-

    Create axes in tiled positions :-.H = SUBPLOT(m,n,p), or SUBPLOT(mnp), breaks the Figure windowinto an m-by-n matrix of small axes, selects the p-th axes for the current

    plot, and returns the axis handle. The axes are counted along the top

    row of the Figure window, then the second row, etc. For example,

    SUBPLOT(2,1,1), PLOT(income)SUBPLOT(2,1,2), PLOT(outgo)

    16) FIR1 :-

    SYNTAX :-

    FIR filter design using the window method :-

    B = FIR1(N,Wn) designs an N'th order lowpass FIR digital filter andreturns the filter coefficients in length N+1 vector B.The cut-off frequencyWn must be between 0 < Wn < 1.0, with 1.0 corresponding to half thesample rate. The filter B is real and has linear phase.The normalized gain

    of the filter at Wn is -6 dB.

    B = FIR1(N,Wn,'high') designs an N'th order highpass filter.You can also use B = FIR1(N,Wn,'low') to design a lowpass filter.

    If Wn is a two-element vector, Wn = [W1 W2], FIR1 returns an

    order N bandpass filter with passband W1 < W < W2. You can also

    specify B = FIR1(N,Wn,'bandpass'). If Wn = [W1 W2],B = FIR1(N,Wn,'stop') will design a bandstop filter.

    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 2^ielse

    disp('Not a proper number') ;break ;

    end ;end ;

    Digital Signal Processing 55

  • 7/29/2019 Mat Lab practicals for final year

    56/73

    SIES College of Arts, Commerce & Science, Nerul

    Output :-

    Enter Length Of FFT : - 8Input The Sequence [1 2 3 4 5 6 7 8]FFT Sequence

    Columns 1 through 6

    36.0000 -4.0000 + 9.6569i -4.0000 + 4.0000i -4.0000 +1.6569i -4.0000-4.0000 - 1.6569i

    Columns 7 through 8

    -4.0000 - 4.0000i -4.0000 - 9.6569i

    1 2 3 4 5 6 7 80

    2

    4

    6

    8

    n---->

    Amplitude---->

    Original Signal

    -5 0 5 10 15 20 25 30 35 40-10

    -5

    0

    5

    10

    n---->

    Amplitu

    de---->

    FFT Signal

    Digital Signal Processing 56

  • 7/29/2019 Mat Lab practicals for final year

    57/73

    SIES College of Arts, Commerce & Science, Nerul

    PRACTICAL 14

    Radix 2 DIT FFT Algorithm

    Problem Statement :-

    Write a program in MATLAB to implement Radix2 Decimation In

    Time (DIT) FFT Algorithm.

    (MATLAB) Functions used:

    Y = fft(X)

    Returns the Discrete Fourier Transform (DFT) of vector X computed with aFast Fourier Transform (FFT) algorithm.

    Source Code:

    % RADIX-2 DIT FFT ALGORITHM

    clc ;clear all ;close all ;

    format short ;

    N = input('Please enter the length of the input sequence : ') ;for i = 1:N+1

    if N == 2^ix = input('Please enter the input sequence : ') ;

    y = bitrevorder(x) ;

    Z = fft(y,N) ;disp('Input Sequence After Reversal') ;disp(y) ;disp('FFT Sequence') ;disp(Z) ;

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

    xlabel('N') ;ylabel('Amplitude') ;

    title('Input Sequence') ;subplot(2,1,2) ;stem(Z) ;xlabel('N') ;

    ylabel('Amplitude') ;title('FFT of the Input Sequence') ;break ;

    elseif N > 2^i

    elsedisp('Not a proper number') ;

    break ;

    end ;end;

    Digital Signal Processing 57

  • 7/29/2019 Mat Lab practicals for final year

    58/73

    SIES College of Arts, Commerce & Science, Nerul

    Output :-

    Enter Length Of FFT : - 8Input The Sequence [1 2 3 4 5 6 7 8]

    1 5 3 7 2 6 4 8

    FFT SequenceColumns 1 through 6

    36.0000 -4.0000 + 9.6569i -4.0000 + 4.0000i -4.0000 +1.6569i -4.0000 -4.0000 - 1.6569i

    Columns 7 through 8

    -4.0000 - 4.0000i -4.0000 - 9.6569i

    1 2 3 4 5 6 7 80

    2

    4

    6

    8

    n---->

    Amplitude---->

    Original Signal

    -5 0 5 10 15 20 25 30 35 40-10

    -5

    0

    5

    10

    n---->

    Amplitude---->

    FFTSignal

    Digital Signal Processing 58

  • 7/29/2019 Mat Lab practicals for final year

    59/73

    SIES College of Arts, Commerce & Science, Nerul

    PRACTICAL-15

    %POWER SPECTRUM

    clc;

    clear all;

    close all;t=0:0.01:0.5;d=randn(size(t));f=input('enter the freq.');x1=cos(2*pi*f*t)+randn(size(t));

    x=x1+d(1,:);subplot(3,1,1)

    spectrum(x1)subplot(3,1,2)spectrum(x)subplot(3,1,3)

    spectrum(d(1,:))

    Digital Signal Processing 59

  • 7/29/2019 Mat Lab practicals for final year

    60/73

    SIES College of Arts, Commerce & Science, Nerul

    Output :-

    Digital Signal Processing 60

  • 7/29/2019 Mat Lab practicals for final year

    61/73

    SIES College of Arts, Commerce & Science, Nerul

    PRACTICAL-16

    %REMEZ ALGORITHM

    clc;

    clear all;close all;

    F=input('enter the freq edges');A=input('amplitude');

    N=input('order of filter');B=remez(N,F,A);

    [H,w]=freqz(B,1,N+1);

    Digital Signal Processing 61

  • 7/29/2019 Mat Lab practicals for final year

    62/73

    SIES College of Arts, Commerce & Science, Nerul

    hm=abs(H);n=0:1:N;

    stem(n,hm);

    Output :-

    Digital Signal Processing 62

  • 7/29/2019 Mat Lab practicals for final year

    63/73

    SIES College of Arts, Commerce & Science, Nerul

    PRACTICAL -17

    %TWO DIMENSIONAL LINEAR CONVOLUTION

    Digital Signal Processing 63

  • 7/29/2019 Mat Lab practicals for final year

    64/73

    SIES College of Arts, Commerce & Science, Nerul

    clc;

    close all;clear all;xn=input('Enter 1st sequence');hn=input('Enter 2nd sequence');

    yn=conv2(xn,hn)

    subplot(3,1,1)stem(xn);title('Input Sequence')subplot(3,1,2)stem(hn);

    title('Input Response')subplot(3,1,3)

    stem(yn);title('Output Sequence')

    Output :-

    Digital Signal Processing 64

  • 7/29/2019 Mat Lab practicals for final year

    65/73

    SIES College of Arts, Commerce & Science, Nerul

    PRACTICAL-18

    Digital Signal Processing 65

  • 7/29/2019 Mat Lab practicals for final year

    66/73

    SIES College of Arts, Commerce & Science, Nerul

    %Program for 1-Dimensional

    clc;close all;clear all;xn=input('Enter the 1st sequence');

    hn=input('Enter the 2nd sequence');

    a=length(xn);n=-a+1:1:a-1;b=length(hn);c=max(a,b);n1=-c+1:1:c-1;

    ra=xcorr(xn)rc=xcorr(xn,hn)

    subplot(2,1,1)plot(n,ra,'g');title('Auto correlation of xn');subplot(2,1,2)

    plot(n,rc,'m');title('Cross correlation of xn and hn');

    %Program for 2-Dimensional

    clc;close all;clear all;xn=input('Enter 1st sequence');

    hn=input('Enter 2nd sequence');

    ra=xcorr2(xn)rc=xcorr2(xn,hn)

    Output :-

    Digital Signal Processing 66

  • 7/29/2019 Mat Lab practicals for final year

    67/73

    SIES College of Arts, Commerce & Science, Nerul

    Digital Signal Processing 67

  • 7/29/2019 Mat Lab practicals for final year

    68/73

    SIES College of Arts, Commerce & Science, Nerul

    PRACTICAL-19

    %STABILITY

    Digital Signal Processing 68

  • 7/29/2019 Mat Lab practicals for final year

    69/73

    SIES College of Arts, Commerce & Science, Nerul

    clc;

    clear all;close all;xn=input('enter input seq.');hn=input('enter impulse response');

    yn=conv(xn,hn)

    d=sum(yn)if d

  • 7/29/2019 Mat Lab practicals for final year

    70/73

    SIES College of Arts, Commerce & Science, Nerul

    Digital Signal Processing 70

  • 7/29/2019 Mat Lab practicals for final year

    71/73

    SIES College of Arts, Commerce & Science, Nerul

    PRACTICAL-20

    Digital Signal Processing 71

  • 7/29/2019 Mat Lab practicals for final year

    72/73

    SIES College of Arts, Commerce & Science, Nerul

    %Bit Reversal Algorithm

    clc;close all;clear all

    x=input('enter the seq');

    x0=x(1,1);x1=x(1,2);x2=x(1,3);x3=x(1,4);X0=x0+x1+x2+x3;

    X1=x0-x1*j-x2+x3*j;X2=x0-x1+x2-x3;

    X3=x0+x1*j-x2-x3*j;Xk=[X0 X1 X2 X3];x=[0:3][y,I]=bitrevorder(x)

    a=dec2bin(x)b=dec2bin(y)p=sprintf('%s%s%s%s\n','[linear index]','[bits]','[bitreve]','[index]');disp(p);

    s=sprintf('%d\t%d%d\t%d%d\t%d',x(1:1),a(1,:),b(1,:),I(1:1));disp(s);

    Output :-

    Digital Signal Processing 72

  • 7/29/2019 Mat Lab practicals for final year

    73/73

    SIES College of Arts, Commerce & Science, Nerul