Basic Simulation lab
1. BASIC OPERATIONS ON MATRICES
AIM:To write a MATLAB Program for performing Basic Operations on Matrices. SOFTWARE USED: MATLAB 7.01 Version COMMANDS USED: det( ): This command is used to find the determinant of a matrix. inv( ): This command is used to find the inverse of a square matrix. disp( ): This command is used to display the output and the values. input( ): This command is used for accepting the data through keyboard. PROGRAM:
%Basic Operations on Matrices
A=input (Enter the elements in the matrix A :); B= input (Enter the elements in the matrix B :);
%Addition of two matrices
MAdd=A+B; disp(Addition of two matrices A and B is:); disp(MAdd);
%Subtraction of two matrices
MSub=A-B; disp(subtraction of two matrices A and B is:); disp(MSub);
%Multiplication of two matrices
MMul=A*B; disp(Multiplication of two matrices A and B is:); disp(MMul);
%Dot Multiplication or vector Multiplication of two matrices
DMMul=A.*B; disp(Dot Multiplication of two matrices A and B is:); disp(DMMul);
Sri Venkateswara College of Engineering, Dept of E.C.E
Basic Simulation lab
%Transpose of two matrices
MtransA=A; disp(Transpose of matrix A is:); disp(Mtrans A); MtransB=B; disp(Transpose of matrix B is:); disp(Mtrans B);
%Determinent of two matrices
MdetA=det(A); disp(Determinent of Matrix A:); disp(MdetA); MdetB=det(B); disp(Determinent of Matrix B:); disp(MdetB);
%Inverse of two matrices
If(det(A)==0) disp(Inverse of matrix A does not exists); else MinvA=inv(A); disp(Inverse of matrix A is:); disp(MinvA); end If (det(B)==0) disp(Inverse of matrix B does not exists); else MinvB=inv(B); disp(Inverse of matrix B is:); disp(MinvB); end OUTPUT:
RESULT: The basic operations on matrices are performed by using MATLAB.
Sri Venkateswara College of Engineering, Dept of E.C.E
Basic Simulation lab
2. GENERATION OF VARIOUS SIGNALS AND SEQUENCES
AIM:To write a MATLAB Program for the generation of signals and sequences such as unit impulse, unit step, ramp, square, triangular, sawtooth, sinusoidal, sinc function. SOFTWARE USED: MATLAB 7.01 Version COMMANDS USED: zeros(M,N) :This command is used to generate a matrix of all zeros of order M N. ones( M,N):This command is used to generate a matrix of all ones of order M N. plot ( ):This command is used for plotting the continuous signals. stem( ):This command is used for plotting the discrete signals. subplot ( ): This command is used for plotting one or more plots within the same window. axis ( ): This command is used for setting the range of values for x and y-axis. disp( ): This command is used to display the output and the values. input( ): This command is used for accepting the data through keyboard. sin ( ):This command is used for generating a sinusoidal signal. square ( ):This command is used for generating a square wave. sinc ( ): This command is used for generating a sinc pulse. sawtooth ( ):This command is used for generating a sawtooth waveform. pulstran ( ):This command is used for generating a sequence of a train of pulses. PROGRAM: %Generation of Signals %Generation of Unit Impulse Signal t=0:0.001:1; unit_impc=[1 zeros(1,1000)]; figure; subplot(3,1,1); plot(t,unit_impc); xlabel('t');
ylabel('Imp');
Sri Venkateswara College of Engineering, Dept of E.C.E
Basic Simulation lab
title('Unit Impulse Signal'); %Generation of Unit Step Signal unit_stepc=ones(1,1001); subplot(3,1,2); plot(t,unit_stepc); xlabel('t'); ylabel('step'); %Generation of Ramp Signal ramp_signal=t; subplot(3,1,3); plot(t,ramp_signal); xlabel('t'); ylabel('ramp'); title('ramp Signal'); %Generation of sequences N=input('Enter the value of N:'); %Generation of unit sample sequence unit_sampleD=[zeros(1,N) ones(1,1) zeros(1,N)]; n=-N:N; figure; subplot(3,1,1); stem(n,unit_sampleD); axis([-N N -1 2]); xlabel('n'); ylabel('unit_sampleD'); title('unit sample sequence'); %Generation of unit Step sequence unit_stepD=ones(1,N); n1=0:N-1; subplot(3,1,2); stem(n1,unit_stepD); xlabel('n'); ylabel('n'); title('unit step sequence');
Sri Venkateswara College of Engineering, Dept of E.C.E
Basic Simulation lab
%Generation of ramp sequence ramp_seq=n1; subplot(3,1,3); stem(n1,ramp_seq); xlabel('n1'); ylabel('ramp_seq'); title('ramp sequence'); %Generation of signals and sequences f=input('Enter the frequency of the signal:'); fs=10*f; n2=0:1/fs:1; %Generation of sinusoidal signal and sequences sine_signal=sin(2*pi*f*n2); figure; subplot(3,2,1); plot(n2,sine_signal); xlabel('n2'); ylabel('sine_signal'); axis([0 1 -1.5 1.5]); title('sinusoidal signal'); subplot(3,2,2); stem(n2,sine_signal'); xlabel('n2'); ylabel('sine_sequence'); axis([0 1 -1.5 1.5]); title('sinusoidal sequence'); %Generation of square wave signal and sequences square_signal=square(2*pi*f*n2); subplot(3,2,3); plot(n2,square_signal); xlabel('n2'); ylabel('square signal'); axis([0 1 -1.5 1.5]); title('square signal'); subplot(3,2,4); stem(n2,square_signal); xlabel('n2');
Sri Venkateswara College of Engineering, Dept of E.C.E
Basic Simulation lab
ylabel('square sequence'); axis([0 1 -1.5 1.5]); title('square wave sequence'); %Generation of sinc signal and sequence x=-5:0.1:5; sinc_signal=sinc(x); subplot(3,2,5); plot(x,sinc_signal); xlabel('x'); ylabel('sinc signal'); title('sinc signal'); subplot(3,2,6); stem(x,sinc_signal); xlabel('x'); ylabel('sinc sequence'); title('sinc sequence'); %Generation of sawtooth waveform and sequence sawtooth_signal=sawtooth(2*pi*2*n2); figure; subplot(2,2,1); plot(n2,sawtooth_signal'); xlabel('n2'); ylabel('sawtooth signal'); title('sawtooth signal'); subplot(2,2,2); stem(n2,sawtooth_signal); xlabel('n2'); ylabel('sawtooth_sequence'); title('sawtooth sequence'); %Generation of triangular waveform and sequence d1=0:0.2:1;%repetitive frequency defined how many times the waveform should repeat y1=pulstran(n2,d1,'tripuls',0.1); d2=0.1:0.2:1;%repetitive frequency defined how many times the waveform should repeat y2=pulstran(n2,d2,'tripuls',0.1); tri=y1-y2;%Mixing both -ve and +ve sides subplot(2,2,3); plot(n2,tri);
Sri Venkateswara College of Engineering, Dept of E.C.E
Basic Simulation lab
xlabel('n2'); ylabel('triangular waveform'); title('triangular waveform'); subplot(2,2,4); stem(n2,tri); xlabel('n2'); ylabel('triangular sequence'); title('triangular sequence'); OUTPUT:
RESULT: The generation of signals and sequences is executed successfully using MATLAB.
Sri Venkateswara College of Engineering, Dept of E.C.E
Basic Simulation lab
3. OPERATIONS ON SIGNALS AND SEQUENCES
AIM : To write a MATLAB program for performing operations on signals and sequences such as addition, multiplication, scaling, shifting, folding, computation of energy and average power. SOFTWARE USED: MATLAB 7.0.1 version. COMMANDS USED: sin ( ): This command is used for the generation of sinusoidal signal. input ( ): This command is used for accepting the data to the keyboard. disp ( ): This command is used for displaying the messages and the values stored in a variable. plot( ): This command is used for plotting the continuous signals. stem( ): This command is used for plotting the discrete signals. grid( ): This command is used for displaying the grid lines in the plot. axis( ): This command is used for setting the range of values for the x-axis and y-axis. sum ( ): This command is used for finding the sum of the values of a vector or a matrix. area(x, y): This command is same as plot(x, y) except that the area between 0 and y is filled when y is a matrix ,area(x, y) plots the columns of y as filled areas. hold on: This command is used to hold the current plot and all axis properties so that the Subsequent graphing commands add to the existing graph PROGRAM: clc, closeall; clearall; t=0:0.001:1; L=length(t); f1=1; f2=3; x1=sin(2*pi*f1*t); x2=cos(2*pi*f2*t); figure; subplot(3,2,1); plot(t,x1,'b',t,x2,'r'); xlabel('time t---->'); ylabel('amplitude---->');
Sri Venkateswara College of Engineering, Dept of E.C.E
Basic Simulation lab
title('the signals x1(t) and x2(t)'); x3=x1+x2; subplot(3,2,2); plot(t,x3); xlabel('time t---->'); ylabel('amplitude---->'); title('the sum of x1(t) and x2(t)'); x4=x1.*x2; subplot(3,2,3); plot(t,x4); xlabel('time t---->'); ylabel('amplitude---->'); title('the multiplication of x1(t) and x2(t)'); t1=-1:0.001:0; % x5=sin(2*pi*f1*(-t)); % x6=sin(2*pi*f2*(-t)); x5=fliplr(x1); x6=fliplr(x2); subplot(3,2,4); plot(t1,x5,'b',t1,x6,'r'); xlabel('time t---->'); ylabel('amplitude---->'); title('the folding of x1(t)and x2(t)'); x7=[zeros(1,200),x2(1:(L-200))]; subplot(3,2,5); plot(t,x7); xlabel('time t---->'); ylabel('amplitude---->'); title('the shifting of x2(t)'); x8=x2.^2; subplot(3,2,6); plot(t,x8); xlabel('time t---->'); ylabel('amplitude---->'); title('the squaring of x2(t)');
%Computation of energy and power clc; t=0:0.01:1; f1=input('Enter the fundamental frequency:'); a=input('Enter the amplitude:'); x=a*sin(2*pi*f1*t); figure; subplot(2,1,1);
Sri Venkateswara College of Engineering, Dept of E.C.E
Basic Simulation lab
plot(t,x); xlabel('t'); ylabel('x(t)'); title('test signal for energy calculation'); gridon; subplot(2,1,2); Energy=area(t,x.^2) xlabel('t'); ylabel('x^2(t)'); title('The colored area gives the energy') grid; disp('Power, one period:'); Power = (1/20)*sum(x(1:20).^2)
Output:
RESULT:The operation on signals and sequences like addition, multiplication, scaling, shifting, folding, computation of energy and average power is executed successfully using MATLAB.
Sri Venkateswara College of Engineering, Dept of E.C.E
10
Basic Simulation lab
4. FINDING EVEN, ODD, REAL AND IMAGINARY PARTS OF SIGNALS AND SEQUENCE
AIM: To write a MATLAB program for finding the even and odd parts, real and imaginary parts of a signal or sequence. SOFTWARE USED: MATLAB 7.0.1 version. COMMANDS USED: sin ( ): This command is used for generation of a sinusoidal signal. cos ( ): This command is used for generation of a cosine signal. plot ( ): This command is used for plotting continuous signals stem ( ): This command is used for plotting discrete signals subplot ( ): This command is used for plotting one or more plots within the same window. complex ( ): This command is used for generation of a complex signal. real ( ): This command is used for finding the real part of a complex signal. imag ( ): This command is used for finding the imaginary part of a complex signal. grid on: This command is used for displaying the grid lines in the plot. PROGRAM: % Even/ odd part of signal clc; clearall; closeall; % CONTINUOUS TIME SIGNALS t=0:0.001:10; A=0.8; x1=sin(t); x2=sin(-t); if(x2==x1) disp('The given signal is even signal'); else if(x2==(-x1)) disp('The given signal is odd signal'); else disp('The given signal is neither even nor odd'); end
Sri Venkateswara College of Engineering, Dept of E.C.E
11
Basic Simulation lab
end xe=(x1+x2)/2; xo=(x1-x2)/2; % DISCRETE TIME SIGNALS N=5; n=-N:N; y1=-N:N; y2=fliplr(y1); if(y2==y1) disp('The given sequence is even sequence'); else if(y2==(-y1)) disp('The given sequence is odd sequence'); else disp('The given sequence is neither even nor odd'); end end ye=(y1+y2)/2; yo=(y1-y2)/2; figure subplot(2,2,1); plot(t,x1); xlabel('t');ylabel('x1(t)');title('signal x(t)'); subplot(2,2,2); plot(t,x2); xlabel('t');ylabel('x2(t)');title('signal x(-t)'); subplot(2,2,3); plot(t,xe); xlabel('t');ylabel('xe(t)');title('even part signal x(t)'); subplot(2,2,4); plot(t,xo); xlabel('t');ylabel('xo(t)');title('odd part signal x(t)'); figure subplot(2,2,1); stem(n,y1); xlabel('n');ylabel('amplitude');title('sequence y1[n]'); subplot(2,2,2); stem(n,y2); xlabel('n');ylabel('amplitude');title('sequence y2[n]'); subplot(2,2,3); stem(n,ye); xlabel('n');ylabel('amplitude');title('even part sequence'); subplot(2,2,4); stem(n,yo); xlabel('n');ylabel('amplitude');title('odd part sequence');
Sri Venkateswara College of Engineering, Dept of E.C.E
12
Basic Simulation lab
% Real and Imaginary part of signal % Real and Imaginary signals separation form complex signal t=0:0.01:1; x1 = cos(2*pi*t); x2 = sin(2*pi*t); x3 = complex(x1,x2); % generation of complex signal r = real(x3); % Real part of the complex signal i = imag(x3); % Imaginary part of complex signal figure(5); subplot(2,1,1); plot(x1); title('x1 - as real part'); xlabel('time ------- t'); ylabel('amplitude'); gridon; subplot(2,1,2); plot(x2); title('x2 - as Imaginary part'); xlabel('time ------- t'); ylabel('amplitude'); gridon; figure(6); subplot(3,1,1); plot(x3); title('Complex Signal'); xlabel('Real axes'); ylabel('Imaginary axes'); gridon; subplot(3,1,2); plot(r); title('Real part of Complex Signal'); xlabel('time ------- t'); ylabel('amplitude'); gridon; subplot(3,1,3); plot(i); title('Imaginary part of Complex Signal'); xlabel('time ------- t'); ylabel('amplitude'); gridon; OUTPUT: RESULT: Finding the even and odd parts, real and imaginary parts of a signal and sequence is executed successfully using MATLAB.
Sri Venkateswara College of Engineering, Dept of E.C.E
13
Basic Simulation lab
5. CONVOLUTION BETWEEN SIGNALS / SEQUENCES AIM: To perform linear and circular convolution of two sequences or signals. SOFTWARE USED: MATLAB 7.0.1 version. COMMANDS USED: input ( ): This command is used for accepting the data from the keyboard. length( ): This command is used for finding the length of a vector or a sequence. plot ( ): This command is used for plotting the linear plots or continuous signals. subplot ( ): This command is used for plotting one or more plots within the same window. disp ( ): This command is used for displaying the messages and the values stored in the variables. conv( ): This command is used for finding the linear convolution between the two signals or sequences. stem ():This command is used for plotting descrete signals. max ( ): This command is used for finding the maximum value among the given numbers. zeros (m,n): This command is used for generating a matrix of all zeros of order m*n.
PROGRAM:
%Linear Convolution of two sequences
clc; clear all; x=input('Enter the input sequence :'); h=input('Enter the impulse response :'); n1=length(x); disp(n1) n2=length(h); disp(n2) n3=n1+n2-1; disp('The resultant length is:'); disp(n3); y=conv(x,h); figure(1);
Sri Venkateswara College of Engineering, Dept of E.C.E
14
Basic Simulation lab
subplot(3,1,1); t1=0:1:(n1-1); stem(t1,x); xlabel('n'); ylabel('x(n)'); title('Input sequence'); subplot(3,1,2); t2=0:1:(n2-1); stem(t2,h); xlabel('n'); ylabel('h(n)'); title('Impulse response'); subplot(3,1,3); t3=0:1:(n3-1); stem(t3,y); xlabel('n'); ylabel('y(n)'); title('Linear convoluted response'); disp('Linear convoluted output'); disp(y);
%Circular Convolution of sequences
x=input('Enter first sequence:'); y=input('Enter second sequence:'); n1=length(x); n2=length(y); N=max(n1,n2); n3=n1-n2; if n3>=0 y= [y,zeros(1,n3)]; else x=[x,zeros(1,n3)]; end a=length(x); figure(2); subplot(3,1,1); t1=0:1:(a-1); stem(t1,x); xlabel('n');
Sri Venkateswara College of Engineering, Dept of E.C.E
15
Basic Simulation lab
ylabel('x(n)'); title('first seq after zero padding'); subplot(3,1,2); b=length(y); t2=0:1:(b-1); stem(t2,y); xlabel('n'); ylabel('y(n)'); title('second seq after zero padding'); for n=1:N; k(n)=0; for i=1:N j=n-i+1; if j<=0 j=N+j; k(n)=k(n)+x(i)*y(j); end end end subplot(3,1,3); t3=0:1:(a-1); stem(t3,k); xlabel('n'); ylabel('circular convoluted result'); disp('Circular Convoluted output'); disp(k);
%Convolution of two signals
t= -pi:0.01:pi; f=input('Enter the fundamental freq:'); x=sin(2*pi*f*t); h=cos(2*pi*f*t); n1=length(x); disp(n1); n2=length(h); disp(n2); n3=n1+n2-1; disp('The resultant length is:');
Sri Venkateswara College of Engineering, Dept of E.C.E
16
Basic Simulation lab
disp(n3); y=conv(x,h); figure(3); subplot(3,1,1); t1=0:1:(n1-1); stem(t1,x); xlabel('n'); ylabel('x(n)'); title('Input sequence'); subplot(3,1,2); t2=0:1:(n2-1); stem(t2,h); xlabel('n'); ylabel('h(n)'); title('Impulse response'); subplot(3,1,3); t3=0:1:(n3-1); stem(t3,y); xlabel('n'); ylabel('y(n)'); title('Linear convoluted response');
OUTPUT:
RESULT: Convolution between signals / sequences are executed successfully using MATLAB.
Sri Venkateswara College of Engineering, Dept of E.C.E
17
Basic Simulation lab
6. AUTO CORRELATION AND CROSS CORRELATION
AIM: To evaluate auto and cross correlation of signals/sequences SOFTWARE USED: MATLAB 7.0.1.version COMMANDS USED: input ( ): This command is used for accepting the data from the keyboard. sin( ): This command is used for generation of a sinusoidal signal. length( ): This command is used for finding the length of a vector or a sequence. plot( ): This command is used for plotting the linear plots or continuous signals. subplot ( ): This command is used for plotting one or more plots within the same window. stem():This command is used for plotting discrete signals. disp ( ): This command is used for displaying the messages and the values stored in the variables. xcorr( ):This command is used for finding the correlation between the similar signals or different signals. PROGRAM: %Auto Correlation of a signal clc; t= -pi:0.01:pi; f=input('Enter the fundamental freq:'); x=sin(2*pi*f*t); n2=(length(x)-1); k=(-n2):n2'; subplot(2,1,1); t1=0:1:n2; stem(t1,x); xlabel('n'); ylabel('amplitude'); title('Input for auto correlation'); r=xcorr(x,x); subplot(2,1,2);
Sri Venkateswara College of Engineering, Dept of E.C.E
18
Basic Simulation lab
stem(k,r); xlabel('lag index k'); ylabel('amplitude'); title('auto correlated result'); % Cross correlation between sequences x=input('Enter the first seq:'); y=input('Enter the second seq:'); n1=(length(y)-1); n2=(length(x)-1); k=(-n1):n2'; subplot(3,1,1); t1=0:1:n2; stem(t1,x); xlabel('n'); ylabel('amplitude'); title('seq 1'); subplot(3,1,2); t2=0:1:n1; stem(t2,y); xlabel('n'); ylabel('amplitude'); title('seq 2'); r=xcorr(x,y); subplot(3,1,3); stem(k,r); xlabel('lag index k'); ylabel('amplitude'); title('cross correlated result'); disp('cross correlated result is:'); disp(r); OUTPUT:
RESULT: Auto correlation and cross correlation of signals and sequences are executed successfully using MATLAB.
Sri Venkateswara College of Engineering, Dept of E.C.E
19
Basic Simulation lab
7.LINEARITY AND TIME INVARIANCE PROPERETIES
AIM: To write a MATLAB program to verify the linearity and time invariance properties of a given system. SOFTWARE USED: MATLAB 7.0.1 version. COMMANDS USED: input ( ): This command is used for accepting the data from the keyboard. menu ( ): This command is used for generating the choices for user input. disp ( ): This command is used for displaying the messages and the values stored in the variables. cos ( ): This command is used for generation of a cosine signal.
PROGRAM:
%Linearity property
x1 = input('Enter the value for first input:'); x2 = input('Enter the value for second input:'); a1 = input('Enter weighting value for first input a1= '); a2 = input('Enter weighting value for second input a2= '); f = menu('Chose a function','cos(x)','2*x(n)'); % To choose which functionfor which you want to % verify the Linearity property if f == 1 % defined system function y = cos(x) y1 = a1*cos(x1) + a2*cos(x2); y2 = cos((a1*x1)+(a2*x2)); if y1==y2 disp('y = cos(x) is linear system'); else disp('y = cos(x) is non-linear system'); end else % defined system function y = 2*x y1 = a1*2*x1 + a2*2*x2;
Sri Venkateswara College of Engineering, Dept of E.C.E
20
Basic Simulation lab
y2 = 2*((a1*x1)+(a2*x2)); if y1==y2 disp('y = 2*x is linear system'); else disp('y = 2*x is non-linear system'); end end
%Time variance property
x = input('Enter input applied to the system:'); p= input('Enter the time at which the input arrived:'); m = input('Enter the time instant at which you want to calculate output:'); k= m-p; x(p)=x; f = menu('Chose a function','cos(x)','n*x(n)'); % to chose which function % you want to verify the time variance if f == 1 % defined system function y(n) = cos(x(n)) y1 = cos(x(p)); y2 = cos(x(m-k)); if y1 == y2 disp('The given system y(n) = cos(x(n)) is Time invariant') else disp('The given system y(n) = cos(x(n)) is Time variant') end else % defined system function y(n) = n x(n) y1 = p*x(p); y2 = m*x(m-k); if y1 == y2 disp('The given system y(n) = n*x(n) is Time invariant') else disp('The given system y(n) = n*x(n) is Time variant') end end OUTPUT: RESULT:Linearity and time invariance properties of a given system are verified successfully using MATLAB.
Sri Venkateswara College of Engineering, Dept of E.C.E
21
Basic Simulation lab
8. UNIT SAMPLE, UNIT STEP, SINUSOIDAL RESPONSE OF AN LTI SYSTEM AND TOVERIFY THE STABILITY OF AN LTI SYSTEM AIM: To write a MATLAB program for computing the unit sample, unit step responses of a given LTI system and to verify the stability of an LTI system using MATLAB. SOFTWARE USED: MATLAB 7.0.1 version. COMMANDS USED: input ( ): This command is used for accepting the data from the keyboard. sin ( ): This command is used for generation of a sinusoidal signal. plot( ): This command is used for plotting the linear plots or continuous signals. if(num,den): This command is used for finding the transfer function of the system if the numerators, denominator of the system are defined. step (sys): This command is used for finding the step response of a system of the transfer function sys is defined impulse (sys): This command is used for finding the impulse response of a system if the transfer function sys is defined. .lsin(sys,u,t): This command is used to plot the time response of the LTI system model sys to the input signal described by u and t. The time vector T consists of regularly spaced time samples and u is a matrix with as many columns as inputs.. ltiview(plot type,sys): This command is used to open an LTI viewer and specifies which responses to plot in the LTI viewer. plot type may be any one of the following strings. step---step response impulse-----impulse response bode----------Bode diagram bode mag---------Bode magnitude diagram nicholas--------Nicholas plot sigma------------singular value plot pzmap--------pole/zero map
Sri Venkateswara College of Engineering, Dept of E.C.E
22
Basic Simulation lab
iopzmap-------I/o pole/zero map.
max ( ): This command is used for finding the maximum value among the given vectors or signals. real ( ): This command is used for finding the real part of a complex signal. pole (sys): This command is used to find the poles of a transfer function sys. PROGRAM: %UNIT SAMPLE, UNIT STEP, SINUSOIDAL RESPONSES OF LTI SYSTEM TO VERIFY STABILITY num = input('Enter the numerator polynomial = '); den = input('Enter the denominator polynomial = '); sys=tf(num,den); % the transfer function of system is defined by its num& den % To find the Step response stp = step(sys); figure; plot(stp); title('STEP RESPONSE'); xlabel('time ------- t'); ylabel('amplitude'); grid; % To find the Impulse response i=impulse(sys); figure; plot(i); title('IMPULSE RESPONSE'); xlabel('time ------- t'); ylabel('amplitude'); grid; % To find Sinusoidal response t=0:0.01:1; x=sin(2*pi*1*t); s=lsim(sys,x,t); % simulate time response of LTI system where x is type of input figure; subplot(2,1,1); plot(t,x); xlabel('t'); ylabel('x'); title('Sinusoidal signal'); grid; subplot(2,1,2); plot(t,s);
Sri Venkateswara College of Engineering, Dept of E.C.E
23
Basic Simulation lab
title('SINUSOIDAL RESPONSE') xlabel('time ------- t'); ylabel('amplitude'); grid; % Alternate method to plot system response ltiview({'step','impulse'},sys); % To find the stability of the system p=pole(sys) P=real(p) P1 = max(P) if P1 > 0 disp('the given system is unstable'); else disp('the given system is stable'); end OUTPUT:
RESULT: Unit step, unit impulse, sinusoidal responses of the given LTI system are obtained and the stability of given LTI system is verified using MATLAB.
Sri Venkateswara College of Engineering, Dept of E.C.E
24
Basic Simulation lab
9. DEMONSTRATION OF GIBBS PHENOMENON AIM: To write a MATLAB program that demonstrates Gibbs phenomenon. SOFTWARE USED: MATLAB 7.0.1 version. COMMANDS USED: linspace (p,q,n): This command is used for generating n values from starting value p to ending value q. zeros(m,n): This command is used for generating a matrix of all zeros of order m*n. ones(m,n): This command is used for generating a matrix of all ones of order m*n. sin( ): This command is used for generation of a sinusoidal signal. cos( ): This command is used for generation of a cosine signal. axis([m n p q]): This command is used for setting range of values between m and n to x-axis and p and q to y-axis. int2str(x): This command is used for rounding the elements of the matrix X to integers and converts the result into a string matrix. Return NAN and INF elements as string NAN and INF respectively.
PROGRAM: t=linspace(-2,2,2000); u=linspace(-2,2,2000); sq=[zeros(1,500),2*ones(1,1000),zeros(1,500)]; k=2; N=[1,3,7,19,49,70]; for n=1:6; an=[]; for m=1:N(n) an=[an,2*k*sin(m*pi/2)/(m*pi)]; end fN=k/2; for m=1:N(n) fN=fN+an(m)*cos(m*pi*t/2); end
Sri Venkateswara College of Engineering, Dept of E.C.E
25
Basic Simulation lab
nq=int2str(N(n)); subplot(3,2,n); plot(u,sq,'r','LineWidth',2); hold on; plot(t,fN,'LineWidth',2); hold off; axis([-2 2 -0.5 2.5]); grid; xlabel('Time'); ylabel('y_N(t)'); title(['N= ',nq]); end OUTPUT:
RESULT:Hence Gibbs phenomenon is verified using MATLAB.
Sri Venkateswara College of Engineering, Dept of E.C.E
26
Basic Simulation lab
10.FOURIER TRANSFORM OF SIGNAL
AIM: To obtain Fourier Transform and inverse Fourier Transform of given signal/sequence and plot magnitude and phase spectrum. SOFTWARE USED: MATLAB 7.0.1 version. COMMANDS USED: fft(X,N): For finding Fourier Transform of a signal or sequence X of N-points ifft(X,N): For finding Inverse Fourier Transform of signal or sequence of N-points. sin( ): This command is used for generation of a sinusoidal signal. abs( ): For finding absolute/magnitude of signal or sequence or any complex vector or number. angle( ):For finding phase of a signal or a complex vector. plot( ): This command is used for plotting the linear plots or continuous signals. disp ( ): This command is used for displaying the messages and the values stored in the variables.
PROGRAM: N=input('Enter the length of input signal='); %prepare to sample a signal for two seconds,at a rate of 100 samples per second. Fs = 100; % Sampling rate t=[0:2*Fs+1]'/Fs; % Time points for sampling x=sin(2*pi*t)+sin(4*pi*t); figure(1); subplot(4,1,1); plot(t,x); title('signal in time domain'); XK=fft(x,N); m=abs(XK); subplot(4,1,2); plot(m); title('Magnitude spectra'); a=angle(XK); subplot(4,1,3); plot(a);
Sri Venkateswara College of Engineering, Dept of E.C.E
27
Basic Simulation lab
title('Phase spectra'); xn=ifft(XK,N); subplot(4,1,4); plot(xn); %FFT AND IFFT OF A COMPLEX SIGNAL f1 = input('Enter the frequency of signal 1 :') f2 = input('Enter the frequency of signal 2 :') fs = 5*max(f1,f2) N = fs % Enter the size of fft t1= 0:1/fs:1-1/fs; x1=sin(2*pi*f1*t1)+sin(2*pi*f2*t1); figure(2); subplot(4,1,1); plot(t1,x1); title('signal in time domain'); y=fft(x1,N); m=abs(y); f=(0:N-1)/(N/fs); subplot(4,1,2); plot(f,m); title('Magnitude spectra'); a=angle(y); subplot(4,1,3); plot(f,a); title('Phase spectra'); xn1=ifft(y); subplot(4,1,4); plot(t1,xn1); title('Inverse fourier transform');
OUTPUT:
RESULT: Magnitude and phase spectrum of Fourier Transform signal are evaluated and plotted.
Sri Venkateswara College of Engineering, Dept of E.C.E
28
Basic Simulation lab
11. LAPLACE TRANSFORM OF A FUNCTION AIM: To write a MATLAB program to obtain Laplace and inverse Laplace transform of different functions. SOFTWARE USED: MATLAB 7.0.1 version. COMMANDS USED: syms( ): This command is used to represent as a short cut for constructing symbolic objects.syms arg1,arg2,is short hand notation for arg1= sym(arg 1); arg2=sym(arg 2); syms arg1,arg2,.. real is short hand notation for arg1=sym(arg 1,real); arg2=sym(arg 2,real); exp(x): This command is used to find the exponential of the elements of X i.e,e^x. Ex: For complex signals z=X+iY then Exp(z)=exp(x)[cosY+IsinY]. laplace(F): This command is used for finding the laplace transform of the scalar sym F with default independent variable t. The default return is a function of s.if F=F(s),thenlaplace returns the function of t.L=L(t) by definition L(s)=F(t)e^-stdt where integration occurs with respesct to t. B=simplify(A): This command is used to perform model reduction like techniques to detect and eliminate redundant copies of uncertain elements. Depending on the result, the class of B may be lower than A. The auto simplify property of each uncertain element in A governs what reduction methods are used. After reduction any uncertain element which doesnot actually affect the result is deleted from the representation. F=ilaplace(L):This command is used to find the inverse laplace transform of the scalars in L with default independent variable s. The default return is a function of t. pretty(s): This command is used to print the symbolic expression s in a format that resembles typeset mathematics.
Sri Venkateswara College of Engineering, Dept of E.C.E
29
Basic Simulation lab
PROGRAM:
%Finding laplace transform
syms t s % specify that variables t and s are symbolic ones f=-1.25+3.5*t*exp(-2*t)+1.25*exp(-2*t) F=laplace(f,t,s) simplify(F) pretty(ans)
%Finding inverse laplace transform
a=(s-5)/(s*(s+2)^2) f=ilaplace(a) simplify(f) pretty(f) OUTPUT:
RESULT:Hence Laplace Transform and Inverse Laplace Transform are verified using MATLAB.
Sri Venkateswara College of Engineering, Dept of E.C.E
30
Basic Simulation lab
12. POLES AND ZEROS OF A TRANSFER FUNCTION AIM: To plot pole-zero map for a given transfer function in s-plane and z-plane. SOFTWARE USED: MATLAB 7.0.1 version. COMMANDS USED: tf(num,den): This command is used for finding the transfer function of the system if the numerator, denominator of the system are defined. pole(h): This command is used for finding the pole of a transfer function h. zero(h): This command is used for finding the zero of a transfer function h. zpk(h): This command is used for finding the zeroes, poles, gain of a transfer function h. pzmap(h): This command is used to plotting the poles, zeros of a transfer function h in z-plane and in s-plane. PROGRAM: % POLES AND ZEROS OF A TRANSFER FUNCTION num = [-10 20 0] den= [1 7 20 28 19 5] h = tf(num,den); p=pole(h) z=zero(h) hpz=zpk(h); hz = tf(num,den,0.1,'variable','z^-1'); pa=pole(hz) za=zero(hz) hzpz=zpk(hz) subplot(1,2,1); pzmap(h) subplot(1,2,2); pzmap(hz) OUTPUT:
RESULT:Poles and zeros of a given transfer function are located and plotted in s-plane and z-plane.
Sri Venkateswara College of Engineering, Dept of E.C.E
31
Basic Simulation lab
13. SAMPLING THEORM AIM: To Demonstrate Sampling Theorem and aliasing effect using MATLAB.
SOFTWARE USED: MATLAB 7.0.1 version. COMMANDS USED: sin ( ): This command is used for generation of a sinusoidal signal. plot ( ): This command is used for plotting continuous signals disp( ): This command is used to display the output and the values. input( ): This command is used for accepting the data through keyboard.
zeros(M,N) : This Command generates a Matrix of all zeros of size M by N
PROGRAM: f= input('Enter the frequency:') fs= input('Enter the sampling frequency:') t=0:1:fs1; iffs>= 2*f disp('sampling theorem is satisfied '); %ploting of sinusoidal signal n=0:1/fs:pi; x=sin(2*pi*f*n); figure(1); plot(n,x); title('sinusoidal signal'); xlabel('time ------- t'); ylabel('amplitude'); % To plot the frequency spectrum (indirect method) s1=0:1/f:1-1/f; s2=(1-1/f)-s1; w1=[s2,zeros(1,(fs))]; w2=[zeros(1,fs-f),s1,s2]; figure(2); plot(t,w1,'r',t,w2,'b'); title('Frequency plot'); xlabel('frequency ------- f');
Sri Venkateswara College of Engineering, Dept of E.C.E
32
Basic Simulation lab
ylabel('amplitude'); legend('original signal', 'lower side band & upper side band'); else disp('sampling theorem is not satisfied'); % To plot the frequency spectrum (indirect method) s1=0:1/f:1-1/f; s2=(1-s1); w1=[s2,zeros(1,(fs))]; w2=[zeros(1,fs-f),s1,s2]; figure(2); plot (t,w1,'r',t,w2,'b'); title ('Frequency plot'); xlabel ('frequency ------- f'); ylabel ('amplitude'); legend ('original signal', 'lower side band & upper side band'); end
OUTPUT:
RESULT: Sampling theorem was verified and aliasing was identified when the sampling frequency is less than the nyquist frequency using MATLAB.
Sri Venkateswara College of Engineering, Dept of E.C.E
33
Basic Simulation lab
14. GAUSSIAN NOISE
AIM:To Generate Gaussian Noise and to compute its Mean, M.S. Values, Skew, kurtosis, PSD and PDF SOFTWARE USED: MATLAB 7.0.1 version. COMMANDS USED: wgn (M, N, P): This Command generates an M-by-N matrix of white Gaussian noise specifies the power of theoutput noise in dBW. mean (X) : is a row vector containing the mean value of each column. For N-D arrays, mean(X) is the mean value of the elements along the first nonsingleton dimension of X. std(X) :This Command returns the standard deviation. For matrices is a row vector containing the standard deviation of each column. ForN-D arrays, STD operates along the first non-singleton dimension of X.
kurtosis (X): returns the sample kurtosis of the values in X. For avector input, K is the fourth central moment of X, divided by fourth power of its standard deviation. For a matrix input, K is a row vector containing the sample kurtosis of each column of X. For N-D arrays, kurtosis operates along the first non-singleton dimension.
skewness(X): returns the sample skewness of the values in X. For a vector input, S is the third central moment of X, divided by the cube of its standard deviation. For a matrix input, S is a row vector containing the sample skewness of each column of X. For N-D arrays,skewnessoperates along the first non-singleton dimension.
H = Spectrum.welch: returns a Welch spectral estimator in H. psd( ): Calculates the power spectral density
Sri Venkateswara College of Engineering, Dept of E.C.E
34
Basic Simulation lab
pdf (NAME, X,A) : returns an array of values of the probability density function for the oneParameter probability distribution specified by NAME with parameter values A, evaluated at the values in X.
PROGRAM:
% Generate white gaussian noise x=wgn(100,1,1); figure(1); plot(x); title('AWGN'); % To find mean of x m = mean(x) % To find standard deviation sd = std(x) % To find skewness s = skewness(x) % To find Kurtosis k = kurtosis(x) % To find psd of x fs =100; h=spectrum.welch figure(2) psd(h,x,'fs',fs); % To find probability distribution function p = pdf('norm',x,0,1); figure(3); plot(p); title('pdf'); OUTPUT:
RESULT: Additive White Gaussian noise was generated and its PSD and PDF were plotted and its Mean, Standard Deviation, Kurtosis, Skew were Computed using MATLAB.
Sri Venkateswara College of Engineering, Dept of E.C.E
35
Basic Simulation lab
PROCEDURE: Open MATLAB Open new M-file Type the program Save in current directory Compile and Run the program For the output see command window\ Figure window
Sri Venkateswara College of Engineering, Dept of E.C.E
36