Professional Documents
Culture Documents
AIM:
To understand MATLAB basic features and built in functions available in MATLAB.
OPERATING SYSTEM:
Windows XP
THEORY:
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.
Matrices :
1. row matrix: Elements in a row are separated either by using white spaces or
commas eg: a=[1 2 4 5]
2. column matrix: Elements which differ by a column are seperated by enter or
semicolumn eg: b=[1; 2; 3]
3. square matrix: eg: c = [a,b;c,d] is a 2*2 matrix
Vectors:
d= [0:7]
”d” is a vector or row matrix with first element as 0 and last element as 7 and increment is by
default 1.
The default increment can be changed (to 0.1) by using increment field in between as
e= [0:0.1:7].
d(1:2) allows to look into vector with increment 1
e(1:2:4) look with increment
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.
eg: diag([0.9092;0.5163;0.2661])
ans =
0.9092 0 0
0 0.5163 0
0 0 0.2661
Operations on matrices:
eg: A = [9,7,0;0,8,6;7,1,-6];
The eigenvalues and eigenvectors of A (i.e. the numbers λ and vectors x that satisfy
Ax = λx ) can be obtained through the eig command.
eig(A)
[X,D]=eig(A)
produces a diagonal matrix D with the eigen values on the main diagonal, and a full matrix X
whose columns are the corresponding eigenvectors.
RESULT:
MATLAB basic features and built in functions were practiced thoroughly.
OPERATING SYSTEM:
Windows XP
THEORY:
If x and y are two vectors of the same length then plot(x,y) plots x versus y.
For example, to obtain the graph of y = cos(x) from – π to π, we can first define the vector x
with components equally spaced numbers between – π and π, with increment, say 0.01.
» x=-pi:0.01:pi;
We placed a semicolon at the end of the input line to avoid seeing the (long) output.
Note that the smallest the increment, the “smoother” the curve will be.
Next, we define the vector y
» y=cos(x);
(using a semicolon again) and we ask for the plot
» plot(x,y)
It is good practice to label the axis on a graph and if applicable indicate what each axis
represents. This can be done with the xlabel and ylabel commands.
» xlabel('x')
» ylabel('y=cos(x)')
Inside parentheses, and enclosed within single quotes, we type the text that we wish to be
displayed along the x and y axis, respectively. We could even put a title on top using
» title('Graph of cosine from -pi to pi')
Various line types, plot symbols and colors can be used. If these are not specified (as in the
case above) MATLAB will assign (and cycle through) the default ones as given in the table
below.
y yellow . point
m magenta o circle
c cyan x x-mark
r red + plus
g green - solid
b blue * star
w white : dotted
k black -. dashdot
-- dashed
So, to obtain the same graph but in green, we type
» plot(x,y,’g’)
where the third argument indicating the color, appears within single quotes. We could get a
dashed line instead of a solid one by typing
» plot(x,y,’--’)
or even a combination of line type and color, say a blue dotted line by typing
» plot(x,y,’b:’)
Multiple curves can appear on the same graph. If for example we define another vector
ECE Dept., SVEC 5
Basic simulation lab
» z = sin(x);
we can get both graphs on the same axis, distinguished by their line type, using
» plot(x,y,'r--',x,z,'b:')
When multiple curves appear on the same axis, it is a good idea to create a legend to label
and distinguish them. The command legend does exactly this.
» legend('cos(x)','sin(x)')
The text that appears within single quotes as input to this command, represents the legend
labels. We must be consistent with the ordering of the two curves, so since in the plot
command we asked for cosine to be plotted before sine, we must do the same here.
At any point during a MATLAB session, you can obtain a hard copy of the current plot by
either issuing the command print at the MATLAB prompt, or by using the command
menus on the plot window. In addition, MATLAB plots can by copied and pasted (as
pictures) in your favorite word processor (such as Microsoft Word). This can be achieved
using the Edit menu on the figure window. Another nice feature that can be used in
conjunction with plot is the command grid, which places grid lines to the current axis
(just like you have on graphing paper). Type help grid for more information. Other
commands for data visualization that exist in MATLAB include subplot create an array
of (tiled) plots in the same window loglog plot using log-log scales semilogx plot
using log scale on the x-axis semilogy plot using log scale on the y-axis
The sinc function has a value of 1 whenx is equal to zero, and a value of
where N is a user-specified positive integer. For N odd, the Dirichlet function has a period of
2π; for N even, its period is 4π. The magnitude of this function is (1/N) times the magnitude
of the discrete-time Fourier transform of the N-point rectangular window.
2.1 PROGRAM:
t = (0:0.001:1)';
% 1001-element row vector that represents time running from 0 to 1 s in steps of 1 ms.
%the transpose operator (') changes the row vector into a column
imp= [1; zeros(99,1)]; % Impulse
unit_step = ones(100,1); % Step (with 0 initial cond.)
ramp_sig= t; % Ramp
quad_sig=t.^2; % Quadratic
sq_wave = square(4*pi*t); % Square wave with period 0.5
% To generate 1.5 s of a 50 Hz saw tooth wave with a sample rate of 10 kHz and plot 0.2 s of the
%generated waveform
fs = 10000;
t = 0:1/fs:1.5;
x = sawtooth(2*pi*50*t);
plot(t,x), axis([0 0.2 -1 1])
% To compute 2 s of a linear chirp signal with a sample rate of 1 kHz, that starts at DC and crosses %
150 Hz at 1 s, use
t = 0:1/1000:2;
y = chirp(t,0,1,150);
% The pulse train is defined to have a sample rate of 50 kHz, a pulse train length of 10 ms, and a pulse
repetition rate of 1 kHz; D specifies the delay to each pulse repetition in column 1 and an optional
attenuation for each repetition in column 2. The pulse train is constructed by passing the name of the
% To plot the sinc function for a linearly spaced vector with values ranging from -5 to 5
x = linspace(-5,5);
y = sinc(x);
plot(x,y)
%To plot the Dirichlet function over the range 0 to 4π for N = 7 and N = 8, use
x = linspace(0,4*pi,300);
plot(x,diric(x,7)); axis tight;
plot(x,diric(x,8)); axis tight;
Output:
1.5 s of a 50 Hz sawtooth wave with a sample rate of 10 kHz and plot 0.2 s
Plot of the sinc function for a linearly spaced vector with values ranging from -5 to 5
2.2 PROGRAM:
% step sequence
x = ones(1,N) ;
n=0:1:N-1;
figure(1)
stem(n,x)
axis([-1 N -1 2])
title('STEP SEQUENCE')
xlabel('samples ------- n')
ylabel('amplitude')
GRID
% impulse sequence
N1=N/2;
n=-N1:1:N1;
figure(2)
stem(n,x)
axis([-(N1) N1 -1 2])
title('IMPULSE SEQUENCE')
xlabel('samples ------- n')
ylabel('amplitude')
grid
% ramp sequence
n=0:1:N-1;
x=n;
figure(3)
stem(n,x)
axis([-1 N -1 N])
title('RAMP SEQUENCE')
xlabel('samples ------- n')
ylabel('amplitude')
grid
% exponential sequence
n=0:1:N-1;
x=exp(-n);
figure(4)
stem(n,x)
axis([-1 N -1 2])
title('EXPONENTIAL SEQUENCE')
xlabel('samples ------- n')
ylabel('amplitude')
grid
% signal generation
n=0:1/fs:1;
figure(5)
subplot(2,1,1),plot(n,ss)
title('sinusoidal signal')
xlabel('time ------- t')
ylabel('amplitude')
subplot(2,1,2),stem(n,ss)
title('sinusoidal sequence')
xlabel('samples ------- n')
ylabel('amplitude')
sq = square(2*pi*f*n)
figure(6)
subplot(2,1,1),plot(n,sq)
title('square wave signal')
xlabel('time ------- t')
ylabel('amplitude')
subplot(2,1,2),stem(n,sq)
title('square wave sequence')
xlabel('samples ------- n')
ylabel('amplitude')
x= -5:0.1:5
ts = sinc(x)
figure(7)
subplot(2,1,1),plot(x,ts)
title('sinc wave form')
xlabel('time ------- t')
ylabel('amplitude')
subplot(2,1,2),stem(x,ts)
title('sinc sequence')
xlabel('samples ------- n')
ylabel('amplitude')
sas= Sawtooth(2*pi*2*n)
figure(8)
subplot(2,1,1),plot(n,sas)
title('sawtooth signal')
xlabel('time ------- t')
ylabel('amplitude')
subplot(2,1,2),stem(n,sas)
title('sawtooth sequence')
xlabel('samples ------- n')
ylabel('amplitude')
subplot(2,1,2),stem(n,ch)
title('sweep sequence')
xlabel('samples ------- n')
ylabel('amplitude')
%t=0:0.01:1
d=0.1:0.2:1
y2=pulstran(n,d,'tripuls',0.1)
figure(10)
subplot(2,1,1),plot(n,tri)
title('triangular waveform')
xlabel('time ------- t')
ylabel('amplitude')
subplot(2,1,2),stem(n,tri)
title('triangular sequence')
xlabel('samples ------- n')
ylabel('amplitude')
1.5
1
amplitude
0.5
-0.5
-1
-1 0 1 2 3 4 5 6
samples ------- n
IMPULSE SEQUENCE
2
1.5
1
amplitude
0.5
-0.5
-1
-3 -2 -1 0 1 2 3
samples ------- n
RAMP SEQUENCE
6
3
amplitude
-1
-1 0 1 2 3 4 5 6
samples ------- n
EXPONENTIAL SEQUENCE
2
1.5
1
amplitude
0.5
-0.5
-1
-1 0 1 2 3 4 5 6
samples ------- n
sinusoidal signal
1
0.5
amplitude
0
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
time ------- t
sinusoidal sequence
1
0.5
amplitude
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
samples ------- n
0.5
amplitude
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
time ------- t
square wave sequence
1
0.5
amplitude
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
samples ------- n
0.5
amplitude
-0.5
-5 -4 -3 -2 -1 0 1 2 3 4 5
time ------- t
sinc sequence
1
0.5
amplitude
-0.5
-5 -4 -3 -2 -1 0 1 2 3 4 5
samples ------- n
sawtooth signal
1
0.5
amplitude
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
time ------- t
sawtooth sequence
1
0.5
amplitude
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
samples ------- n
sweep signal
1
0.5
amplitude
0
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
time ------- t
sweep sequence
1
0.5
amplitude
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
samples ------- n
triangular waveform
1
0.5
amplitude
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
time ------- t
triangular sequence
1
0.5
amplitude
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
samples ------- n
AIM:
To performs functions on signals and sequences such as addition, multiplication,
scaling, shifting, folding, computation of energy and average power.
OPERATING SYSTEM:
Windows XP
THEORY:
Power is a time average of energy (energy per unit time). This is useful when the energy of the signal
goes to infinity.
clc; t=-2:0.01:2;
f=input('enter the fundamental frequency:');
x=sin(2*pi*f*t);x1=x';
x_sum=x+x;
x_pro=x*x1;
x_sca=2*(x);
x_shi=(0.2-x);
x_fol=-x;
subplot(3,2,1)
plot(t,x);grid on;
title('original');
xlabel('t');
ylabel('x(t)');
subplot(3,2,2);
plot(t,x_sum);grid on;
title('addition');
xlabel('t');ylabel('x(t)');
subplot(3,2,3);
plot(t,x_pro);grid on;
subplot(3,2,4);
plot(t,x_sca); grid on;
title('scaled');
xlabel('t');
ylabel('x(t)');
subplot(3,2,5);
plot(t,x_shi);grid on;
title('shifted');
xlabel('t');
ylabel('x(t)');
subplot(3,2,6);
plot(t,x_fol);grid on;
title('folded');
xlabel('t');
ylabel('x(t)');
output:
original addition
1 2
x(t)
x(t)
0 0
-1 -2
-2 -1 0 1 2 -2 -1 0 1 2
t t
multiplication scaled
500 2
x(t)
x(t)
0 -2
-2 -1 0 1 2 -2 -1 0 1 2
t t
shifted folded
2 1
x(t)
x(t)
0 0
-2 -1
-2 -1 0 1 2 -2 -1 0 1 2
t t
clc;
t=-2:0.01:2;
f=input('enter the fundamental frequency:');
a=input('enter the amplitude:');
x=a*sin(2*pi*f*t);
ECE Dept., SVEC 19
Basic simulation lab
subplot(2,1,1);
title('test signal for energy calculation');
xlabel('t');
ylabel('x(t)');
plot(t,x);grid on;
subplot(2,1,2);
area(t,x.^2);
xlabel('t');
ylabel('x^2(t)');
title('The colored area gives the energy')
grid;
OUTPUT SCREEN:
1
x(t)
-1
-2
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
t
The colored area gives the energy
4
3
x 2(t)
0
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
t
figure(1);
subplot(2,1,1)
plot(t,x1);
title('Analog sine');
xlabel('t');
ECE Dept., SVEC 20
Basic simulation lab
ylabel('x_1(t)');
subplot(2,1,2);
stem(n,x2);
xlabel('n');
ylabel('x_2(n)');
title('discrete sine');
figure(2);
subplot(2,1,1);
plot(t,x1.^2);
hold on
% red lines marking 0 & 1
plot([0 0], [-.2 1.2],'r', [1 1], [-.2 1.2],'r');
hold off
subplot(2,1,2);
stem(n,x2.^2);
hold on;
% red lines marking 1 & 20
plot([1 1], [-.2 1.2],'r', [20 20], [-.2 1.2],'r');
hold off;
OUTPUT SCREEN:
P=
0.5000
x 1(t) 0.5
-0.5
-1
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
t
discrete sine
1
0.5
x 2(n)
-0.5
-1
-40 -30 -20 -10 0 10 20 30 40
n
Figure(2)
Squared sines
1
x 21(t)
0.5
1
x 22(n)
0.5
RESULT: various functions on signals and sequences such as addition, multiplication, scaling,
shifting, folding, computation of energy and power were performed in MATLAB
ECE Dept., SVEC 22
Basic simulation lab
4. FINDING EVEN AND ODD PARTS / REAL AND IMAGINARY PARTS OF A
SIGNAL/SEQUENCE
AIM:
OPERATING SYSTEM:
Windows XP
THEORY:
C is a vector whose elements are the coefficients of the polynomial P and d is the highest
degree of the monomials of P.
P = laurpoly(C,'dmin',d)
dmin specifies the lowest degree instead of the highest degree of monomials of P. The
corresponding output P represents the following Laurent polynomial: P(z) = C(1)*z^(d+m-1)
+ ... + C(m-1)*z^(d+1) + C(m)*z^d
x = -5:0.01:5;
a=sinh(x*0.1);
subplot(5,1,1);
plot(x,a), grid on % hyperbolic sine function
title('hyperbolic sine in range -5 to 5');
b=50+10i+sin(2*pi*x);
subplot(5,1,2);
plot(x,b);
title(' seq 2');
c=a+b
subplot(5,1,3);
plot(c)
title('complex signal');
d=real(c)
subplot(5,1,4);
plot(d)
ECE Dept., SVEC 23
Basic simulation lab
title('real part')
e=imag(d)
subplot(5,1,5);
plot(e)
title('imaginary part')
OUTPUT:
4.2 PROGRAM: Even and odd part of a sequence expressed as Laurent polynomial.
a = laurpoly([1:3],'dmax',2);
disp('the polynomial under test for seperating even and odd parts is:');a
b=even(a);
disp('even part of the polynomial is:');b
c=odd(a);
disp('odd part of the polynomial is:');c
OUTPUT:
the polynomial under test for seperating even and odd parts is:
c(z) = 2*z^(+1)
figure(1)
subplot(2,1,1),plot(n,s1)
title('X(n) SIGNAL')
xlabel('time ------- t')
ylabel('amplitude')
GRID
subplot(2,1,2),plot(n,s2)
title('X(-n) SIGNAL')
xlabel('time ------- t')
ylabel('amplitude')
GRID
figure(2)
subplot(2,1,1),plot(n,x1)
title('EVEN SIGNAL')
xlabel('time ------- t')
ylabel('amplitude')
GRID
subplot(2,1,2),plot(n,x2)
title('ODD SIGNAL')
xlabel('time ------- t')
ylabel('amplitude')
GRID
EVEN SIGNAL
1
0.5
amplitude
-0.5
-1
-1 -0.5 0 0.5 1 1.5 2
time ------- t
ODD SIGNAL
1
0.5
amplitude
-0.5
-1
-1 -0.5 0 0.5 1 1.5 2
time ------- t
n=-(N-1)/2:1:(N-1)/2;
%n=1:1:N;
y2=0;
for i=1:N
y2(i)= y1(N+1-i); % X(-n) sequence
end
figure(3)
subplot(2,1,1),stem(n,y1)
title('X(n) SEQUENCE')
xlabel('samples ------- n')
ylabel('amplitude')
GRID
subplot(2,1,2),stem(n,y2)
title('X(-n) SEQUENCE')
xlabel('samples ------- n')
ylabel('amplitude')
GRID
figure(4)
subplot(2,1,1),stem(n,x1)
ECE Dept., SVEC 26
Basic simulation lab
title('EVEN SEQUENCE')
xlabel('samples ------- n')
ylabel('amplitude')
GRID
subplot(2,1,2),stem(n,x2)
title('ODD SEQUENCE')
xlabel('samples ------- n')
ylabel('amplitude')
GRID
OUTPUT:
y1 = 1 2 3 4
N = 4
X(n) SEQUENCE
4
3
amplitude
0
-1.5 -1 -0.5 0 0.5 1 1.5
samples ------- n
X(-n) SEQUENCE
4
3
amplitude
0
-1.5 -1 -0.5 0 0.5 1 1.5
samples ------- n
EVEN SEQUENCE
3
2
amplitude
1
0
-1.5 -1 -0.5 0 0.5 1 1.5
samples ------- n
ODD SEQUENCE
2
1
amplitude
-1
-2
-1.5 -1 -0.5 0 0.5 1 1.5
samples ------- n
n=0:0.01:1
x1 = cos(2*pi*n)
x2 = sin(2*pi*n)
figure(5)
subplot(2,1,1),plot(x1)
title('Signal 1 - as real part')
xlabel('time ------- t')
ylabel('amplitude')
GRID
subplot(2,1,2),plot(x2)
title('Signal 2 - as Imaginary part')
xlabel('time ------- t')
ylabel('amplitude')
GRID
figure(6)
subplot(3,1,1),plot(x3)
title('Complex Signal')
xlabel('Real axes')
ylabel('Imaginary axes')
ECE Dept., SVEC 28
Basic simulation lab
GRID
subplot(3,1,2),plot(r)
title('Real part of Complex Signal')
xlabel('time ------- t')
ylabel('amplitude')
GRID
subplot(3,1,3),plot(i)
title('Imaginary part of Complex Signal')
xlabel('time ------- t')
ylabel('amplitude')
GRID
Output:
Signal 1 - as real part
1
0.5
amplitude
-0.5
-1
0 20 40 60 80 100 120
time ------- t
Signal 2 - as Imaginary part
1
0.5
amplitude
-0.5
-1
0 20 40 60 80 100 120
time ------- t
Complex Signal
Imaginary axes
1
-1
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
Real axes
Real part of Complex Signal
1
amplitude
-1
0 20 40 60 80 100 120
time ------- t
Imaginary part of Complex Signal
1
amplitude
-1
0 20 40 60 80 100 120
time ------- t
RESULT:
MATLAB Functions to find Even and odd parts / real and imaginary parts of a
signal/sequence were verified.
OPERATING SYSTEM:
Windows XP
THEORY:
Linear Convolution:
Note that convolving two signals is equivalent to multiplying the Fourier transform of the two
signals.
Mathematic Formula:
The linear convolution of two continuous time signals and is defined by
Circular Convolution:
For the discrete case, multiplication in the frequency domain translates to circular
convolution in the time domain
clc;
clear all;
close 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:');n3
y=conv(x,h);
figure;
subplot(3,1,1);
Output Screen :
3 11 25 39 38 24
input sequence
4
x(n)
2
0
0 0.5 1 1.5 2 2.5 3
n
impulse response
10
h(n)
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
n
linear convoluted response
40
y(n)
20
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
n
clc;
clear all;
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);
subplot(3,1,1);
t1=0:1:(a-1);
stem(t1,x);
xlabel('n');
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;
ECE Dept., SVEC 33
Basic simulation lab
end
k(n)=k(n)+x(i)*y(j)
end
end
subplot(3,1,3);
t3=0:1:(a-1);
stem(t3,k);
xlabel('n');
ylabel('circular convoluted result');
output screen:
k= 1
k= 1
k= 1
k = 10
k= 8
k= 8 2
k= 8 1
k= 8 1
k= 8 1
k = 8 -2
k = 8 -2 3
k = 8 -2 1
k = 8 -2 -1
k = 8 -2 -1
k = 8 -2 -1
k = 8 -2 -1 0
k = 8 -2 -1 -3
k = 8 -2 -1 -7
k = 8 -2 -1 -4
k = 8 -2 -1 -4
k = 8 -2 -1 -4 0
k = 8 -2 -1 -4 0
k = 8 -2 -1 -4 -6
k = 8 -2 -1 -4 0
k = 8 -2 -1 -4 -1
x(n)
0
-5
0 0.5 1 1.5 2 2.5 3 3.5 4
n
second seq after zero padding
4
y(n)
0
0 0.5 1 1.5 2 2.5 3 3.5 4
circular convoluted result
10
-10
0 0.5 1 1.5 2 2.5 3 3.5 4
n
clc;
clear all;
close all;
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:');n3
y=conv(x,h);
figure;
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('the resultant signal is:');y
ECE Dept., SVEC 35
Basic simulation lab
OUTPUT SCREEN:
input sequence
1
x(n)
-1
0 100 200 300 400 500 600 700
n
impulse response
1
h(n)
-1
0 100 200 300 400 500 600 700
n
linear convoluted response
500
y(n)
-500
0 200 400 600 800 1000 1200 1400
n
OPERATING SYSTEM:
Windows XP
THEORY:
output screen:
n1 =
n2 =
r=
Output:
seq 1
4
amplitude
0
0 0.5 1 1.5 2 2.5 3
n
seq 2
4
amplitude
0
0 0.5 1 1.5 2 2.5 3
n
cross correlated result
40
amplitude
20
0
-3 -2 -1 0 1 2 3
lag index
clc;
clear all;
close all;
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');
ECE Dept., SVEC 38
Basic simulation lab
r=xcorr(x,x);
subplot(2,1,2);
stem(k,r);
xlabel('lag index');
ylabel('amplitude');
title('auto correlated result');
OUTPUT:
n2 =
628
input for auto correlation
1
0.5
amplitude
-0.5
-1
0 100 200 300 400 500 600 700
n
auto correlated result
400
200
amplitude
-200
-400
-800 -600 -400 -200 0 200 400 600 800
lag index
RESULT: Auto and Cross Correlation of Sequences/ Signal was verified using MATLAB
To Verify the Linearity and Time Invariance Properties of a given system using MATLAB
OPERATING SYSTEM:
Windows XP
THEORY:
A linear system is any system that obeys the properties of scaling (homogeneity) and
superposition, while a nonlinear system is any system that does not obey at least one of
these.
H(kf(t)) =kH(f(t)
To demonstrate that a system H obeys the superposition property of linearity is to show that
It is possible to check a system for linearity in a single (though larger) step. To do this,
simply combine the first two steps to get
A time invariant system is one that does not depend on when it occurs: the shape of the
output does not change with a delay of the input.
H(f(t−T)) =y(t−T)
This block diagram shows what the condition for time invariance. The output
is the same whether the delay is put on the input or the output.
When this property does not hold for a system, then it is said to be time variant, or time-
varying.
PROGRAM:
% linearity property
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
y1 = a1*2*x1 + a2*2*x2;
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
f=
PROGRAM:
k= m-p
x(p)=x;
y1 = cos(x(p))
y2 = cos(x(m-k))
if y1 == y2
disp('The given system y(n) = cos(x(n)) is Time invarient')
else
disp('The given system y(n) = cos(x(n)) is Time varient')
end
y1 = p*x(p)
y2 = m*x(m-k)
if y1 == y2
disp('The given system y(n) = n*x(n) is Time invarient')
else
disp('The given system y(n) = n*x(n) is Time varient')
end
end
p= 3
m= 5
k= 2
f= 1
y1 =
-0.4161
y2 =
-0.4161
RESULT: Linearity and Time Invariance Properties of a given System was verified.
To Compute Unit Sample, Unit Step, Sinusoidal Responses of given LTI system and to verify
its Stability using MATLAB
OPERATING SYSTEM:
Windows XP
THEORY:
An LTI system is stable if every bounded input produces a bounded output, i.e.,
is true for all . But as the output and input of an LTI is related by convolution, we
have:
i.e.,
In other words, the LTI system is stable if its impulse response function is
absolutely integrable.
stp = step(sys)
figure(1)
plot(stp)
title('STEP RESPONSE')
xlabel('time ------- t')
ylabel('amplitude')
GRID
i=impulse(sys)
figure(2)
plot(i)
title('IMPULSE RESPONSE')
xlabel('time ------- t')
ylabel('amplitude')
GRID
n=0:0.01:1
x=sin(2*pi*1*n)
figure(3)
subplot(2,1,1),plot(n,x)
subplot(2,1,2),plot(n,s)
title('SINUSOIDAL RESPONSE')
xlabel('time ------- t')
ylabel('amplitude')
GRID
ltiview({'step','impulse'},sys)
p=pole(sys)
P=real(p)
P1 = max(P)
if P1 > 0
disp('the given system is unstable')
OUTPUT
num =
1 2
den =
3 1
Transfer function:
s + 2
-------
3 s + 1
p = -0.2500
P = -0.2500
P1 = -0.2500
STEP RESPONSE
2
1.8
1.6
1.4
amplitude
1.2
0.8
0.6
0.4
0.2
0 20 40 60 80 100 120
time ------- t
IMPULSE RESPONSE
0.45
0.4
0.35
0.3
amplitude
0.25
0.2
0.15
0.1
0.05
0
0 20 40 60 80 100 120
time ------- t
0.5
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
SINUSOIDAL RESPONSE
0.4
0.2
amplitude
-0.2
-0.4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
time ------- t
RESULT:
Unit Sample, Unit Step, Sinusoidal Responses of given LTI system were obtained and its
Stability was enquired using MATLAB.
ECE Dept., SVEC 47
Basic simulation lab
9. DEMONSTRATION OF GIBB’S PHENOMENON
AIM:
OPERATING SYSTEM:
Windows XP
THEORY:
Gibbs Phenomenon:
The peculiar manner in which the Fourier series of a piecewise continuously differentiable
periodic function behaves at a jump discontinuity: the nth partial sum of the Fourier series
has large oscillations near the jump, which might increase the maximum of the partial sum
above that of the function itself. The overshoot does not die out as the frequency increases,
but approaches a finite limit.
The Gibbs phenomenon involves both the fact that Fourier sums overshoot at a jump
discontinuity, and that this overshoot does not die out as the frequency increases.
The best known version of the Gibbs phenomenon is the overshoot that arises when a discontinuous
function is represented by a truncated set of Fourier expansion terms. The situation is similar if the
truncated Fourier expansion is instead obtained by means of interpolation on an equispaced grid.
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;
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:
N= 1 N= 3
2 2
y N(t)
y N(t)
1 1
0 0
-2 -1 0 1 2 -2 -1 0 1 2
Time Time
N= 7 N= 19
2 2
y N(t)
y N(t)
1 1
0 0
-2 -1 0 1 2 -2 -1 0 1 2
Time Time
N= 49 N= 70
2 2
y N(t)
y N(t)
1 1
0 0
-2 -1 0 1 2 -2 -1 0 1 2
Time Time
To obtain Fourier Transform and Inverse Fourier Transform of a given signal / sequence and
to plot its Magnitude and Phase Spectra.
OPERATING SYSTEM:
Windows XP
THEORY:
The fast Fourier transform (FFT) is an efficient algorithm for computing the DFT of a
sequence; it is not a separate transform. It is particularly useful in areas such as signal and
image processing, where its uses range from filtering, convolution, and frequency analysis to
power spectrum estimation
For length N input vector x, the DFT is a length N vector X, with elements
N
X(k) = sum x(n)*exp(-j*2*pi*(k-1)*(n-1)/N), 1 <= k <= N.
n=1
N
x(n) = (1/N) sum X(k)*exp( j*2*pi*(k-1)*(n-1)/N), 1 <= n <= N.
k=1
output:
-2
0 0.5 1 1.5 2 2.5
magnitude spectra
200
100
0
0 100 200 300 400 500 600 700 800 900 1000
Phase spectra
5
-5
0 100 200 300 400 500 600 700 800 900 1000
inverse fourier transformed
2
0
-2
0 100 200 300 400 500 600 700 800 900 1000
t= 0:1/fs:1-1/fs
x = sin(2*pi*f1*t) + sin(2*pi*f2*t);
subplot(4,1,1);
plot(t,x);
title('signal in time domain');
y=fft(x,N);
disp(y);
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');
xn=ifft(y);
disp(xn);
output:
0
-2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
magnitude spectra
400
200
0
0 50 100 150 200 250 300 350 400 450 500
Phase spectra
5
-5
0 50 100 150 200 250 300 350 400 450 500
inverse fourier transformed
2
-2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
RESULT: The Magnitude and Phase Spectra of Fourier Transformed Signal was Plotted
OPERATING SYSTEM:
Windows XP
THEORY:
if the integral exists. The notation L[y(t)](s) means take the Laplace transform of y(t). The
functions y(t) and Y(s) are partner functions. Note that Y(s) is indeed only a function of s
since the definite integral is with respect to t.
Example :
The integral converges if s>1. The functions exp(t) and 1/(s-1) are partner functions.
If y(t) is piecewise continuous for t>=0 and of exponential order, then the Laplace Transform
exists for some values of s. A function y(t) is of exponential order c if there is exist constants
M and T such that
All polynomials, simple exponentials (exp(at), where a is a constant), sine and cosine
functions, and products of these functions are of exponential order. An example of a function
not of exponential order is exp(t^2). This function grows too rapidly. The integral
The inverse Laplace transform of the function Y(s) is the unique function y(t) that is
continuous on [0,infty) and satisfies L[y(t)](s)=Y(s).
PROGRAM:
OUTPUT:
f=
-5/4+7/2*t*exp(-2*t)+5/4*exp(-2*t)
F=
-5/4/s+7/2/(s+2)^2+5/4/(s+2)
ans =
(s-5)/s/(s+2)^2
s-5
----------
2
s (s + 2)
f=
1/2*exp(-t)*(cosh(t)-6*sinh(t))
ans =
1/2*exp(-t)*(cosh(t)-6*sinh(t))
OPERATING SYSTEM:
Windows XP
THEORY:
A Transfer Function is the ratio of the output of a system to the input of a system, in the
Laplace domain considering its initial conditions to be zero. If we have an input function of
X(s), and an output function Y(s), we define the transfer function H(s) to be:
Given a continuous-time transfer function in the Laplace domain, H(s) or a discrete-time one
in the Z-domain, H(z), a zero is any value of s or z such that the transfer function is zero, and
a pole is any value of s or z such that the transfer function is infinite.
Zeros:1. The value(s) for z where the numerator of the transfer function equals zero
2. The complex frequencies that make the overall gain of the filter transfer function
zero.
Poles: 1. The value(s) for z where the denominator of the transfer function equals zero
2. The complex frequencies that make the overall gain of the filter transfer function
infinite.
OUTPUT:
1 1
0.5 0.5
Imaginary Axis
Imaginary Axis
0 0
-0.5 -0.5
-1 -1
-1.5 -1.5
-4 -2 0 2 -4 -2 0 2
Real Axis Real Axis
RESULT: Poles and Zeros of given transfer function are located and plotted in S- plane and
Z- plane
To Generate Gaussian Noise and to Compute its Mean, M.S. Values, Skew, kurtosis, PSD
and PDF
OPERATING SYSTEM:
Windows XP
THEORY:
Gaussian noise is statistical noise that has a probability density function (abbreviated pdf) of
the normal distribution (also known as Gaussian distribution). In other words, the values that
the noise can take on are Gaussian-distributed. It is most commonly used as additive white
noise to yield additive white Gaussian noise (AWGN).
Gaussian noise is properly defined as the noise with a Gaussian amplitude distribution. This
says nothing of the correlation of the noise in time or of the spectral density of the noise.
Labeling Gaussian noise as 'white' describes the correlation of the noise. It is necessary to use
the term "white Gaussian noise" to be correct. Gaussian noise is sometimes misunderstood to
be white Gaussian noise, but this is not the case.
PROGRAM:
x=wgn(100,1,1)
figure(1)
plot(x)
title('AWGN');
% to find mean of x
m = mean(x)
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
ECE Dept., SVEC 57
Basic simulation lab
figure(2)
psd(h,x,'fs',fs)
p = pdf('norm',x,0,1)
figure(3)
plot(p)
title('pdf');
output:
m= 0.0538
sd = 0.9745
s = -0.2489
k= 2.6987
h=
EstimationMethod: 'Welch'
FFTLength: 'NextPow2'
SegmentLength: 64
OverlapPercent: 50
WindowName: 'Hamming'
SamplingFlag: 'symmetric'
AWGN
2.5
1.5
0.5
-0.5
-1
-1.5
-2
-2.5
0 10 20 30 40 50 60 70 80 90 100
-14
-16
-18
Power/frequency (dB/Hz)
-20
-22
-24
-26
-28
-30
-32
0 5 10 15 20 25 30 35 40 45 50
Frequency (Hz)
pdf
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
0 10 20 30 40 50 60 70 80 90 100
RESULT: Additive White Gaussian noise was generated and its PSD and PDF were plotted
and its Mean, Standrad Deviation, Kurtosis, Skew were Computed using MATLAB
functions.
ECE Dept., SVEC 59
Basic simulation lab
14. SAMPLING THEORM
AIM:
To Demonstrate Sampling Theorem and aliasing effect using MATLAB.
OPERATING SYSTEM:
Windows XP
THEORY:
The theorem shows that a band limited analog signal that has been sampled can be perfectly
reconstructed from an infinite sequence of samples if the sampling rate exceeds 2B samples
per second, where B is the highest frequency in the original signal.
If a signal contains a component at exactly B hertz, then samples spaced at exactly 1/(2B)
seconds do not completely determine the signal, Shannon's statement notwithstanding.
PROGRAM:
if fs >= 2*f
disp('sampling theorem is satisfied ')
%ploting of sinusoidal
n=0:1/fs:pi;
x=sin(2*pi*f*n);
figure(1)
plot(n,x)
title('sinusoidal signal')
xlabel('time ------- t')
ylabel('amplitude')
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')
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)
ECE Dept., SVEC 60
Basic simulation lab
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:
Case 1:
f=
30
fs =
70
sinusoidal signal
1
0.8
0.6
0.4
0.2
amplitude
-0.2
-0.4
-0.6
-0.8
-1
0 0.5 1 1.5 2 2.5 3 3.5
time ------- t
Frequency plot
1
original signal
0.9 lower side band & upper side band
0.8
0.7
0.6
amplitude
0.5
0.4
0.3
0.2
0.1
0
0 10 20 30 40 50 60 70 80 90 100
frequency ------- f
Case 2:
f=
20
fs =
30
sampling theorem is not satisfied
Frequency plot
1
original signal
0.9 lower side band & upper side band
0.8
0.7
0.6
amplitude
0.5
0.4
0.3
0.2
0.1
0
0 5 10 15 20 25 30 35 40 45 50
frequency ------- f
RESULT: Sampling theorem was verified and aliasing was identified when the sampling
frequency is less than the nyquist frequency using MATLAB.
AIM:
To generate a periodic sequence, corrupt it with zero mean White noise and extract the
sequence using periodic circular cross correlation.
OPERATING SYSTEM:
Windows XP
THEORY:
Removal of noise by Filtering is not the solution because noise contains power at all
frequencies. Hence we use correlation techniques.
Circular
x(k) Cross- N/L
Correlation
PROGRAM:
% Extracting a periodic signal from noise
clear
clc
rand('state',1000)
N = 256;
k = 0 : N-1;
x = cos(32*pi*k/N) + sin(48*pi*k/N); % periodic signal which include
%two sin components
a = 0.5;
y = f_randu(1,N,-a,a) + x; % + white noise uniformly distributed over
%interval(-.5 , 0.5)
figure
plot (k,y)
f_labels ('A noisy periodic signal','\it{k}','\it{y(k)}')
f_wait
M = N/8;
L = floor(N/M); %no. of complete cycles of x(k) in y(k)
OUTPUT:
1.5
0.5
y(k)
-0.5
-1
-1.5
-2
-2.5
0 50 100 150 200 250 300
k
1.5
0.5
0
signals
-0.5
-1
-1.5
-2
-2.5
0 10 20 30 40 50 60 70
k
RESULT: using Cross- correlation the periodic signal from noise was estimated using
MATLAB.
To Design an FIR Raised Cosine Filter and to plot its Magnitude, Phase and Impulse
responses using MATLAB.
OPERATING SYSTEM:
Windows XP
THEORY:
The ideal raised cosine filter frequency response consists of unity gain at low
frequencies, a raised cosine function in the middle, and total attenuation at high frequencies.
The width of the middle frequencies are defined by the roll off factor constant Alpha,
(0<Altpha<=1). In Filter Solutions, the pass band frequency is defined as the 50% signal
attenuation point. The group delay must remain constant at least out to 15 to 20 dB of
attenuation.
When the pass band frequency of a raised cosine filter is set to half the data rate, then
the impulse response Nyquist's first criteria is satisfied in that the impulse response is zero for
T = NTs, where N is an integer, and T is the data period.
Filter Solutions provides analog, IIR and FIR raised cosine filters. FIR filters are the
most accurate and are best to use. However, if it is not possible to use an FIR filter, analog
filters may approximate the raised cosine response. The higher the order of the filter, the
greater the raised cosine approximation. High order raised cosine filters also produce longer
time delays. The lower alpha values use less bandwidth, however, they also produce more ISI
due to element value errors and design imperfections.
PROGRAM:
fvtool(rcfilter)
OUTPUT:
-20
Magnitude (dB)
-40
-60
-80
-100
-120
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
Normalized Frequency ( rad/sample)
Phase Response
0
-200
-400
Phase (degrees)
-600
-800
-1000
-1200
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
Normalized Frequency ( rad/sample)
0.8
0.6
Amplitude
0.4
0.2
-0.2
0 5 10 15 20 25 30 35 40
Samples
RESULT:
The Impulse Response of a Raised Cosine Filter was plotted using fvtool in MATLAB.
OPERATING SYSTEM:
Windows XP
THEORY:
Continuous case:
where
The asterisk denotes complex conjugate, and can be omitted if the random process is real-
valued.
Discrete case:
where
and where is the power spectral density of the function with discrete values .
Being a sampled and discrete-time sequence, the spectral density is periodic in the frequency
domain.
Nint=500;
t=0:.05:200;
tau=1.15
A=0.85
noise=randn(1,length(t)+Nint);
e=zeros(1,length(t));
memfun=exp(-(t(Nint)-t(1:Nint))/tau);
for i=1:length(e)
e(i)=sum( noise(i:(i+Nint-1)).*memfun );
end
e=A*e/std(e);
vare=var(e)
Ne=length(e);
0.8
0.7
0.6
0.5
0.4
X: 1.1
Y: 0.2776
0.3
0.2
0.1
-0.1
0 10 20 30 40 50 60 70
17.2 PROGRAM:
clear all;
clc;
fs=1000;
t=0:1/fs:.5;
x=cos(2*pi*t*200)+randn(size(t)); %a cosine of 200Hz plus noise
k=length(x)
y=fft(x,k); %fft of random process
pyy=y.*conj(y)/k; % auto correlation
f=1000*(0:k-1)/k;
figure(1)
plot(f,pyy(1:k));
title('frequency content of y');
xlabel('frq in hz');
figure(2)
h=spectrum.welch; %instantiate a welch object
psd(h,x,'fs',fs)
figure(3)
h=spectrum.periodogram %instantiate a periodogram object to
calculate psd
psd(h,x,'fs',fs);
frequency content of y
120
100
80
60
40
20
0
0 100 200 300 400 500 600 700 800 900 1000
frq in hz
-18
-20
Power/frequency (dB/Hz)
-22
-24
-26
-28
-30
-32
-34
0 50 100 150 200 250 300 350 400 450 500
Frequency (Hz)
-20
-30
-40
-50
-60
0 50 100 150 200 250 300 350 400 450 500
Frequency (Hz)
RESULT:
To generate a Random process and to check for its Wide Sense Stationary using MATLAB
OPERATING SYSTEM:
Windows XP
THEORY:
A random process is wide sense stationary (WSS) if the mean function and
autocorrelation function are invariant to a time shift. In particular, this implies that
All strict sense stationary random processes are also WSS, provided that the mean and
autocorrelation function exist. The converse is not true. A WSS process does not necessarily
need to be stationary in the strict sense. We refer to a process that is not WSS as non -
stationary.
Many of the processes we deal with are WSS and hence have a constant mean
function and an autocorrelation function that depends only on a single time variable. Hence,
in the remainder of the text, when a process is known to be WSS or if we are assuming it to
be WSS, then we will represent its autocorrelation function by RXX (τ). If a process is non -
stationary or if we do not know if the process is WSS, then we will explicitly write the
autocorrelation function as a function of two variables, RXX (t, t + τ).
clc;
clear all;
close all;
syms pi a wo t t1 t2 theta
l=input('Enter the lower limit');
u=input('Enter the upper limit');
x=input('Enter the PDF');
f=a*cos((wo*t)+theta);
f1=f*x;
y1=int(f1,theta,l,u);
disp('The Expectation is:');
disp(y1);
f2=a*cos((wo*t1)+theta);
f3=a*cos((wo*t2)+theta);
f4=f2*f3*x;
y2=int(f4,theta,l,u);
disp('The AutoCorrelation is:');
disp(y2);
Manual Calculation
1) Consider the random process X(t)=Acos(ω0t+Θ) Where “Θ” is real-valued random
variable and is uniformly distributed over [0, π]. Check if the process is wide sense
stationary.
2) Consider the random process X(t)=A cos(ω0t+Θ) Where “Θ” is real-valued random variable
and is uniformly distributed over [0, 2π]. Check if the process is wide sense stationary.
OUTPUT:
Thus the given random processes are identified whether they are WSS or not through
calculating the mean and autocorrelation functions manually and verified these results
through MATLAB simulation.