bsl lab manual

Upload: kranthi-kumar-reddy

Post on 07-Apr-2018

235 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 BSL Lab Manual

    1/67

  • 8/4/2019 BSL Lab Manual

    2/67

    >> X=det(A)

    X =

    0

    Generation of various signals

    Matlab code for the generation of Unit Step Sequencen = input('enter the n value');

    t = 0:1:n-1;

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

    stem(t,y1);

    ylabel('amp');xlabel('(b)n');

    Output:

    Enter the n value 10

    Matlab code for the generation of Unit impulse Sequence

  • 8/4/2019 BSL Lab Manual

    3/67

    clc; %clear the data

    t = -2:1:2;

    y=[zeros(1,2),ones(1,1),zeros(1,2)]; %Create an array of all zeros and onessubplot(2,2,1);

    stem(t,y); %function displays two-dimensional discrete sequence data.

    ylabel('amp'); %label the Yaxesxlabel('(a)n'); %label the Xaxes

    1a) Matlab code to generate sinusoidal(continous , discrete)

    clc;clear all;close all;

    t=0:0.01:1;

    x=sin(2*pi*2*t);subplot(2,1,1);

    plot(t,x);

    xlabel('--> time');ylabel('--> magnitude');

    title(' continuoes time cose wave ');

    grid;

    n=0:0.05:1;x=sin(2*pi*2*n);

    subplot(2,1,2);

    stem(n,x,'r');xlabel('--> time');

    ylabel('--> magnitude');

    title('discrete time cose wave');grid;

  • 8/4/2019 BSL Lab Manual

    4/67

    Matlab code to generate cosine(continous , discrete)

    clc;

    clear all;

    close all;t=0:0.01:1;

    x=cos(2*pi*2*t);subplot(2,1,1);plot(t,x);

    xlabel('--> time');

    ylabel('--> magnitude');title(' continuoes time cose wave ');

    grid;

    n=0:0.05:1;x=cos(2*pi*2*n);

    subplot(2,1,2);

    stem(n,x,'r');

    xlabel('--> time');ylabel('--> magnitude');

    title('discrete time cose wave');

    grid;

  • 8/4/2019 BSL Lab Manual

    5/67

    Matlab code to generate sawtooth signal(continous, discrete)

    clc;clear all;

    close all;

    t=-1:0.001:1;x=sawtooth(2*pi*2*t);

    subplot(2,1,1);

    plot(t,x);

    xlabel('--> time');ylabel('--> magnitude');

    title(' continuoes time sawtooth wave ');

    grid;

    n=-1:0.05:1;x=sawtooth(2*pi*2*n);

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

    xlabel('--> time');

    ylabel('--> magnitude');title('discrete time sawtooth wave');

    grid;

  • 8/4/2019 BSL Lab Manual

    6/67

    Matlab code to generate triangular waveform

    fs = 10000; %Set the sampling frequency fs as 10000.

    t = 0 : 1/fs : 1.5; %Generate a vector of 15001 samples for t with a

    value between 0 & 1.5

    %with an increment of 0.0001x = sawtooth (2 * pi * 50 * t , 0.5);%Generate a Triangular wave of

    frequency 50 Hz, by calling the

    %MATLAB built-in function sawtooth with 0.5 as the

    duty cycle parameter.

    plot(t,x); axis ( [ 0 0.1 -1 1 ] ); %Plot the graph. Adjust the X and Y

    coordinates to view a few cycles of the triangular wave.

    xlabel ('Time Index t (sec.)');

    ylabel ('Amplitude');%Label the X and Y axes

    title ('Triangular Wave Signal Sequence'); %Add an appropriate title to thegraph

  • 8/4/2019 BSL Lab Manual

    7/67

    clc;

    clear all;

    close all;t=0:0.001:1;

    x=square(2*pi*2*t);

    subplot(2,1,1);plot(t,x);

    xlabel('--> time');

    ylabel('--> magnitude');

    title(' continuoes time square wave ');axis([0 1 -2 2]);

    grid;

    n=0:0.1:1;x=square(2*pi*2*n);

    subplot(2,1,2);

    stem(n,x);xlabel('--> time');

    ylabel('--> magnitude');

    title('discrete time square wave');grid;

  • 8/4/2019 BSL Lab Manual

    8/67

    APERIODIC SIGNALS

    clc;

    clear all;close all;t=-5:0.01:5;

    %Unit impulse

    x1=1;

    x2=0;

    x=x1.*(t==0)+x2.*(t~=0);subplot(3,3,1);

    plot(t,x,'r');

    xlabel('->t');

    ylabel('->x(t)');axis([-5 5 -1 2]);

    title('Unit impulse signal');

    grid;

    %unit step

    x=x1.*(t>=0)+x2.*(t

  • 8/4/2019 BSL Lab Manual

    9/67

    plot(t,x);

    xlabel('t');

    ylabel('x(t)');axis([-5 5 -1 2]);

    title('unit step signal');

    grid;

    %ramp signal

    x1=t;x2=0;

    x=x1.*(t>=0)+x2.*(t=0)+x2.*(t0)+x2.*(t==0)+x3.*(t

  • 8/4/2019 BSL Lab Manual

    10/67

    subplot(3,3,6);

    plot(t,x);

    xlabel('t');ylabel('x(t)');

    axis([-5 5 -2 2]);

    grid;title('signum signal');

    %gaussian pulse

    x=exp(-t.^2);subplot(3,3,7);

    plot(t,x);

    xlabel('t');

    ylabel('x(t)');grid;

    title('gaussian pulse');

    % decreasing exponential signal

    x=exp(-0.2*t);subplot(3,3,8);

    plot(t,x);xlabel('t');

    ylabel('x(t)');

    title('decreasing exponential signal');

    grid;

    % increasing exponential signal

    x=exp(0.2*t);

    subplot(3,3,9);plot(t,x);

    xlabel('t');

    ylabel('x(t)');title('increasing exponential signal');

    grid;

  • 8/4/2019 BSL Lab Manual

    11/67

    3.Operations on signals

    3a) MATLAB CODE FOR SIGNAL ADDITION

    clc; % Clear Command Window

    clear all ; %Clears all the previously defined variables

    t = 0 : 0.01 : 30; %Generate a vector of 3001 samples for t with a value%between 0 & 30 with an increment of 0.01

    x1 = sin( 2 * pi * 1/3 * t ); %Generate the sinusoidal signal of frequency 1/3 Hz

    x2 = sin( 2 * pi * 1/5 * t ); % Generate the sinusoidal signal of frequency 1/5 Hzy = x1 + x2; % Perform sample by sample vector addition on x1 and x2

    subplot( 3,1,1 ); plot( t , x1 ); %Divide the graph space into three parts.

    grid; %Plot signal 1 in the first part with grid.xlabel( ' Time Index t (sec.) ' );

    ylabel( ' x1(t) ' ); %Label the X and Y axes

    title( ' Signal 1 : Sine Wave of Frequency 1/3 Hz ' ); %Add a title to the graph

    subplot( 3,1,2 ); plot( t , x2 );

  • 8/4/2019 BSL Lab Manual

    12/67

    grid; %Plot the graph of signal 2 in the second part with grid.

    xlabel( ' Time Index t (sec.) ' );

    ylabel( ' x2(t) ' ); %Label the X and Y axestitle( ' Signal 2 : Sine Wave of Frequency 1/5 Hz ' ); %Add a title to the graph

    subplot( 3,1,3 ); plot( t , y );

    grid; % Plot the graph of resultant signal after summation, in the third part with grid.xlabel( ' Time Index t (sec.) ' );

    ylabel( ' y(t) = x1(t) + x2(t) ' ); %Label the X and Y axes

    title( ' Resultant Signal : Signal 1 + Signal 2 ' ); %Add A title to the graph

    3b MATLAB CODE FOR SIGNAL MULTIPLICATION

    clc; clear all; % Clear Command Window and all the previously defined variables

    t = 0 : 0.01 : 30; % Generate a vector of 3001 samples for t with a%value between 0 & 30 with an increment of 0.01

    x1 = sin( 2 * pi * 1/3 * t ); %Generate the sinusoidal signal of frequency 1/3 Hz

    x2 = sin( 2 * pi * 1/5 * t ); %Generate the sinusoidal signal of frequency 1/5 Hzy = x1 .* x2; %Perform sample by sample multiplication on x1 and x2.

    subplot( 3,1,1 ); plot( t , x1 ); %Divide the graph space into three parts.grid; % Plot signal 1 in the first part, with grid.xlabel( ' Time t (sec.) ' );

    ylabel( ' x1(t) ' ); %Label the X and Y axes

    title( ' Signal 1 : Sine Wave of Frequency 1/3 Hz ' ); %Add an appropriate title to the graph

    subplot( 3,1,2 ); plot( t , x2 );grid; %Plot the graph of signal 2 in the second part.Include the grid.

    xlabel( ' Time Index t (sec.) ' ); ylabel( ' x2(t) ' ); %Label the X and Y axes

  • 8/4/2019 BSL Lab Manual

    13/67

    title( ' Signal 2 : Sine Wave of Frequency 1/5 Hz ' ); %Add an appropriate title to the graph

    subplot( 3,1,3 ); plot( t , y );

    grid; %Plot the graph of resultant signal after summation, in the thirdpart.

    xlabel( ' Time Index t (sec.) ' );

    ylabel( ' y(t) = x1(t) .* x2(t) ' ); %Label the X and Y axestitle( ' Resultant Signal : Dot Product of Signal 1 and Signal 2 ' );

    3c) MATLAB CODE FOR SIGNAL SCALING

    clc; % Clear Command Windowclear all; % Clear all the previously defined variablesN = input ( ' Type the desired length of the signal '); %Input a number for N, thelength of the signalt = 0 : 0.01 : N-1; %Generate a vector t of N samplesx = sin( 2 * pi * 1/5 * t ); % Generate a sine wave for exampleA = input ( ' Please input a SCALE FACTOR(>1 or a +ve fraction) for A ');%Input a No. greater than unity or a positive fraction, using which the givensignal is to be scaledy = A * sin( 2 * pi * 1/5 * t ); %Scale the signal by the scalefactor A

    subplot( 2,1,1 ); plot( t , x ); % Divide the graph space into twoparts, Plot x in first part.axis ( [ 0 N-1 -A A ] ); grid; %Aadjust the axes appropriatelyand use grid.xlabel( ' Time Index t (sec.) ' );ylabel( ' x(t) ' ); %Label the X and Y axestitle( ' Signal 1 : Sine Wave of Frequency 1/5 Hz ' ); %Add a title to the graph

  • 8/4/2019 BSL Lab Manual

    14/67

    subplot( 2,1,2 ); plot( t , y ); % Plot the scaled signal y in secondpart.axis ( [ 0 N-1 -A A ] ); grid; %adjust the axes appropriately anduse grid.xlabel( ' Time Index t (sec.) ' );ylabel( ' y(t) ' ); %Label the X and Y axestitle( ' Signal 2 : Scaled Version of Signal 1 ' );%Add a title to the graph

    output:Type the desired length of the signal 10Please input a SCALE FACTOR(>1 or a +ve fraction) for A 2

    3d) MATLAB CODE FOR SIGNAL SHIFTINGclear all; clc ; %Clear Command Window and all the previously defined variablesx = input( ' Type the samples of a signal ' ); %Generate any arbitrary signal vector x by

    %typing the samples

    n1 = input( ' Type the time origin at which the first signal sample is ' );

    %Considering the X-axis range from n1 to n2 for the signal x, enter the value ofn1, where thefirst signal sample begins.

    N = length(x); %Find the length of the signal sequence

    n2 = n1 + N -1; %Find the time index of the last sample of the signal xn = n1 : n2; %Set the range for X-axis from n1 to n2

    disp( ' The program will now ask for the amount of shift ' );

    %Display on screen the statement in parenthesisdisp( ' Enter a POSITIVE number for delay & NEGATIVE number for Advancement ' );

    %Display on screen the statement in parenthesis

    disp( ' Press any key to continue ' ); pause; %Display on screen the statement in parenthesis

    %and wait until the above two statements are read.d = input( ' Enter the desired amount of shift of the signal ' ); %Input the value of time shift

    nn = n + d; xs = x; %Shift the time index by the amount of shift and leave the original

    %signal as it is by just assigning it to a different variable.

  • 8/4/2019 BSL Lab Manual

    15/67

    subplot(2,1,1); stem( n , x ); %Plot the original signal as stems in the First part of the graphic.

    xlabel( ' Time Index n (sec.) ' );

    ylabel( ' x(n) ' ); %Label the X and Y axestitle( ' Original Signal ' ); %Add a title to the graph

    Subplot(2,1,2); stem( nn , x ); %Plot the shifted signal in the second part of the

    graphic.xlabel( ' Time Index n (sec.) ' ); ylabel( ' xs(n) ' ); %Label the X and Y axes

    title( ' Time Shifted Signal ' ); %Add a title to the graph

    output:

    Type the samples of a signal 7

    Type the time origin at which the first signal sample is 0

    The program will now ask for the amount of shiftEnter a POSITIVE number for delay & NEGATIVE number for Advancement

    Press any key to continue

    Enter the desired amount of shift of the signal 4

    3e) MATLAB CODE FOR SIGNAL FOLDING

    clc; % Clear Command Window

    clear all ; %Clear all the previously defined variables

    t = 0 : 0.1 : 5; %Generate a vector n using a step size of 0.1

    x = 0.5 * t; %Generate a ramp as an example signallx = length(x); %Find the length of the signal sequence

    nx = 0 : lx-1; %Set a range for positive X-axisxf = fliplr( x ); %Perform the folding operation of signal x by using the built-in function

    fliplr

    nf = -fliplr( nx ); %Fold the vector nx to get a mirror image vector nf.%This will set the range of negative X-axis for the folded signal

    subplot(2,1,1); % Divide the graph space into two parts

  • 8/4/2019 BSL Lab Manual

    16/67

    stem( nx , x ); % Plot the original signal in the first part as stems

    xlabel( ' nx ' );

    ylabel( ' x(nx) ' ); %Label the X and Y axestitle('Original Signal' ); %Add a title to the graph

    subplot(2,1,2);

    stem( nf , xf ); %Plot the folded signal in the second part.xlabel( ' nf ' );

    ylabel( ' xf(nf) ' ); %Label the X and Y axes

    title( ' Folded Signal ' ); %Add a title to the graph

    The

    3f) MATLAB CODE FOR COMPUTATION OF ENERGY

    n = 0:1:20; %Create a vector n of 101 samples

    x = (1/2) .^ n; %Create the signal x(n) = (1/2)n u(n)

    stem(n,x); axis([0 25 0 1]); %Plot the signal as stems and adjust the axes appropriately.disp( ' The calculated Energy E of the signal is '); %Display the message in brackets on screen

    E = sum(abs(x).^2) %Calculate the energy of the signaldisp( ' The Theoretical Energy of the signal is ' ); %Display the message in brackets on screen

    E_Theory = 4/3 ; %Calculate the theoretical value of the energy of the signal

    Output:The calculated Energy E of the signal is

    E =

    1.3333

  • 8/4/2019 BSL Lab Manual

    17/67

    3g) MATLAB CODE FOR COMPUTATION OF POWER

    N = input( 'Type a value for N ');% Type a value for N, which is the one sided length of the

    signal.t = -N : .0001 : N; %Create a vector t that will act as a time index

    x = cos (2 * pi * 50 * t ) .^ 2; %Generate the signal x = cos 2 (2 f t)

    disp( ' The calculated Power P of the signal is ' ) %Display the message on the screen

    P = sum(abs(x).^2)/length(x) %Calculate the power of the signal x anddisplay its value on screen.

    plot(t,x); axis ([0 0.1 0 1]); %Plot the signal by adjusting the axes appropriately.disp( ' The Theoretical Power of the signal is ' ) %Display the message in brackets on screen

    P_Theory = 3/8 %Show the theoretical value of the power of the signal on screen.

    Output:

    Type a value for N 20

    The calculated Power P of the signal is

    P =

    0.3750The Theoretical Power of the signal is

    P_Theory =

  • 8/4/2019 BSL Lab Manual

    18/67

    0.3750

    4. Finding the even and odd parts of a signal or sequence and real and imaginary

    parts of a signal4a) MATLAB CODE TO FIND WHETHER A SIGNAL IS EVEN OR ODD AND FIND

    ITS EVEN AND ODD PARTS:

    clc;clearall;close all;tmin=-4;tmax=4; dt=0.1;

    t=tmin:dt:tmax;

    %x1=exp(2*t);%x2=exp(-2*t);

    x1=sin(2*pi*0.2*t);

    x2=sin(-(2*pi*0.2*t));

    %x1=cos2*pi*0.2*t);

    %x2=cos(-(2*pi*0.2*t));if(x2==x1)

    disp(' The signal is even signal ');

    elseif(x2==(-x1))

    disp(' The signal odd signal ');else

    disp('The signal is neither even nor odd signal');

    endend

    xe=(x1+x2)/2;

    xo=(x1-x2)/2;ymin=min([min(x1),min(x2),min(xe),min(xo)]);

  • 8/4/2019 BSL Lab Manual

    19/67

    ymax=max([max(x1),max(x2),max(xe),max(xo)]);

    subplot(2,2,1); plot(t,x1); axis([tmin tmax ymin ymax]);

    xlabel('t'); ylabel('x1(t)'); title('signal x(t)');grid;

    subplot(2,2,2); plot(t,x2); axis([tmin tmax ymin ymax]);

    xlabel('t'); ylabel('x2(t)'); title('signal x(-t)');grid;

    subplot(2,2,3); plot(t,xe); axis([tmin tmax ymin ymax]);

    xlabel('t'); ylabel('xe(t)'); title('even part of x(t)');grid;

    subplot(2,2,4); plot(t,xo); axis([tmin tmax ymin ymax]);

    xlabel('t'); ylabel('xo(t)'); title('odd part of x(t)');

    grid;

    4b) MATLAB CODE TO FIND THE REAL AND IMAGINARY PARTS OF A SIGNAL

    clc;

    clearall;

  • 8/4/2019 BSL Lab Manual

    20/67

    x = [0, 2+j*4, -3+j*2, 5-j*1, -2-j*4, -j*3, 0] %Create the complex signal

    %vector x

    n = -3 : 3; %Set a range for X-axis using vector nxc = conj( x ); %Compute the complex conjugate of x

    xc_folded = fliplr(xc); %Fold the signal xc using the built-in %function 'fliplr'

    xc_even = 0.5 * [x + xc_folded]; %Compute the even part of xcxc_odd = 0.5 * [x - xc_folded]; %Compute the odd part of xc

    figure; %Open a new figure window (Figure 1)

    subplot(2,1,1) ; stem(n , real(x) ) %Plot the real part of complex signal x in the 1st half ofFigure1.

    title('Real part of Complex Signal x(n)')

    xlabel ( ' n ' );

    ylabel ('Magnitude of Real [x(n)]');

    subplot(2,1,2); stem( n , imag(x) ) % Plot the imaginary part of complex

    %signal x in the 2nd half of Figure 1.

    title('Imaginary part of Complex Signal x(n)')%Add A title to the graphxlabel ( ' n ' );

    ylabel ('Magnitude of Imag[ x(n)]');figure; %Open a new figure window (Fig 2)

    subplot(2,1,1) ; stem(n , real(xc_even)); %Plot the real part of even signal

    %xc_even in the 1st half of Fig 2.

    title('Real part of even signal xc(n)');xlabel ( ' n ' );

    ylabel ('Magnitude of Real (xc-even)');

    subplot(2,1,2) ; stem( n , imag(xc_even) ) %Plot the imaginary part of even%signal xc_even in the 2nd half of %Figure 2.

    title('Imaginary part of even signal xc(n)')

    xlabel ( ' n ' );ylabel ('Magnitude of Imag (xc-even)');

    figure; %Open a new figure window (Fig3)

    subplot(2,1,1) ; stem( n , real(xc_odd)) %Plot the real part of odd signal%xc_odd in the 1st half of Fig 3.

    title('Real part of odd signal xc(n)')

    xlabel ( ' n ' );

    ylabel ('Magnitude of Real (xc-odd)');subplot(2,1,2);stem(n,imag(xc_odd)) %Plot the imaginary part of odd signal

    % xc_odd in the 2nd half of Fig 3.

    title('Imaginary part of odd signal xc(n)')xlabel ( ' n ' );

    ylabel ('Magnitude of Imag (xc-odd)');

  • 8/4/2019 BSL Lab Manual

    21/67

  • 8/4/2019 BSL Lab Manual

    22/67

  • 8/4/2019 BSL Lab Manual

    23/67

    5.Convolution between signals

    MATLAB Code Explanation

    x = input (' type the samples of input signal x(n) '); %Enter the values of Input Signal x(n)

    nx1 = input( ' type the time origin of the first sample of x(n) ' );%Enter the time origin of the firstsample of x(n)

    NX = length(x); % Find the length of the signal x(n)

    nx2 = nx1 + NX -1; %Find the upper limit of X-axis range

    nx = nx1 : nx2; %Set X-axis range for the graph of x(n)figure;

    stem(nx,x) % Open a new figure window (Figure 1) and plot a stem graph of x(n)

    title (' Input Signal x(n) '); %Add an appropriate title to the graphxlabel ('Time Index n');

    ylabel ( ' x(n) ' ); %Label the X and Y axes

    h = input (' type the samples of Impulse response signal h(n) '); %Enter the values of Impulseresponse Signal h(n)

    nh1 = input( ' type the time origin of the first sample of h(n) ' ); %Enter the time origin of the

    first sample of h(n)NH = length(h); %Find the length of the signal h(n)

  • 8/4/2019 BSL Lab Manual

    24/67

    nh2 = nh1 + NH -1; %Find the upper limit of X-axis range

    nh = nh1 : nh2; %Set X-axis range for the graph of h(n)

    figure; stem(nh,h) %Open a new figure window (Figure 2) and plot a stem graph of h(n)title (' Impulse Response Signal h(n) ');%Add an appropriate title to the graph

    xlabel ('Time Index n');

    ylabel ( ' h(n) ' ); %Label the X and Y axesy = conv ( x , h ); %Compute the LTI system output y(n) using the built-in MATLAB

    function 'conv'

    NY = NX + NH -1; %Find the length of the signal y(n)ny1 = nx1 + nh1; %Find the time origin at which the first signal sample of y(n) is available

    ny = ny1 : NY-abs(ny1)-1; %Set X-axis range for the graph of y(n)

    figure; stem(ny,y) % Open a new figure window (Figure 3) and plot a stem graph of y(n)

    title (' Output Signal y(n) = x(n)*h(n)'); %Add an appropriate title to the graphxlabel ('Time Index n'); ylabel ( ' y(n) ' ); %Label the X and Y axes

    disp ('Time Index of x(n) = '); disp(nx); %Display on screen the message in parentheses and

    the time index of x(n)

    disp ('Input Signal Sequence x(n) = ');disp(x);

    disp ('Time Index of h(n) = ');disp(nh);

    disp ('Impulse Response Sequence h(n) = ');

    disp(h);

    disp ('Time Index of y(n) = ');disp(ny);

    disp ('output Signal Sequence y(n)= ');

    disp(y);

    Output:

  • 8/4/2019 BSL Lab Manual

    25/67

  • 8/4/2019 BSL Lab Manual

    26/67

    7 a)MATLAB code for verification of linearity property of a discrete time system

    x1=input(type the samples of x1);%enter the values of first input signal x1.x2=input(type the samples of x2); % enter the values of second input signal x2

    if(length(x1)~= length(x2) )% check if the lengths of two vectors x1 and x2 are same

    disp(ERROR :Lengths of x1&x2 are different); %If the sizes are different display the errormessage

    return; %get out of the program, as you cannot proceed further, and go to the command promptend;

    h=input(type the samples of h); % enter the samples of impulse response h of the LTI systemN=length(x1)+length(h)-1; %find the length of the o/p sequence

    disp(length of the output signal will be );

    disp(N);a1=input(the scale factor a1 is); %Enter the scale factor a1 of the path of x1

    a2=input(the scale factor a2 is); %Enter the scale factor a2 of the path of x2

    x=a1*x1+a2*x2; % find the intermediate o/p x of system 1 as a1x1 + a2 x 2

    y01=conv(x,h); %find the final o/p of system 1 as the convolution of x and h.

    y1=conv(x1,h); %find the 1st

    intermediate o/p y1 of system 1 as the convolution of x1 andh.

    y1s=a1*y1; %scale y1 with the scaling factor a1

    y2=conv(x2,h); %find the 1st intermediate o/p y1 of system 2 as the convolution of x1 andh.

    y2s=a2*y2; %scale y2 with the scaling factor a2

  • 8/4/2019 BSL Lab Manual

    27/67

    y02=y1s+y2s; % find the final o/p of system 2 by adding the two intermediate outputs.

    disp(input signal x1 is);

    disp(x1);

    disp(input signal x2 is);disp(x2);

    disp(Output sequence y01 is);

    disp(y01);disp(Output sequence y02 is);

    disp(y02);

    if(y01==y02)

    disp(y01=y02.Hence the LTI system is LINEAR);end;

    output:

    type the samples of x1[2 5 3 7 -1]

    type the samples of x2[-4 6 -2 3 1]

    type the samples of h[1 -3 4 -2 7 2 -1 3]

    length of the output signal will be

    12

    the scale factor a1 is3

    the scale factor a2 is2input signal x1 is

    2 5 3 7 -1

    input signal x2 is

    -4 6 -2 3 1

    Output sequence y01 is-2 33 -84 124 -130 286 33 168 123 -14 82

    -3

    Output sequence y02 is

  • 8/4/2019 BSL Lab Manual

    28/67

    -2 33 -84 124 -130 286 33 168 123 -14 82

    -3

    y01=y02.Hence the LTI system is LINEAR>>

    7 b)MATLAB code for verification of time invariance property of a discrete time system

    x=input(type the samples of x(n)); %enter the values of the input signal x(n)

    h=input(type the samples of h(n)); %enter the values of the impulse response h(n) of system H

    y=conv(x,h); %find the output signal y(n) as the convolution of x(n)and h(n)disp(enter a POSITIVE number for delay);

    d=input(desired delay of the signal is); %enter the desired shift by which the signal x(n) needs

    to be delayedxd=[zeros(1,d),x]; %generate the delayed input signal xd(n) by padding x(n) with d zeros

    nxd=0:length(xd)-1; %set the x-axis range for input signals graphyd=conv(xd,h); %convolve the delayed input signal xd(n) with impulse response h(n) to produce

    a new output signalyd(n)nyd=0:length(yd)-1; %set the x-axis range for output signals graph

    disp(original input signal x(n) is);

    disp(x);disp(delayed input signal xd(n) is);

    disp(xd);

    disp(original output signal y(n) is);disp(y);

    disp(delayed output signal y(n) is);

    disp(yd);xp=[x,zeros(1,d)]; %append d zeros to vector x so as to plot x with xd on the same graphfigure;

    subplot(2,1,1);

    stem(nxd,xp);grid;

    xlabel(time index n);

    ylabel(x(n));title(original input signal x(n));

    subplot(2,1,2);

    stem(nxd,xd);

    grid;xlabel(time index n);

    ylabel(xd(n));

    title(delayed input signal xd(n));yp=[y,zeros(1,d)]; %append d zeros to vector y so as to plot y with yd on the same graph

    figure

    subplot(2,1,1);stem(nyd,yp);

  • 8/4/2019 BSL Lab Manual

    29/67

    grid;

    xlabel(time index n);

    ylabel(y(n));title(original output signal y(n));

    subplot(2,1,2);

    stem(nyd,yd);grid;

    xlabel(time index n);

    ylabel(yd(n));title(delayed output signal yd(n));

    type the samples of x(n)[0 1 1 1 2 2 2]

    type the samples of h(n)[1 3 5 2 1]

    enter a POSITIVE number for delaydesired delay of the signal is4

    original input signal x(n) is

    0 1 1 1 2 2 2

    delayed input signal xd(n) is

    0 0 0 0 0 1 1 1 2 2 2

    original output signal y(n) is

    0 1 4 9 12 16 21 21 16 6 2

    delayed output signal y(n) is

    Columns 1 through 14

    0 0 0 0 0 1 4 9 12 16 21 21 16 6

    Column 15

    2

  • 8/4/2019 BSL Lab Manual

    30/67

    8. Computation of unit sample , unit step responses of the given LTI system

    and verifying its physical realizability and stability properties

    8a)MATLAB code for Unit Impulse Response of LTI System

    num = input ('type the numerator vector ');%Enter the fficients of the Numerator polynomial');den = input ('type the denominator vector '); %Enter the coefficients of the Denominator

    polynomial

    N = input ('type the desired length of the output sequence N '); %Enter the number of outputsamples that you wish to see in a graph

    n = 0 : N-1;% Set a range for X-axis

    imp = [ 1 zeros(1, N-1) ]; %Generate the Unit Impulse signal of N samples

    h = filter ( num, den, imp );% Obtain the output of the system, using the builtof h(n)

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

    stem(n,h);xlabel ('time index n');

    ylabel ('h(n)');

    title ('Impulse Response of LTI system');

    outputAll zero system:

  • 8/4/2019 BSL Lab Manual

    31/67

    All pole system:

  • 8/4/2019 BSL Lab Manual

    32/67

    Pole zero system :

  • 8/4/2019 BSL Lab Manual

    33/67

    8b)MATLAB code for Unit Step Response of LTI System

    num = input ('type the numerator vector ');%Enter the coefficients of the Numerator polynomial

    den = input ('type the denominator vector ');%Enter the coefficients of the Denominator

    polynomialN = input ('type the desired length of the output sequence N ');%Enter the number of output

    samples that you wish to see in a graph

    n = 0 : 1 : N-1; %Set a range for X-axisu = ones (1, N); %Generate the Unit Step signal of N samples

    s = filter ( num, den, u );% Obtain the output s(n) of the system,using the built-in MATLABfunction 'filter'

    disp('The step response of LTI system is'); disp(s); %Display the message in parentheses and thevalues of s(n)

    stem(n,s) %Plot a stem graph of s(n)

    xlabel ('time index n');ylabel ('s(n)');

    title ('Step Response of LTI system');

  • 8/4/2019 BSL Lab Manual

    34/67

    Pole-zero system:

  • 8/4/2019 BSL Lab Manual

    35/67

    8c)MATLAB code for Frequency Response of LTI System

    num = input ('type the numerator vector '); %Enter the coefficients of the Numerator polynomial

    den = input ('type the denominator vector '); %Enter the coefficients of the Denominatorpolynomial

    N = input ('number of frequency points '); %Enter the number of frequency points at which the

    frequency response need to be calculatedw = 0 : pi / N : pi; %Set a range for X-axis from 0 to ?

    H = freqz(num, den, w); %Calculate the frequency response H(ejw) of the transfer function H(z)

    for w ranging from 0 to ?figure; %Open a new figure window (Figure 1) for plotting the real and imaginary parts of H

    subplot(2 , 1 , 1 );

    plot( w/pi , real(H) ); %Divide figure window into two portions and plot the real part of H in the

    1st portionxlabel( ' omega \pi' );

    ylabel(' Amplitude ' )

    title ( 'Real part' );

    subplot( 2 , 1 , 2 );plot( w/pi , imag(H) );%Plot the imaginary part of H in the 2nd portion of figure window

    xlabel( ' \omega / \pi' );ylabel(' Amplitude ' )

    title ( 'Imaginary part' );

    figure;% Open a new figure window (Figure 2) for plotting the magnitude and phase of H

    subplot( 2 , 1 , 1 );plot( w/pi , abs(H) );% plot the magnitude part of H in the 1st portion

    xlabel(' omega \pi');

    ylabel(' Magnitude')title ('Magnitude Spectrum');

    subplot( 2 , 1 , 2 );

    plot( w/pi , angle(H) ); %Plot the phase part of H in the 2nd portion of figure windowxlabel(' omega \pi');

    ylabel(' Phase(radians) ');

    title ('Phase ');

    output :

  • 8/4/2019 BSL Lab Manual

    36/67

    8d)MATLAB code for finding the Stability of LTI System

    num=input(type the numerator);

  • 8/4/2019 BSL Lab Manual

    37/67

    den = input (' type the denominator vector ');

    [z,p,k] = tf2zp(num,den); %Obtain the location of zeros, poles and the gain constant using the

    built-in function 'tf2zp'disp ('Gain constant is '); disp(k); %Display the message in parentheses and the value of gain

    constant k

    disp (' Zeros are at '); disp(z); %Display the message in parentheses and the location of zerosdisp ('radius of Zeros ') ;

    radzero =abs(z)%Display the message in parentheses and the radius of zeros

    disp ('Poles are at '); disp(p); %Display the message in parentheses and the location of polesdisp ('radius of Poles ') ; radpole = abs(p) %Display the message in parentheses and the radius of

    poles

    if max(radpole) >= 1 %If at least one pole is outside the unit circle, do the following

    disp (' ALL the POLES do not lie within the Unit Circle '); %Display the message in parenthesesdisp (' Oooooops..The given LTI system is NOT a stable system '); %Display the message in

    parentheses

    else %If all the poles lie within the unit circle, do the following

    disp (' ALL the POLES lie WITHIN the Unit Circle ');disp (' The given LTI system is a REALIZABLE and STABLE system ');

    end; %End of IF statementzplane(num,den) %Plot the pole zero map of the given system in Z-plane, using the built-in

    function 'zplane'

    title ( ' Pole-Zero Map of the LTI system ' );

    Output:

  • 8/4/2019 BSL Lab Manual

    38/67

    9.Gibbs phenomenon

    Matlab code for observing gibbs phenomenon

  • 8/4/2019 BSL Lab Manual

    39/67

    clc; clear all; %Clear C ommand Window and all the previously defined

    variables

    N = input( 'type the total number of harmonics' ); %Input the number of sine wave%harmonics that you wish to add to generate the synthesized signal

    t = 0 : 0.001 : 1; %Generate a vector of 1001 samples for t with values between 0 & 1 andwith %an increment of 0.001y = square( 2 * pi * t ); %Generate the square wave signal of period T=1, using the built-in

    %function 'square'

    plot( t , y , 'r' , 'linewidth' , 2 ) %Plot the original square wave signal y(t) with a red line and%thickness of 2 units, for comparison with the synthesized square wave signal

    axis( [ 0 1 -1.5 1.5 ] ) Adjust the scale of X and Y axes to have a clear view of signals

    hold; %Hold the original square wave plot so as to superimpose the synthesized square%wave on to it for comparison

    sq = zeros( size(t) ); %Generate a vector of 1001 zeros to initialize the sq vector

    for n = 1 : 2 : N %Generate odd values for n beginning with 1 and with an

    %increment of 2, in each for loopsq = sq + (4 / (pi * n) * sin( 2 * pi * n * t)); %Generate the synthesized square

    %wave based on equation (10.9)

    end; %End of 'For' loopplot( t , sq ) %Plot the graph of synthesized square wave signal .

    grid; %Include grid lines in the graph

    xlabel( 't' ); ylabel( 'sq(t)' ); %Label the X and Y axestitle('Synthesized Square Wave Using Fourier Series'); %Add an appropriate title to the

    graph

    output:

    type the total number of harmonics5

    Current plot held

    type the total number of harmonics50

  • 8/4/2019 BSL Lab Manual

    40/67

    Current plot held

    10. Finding the fourier transform of a given signal and plotting magnitude and

    phase spectrum

    Matlab code for computing the DFT

    clc; clear all; %Clear Command Window and all the previously defined variables

    f = 100; %Fix the frequency of the Cosine wave at 100 Hz.

    Fs = 1000; % Fix the Sampling frequency at 1 KHz, following the Nyquist criterion.Ts = 1/Fs; % Find the sampling interval

    N = 1024; % Let the length of the DFT sequence N be 1024.

    n = [0 : N-1]* Ts; %Set the scale of X-axis for plotting the example signal.x = 0.8 * cos( 2 * pi * f * n); % Generate the Cosine wave of frequency 100 Hz

    figure; % Open a new figure window (Fig. 1) to plot the Cosine signal x(n).

    plot( n , x ); grid; % Plot x(n) and include grid.

    axis( [ 0 0.05 -1 1 ] ); % Adjust the scale of X and Y axes to have a clear view of the signaltitle(' Cosine Signal of Frequency f'); % Add an appropriate title to the graph

    xlabel('time n (sec.)'); ylabel('x(n)'); ````````% Label the X and Y axes

    Xk = fft( x , N ); % Compute the N-point DFT using the built-in function 'fft'k = 0 : N-1; % Set a range for the frequency index variable k

    figure; % Open a new figure window 2 to plot the magnitude and phase spectra of x(n)

    Xmag = abs(Xk); %Compute the magnitude of X(k)subplot(2,1,1); % Divide the Fig. 2 graphic space into two portions

    plot(k,Xmag); , plot the magnitude of X(k) in the 1st portion.

    title(' Magnitude of Fourier Transform'); %Add an appropriate title to the graph

    xlabel('frequency index k');

    ylabel('Magnitude'); %Label the X and Y axessubplot(2,1,2);

    plot(k,angle(Xk)); %Plot the phase angle of X(k) in the 2nd portion of Fig.2.grid;

    title(' Phase of Fourier Transform'); %Add an appropriate title to the graph

    xlabel('frequency index k');ylabel('Phase');

  • 8/4/2019 BSL Lab Manual

    41/67

  • 8/4/2019 BSL Lab Manual

    42/67

    12. Locating zeros and poles, and plotting the pole-zero maps in S-plane and

    Z-plane

    12a) MATLAB Code for drawing the pole zero map in S-domainclc; clear all;

    num = input ( 'type the numerator polynomial vector ' );

    %Enter the coefficients of numerator polynomial N(s) in square bracketsden = input( ' type the denominator polynomial vector ' );

    %Enter the coefficients of denominator polynomial D(s) in square brackets

    H = tf( num , den ) %Find the transfer function H(s)[ p , z ] = pzmap( H ); %Find the locations of poles and zeros. p and z will be column

    vectors.

    disp (' zeros are at ');disp( z ) %Display the message in parentheses and the location of zeros

    disp ('poles are at '); disp( p ) %Display the message in parentheses and the location of poles

    figure; %Open a new figure window (Fig.1) for plotting the pole zero map

    pzmap( H ) %Plot the pole zero map

    [ r, p, k ] = residue( num , den );% Find the PFE coefficients (residues) r,%pole locations p and the gain constant k of H(s)

    disp ('PFE coefficients '); disp( r ); %Display the message in parentheses and%the residues r

    disp ('Gain constant is '); disp( k );% Display the message in parentheses and

    %the gain constant kif max(real(p)) >= 1 %If the real part of any of the poles is greater than or equal to 1,

    do the%following

    disp (' All poles DO NOT LIE in the Left Half of S-Plane ');%Display the message in parentheses

    disp (' Oooooops..The given LTI system is NOT a stable system ');

    Else %If the real part of any of the poles is less than 1, do the followingdisp (' ALL the POLES lie in the Left Half of S-Plane ');

    disp (' The given LTI system is a STABLE system ');end; %End of the IF statement

    figure; %Open a new figure window (Fig.2) for plotting the impulse response h(t)

    t = 0 : 0.1 : 5; %Set a time range for X-axish = impulse( H , t ); %Find the impulse response h(t), using the built-in function

    'impulse'

    plot( t , h ) %Plot the impulse response h(t) versus time t

    xlabel('t'); ylabel('h(t)'); %Label the X and Y axestitle ( ' Impulse Response of the LTI system ' );

    output:

    simple distinct poles:type the numerator polynomial vector [1 -2 1]

    type the denominator polynomial vector [1 6 11 6]

    Transfer function:s^2 - 2 s + 1

  • 8/4/2019 BSL Lab Manual

    43/67

    ----------------------

    s^3 + 6 s^2 + 11 s + 6

    zeros are at

    1

    1poles are at

    -3.0000

    -2.0000-1.0000

    PFE coefficients

    8.0000

    -9.00002.0000

    Gain constant is

    ALL the POLES lie in the Left Half of S-Plane

    The given LTI system is a STABLE system

  • 8/4/2019 BSL Lab Manual

    44/67

    Complex conjugate poles:

    type the numerator polynomial vector [0 1 2 1]

    type the denominator polynomial vector [1 2 4 8]

    Transfer function:s^2 + 2 s + 1

    ---------------------s^3 + 2 s^2 + 4 s + 8

    zeros are at

    -1

    -1poles are at

    -2.0000

    0.0000 + 2.0000i0.0000 - 2.0000i

    PFE coefficients0.12500.4375 - 0.0625i

    0.4375 + 0.0625i

    Gain constant isAll poles DO NOT LIE in the Left Half of S-Plane

    Oooooops..The given LTI system is NOT a stable system

    Multiple Order Poles:

  • 8/4/2019 BSL Lab Manual

    45/67

    type the numerator polynomial vector [1]

    type the denominator polynomial vector [1 2 0 0]

    Transfer function:1

    -----------

    s^3 + 2 s^2

    zeros are at

    poles are at0

    0

    -2

    PFE coefficients0.2500

    -0.2500

    0.5000

    Gain constant isAll poles DO NOT LIE in the Left Half of S-Plane

    Oooooops..The given LTI system is NOT a stable system

  • 8/4/2019 BSL Lab Manual

    46/67

    12b) MATLAB Code for drawing the pole zero map in Z-domain

    clc; clear all; %Clear Command Window and all the previously defined variablesnum = input (' type the numerator vector '); %Enter the coefficients of numerator

    %polynomial N(z) in square brackets

    den = input (' type the denominator vector ');%Enter the coefficients of denominator polynomial D(z) in square

    brackets

    H = filt(num , den) %Find the transfer function H(z) using the%built-in function 'filt'

    z = zero(H); %Find the locations of zeros. z will be a column vector.

    disp (' zeros are at '); disp(z) %Display the message in parentheses and the

    %location of zerosdisp ('radius of Zeros ') ; radzero = abs(z)

    %Display the message in parentheses and the radius of zeros

    [r,p,k] = residuez(num,den); %Find the PFE coefficients (residues) r, pole%locations p and the gain constant k of H(z)

    disp ('poles are at '); disp(p) %Display the message in parentheses and the

    %location of polesdisp ('radius of poles ') ; radpole = abs(p)

    %Display the message in parentheses and the radius of poles

    disp ('PFE coefficients '); disp(r); %Display the message in parentheses and %residues r

    disp ('Gain constant is '); disp(k); %Display the message in parentheses and

    %gain constant k

    figure; %Open a new figure window (Fig.1) for plotting the pole zero map

  • 8/4/2019 BSL Lab Manual

    47/67

    zplane(num,den) %Plot the pole zero map in z-plane

    title ( ' Pole-Zero Map of the LTI system in Z-Plane' );

    if max(radpole) >= 1 %If the magnitude of any of the poles is

    %greater than or equal to 1, do the following

    disp (' ALL the POLES do not lie within the Unit Circle ');

    disp (' Oooooops..The given LTI system is NOT a stable system ');

    else %If the magnitude of any of the poles is less than 1, do the following

    disp (' ALL the POLES lie WITHIN the Unit Circle ');

    disp (' The given LTI system is a REALIZABLE and STABLE system ');

    end; %End of the IF statement

    figure; %Open a new figure window (Fig.2) for plotting the impulse response h(n)

    impz(num,den) %Plot the time domain impulse response h(n) of the LTI system H(z)

    output:simple and distinct poles:type the numerator vector [1 -1 1]

    type the denominator vector [1 1 0.16]Transfer function:

    1 - z^-1 + z^-2

    --------------------1 + z^-1 + 0.16 z^-2

    Sampling time: unspecified

    zeros are at0.5000 + 0.8660i0.5000 - 0.8660i

    radius of Zeros

    radzero =

    1.0000

    1.0000poles are at

    -0.8000

    -0.2000

    radius of polesradpole =

    0.80000.2000

    PFE coefficients

    5.0833-10.3333

  • 8/4/2019 BSL Lab Manual

    48/67

    Gain constant is

    6.2500

    ALL the POLES lie WITHIN the Unit CircleThe given LTI system is a REALIZABLE and STABLE system

  • 8/4/2019 BSL Lab Manual

    49/67

    Complex and conjugate poles:

    type the numerator vector [1 1]type the denominator vector [1 -1 0.5]

    Transfer function:1 + z^-1

    -------------------

    1 - z^-1 + 0.5 z^-2Sampling time: unspecified

    zeros are at

    0

    -1radius of Zeros

    radzero =

    0

    1poles are at

    0.5000 + 0.5000i0.5000 - 0.5000i

    radius of poles

    radpole =

    0.7071

    0.7071PFE coefficients

    0.5000 - 1.5000i

    0.5000 + 1.5000iGain constant is

    ALL the POLES lie WITHIN the Unit Circle

    The given LTI system is a REALIZABLE and STABLE system

  • 8/4/2019 BSL Lab Manual

    50/67

  • 8/4/2019 BSL Lab Manual

    51/67

    Multiple order poles:

    type the numerator vector [1]

    type the denominator vector [1 0.5 -1.25 0.375]Transfer function:

    1

    -------------------------------------1 + 0.5 z^-1 - 1.25 z^-2 + 0.375 z^-3

    Sampling time: unspecified

    zeros are at0

    0

    0

    radius of Zerosradzero =

    0

    00

    poles are at-1.5000

    0.5000 + 0.0000i

    0.5000 - 0.0000i

    radius of polesradpole =

    1.50000.5000

    0.5000

    PFE coefficients0.5625

    0.1875 + 0.0000i

    0.2500 - 0.0000iGain constant is

    ALL the POLES do not lie within the Unit Circle

    Oooooops..The given LTI system is NOT a stable system

  • 8/4/2019 BSL Lab Manual

    52/67

    13.Generation of Gaussian noise(real and complex), computation its mean,

    mean square values, Skew, Kurtosis, PSD, Probability Distribution function

    clc; clear all; % Clear Command Window and all the previously defined variables

  • 8/4/2019 BSL Lab Manual

    53/67

    x1 = randn(1,5000); %Generate first set of 5000 samples of Gaussian distributed random

    numbers

    x2 = randn(1,5000); %Generate second set of 5000 samples of Gaussian distributed randomnumbers

    figure; plot( x1 , x2 , ' . ' ) %Plot the joint distribution of both the sets using 'dots' so as to get a

    scatter plot in a new figure windowtitle('Scatter Plot of Gaussian Distributed Random Numbers'); %Add an appropriate title to

    thegraph

    x1 = rand(1,5000);% Generate first set of 5000 samples of uniformly distributed randomnumbersx2 = rand(1,5000); %Generate second set of 5000samples of uniformly distributed random

    numbersfigure; plot( x1 , x2 , ' . ' ) Plot the joint distribution of boththe sets using 'dots' so as to

    get a scatter plot in a new figure window

    title('Scatter Plot of Uniform Distributed Random Numbers');%Add an appropriate title to thegraph

    x3 = rand(1,100000);% Generate one lakh samples ofuniformly distributed random numbers

    figure; subplot(2,1,1); hist(x3)% Plot a histogram graph of x3 in the1st portion of a new figure

    windowtitle('Uniform Distribution');%Add an appropriate title to the graph

    y = randn(1,100000);% Generate one lakh samples of Gaussian distributed randomnumbers TextBook : Basic Simulation Lab with MATLAB

    subplot(2,1,2); hist(y)% Plot a histogram graph of y in the2nd portion of the figure window

    title('Gaussian Distribution');%Add an appropriate title to thegraph

    ymu = mean(y) %Find the mean value of yymsq = sum(y .^ 2 ) / length(y)% Find the mean square value of y

    ysigma = std(y) %Find the standard deviation of y

    yvar = var(y)% Find the variance value of yyskew = skewness(y) %Find the skew value of y

    ykurt = kurtosis(y) %Find the kurtosis value of y

    output:

    ymu =

    0.0038

    ymsq =

    1.0043

    ysigma =

    1.0022

    yvar =

    1.0043

    yskew =

    -0.0178

    ykurt =

    3.0212

  • 8/4/2019 BSL Lab Manual

    54/67

    14. Matlab code for verification of Sampling Theoremclc;clear allt=-5:0.0001:5;

  • 8/4/2019 BSL Lab Manual

    55/67

    F1=3;F2=23;x=cos(2*pi*F1*t)+cos(2*pi*F2*t);figure(1);plot(t,x);axis([-0.4 0.4 -2 2]);

    xlabel('timet(sec)');ylabel('x(t)');title('continous signal:x(t)=cos(2piF1t)+cos(2piF2t)');%case 1Fs1=1.4*F2;ts1=1/Fs1;n1=-0.4:ts1:0.4;xs1=cos(2*pi*F1*n1)+cos(2*pi*F2*n1);figure(2);stem(n1,xs1);hold on;plot(t,x,'r');

    axis([-0.4 0.4 -2 2]);hold off;xlabel('time sample(n)');ylabel('amplitude');title('discrete time signal');legend('Fs

  • 8/4/2019 BSL Lab Manual

    56/67

    xlabel('time sample(n)');ylabel('amplitude');title('discrete time signal');legend('Fs>2Fmax');

  • 8/4/2019 BSL Lab Manual

    57/67

  • 8/4/2019 BSL Lab Manual

    58/67

    17.Impulse response of a raised cosine filterMatlab code for plotting Impulse Response of RCFclc;clear all;

    t=linspace(-5,5,1000);beta1=0.2;beta2=0.4;beta3=0.6;

    Ts=1;h=(sin(pi*t/Ts))./(pi*t/Ts);h1=(cos(pi*beta1*t/Ts))./(1-(2*beta1*t/Ts).^2);h2=(cos(pi*beta2*t/Ts))./(1-(2*beta2*t/Ts).^2);h3=(cos(pi*beta3*t/Ts))./(1-(2*beta3*t/Ts).^2);ht=h.*h1;plot(t,ht);hold on;

    ht=h.*h2;plot(t,ht,'r');ht=h.*h3;plot(t,ht,'g');xlabel('timet(sec)');ylabel('h(t)');grid;legend('beta-->0.2','0.4','0.6');hold off;

    Matlab code for Cross Correlation of x(n) and Delayed x(n)

  • 8/4/2019 BSL Lab Manual

    59/67

    x= [1 2 3 -2 -1];% Create a sample signal vector x(n)D = input( ' type the delay D ' );%Enter the desired amount of delay Dxd = [ zeros(1,D) x ]; %Create a delayed signal xd(n) with a delay D[ r , lag ] = xcorr( x , xd ); %Compute the cross correlation of x(n)and xd(n) usingthe built-in function'xcorr'stem( lag , r ) %Plot a stem graph of cross correlated output versus the lag

    title( ' Cross Correlation of x(n) andDelayed x(n) ' );%Add appropriate title to thegraphxlabel( ' Lag Index l ' );ylabel( ' Rxy( l ) ' );%Label the X and Y axesoutput:type the delay D 5

    Matlab code for Time Delay Estimation using CrossCorrelation

    x= [1 2 3 -2 -1];% Create a sample signal vector x(n)

    D = input('type the round tripdelay D ');%Enter the desired amount of delay Dalpha= input('type the attenuation factor ');%Enter a value (

  • 8/4/2019 BSL Lab Manual

    60/67

    if obj == 1 %If the object is present run the next two statements to attenuate the delayed signal

    and add noise to it

    xa= alpha * xd; %Attenuate the strength of xd(n) by timesto account for loss of amplitudey = xa + w; %Corrupt the attenuated signal xa(n) byadding random noise

    else %If the object is not present, then thereceived signal will have noise only. Runthe next

    statement.y = w; %Equate the received signal to noise

    end; %End of IF statement.figure; Open a new figure window (Fig. 1)

    xp = [x zeros(1,D)]; %Append 'D' zeros to x(n) so as to plot it onthe same scale as that of y(n)subplot(2,1,1); stem(nxd,xp); %Divide Fig.1 window into two parts, plot astem graph of x(n) in

    the 1st part

    xlabel ('n '); ylabel ('x(n)'); %Label the X and Y axes

    title(' Transmitted Signal x(n)'); %Add appropriate title to the graphNY = length(y); %Find the length of the received signal

    ny = 0 : NY-1; %Set a range for X-axis of the plot of y(n)

    subplot(2,1,2); stem(ny,y); %Plot a stem graph of y(n) in the 2nd part ofFig. 1 window

    xlabel ('n '); ylabel ('y(n)'); %Label the X and Y axestitle(' Received Signal y(n)');%Add appropriate title to the graph

    [r,lag] = xcorr(x,y); %Compute the cross correlation of x(n) and y(n) using the built-in function'xcorr'

    figure; %Open a new figure window (Fig. 2)

    stem(lag,r); %Plot a stem graph of cross correlatedoutput versus the lag

    xlabel ('Lag '); ylabel ('Rxy(l)');% Label the X and Y axestitle ('Time Delay Estimation Using Cross Correlation ');%Add appropriate title to the graph

    output:

    type the round tripdelay D 4type the attenuation factor 0.5

    type 1 for presence of object or a 0 for absence 1

  • 8/4/2019 BSL Lab Manual

    61/67

    clc; clear all; %Clear Command Window and all the previously defined variables

    M = 256; %Set an upper limit for X-axis

    n = 0 : M-1; %Set the range for X-axis from 0 to 255

    x = cos(16*pi*n/M) + sin(32*pi*n/M); % Generate the periodic signal x(n)snr = input('Type the desired SNR '); %Enter a value for the desired SNR

  • 8/4/2019 BSL Lab Manual

    62/67

    px = var(x) %Find the power of x(n)

    w = sqrt(12) * (rand( 1 , M )-0.5); %Generate the zero mean unity power white random noise

    signalan = sqrt(px * (10 ^ ((-1*snr)/10))) %Find the scale factor by which to scale w(n) so as to get

    the desired SNR

    w = w .* an; %Scale w(n) by the scaling factor 'an'pn = var(w) %Find the power of noise

    SNRdb = 10 * log10(px/pn) %Find the actual SNR

    y = x + w; %Generate noisy signal y(n) by corrupting x(n) with w(n)N = M /8; %Find the common period of x(n)

    L = floor( M / N ); %Find L, the number of complete cycles of x(n) in y(n)

    d = zeros( 1 , M );% Generate a row vector 'd' of M zeros for storing the impulse train

    for i = 1 : M %Begin a 'for' loopif rem(i-1,N) == 0 %If the remainder of (i-1) / N is zero i.e.at i = 0, 32, 64 etc. do the following

    d(i) = 1; %Generate a unit impulse at that value of i in the vector

    end; %End of 'IF' loop

    end; %End of 'FOR' loopCyd = ifft( fft(y,M).*fft(d,M)) / M; %Find the circular cross correlation of the impulse train y(n)

    and d(n) using 'fft' and 'ifft' commandsr = Cyd * (M/L); %Multiply the result by the scale factor M/L as per (17.15)

    figure(1); %Open a new figure window (Fig.1) for plotting x(n)

    plot(n,x,'b'); axis([1 80 -3 3]) %Plot x(n) in Fig.1 in blue color and adjust the scale of X and Y

    axes for a clear view of few cycles of x(n)xlabel( 'n' ); ylabel( 'x(n)' ); %Label the X and Y axes

    title('Periodic Signal x(n)') %Add an appropriate title to the graph

    figure(2); %%Open a new figure window (Fig.2)subplot(2,1,1); plot(n,y,'r'); % Divide Fig.2 into two parts, plot the noisy signal y(n) in red color

    in the 1stpart.

    grid; axis([1 96 -3 3]); %Include grid and adjust the scale of X and Y axes for a clear view offew cycles of y(n)

    xlabel( 'n' ); ylabel( 'y(n)' ); %Label the X and Y axes

    title('Noisy Signal y(n)') %%Add an appropriate title to the graphsubplot(2,1,2);stem(n,d);% Plot a stem graph of Impulse train d(n) in the 2ndpart of Fig.2.

    grid;axis([1 96 -0.5 1.5]); %Include grid and adjust the scale of X and Y axes for a clear view of

    few cycles of d(n)

    xlabel( 'n' ); ylabel( 'd(n)' ); %Label the X and Y axestitle('Impulse Train d(n)') %Add an appropriate title to the graph

    figure(3); %Open a new figure window (Fig.3) for plotting the extracted periodic signal r(n)

    plot(n,r,'k'); axis([1 80 -3 3])% Plot r(n) in black color and adjust the scale of axes for a clearview of few cycles of r(n)

    xlabel( 'n' ); ylabel( 'r(n)' ); %Label the X and Y axes

    title('Extracted Periodic Signal r(n)') %Add an appropriate title to the graphfigure(4); %Open a new figure window (Fig.4) for plotting r(n) in comparison to x(n)

    plot(n,x,'b'); hold on;

    axis([1 80 -3 3]) %Plot x(n) in blue color, hold the graph so as to superimpose the plot of r(n) on

    the same graph and adjust the scale of axes

  • 8/4/2019 BSL Lab Manual

    63/67

    plot(n,r,'r:'); hold off;

    axis([1 80 -3 3]) %Plot r(n) using red dotted line, release the graph and adjust the scale of axes

    legend('x(n)','r(n)') %Add an appropriate legend to the graphoutput:

    Type the desired SNR 5

    px =

    1.0039

    an =

    0.5634

    pn =

    0.3095

    SNRdb =

    5.1105

  • 8/4/2019 BSL Lab Manual

    64/67

  • 8/4/2019 BSL Lab Manual

    65/67

    clc; clear all; %Clear Command Window and all the previously defined variablesN = 100; %Fix the length of the signals as 100

    n = 0:N-1; %Set a range for time on the X-axis

    dsnr = input('type desired SNR in dB'); %Enter the desired Signal to Noise Ratio in decibels (dB)x = sqrt(2) * sin((pi/5)*n); %Generate an example sine wave signal of period 10 and unity power

    figure(1); stem(n,x);grid% Open a new figure window(Fig.1), Plot a stem graph of x(n) and

    includegrid

    axis([ 0 50 -1.5 1.5]) %Adjust the scale of X and Y axes for a clear view of few cycles of

    x(n)

    xlabel( ' n ' ); ylabel( 'x(n)' ); %Label the X and Y axestitle('Sinusoidal Signal x(n)'); %Add an appropriate title to the graph

    px = var(x) %Find the power px of x(n) and display it on screen

    an = sqrt(px * (10^(-1 * dsnr/10)));% Find the scale factor by which to scale w(n) so as to get thedesired SNR

    w = sqrt(12) * (rand(1, N )-0.5); %Generate 100 samples of zero mean unit y power random

    signal w(n)

    w = w * an;% Scale w(n) by the scaling factor vnpn = var(w) %Find the power of noise signal w(n) and display it on screen

    disp('The calculated SNR ');% Display a message on screen

    SNRdb = 10*log10(px / pn)% Find the SNR in dB and display on screenfigure(3); stem(n,w);

    grid; %Open a new figure window(Fig.3),

    %Plot a stem graph of w(n) and include grid

  • 8/4/2019 BSL Lab Manual

    66/67

    axis([0 50 min(w) max(w)])% Adjust the scale of X and Y axes for a clear view of few cycles

    of w(n)

    xlabel('n'); ylabel('w(n)'); %Label the X and Y axestitle('Random Noise Signal w(n)');% Add an appropriate title to the graph

    y = x + w; %Corrupt x(n) by adding noise w(n)

    figure(6); %Open a new figure window (Fig.6)subplot(2,1,1);

    stem(n,y);

    grid ;%Divide the Fig.6 into two parts, plot y(n) as a stem graph in the 1st part, include grid

    axis( [ 0 50 min(y) max(y) ] ) %Adjust the scale of X and Y axes for a

    %clear view of few cycles of y(n)

    xlabel( ' n ' );ylabel( 'y(n)= x(n) + w(n)' );%Label the X and Y axes

    title('Sinusoidal Signal Corrupted with Random Noise')%Add an appropriate title to the graph

    [ryy,lag] = xcorr(y,y,'unbiased'); %Find the normalized autocorrelation of y(n)

    subplot(2,1,2);stem(lag,ryy);

    grid; %plot Ryy(l) as a stem graph in the 2nd part, include gridaxis([0 50 -1.5 1.5]) %Adjust the scale of X and Y axes for a clear view of few cycles of y(n)

    xlabel( 'Lag Index l' ); ylabel( 'R_y_y(l)' );%Label the X and Y axes

    title('Autocorrelation Signal R_y_y(l)') %Add an appropriate title to the graph

    [rxx,lag] = xcorr(x,x); %Find the autocorrelation Rxx(l) of x(n)figure(2);

    stem(lag,rxx);

    grid;% Open a new figure window(Fig.2), Plot a stem graph of Rxx(l) and include gridaxis([-20 20 min(rxx) max(rxx)]) %Adjust the scale of X and Y axes for a clear view of few

    cycles of Rxx(l)

    xlabel('Lag Index l');ylabel('R_x_x(l)');%Label the X and Y axes

    title('Autocorrelation Signal R_x_x(l)') %Add an appropriate title to the graph

    [rxw,lag] = xcorr(x,w); %Find the cross correlation Rxw(l) between x(n) and w(n)figure(5);

    stem(lag,rxw);

    grid;% Open a new figure window(Fig.5), Plot a stem graph of Rxw(l) and include grid

    axis([-20 20 min(rxw) max(rxw)]) %Adjust the scale of X and Y axes for a clear view of fewcycles of Rxw(l)

    xlabel('Lag Index l');

    ylabel('R_x_w(l)');%Label the X and Y axestitle('Cross Correlation Between x(n) and w(n) ')%Add an appropriate title to the graph

    [rww,lag] = xcorr(w,w); %Find the autocorrelation Rww(l) of w(n)

    figure(4);stem(lag,rww);grid; %Open a new figure window(Fig.4), %Plot a stem graph of Rww(l) and include grid

    axis([-20 20 min(rww) max(rww)]) %Adjust the scale of X and Y axes for a clear view of few

    cycles of Rww(l)

    xlabel('Lag Index l');

  • 8/4/2019 BSL Lab Manual

    67/67

    ylabel('R_w_w(l)');%Label the X and Y axes

    title('Autocorrelation Signal R_w_w(l)') %Add an appropriate title to the graph