Tuesday, August 26, 2008

Laplace transform Lab

Please refer a text book for a very detailed study about Laplace transforms

Laplace Transform (LT) helps in converting differentiation, integration and many other complex functions into simple algebraic functions or expressions there by making the analysis of a system easier. LT are applied to systems which use continuous time signals and many a times on problems which involve absolutely non-integrable functions like impulse response of an unstable system. Also the convolution(shift and add repeatedly) also can be performed by pure multiplication after converting the functions into respective LT’s.

LT exist in 2 varieties namely Unilateral(one-side) and Bilateral(2-sided) transforms. Many properties are similar in nature hence studying one of them and extending it to the other form is felt easier(Changes to be mentioned where ever necessary).

Definition

LT of a function f(t) is defined as



It contains complex exponentials,e-st and s= σ + jω


We will be using laplace transform to find the solutions of differential equations, Operations on elementary signals, checking the basic properties of functions etc.

Inverse LT can be achieved by complex contour integrals or by partial fraction method. We will be following the partial fraction method and then find the Inverse Laplace Transform

Let us start with some basic elementary functions (signals)

Unit step function



For an impulse function, sifting property holds good and this will be dealt later when we are taking the convolution of signals.










Properties of LT





















Matlab programs

Some cautions before going in for the programs

1.Save the file name with no spaces

2.It must have *.m extension

3.Make sure you are committing any spelling mistakes for variables and functions

4.take care for right division and left division

5.Use “syms” for single character objects

6.Use “sym” for strings but make sure you put it in single quotes

7.Work out the problem by hand to make sure your results are correct.

8. If there an error displayed in a function then check for spelling mistakes. If it did not solve the problem then type the following lines in all your codes or type in command line
close all;
clear all;

Find laplace transform of e-2t, 10e-5t, te-3t, 5d2y/dt2
/***********/
%Find the LT of e-2t
syms t;
solution = laplace(exp(-2*t));
/***********/
%Find the LT of 10e-5t
syms t;
solution = laplace(10*exp(-5*t));
/***********/
%Find the LT of te-3t
syms t;
syms s; %can be omitted
solution = laplace(t*exp(-3*t));
/***********/
%Find the LT of 5d2y/dt2
sym ‘y(t)’;
solution = laplace(5*diff(‘y(t)’,2));
/***********/

Find the inverse LT of

1). Y(s) = 1/s – 2/(s+3) + 10/(s+1)
2). Y(s) = s(s+1)/((s+2)(s2+2s+1))

%Program for inverse laplace transform of Y(s) = 1/s – 2/(s+3) + 10/(s+1)
syms t; %can be omitted
syms s;
soln = ilaplace(1/s – 2/(s+3) + 10/(s+1));
/***********/
%Program for inverse
laplace transform of Y(s) = s(s+1)/((s+2)(s2+2s+1))
syms s;
soln = ilaplace((s*(s+2))/((s+2)*(s^2+2*s+1));
/************/

Solve the DE by laplace transform
D2(y) + 12 D(y) +32y = 32u(t)

Initial and Final Value Theorem

Lt sX(s) = x(0+)
s->inf

Lt sX(s) = x(inf)
s->0

Friday, August 8, 2008

Matlab - Week 2

Copy and paste the code below into a *.m file and then run the program.The file name must be saved with *.m extension , without spaces in between and no clashing of already existing functions in MATLAB Library


Example code for sinusoidal signals

%Generate Sine Wave
N=30;%Number of samples
t=-10:0.1:10;
x=sin(2*pi*25*t/N); %this is x(t)
plot(t,x)
title('Sine Wave');
xlabel('time scale');
ylabel('Amplitude');
axis([-70 70 -2 2]);
grid

Example code for square wave
%Square wave generation
A=2;
t = 0:0.0005:1;
x=A*square(2*pi*5*t,25); %5 Hertz wave with duty cycle 25%
plot(t,x);
grid
axis([0 1 -3 3]);

Example code for triangular wave
%Triangular wave generation
A=2;
t = 0:0.0005:1;
x=A*sawtooth(2*pi*5*t,0.25); %5 Hertz wave with duty cycle 25%
plot(t,x);
grid
axis([0 1 -3 3]);

Example code for rectangular pulse
%To generate a rectangular pulse
t=-5:0.01:5;
pulse = rectpuls(t,2); %pulse of width 2 time units
plot(t,pulse)
axis([-5 5 -1 2]);
grid

Example code for exponential signals
%Exponential signal
B=5;
alpha=5;
t=-1:0.1:1;
x=B*exp(-alpha*t);
plot(t,x);

%This part is for discrete signal
r=0.85;
n=-10:10;
y=B*r.^n;

stem(n,y);

Example code for decaying exponential signals
%Exponentially damped signal
A=10;
f=5; %5 Hertz
phi=0; %Phase info
a=6;
t=0:0.01:1;
x=A*sin(2*pi*f*t+phi).*exp(-a*t); %Elemental operation is required
plot(t,x);
grid

Elementary signals
%To generate a step signal
%Completely automatic
t=-2:0.01:2;
A=2;
len = length(t);
len_pos = length(find(t>=0));
u=A*([zeros(1,len-len_pos), ones(1,len_pos)]);
figure
plot(t,u);

axis([-2 4 0 3]);
grid

%Impulse function
%To generate impulse signal
delta = [zeros(1,(len-len_pos)+1), 1, zeros(1,len_pos-2)];
figure
plot(t,delta);

%Ramp function
%To generate ramp using step
u=A*([zeros(1,len-len_pos), ones(1,len_pos)]);
ramp = u.*t;

figure
plot(t,ramp);

Find Even and Odd signal
%Even and Odd signals
%Continuous time signal
t=-1:0.01:1;
len = length(t);
%Generate x(t)
x=sin(2*pi*t);
x_rev = sin(2*pi*(-t));

%Find Even and odd of x(t)
x_even = 0.5*(x + x_rev);
x_odd = 0.5*(x - x_rev);
plot(t,x,'r--',t,x_rev,'g-.',t,x_even,'m-',t,x_odd,'b*');
legend('x','x_rev','x_even', 'x_odd');
grid

Find the energy of the signal

%Find Energy

%To generate a rectangular pulse
t=-5:0.5:5;
pulse = 2*rectpuls(t,2.1); %pulse of width 2 time units
stem(t,pulse)
axis([-5 5 -1 2]);
grid

%Energy
Energy = 0;
t_mod = find(pulse > 0);
for i=t_mod(1):t_mod(length(t_mod))
Energy = Energy+ pulse(i).^2;
end
Energy


Operations that are checked are
1. Amplitude scaling
2. Addition
3. Multiplication
4. differentiation
5. Integration

Time scaling, reflection, time shifting

Example code to show all the operations on a exponentially damped signal
%Exponentially damped signal
A=10;
f=5; %5 Hertz
phi=0; %Phase info
a=6;
t=0:0.01:1;
x=A*sin(2*pi*f*t).*exp(-a*t); %Elemental operation is required
figure
plot(t,x);
grid

%Amplitude scaling
scale = 0.5;
y= scale * x;
figure
plot(t,x,'-',t,y,':');
grid

%Addition
figure
plot(t,x,'-',t,y,':',t,(x+y),'*');

%Multiplication
figure
plot(t,x,'-',t,y,':',t,(x.*y),'.');

%Time scaling
k=0.2;
scaled_time =t/k;
figure
plot(t,x,'g:',scaled_time, x);
grid

%Time Reflection
reflect_time = -t;
figure
plot(t,x,'g:',reflect_time, x);
grid

%Time shifting
shift = 1;
shifted_time = t-(-shift);
figure
plot(t,x,'g:',shifted_time, x);
grid

Sunday, August 3, 2008

Matlab - First week

Let us start with Simple calculations

1. Addition
2. Multiplication
3. division
4. Exponential(exp)
5. trigonometric (degrees must be converted into radians)( normal and arc normal fns)
6. Logarithimic log and log10

Some of the common functions that may be handy in dealing with many signal applications.

abs Absolute value
sqrt Square root
sign signum fn
conj conjugate of a complex number
imag imaginary part of a complex number
real real part of the complex number
angle phase angle of Complex Number
cos,sin,tan
exp
cosh, sinh,tanh, asin,asinh
round
floor
fix
ceil
rem --> remainder of division


Variable Name rule

Must have letters and digits
Start with a letter.
Special characters are not to be part of the name.
pi,i,j and e are reserved words
names of functions and Matlab commands cannot be used as variable names
variables are case sensitive.

All commands are to be entered in lower case.

Whenever a help is needed type "help command_name" in the command window

Vectors

Row vectors

U=[1 2 3];
V=[4,5,6];
U+V

W=[U,3*V]

sort(W) à sorting in ascending order

Colon notation
a:b:c à sequence of numbers starting with a and end with c in steps of b
1:0.5:2
1:10 automatically unit step size is assumed
1:-1:10 à shoots an error message

Can operate on a section of a vector

W=[1:5, 7:9]
W(4)

W(5:-1:1) à to reverse the vector

[M,N] = Size(W)

Column Vectors

X=[1;2;3];
Y=[4;5;6];
X+Y;

Z=X’ would do a Transpose on X

C=[1+i, 1-i]
D= C’
E=C.’

To store all texts
“diary filename”
diary on/off
save filename
load filename

"who" command displays all the variables used so far

whos --> More details on variables


Elementary Plots and Graphs

To generate a sine wave

N=30;
step_size=pi/N;
x=0:step_size:pi;
y=sin(x);
plot(x,y);
title(‘Sine Wave’);
xlabel(‘x’);
ylabel(‘sin(x)’);
grid
plot(x,y,’m+’)

To plot cos and sine fns
N=30;
step_size=pi/N;
x=0:step_size:2*pi;
plot(x,sin(x),’b-‘,x,cos(x),’m-.’);
legend(‘sine’,’cosine’);
grid
xlabel(‘x’);
ylabel(‘functions’);
title(‘Cosine and Sine Function Plot’);
hold off

Subplots

samples=30;
step_size=2*pi/samples;
x=0:step_size:2*pi;
subplot(121);
plot(x,cos(x));
xlabel(‘x’);
ylabel(‘cosine’);
grid;
subplot(122);
plot(x,sin(x));
xlabel(‘x’);
ylabel(‘sine’);
grid;


axis([0 10 0 100]) à scale the graph with x-axis in [0,10] and y-axis in [0,100] range

Matlab script file
always has a *.m extension.


Please see you have added the directory to the Matlab search path in which you are saving your files


Working with vectors and matrices

Significance of “.” (Element by element operation)
.+, .*, ./

To square a Matrix of size 3 x 3
A=[1 2 3; 4 5 6; 7 8 9];
A.^2

Experiment with A^2

U might have already studied Linear algebra(I guess)
If u have a matrix say U and you want to find the norm of U or the euclidean distance

We would use sqrt(U*U’);
Other way of finding this is norm(U);

How to find the angle between two vectors

Let X=[1 2 3] and Y=[4 5 6]
Angle between X and Y is theta = acos((X.Y)/(||X||*||Y||));

To tabulate the results
X=[0:pi/10:2*pi]’;
[X sin(X) cos(X)];

Other useful functions

zeros à vector elements with 1 as value
ones à vector elements with 1 as value
rand à random matrix
eye à Identity matrix
diag à diagonal matrix

for loop
while
if else elseif
input

Writing UDF
function output_arguments = function_name(input_arguments)
%comments go here

semilogy to change the axis to logarithmic y axis
disp(‘Hai’);

simplify((xˆ3 - yˆ3)/(x - y))
differentiation : diff(x^3)
integration : int(x)

Last tip: Use ctrl C to stop the execution
Finally home work on right division and left division

This post is possible because if lot of books which are available, so many documents available on the internet and many university notes.