MATLAB Experiments C-1
MATLAB Experiments C-1
EXPERIMENT NO-1
AIM: -
To write a MATLAB program to perform some basic operation on matrices such as addition,
subtraction, multiplication.
SOFTWARE REQURIED:
1. MATLAB R2010a.
2.Windows XP SP2.
THEORY: -
MATLAB, which stands for MATrixLABoratory, is a state-of-the-art mathematical software package,
which is used extensively in both academia and industry. It is an interactive program for numerical
computation and data visualization, which along with its programming capabilities provides a very
useful tool for almost all areas of science and engineering. Unlike other mathematical packages, such
as MAPLE or MATHEMATICA, MATLAB cannot perform symbolic manipulations without the use
of additional Toolboxes. It remains however, one of the leading software packages for numerical
computation. As you might guess from its name, MATLAB deals mainly with matrices. A scalar is a
1-by-1 matrix and a row vector of length say 5, is a 1-by-5 matrix. One of the many advantages of
MATLAB is the natural notation used. It looks a lot like the notation that you encounter in a linear
algebra. This makes the use of the program especially easy and it is what makes MATLAB a natural
choice for numerical computations. The purpose of this experiment is to familiarize MATLAB, by
introducing the basic features and commands of the program.
Built in Functions:
1. Scalar Functions:
Certain MATLAB functions are essentially used on scalars, but operate element-wise when
applied to a matrix (or vector). They are summarized below.
1. sin - trigonometric sine
2. cos - trigonometric cosine
3. tan - trigonometric tangent
4. asin - trigonometric inverse sine (arcsine)
5. acos - trigonometric inverse cosine (arccosine)
6. atan - trigonometric inverse tangent (arctangent)
7. exp - exponential
8. log - natural logarithm
9. abs - absolute value
10. sqrt - square root
11. rem - remainder
12. round - round towards nearest integer
13. floor - round towards negative infinity
14. ceil - round towards positive infinity
2. Vector Functions:
Other MATLAB functions operate essentially on vectors returning a scalar value. Some of
these functions are given below.
1
1. max largest component: get the row in which the maximum element lies
2. min smallest component
3. lengthlength of a vector
4. sortsort in ascending order
5. sumsum of elements
6. prod product of elements
7. medianmedian value
8. meanmean value std standard deviation
3. Matrix Functions:
Much of MATLAB‟s power comes from its matrix functions. These can be further separated
into two sub-categories.
The first one consists of convenient matrix building functions, some of which are given
below.
1. eye - identity matrix
2. zeros - matrix of zeros
3. ones - matrix of ones
4. diag - extract diagonal of a matrix or create diagonal matrices
5. triu - upper triangular part of a matrix
6. tril - lower triangular part of a matrix
7. rand - randomly generated matrix
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
PROGRAM:-
clc;
close all;
clear all;
a=[1 2 -9 ; 2 -1 2; 3 -4 3];
b=[1 2 3; 4 5 6; 7 8 9];
disp('The matrix a= ');
a
disp('The matrix b= ');
b
2
% to find sum of a and b
c=a+b;
disp('The sum of a and b is ');
c
% to find difference of a and b
d=a-b;
disp('The difference of a and b is ');
d
%to find multiplication of a and b
e=a*b;
disp('The product of a and b is ');
e
RESULT:-
Finding addition, subtraction, multiplication using MATLAB was Successfully completed.
3
Experiment No-2
AIM:-To write a “MATLAB” Program to generate various signals and sequences, such as unit impulse,
unit step, unit ramp, sinusoidal, square, sawtooth, triangular, sinc signals.
SOFTWARE REQURIED:-
1.MATLAB R2010a.
2.Windows XP SP2.
THEORY:-
4
UNIT STEP FUNCTION
The unit step function and the impulse function are considered to be fundamental functions in
engineering, and it is strongly recommended that the reader becomes very familiar with both of these
functions.The unit step function, also known as the Heaviside function, is defined as such:
Sinc Function
There is a particular form that appears so frequently in communications engineering, that wegive it its
own name. This function is called the "Sinc function and discussed below
The Sinc function is defined in the following manner:
And Sinc(0)=1
The value of sinc(x) is defined as 1 at x = 0, since
Rect Function
The Rect Function is a function which produces a rectangular centered at t = 0. The Rect function pulse
also has a height of 1. The Sinc function and the rectangular function form a Fourier transform pair.A
Rect function can be written in the form:
where the pulse is centered at X and has width Y. We can define the impulse function above in
termsof the rectangle function by centering the pulse at zero (X = 0), setting it's height to 1/A and
setting the pulse width to A, which approaches zero:
We can also construct a Rect function out of a pair of unit step functions
Here, both unit step functions are set a distance of Y/2 away from the center point of (t - X).
5
SAWTOOTH: -
The sawtooth wave (or saw wave) is a kind of non-sinusoidal waveform. It is named a sawtooth
based on its resemblance to the teeth on the blade of a saw. The convention is that a sawtooth wave
ramps upward and then sharply drops. However, there are also sawtooth waves in which the wave
ramps downward and then sharply rises. The latter type of sawtooth wave is called a 'reverse sawtooth
wave' or 'inverse sawtooth wave'. As audio signals, the two orientations of sawtooth wave sound
identical. The piecewise linear function based on the floor function of time t, is an example of a
sawtooth wave with period 1.
TRIANGLE WAVE
A triangle wave is a non-sinusoidal waveform named for its triangular shape.Abandlimited triangle
wave pictured in the time domain (top) and frequency domain (bottom). The fundamental is at 220 Hz
(A2). Like a square wave, the triangle wave contains only odd harmonics. However, the higher
harmonics roll off much faster than in a square wave (proportional to the inverse square of the harmonic
number as opposed to just the inverse).It is possible to approximate a triangle wave with additive
synthesis by adding odd harmonics of the fundamental, multiplying every (4n..1)th harmonic by 1 (or
changing its phase by ), and rolling off the harmonics by the inverse square of their relative frequency
to the fundamental. This infinite Fourier series converges to the triangle wave:
6
where f is the signal frequency, fs is the sampling frequency, 𝜃 is the phase and A is the amplitude
ofthe signal.
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
PROGRAM:-
%unit impulse function%
clc;
clearall;
closeall;
t=-10:1:10;
x=(t==0);
subplot(2,1,1);
plot(t,x,'g');
xlabel('time');
ylabel('amplitude');
title('unit impulse function');
subplot(2,1,2);
stem(t,x,'r');
xlabel('time');
ylabel('amplitude');
title('unit impulse discreat function');
7
%unit ramp function%
clc;
clearall;
closeall;
t=0:20;
x=t;
subplot(2,1,1);
plot(t,x,'g');
xlabel('time');
ylabel('amplitude');
title('unit ramp function');
subplot(2,1,2);
stem(t,x,'r');
xlabel('time');
ylabel('amplitude');
title('unit ramp discreat function');
%sinusoidal function%
clc;
clearall;
closeall;
t=0:0.01:2;
x=sin(2*pi*t);
subplot(2,1,1);
plot(t,x,'g');
xlabel('time');
ylabel('amplitude');
title('sinusoidal signal');
subplot(2,1,2);
stem(t,x,'r');
xlabel('time');
ylabel('amplitude');
title('sinusoidal sequence');
%square function%
clc;
clearall;
closeall;
t=0:0.01:2;
x=square(2*pi*t);
subplot(2,1,1);
plot(t,x,'g');
xlabel('time');
ylabel('amplitude');
title('square signal');
subplot(2,1,2);
stem(t,x,'r');
xlabel('time');
ylabel('amplitude');
title('square sequence');
8
%sawtooth function%
clc;
clearall;
closeall;
t=0:0.01:2;
x=sawtooth(2*pi*5*t);
subplot(2,1,1);
plot(t,x,'g');
xlabel('time');
ylabel('amplitude');
title('sawtooth signal');
subplot(2,1,2);
stem(t,x,'r');
xlabel('time');
ylabel('amplitude');
title('sawtooth sequence');
%trianguler function%
clc;
clearall;
closeall;
t=0:0.01:2;
x=sawtooth(2*pi*5*t,0.5);
subplot(2,1,1);
plot(t,x,'g');
xlabel('time');
ylabel('amplitude');
title('trianguler signal');
subplot(2,1,2);
stem(t,x,'r');
xlabel('time');
ylabel('amplitude');
title('trianguler sequence');
%sinc function%
clc;
clearall;
closeall;
t=linspace(-5,5);
x=sinc(t);
subplot(2,1,1);
plot(t,x,'g');
xlabel('time');
ylabel('amplitude');
title('sinc signal');
subplot(2,1,2);
stem(t,x,'r');
xlabel('time');
ylabel('amplitude');
title('sinc sequence');
9
EXPERIMENT No-3
AIM:-
To performs operations on signals and sequences such as addition, multiplication, scaling, shifting,
folding, computation of energy and average power.
SOFTWARE REQURIED:-
1.MATLAB R2010a.
2.Windows XP SP2.
THEORY:-
Basic Operation on Signals:
Time shifting: y(t)=x(t-T)The effect that a time shift has on the appearance of a signal If T is a
positive number, the time shifted signal, x(t -T ) gets shifted to the right, otherwise it gets shifted left.
x(n) ZT x(n-T)
10
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
PROGRAM:-
clear all;
close all;
t=0:.01:1;
% generating two input signals
x1=sin(2*pi*4*t);
x2=sin(2*pi*8*t);
subplot(2,2,1);
plot(t,x1);
xlabel('time');
ylabel('amplitude');
title('signal1:sine wave of frequency 4Hz');
subplot(2,2,2);
plot(t,x2);
xlabel('time');
subplot(4,1,3);
ylabel('amplitude');
title('signal2:sine wave of frequency 8Hz');
% addition of signals
y1=x1+x2;
subplot(2,2,3);
plot(t,y1);
xlabel('time');
ylabel('amplitude');
title('resultant signal:signal1+signal2');
% multiplication of signals
y2=x1.*x2;
subplot(2,2,4);
plot(t,y2);
xlabel('time');
ylabel('amplitude');
title('resultant signal:dot product of signal1 and signal2');
% scaling of a signal1
A=10;
y3=A*x1;
figure;
11
subplot(2,2,1);
plot(t,x1);
xlabel('time');
ylabel('amplitude');
title('sine wave of frequency 4Hz')
subplot(2,2,2);
plot(t,y3);
xlabel('time');
ylabel('amplitude');
title('amplified input signal1 ');
% folding of a signal1
l1=length(x1);
nx=0:l1-1;
subplot(2,2,3);
plot(nx,x1);
xlabel('nx');
ylabel('amplitude');
title('sine wave of frequency 4Hz')
y4=fliplr(x1);
nf=-fliplr(nx);
subplot(2,2,4);
plot(nf,y4);
xlabel('nf');
ylabel('amplitude');
title('folded signal');
%shifting of a signal
figure;
t1=0:.01:pi;
x3=8*sin(2*pi*2*t1);
subplot(3,1,1);
plot(t1,x3);
xlabel('time t1');
ylabel('amplitude');
title('sine wave of frequency 2Hz');
subplot(3,1,2);
plot(t1+10,x3);
xlabel('t1+10');
ylabel('amplitude');
title('right shifted signal');
subplot(3,1,3);
plot(t1-10,x3);
xlabel('t1-10');
ylabel('amplitude');
title('left shifted signal');
%operations on sequences
n1=1:1:9;
s1=[1 2 3 0 5 8 0 2 4];
figure;
subplot(2,2,1);
stem(n1,s1);
12
xlabel('n1');
ylabel('amplitude');
title('input sequence1');
n2=-2:1:6;
s2=[1 1 2 4 6 0 5 3 6];
subplot(2,2,2);
stem(n2,s2);
xlabel('n2');
ylabel('amplitude');
title('input sequence2');
% addition of sequences
s3=s1+s2;
subplot(2,2,3);
stem(n1,s3);
xlabel('n1');
ylabel('amplitude');
title('sum of two sequences');
% multiplication of sequences
s4=s1.*s2;
subplot(2,2,4);
stem(n1,s4);
xlabel('n1');
ylabel('amplitude');
title('product of two sequences');
% scaling of a sequence
figure;
subplot(2,2,1);
stem(n1,s1);
xlabel('n1');
ylabel('amplitude');
title('input sequence1');
s5=4*s1;
subplot(2,2,2);
stem(n1,s5);
xlabel('n1');
ylabel('amplitude');
title('scaled sequence1');
subplot(2,2,3);
stem(n1-2,s1);
xlabel('n1');
ylabel('amplitude');
title('left shifted sequence1');
subplot(2,2,4);
stem(n1+2,s1);
xlabel('n1');
ylabel('amplitude');
title('right shifted sequence1');
% folding of a sequence
13
l2=length(s1);
nx1=0:l2-1;
figure;
subplot(2,1,1);
stem(nx1,s1);
xlabel('nx1');
ylabel('amplitude');
title('input sequence1');
s6=fliplr(s1);
nf2=-fliplr(nx1);
subplot(2,1,2);
stem(nf2,s6);
xlabel('nf2');
ylabel('amplitude');
title('folded sequence1');
% program for energy of a sequence
e1=sum(abs(z1).^2);
e1
% program for energy of a signal
t=0:pi:10*pi;
z2=cos(2*pi*50*t).^2;
e2=sum(abs(z2).^2);
e2
% program for power of a saequence
p1= (sum(abs(z1).^2))/length(z1);
p1
% program for power of a signal
p2=(sum(abs(z2).^2))/length(z2);
p2
14
EXPERIMENTY No-4
AIM: Finding even and odd part of the signal and sequence and also find real and imaginary parts of
signal.
Software Required:
Matlab software 7.0 and above.
Theory:
EVEN AND ODD PART OF A SIGNAL:
Any signal x(t) can be expressed as sum of even and odd components I e
X(t)=xe(t)+xo(t)
Program:
Clc;
close all;
clear all;
% Even and odd parts of a signal
t=0:.005:4*pi;
x=sin(t)+cos(t); % x(t)=sint(t)+cos(t)
subplot(2,2,1)
plot(t,x)
xlabel('t');
ylabel('amplitude')
title('input signal')
y=sin(-t)+cos(-t) % y=x(-t)
subplot(2,2,2)
plot(t,y)
xlabel('t');
ylabel('amplitude')
title('input signal with t=-t')
z=x+y
subplot(2,2,3)
plot(t,z/2)
xlabel('t');
ylabel('amplitude')
title('even part of the signal')%assigning a name to the plot
p=x-y
subplot(2,2,4)
plot(t,p/2)
xlabel('t');
15
ylabel('amplitude');
title('odd part of the signal');
% Even and odd parts of a sequence
z=[0,2+j*4,-3+j*2,5-j*1,-2-j*4,-j*3,0];
n=-3:3
% plotting real and imginary parts of the sequence
figure;
subplot( 2,1,1);
stem(n,real(z));
xlabel('n');
ylabel('amplitude');
title('real part of the complex sequence');
subplot( 2,1,2);
stem(n,imag(z));
xlabel('n');
ylabel('amplitude');
title('imaginary part of the complex sequence');
zc=conj(z);
zc_folded= fliplr(zc);
zc_even=.5*(z+zc_folded);
zc_odd=.5*(z-zc_folded);
% plotting even and odd parts of the sequence
figure;
subplot( 2,2,1);
stem(n,real(zc_even));
xlabel('n');
ylabel('amplitude');
title('real part of the even sequence');
subplot( 2,2,2);
stem(n,imag(zc_even));
xlabel('n');
ylabel('amplitude');
title('imaginary part of the even sequence');
subplot( 2,2,3);
stem(n,real(zc_odd));
xlabel('n');
ylabel('amplitude');
title('real part of the odd sequence');
subplot( 2,2,4);
stem(n,imag(zc_odd));
xlabel('n');
ylabel('amplitude');
title('imaginary part of the odd sequence');
RESULT: Even and odd part of the signal and sequence is computed.
16
EXPERIMENTY No-5
AIM: -
To find the output with linear convolution operation Using MATLAB Software.
SOFTWARE REQURIED:-
1.MATLAB7.2(2006b) / MATLAB 8.6(2015b)/MATLAB 7.6 2008a(Trial version)/MATLAB
7.9(2009b)(Trial Version)/MATLAB 7.10(2010a) Trial version.
2.Windows XP SP2.
THEORY:-
Linear Convolution involves the following operations.
1. Folding
2. Multiplication
3. Addition
4. Shifting
These operations can be represented by a Mathematical Expression as follows:
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