Signal & Systems
Signal & Systems
FIFTH SEMESTER
SIGNAL & SYSTEMS
Name: ____________________________________________
Semester: _________________________________________
Batch: ____________________________________________
C
COON
NTTE
ENNT
TSS
1
Introduction to MATLAB
2
Complex numbers and matrix manipulation
3
Loops and decision making
4
Basic signals
5 Signals transformations
9 Z-transform
12
Introduction to graphical user interface
13 Introduction to Simulink -I
14
Introduction to Simulink -II
EXPERIMENT NO - 1
INTRODUCTION TO MATLAB
OVERVIEW…
Over the past few decades, the unbelievable progress in the field of digital signal
processing has altered the entire meaning of the word ‘impossible’, by thinking
the unthinkable and doing the un-doable. With an ever-increasing set of
applications, signal processing has crept into almost every sphere of science, and
today, we see the techniques and tools developed by DSP scientists and
engineers being utilized for a better understanding and investigation of the
universe, and hence the life. This field of knowledge has helped us to stretch our
limits--- the very limits of thinking and imagination; the most cherish-able
outcome of all the knowledge. As our first course on DSP, we’ll try to familiarize
ourselves with the concepts and theory of DSP, to have a look at and use the
tools to solve the practical problems, and to have a flavor of the problems and the
challenges faced by man in the past, as well as today.
MATLAB is the most popular tool used for Digital Signal Processing. It
provides one of the strongest environments for study and simulation of the real-
world problems and their solutions, especially in the field of engineering. For
Signal Processing, it has a very comprehensive and easy-to-use toolbox with lots
of DSP functions implemented. Moreover, with the aid of Simulink, we can
create much more complex situations very easily, and solve them.
In this lab…we would have an introduction to MATLAB and get started with
working in its wonderfully simple environment. This lab, as a matter of fact,
would lay the foundation for our next labs.
DEFINITION OF VARIABLES
Variables are assigned numerical values by typing the expression directly, for
example, typing
a = 1+2;
yields: a = 3;
The answer will not be displayed when a semicolon is put at the end of an
expression, for example type
a = 1+2;
+ addition
- subtraction
* multiplication
/ division
^ power operator
' transpose
A variable can be assigned using a formula that utilizes these operators and
either numbers or previously defined variables. For example, since a was defined
previously, the following expression is valid
b = 2*a;
yields: b = 6;
If your expression does not fit on one line, use an ellipsis (three or more periods
at the end of the line) and continue on the next line.
c = 1+2+3+...
5+6+7;
There are several predefined variables which can be used at any time, in the
same manner as user-defined variables:
i sqrt(-1)
j sqrt(-1)
pi 3.1416...
For example,
y= 2*(1+4*j);
There are also a number of predefined functions that can be used when defining
a variable. Some common functions that are used in this text are:
c = abs(y);
yields: c = 8.2462;
c = angle(y);
yields: c = 1.3258;
Note that exp can be used on complex numbers. For example, with y = 2+8i as
defined above,
c = exp(y);
c = exp(2)*cos(8) + j*(exp)2*sin(8);
DEFINITION OF MATRICES
MATLAB is based on matrix and vector algebra; even scalars are treated as 1x1
matrices. Therefore, vector and matrix operations are as simple as common
calculator operations.
Vectors can be defined in two ways. The first method is used for arbitrary
elements
v = [1 3 5 7];
creates a 1x4 vector with elements 1, 3, 5 and 7. Note that commas could have
been used in place of spaces to separate the elements. Additional elements can be
added to the vector:
v(5)=8;
yields the vector v = [1 3 5 7 8]. Previously defined vectors can be used to define
a new vector.
For example, with ‘v’
a= [9 10];
b= [v a];
creates the vector
b = [1 3 5 7 8 9 10].
The second method is used for creating vectors with equally spaced elements:
t = 0: 0.1:10;
creates a 1x101 vector with the elements 0, .1, .2, .3,...,10. Note that the middle
number defines the increment. If only two numbers are given, then the increment
is set to a default of 1
k = 0:10;
creates a 1x11 vector with the elements 0, 1, 2, ..., 10.
Matrices are defined by entering the elements row by row:
M = [1 2 4; 3 6 8];
null matrix:
M = [ ];
M = zeros(n,m);
M = ones(n,m);
nxn identity matrix:
M = eye(n);
A particular element of a matrix can be assigned:
M(1,2) = 5;
Operations and functions that were defined for scalars in the previous section
can also be used on vectors and matrices. For example,
a= [1 2 3];
b= [3 4 5];
c= a + b;
yields: c =[5 7 9];
t=0:10;
x = t .*cos(t);
M-Files
M-files are macros of MATLAB commands that are stored as ordinary text files
with the extension "m", that is ‘filename.m’. An M-file can be either a function
with input and output variables or a list of commands. MATLAB requires that
the M-file must be stored either in the working directory or in a directory that is
specified in the MATLAB path list. For example, consider using MATLAB on a
PC with a user-defined M-file stored in a directory called "\MATLAB\MFILES".
Then to access that M-file, either change the working directory by typing
cd\matlab\mfiles from within the MATLAB command window or by adding
the directory to the path. Permanent addition to the path is accomplished by
editing the \MATLAB\matlabrc.m file, while temporary modification to the
path is accomplished by typing
path(path,'\matlab\mfiles')
from within MATLAB. Or, this can easily be achieved through the path browser.
As example of an M-file that defines a function, create a file in your working
directory named yplusx.m that contains the following commands:
function yplusx(y,x)
z=y+x
The following commands typed from within MATLAB demonstrate how this M-
file is used:
x=2
y=3
z = yplusx(y,x)
All variables used in a MATLAB function are local to that function only.
Variables, which are used in a script m-file, which is not a function, are all global
variables.
MATLAB M-files are most efficient when written in a way that utilizes matrix or
vector operations. Loops and if statements are available, but should be used
sparingly since they are computationally inefficient. An example of the use of the
command for is
for k=1:10;
x(k) = cos(k);
end
This creates a 1 x 10 vector x containing the cosine of the positive integers from 1
to 10. This operation is performed more efficiently with the commands
k= 1:10;
x=cos(k);
if(a<=2);
b=1;
elseif(a>=4)
b=2;
else
b=3;
The allowable comparisons between expressions are >=, <=, <, >, ==, and ~=.
Suppose that you want to run an M-file with different values of a variable T. The
following command line within the M-file defines the value:
Plotting
Commands: plot, xlabel, ylabel, title, grid, axis, axes, stem, subplot, zoom,
hold
The command most often used for plotting is plot, which creates linear plots of
vectors and matrices; plot(t,y) plots the vector t on the x-axis versus vector y on
the y-axis. There are options on the line type and the color of the plot which are
obtained using plot(t,y,'option'). The linetype options are '-' solid line (default), '--
' dashed line, '-.' dot dash line, ':' dotted line. The points in y can be left
unconnected and delineated by a variety of symbols: + . * o x. The following
colors are available options: r, g, b, k, y, m etc.
For example, plot(t,y,'--') uses a dashed line, plot(t,y,'*') uses * at all the points
defined in t and y without connecting the points, and plot(t,y,'g') uses a solid
green line. The options can also be used together, for example, plot(t,y,'g:') plots a
dotted green line.
To plot two or more graphs on the same set of axes, use the command
plot(t1,y1,t2,y2), which plots y1 versus t1 and y2 versus t2.
To label your axes and give the plot a title, type
ylabel(‘step response’)
title(‘my plot’)
grid
The problem that you will encounter most often when plotting functions is that
MATLAB will scale the axes in a way that is different than you want them to
appear. You can easily override the autoscaling of the axes by using the axis
command after the plotting command
where xmin, xmax, ymin, and ymax are numbers corresponding to the limits you
desire for the axes. To return to the automatic scaling, simply type axis.
For discrete-time signals, use the command stem, which plots each point with a
small open circle and a straight line. To plot y[k] versus k, type
stem(k,y)
You can use stem(k,y,'filled') to get circles that are filled in.
To plot more than one graph on the screen, use the command subplot(mnp)
which partitions the screen into an m x n grid where p determines the position of
the particular graph counting the upper left corner as p=1. For example,
subplot(211),semilogx(w,magdb);
subplot(212),semilogx(w,phase);
plots the bode plot with the log-magnitude plot on top and the phase plot below.
Titles and labels can be inserted immediately after the appropriate semilogx
subplot(111).
When using MATLAB, you may wish to leave the program but save the vectors
and matrices you have defined. To save the file to the working directory, type
save filename
where "filename" is a name of your choice. To retrieve the data later, type
load filename
General Information
• MATLAB is case sensitive so "a" and "A" are two different names.
• Comment statements are preceded by a "%".
• You can make a keyword search by using the help command.
• The number of digits displayed is not related to the accuracy. To change
the format of the display, type
format short e for scientific notation with 5 decimal places,
format long e for scientific notation with 15 significant decimal places and
format bank for placing two significant digits to the right of the decimal.
• The commands who and whos give the names of the variables that have
been defined in the workspace.
• The command length(x) returns the length of a vector x and size(x)
returns the dimension of the matrix x.
Exercise:
E.1. Create a vector,
a. “A” of the even whole numbers between 31 and 75.
b. “B” of the odd whole numbers between 75 and 131.
c. “C” of the whole numbers between 1 and 100.
a. 2/2*3
b. 6-2/5+7^2-1
c. 10 / 2 \ 5 - 3 + 2 * 4
d. 3^2/4
e. 3^2^2
f. 2 + round (6 / 9 + 3 * 2) / 2 - 3
g. 2 + floor (6 / 9 + 3 * 2) / 2 - 3
h. 2 + ceil (6 / 9 + 3 * 2) / 2 – 3
a. 2, 4, 6, 8…
b. 10, 8, 6, 4, 2, 0, -2, -4
c. 1, 1/2, 1/3, 1/4, 1/5...
d. 0, 1/2, 2/3, 3/4, 4/5...
Xn = (-1)n+1/(2n-1)
Add up the elements of the version of this vector that has 100 elements.
E.8. Given a vector, t, of length n, write down the MATLAB expressions that
will correctly compute the following:
a. ln(2 + t + t2)
b. et(1 + cos(3t))
c. cos2(t) + sin2(t)
d. tan-1(1) (this is the inverse tangent function)
e. cot(t)
f. sec2(t) + cot(t) - 1
Test that your solution works for t = 1:0.2:2
E.10. Write the following matrix in Matlab and perform the given function.
A= [12 15 16; 23 26 27; 31 40 41]
B= [22 35 56; 63 66 78; 24 30 46]
Find out
a. A+B
b. B*A
c. A.*B
d. B-A
Note:
• The decision of the marks will be made after the demonstration of
the lab exercise.
• Late submission will deduce the obtain marks by 15% on each day.
EXPERIMENT NO – 2
INTRODUCTION
Informally the terms matrix & array are often used interchangeably.
Matrix is the two dimensional array of real & complex numbers.
Matlab contain many functions that create different type of matrices.
>> A=pascal(3)
A= 1 1 1
1 2 3
1 3 6
If A ≠ A '
Then matrix is non-symmetric:
We use magic command in Matlab to create such type of matrix.
>> help magic
⎛8 1 6⎞
⎜ ⎟
B = ⎜3 5 7⎟
⎜ 4 9 2⎟
⎝ ⎠
⎛8 3 4⎞
⎜ ⎟
B' = ⎜1 5 9⎟
⎜6 7 2⎟
⎝ ⎠
⎛9 4⎞
⎜ ⎟
C = ⎜ 2 8⎟
⎜6 7⎟
⎝ ⎠
Here C is the generated matrix with 3 rows and 2 columns.
>> K=10:10:100;
K= [10 20 30 40 50 60 70 80 90 100]
>>K1=K (1:5)
K1= [10 20 30 40 50]
>>K2=K (3:8)
K2= [30 40 50 60 70 80]
>>P=Pascal (3)
⎛1 1 1 ⎞
⎜ ⎟
P= ⎜1 2 3 ⎟
⎜1 3 6 ⎟
⎝ ⎠
>> P1 =P(:, 2)
⎡1 ⎤
P1= ⎢⎢ 2 ⎥⎥
⎢⎣3 ⎥⎦
>>P2=P(1,:)
P2= [1 1 1]
M=[1:5; 2:2:10; 3:3:15]
⎡1 2 3 4 5 ⎤
M = ⎢⎢ 2 4 6 8 10 ⎥⎥
⎢⎣3 6 9 12 15 ⎥⎦
>>M1=M (2:3, 2:4)
⎡4 6 8 ⎤
M1 = ⎢ ⎥
⎣6 7 12 ⎦
>>M (:) % this command will convert the matrix into vectors such
that
columns are taken first. Try it yourself & also below given
commands.
>>K=1:2:10
>>f=5:5:50;
>>f(k)Æchoose position values within the ‘f’
Exercise:
E.1. Create a symmetric matrix ‘m’ with five rows and five columns. Calculate
its transpose and verify that the matrix is symmetric by observing that m=m’.
E.2. Create a random matrix with 3 rows and 5 columns. The matrix values
should be integer b/w (0—100), than change its all 2 rows values by ‘7’.
Hint: Use commands fix & rand
E.3. Use your knowledge to define a vector by incrementing and also how to
cascade the more than one vector to form a matrix try to make a matrix with the
following properties.
(1) Matrix should contain 2 rows.
(2) First row of matrix should contain even number b/w (0—10)
(3) Second row of matrix should contain odd number b/w (11—20)
How many columns are there in above matrix?
………………………………………………
E.4.
Define
⎡1+2j 3 ⎤ ⎡ 2+3j 5+5j⎤
d= ⎢ ⎥ & e= ⎢
⎣7 5j⎦ ⎣0 1-8j ⎥⎦
and calculate
(a) c=d+e
(b) c=d-e
(c) c=d*e
(d) c=d/e
EXPERIMENT NO – 3
INTRODUCTION
As well the concept of iteration or loop is concerned you are suppose to
be familiar with it. There are two most prominent types of loop in Matlab. These
are “for & while” loop. Also you are familiar with the conditional statement in
C++. That are “if & Switch Statement”. We will study some of them very soon
and some in next one lab.
“For” LOOP:
The ‘for’ loop allow us to repeat certain commands. If you want to repeat
some action in a pre determined way, you can use the ‘for’ loop. All of the loop
structures in MATLAB are started with a key word such as ‘for’ or while’ and
they all end with the word ‘end’.
The ‘for’ loop will loop ground some statement, and you must tell
MATLAB where to start and where to end. Basically you give a vector in the ‘for’
statement and MATLAB will loop through for each value in the vector.
For example a simple loop will go around 4 times like
Example 3.1:
>> for j=1:4,
j
end
Observe the above three lines result and write down them below
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
Note: once the MATLAB reads the ‘end’ statement, it will loop through and print
out J each Time.
For an other example, if we define a vector and data want to change the
entries , we can step through and change each individual entry.
Example 3.2:
>> v= 1:3:10
>>for j=1:4,
V (j) =j;
end
Comment on above lines after observing the result in Matlab.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
Important Note: Using loops in MATLAB are not efficient. Loops cause the
programs to run very slow. Always use some alternative way so that programs
run fast.
Example 3.3:
>>v=1:3:4
>>v (1:4) =1:4;
Comment on above lines after observing the result in Matlab.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
Now here is another example of using ‘for’ loops this example declares a
vector ‘x’ and then uses a ‘for’ loop to sum all of the values in ‘x’.
Example 3.4:
>>x=20:20:200
>>total =length(x);
>>temp=0;
>>for i=1:total
temp =temp+x(i);
end
>>temp
total variable. Then we define a ‘temp’ variable and set it to ‘o’, using “temp = 0”
statement.
Then we use a ‘for’ loop & using this ‘for’ loop we add the x (i) value to temp
and reassign that value to temp again.
While LOOP:
The “while” loop repeats a sequence of commands as long as some
condition is met. A simple example using a ‘while’ loop is
Example 3.5:
j= 1 ;
w h ile (j< = 4 )
a (j)= 1 0 * j;
j= j+ 1 ;
end
a
Comment on above lines after observing the result in Matlab.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
The while loop continues to execute until the value of j becomes greater
than 4. When this happens the loop exits.
The above ‘while’ loop body will execute until the condition “j < = 4”.
When this condition becomes false the ‘while’ loop will stop executing.
Example 3.6:
a = 1 :1 0
i= 1 ;
w h ile (i< = 1 0 )
b (i)= 2 ^ a (i);
i= i+ 1 ;
end
b
Comment on above lines after observing the result in Matlab.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
a = [1 2 3 4 5 6 7 8 9 10].
Then using a “while” and tells that loop until the value of ‘i’ is less than or equal
to 10. Then using the statement “b (i) = 2 a (i)” & “i = i + 1" it makes another
vector having values.
b = [21 2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
2
10] at the end it displays the value of ‘b’
IF Statement:
We can make decisions by using the ‘if’ statement. the ‘if’ must be terminated
with an ‘end’ statement
Example 3.7
c=2;
if(c==1)
disp('c is 1')
elseif (c==2)
disp('c is 2')
else
disp('c is neither 1 nor 2');
end
The above example declares a variable ‘c’ & assigns it a value ‘2’ by using “c = 2”.
Then it uses ‘if’, ‘elseif’ and ‘else’ statements to check the value of ‘c’ and
displays the result using “disp” command.
EXERCISE:
E.1. Define a vector a = [1 2 3 4 5] and then using a “for” loop create another
vector ‘b’ haring the following values corresponding to ‘a’
b = [11 22 33 44 55]
E.2. Without using a “for” loop and using some alternative method define
vector
a = [1 2 3 4 5] and create b1 = [11 22 33 44 55]
E.3. Indicate the error in the following. Tell the line # which contains the error
and what is the error.
1.......v=0:2:10
2.......for c=1:length(v)
3........ v(c-1)=v(c)+5;
4........end
5........v
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
E.5. Refer to “example 6”. Change the code so that it creates vector ‘b’ having
values
b = [51 52 53 …………. 510].
E.6. What will be the output of “example 7” if we define C = 3 and other code
remains unchanged?
EXPERIMENT NO – 4
BASIC SIGNALS
DISCRETE SIGNALS:
Discrete time signals are defined only at discrete times, consequently for
these signal, the independent variable (usually time) takes on only a discrete set
of values e.g. stock market index.
UNIT IMPULSE:
One of the simplest discrete time signals is the unit impulse or unit
sample, which is defined as
⎛0 n ≠ 0⎞
δ [n] = ⎜ ⎟
⎝1 n=1 ⎠
0 n Æ
no nÆ
Any discrete time signal can be broken down into a series of individual
impulses which when added together give back the original discrete time signal.
y[n] = 2δ [n] + 5δ [n − 3] + 2δ [n + 1] + 7δ [n + 2]
722 5
-2 -1 0 3
MATLAB IMPLEMENTATION:
Example 1:
Write and save the following function in matlab.
Example 2:
Now call the above function with the value of L=5 & no=2;
You will see the following result.
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-5 -4 -3 -2 -1 0 1 2 3 4 5
-3 -2 -1 0 1 2 3
⎛1 n>=0 ⎞
U[n-no] = ⎜ ⎟
⎝ 0 n<0 ⎠
0 1 2 3 4 5Æ
The following relation gives the general form of a delayed unit step
function
⎛1 n>=no ⎞
U [n-no] = ⎜ ⎟
⎝0 n< no ⎠
noÆ
MATLAB IMPLITATION:
Example # 3:
Function [x,n]=dusbt031018(L,no)
n= -L:L;
len=length(n);
x=zeros(1,len);
x(L+no+1:len)=1;
Example#4:
Function [x,n]=dusbt031018(L,no)
n= -L:L;
len=length(n);
x=[zeros(1,l+no) 1 ones(1,L-no)]
% x=[zeros(1,l+no) ones(1,L-no+1)]
Example 4 & example 5 are basically the some implementation ( I.e. they
perform the same task of creating a discrete unit step function)
CONTINUOUS SIGNALS: In the case of continuous signals the independent
variable is continuous, and thus their signals are defined for a continuum of
values of time e.g. a speech signal that has values defined for all time.
-3 -2 -1 0 1 2 3 Æ
Shifted impulse definition is given by.
⎛1 t-to ⎞
δ(t − to) = ⎜ ⎟
⎝0 ow ⎠
-3 -2 -1 0 1 2 3 Æ
here no=3;
MATLAB IMPLITATION:
Example #5:
Function [x,t]=cuibt031018(L,no,inc)
t= -L:inc:L;
x=zeros(1,length(t));
x((L+no)/inc+1)=1;
Example #6:
Function [x,t]=cuibt031018(L,no,inc)
t= -L:inc:L;
x=[zeros(1, L+no)/inc) 1 zeros(1, (L-no)/inc)];
Example 5 & 6 perform some task but in a deferent way of MATLAB
implementation.
UNIT STEP in Continuous time unit step function is defined in a manner similar
to it s discrete time counterpart i.e.
⎛1 t>=0 ⎞
u (t ) = ⎜ ⎟
⎝ 0 t<0 ⎠
The delayed singles general form will be
⎛ t>=to ⎞
u (t − to) = ⎜ ⎟
⎝ 0< t0 ⎠
MATLAB IMPLIMENTATION:
Example #7
Function [x,t]=cusbt031018(L,no,inc)
t=-L:inc:L;
Len=length(t);
x=zeros(1,len);
x(((L+no)/inc+1:len)=1;
Example #8
Function[x,t]=cusbt031018(L,no,inc)
t=-L:inc:L;
x=[zeros(1,(L+no)/inc) 1 ones(1,(L-no)/inc)];
x(-t)=x(t)
or
x[-n]=x[n]
A signal is considered odd if
x(-t)= -x(t)
or
x[-n]= -x[n]
An important fact is that any signal can be broken into a sum of two
signals, one of which is even and one of which is odd.
The even part of a signal can be expressed
Xe(t ) = 1/ 2[ x(t ) + x(−t )]
The odd part representation of a signal I, given by
Xo(t ) = 1/ 2[ x(t ) − x(−t )]
EXERCISE
E.1. Implement the function y[n] given by the relation
y[n] = 2δ[n + 3] + δ[n − 1] + 5δ[n] + 6δ[n + 5] − 7δ[n − 2]
take L=6
E.2. Implement the continuous time function y(t)
y (t)=3u(t)+t*u(t)-(t-1)*u(t)-5u(t-2)
Take L=5, inc=0.01
E.3. Implement the function given below
(A) X1[n]=u[n+2]-u[n-2] (L=6)
(B) X2[n]=u[n+7]-u[n+5]-2u[n-7] (L=10)
(C) X1(t)=u(t+2)-u(n-2) (L=6; inc=0.01)
( D ) X 4(t ) = δ (t − 1) − 3δ (t − 4) + 2δ (t + 1) (L=5, inc=0.01)
E.4. Shown below is the graph for discrete function Use “for” loop to
implement the below graph. You can use function of “example 1 or Example 2”.
Take L=40. Also plot the given graph
40
30
20
10
-10
-20
-30
-40
-40 -30 -20 -10 0 10 20 30 40
1.8
1.6
1.4
1.2
0.8
0.6
0.4
0.2
0
-4 -3 -2 -1 0 1 2 3 4
E.6. Shown below is the graph for discrete function Use “for” loop to
implement the below graph. You can use function of “example 3 or Example 4”.
Take L=20. Also plot the given graph
EXPERIMENT NO – 5
SIGNALS TANSFORMATIONS
TIME SHIFTING:
Consider a signal x(t). A time shifted version of this signal is
Note that y(t0) = x(0) – Hence if ‘t0’ is positive the shifted signal is delayed
in time (shifted to the right relative to x(t). If to is negative , y(t) is advanced in
time (shifted to the left).
MATLAB IMPLEMENTATION
Example # 1
in c = 0 .0 1 ;
t = - 1 0 : in c :1 0 ;
f = 0 .1 ; to = 2 ;
x = s in ( 2 * p i* f* t);
y = s in (2 * p i* f* (t - t0 ));
s u b p lo t ( 3 1 1 ) ;
h o ld o n ;
p lo t ( t,x );
p l o t ( t , y , 'r : ') ;
g rid
s u b p lo t( 3 1 2 ) ;
p lo t ( t,x );
g rid
s u b p lo t( 3 1 3 ) ;
p l o t ( t , y , 'r : ') ;
g rid
EXPLANATION
This code plots the function x = sin (2πf(t – t0)
Thus
TIME SCALING:
Given a signal x(t) a time scaled version of this signal is
Y(t) = x(at) i
EXPLANATION
This coding plots the function x = sin(2πft) and also plots the function
Y(t) = x(2t);
Here a = 2 so the function y(t) will be compressed in time (speed up) version of
the signal x(t). here the frequency will be doubled.
MATLAB EMPLEMENTATION
Example # 2
in c = 0 .0 1 ;
t = -1 0 : in c :1 0 ;
f = 0 .1 ; a = 2 ;
x = s in (2 * p i* f* t);
y = s in (2 * p i* f* (a 8 t));
s u b p lo t (3 1 1 );
h o ld o n ;
p lo t ( t,x );
p lo t ( t , y , 'r ; ') ;
g rid
su b p lo t (3 1 2 );
p lo t ( t,x );
g rid
s u b p lo t (3 1 3 );
p l o t ( t , y , 'r ; ') ;
g rid
3 – TIME REVERSAL
In time reversal, we create a new, transformed signal y(t) by replacing t with - t
in the original signal x(t)
Y(t) = x(-t)
For any particular value of time t = t0 y(t0) = x(-t0) and y(-t0) = x(t0)
MATLAB IMPLEMENTATION
Example # 3
in c = 0 .0 1 ;
t = -1 0 : in c : 1 0 ;
f = 0 .1 ; x = s in (2 * p i* f* t);
r x = f lip lr ( x ) ;
r t = f lip lr ( t) ;
rt = -1 * rt;
L = le n g th (x );
s u b p lo t ( 3 1 1 );
h o ld o n ;
p lo t ( t( L /2 :L ), x ( l/2 : L )) ;
p l o t ( r t ( 1 : L / 2 ) , r x ( 1 : L / 2 ) , 'r : ') ;
g rid
t i t l e ( 'e x a m p l e o f t i m e R e v e r s a l ') ;
s u b p lo t( 3 1 2 );
p lo t ( t,x ) ;
g r i d y l a b e l ( 'o r i g i n a l s i g n a l ') ;
s u b p lo t ( 3 1 3 );
p l o t ( t , r x , 'r ; ') ;
g rid
y l a b e l ( 'T i m e R e v e r s a l S i g n a l ') ;
inc = 0.01
t = -10 + inc :inc: 10-inc;
f = 0.2;
x = cos (2*pi*f*t);
[even,odd] = even odd bt 02301 (x, t, 'c');
1 x = sin(2*pi*f*t);
2 x = sinc(t);
3 x = sin(2*pi*f*t). *sinc(2*pi*5*f*t*);
4 x= square (2*pi*f*t);
Change inc 0.1 +eps;
[even, odd] = even odd bt 02301 (x,t, 'd');
5 x = cos (2*pi*f*t) + square (2*pi*f*t);
Change inc = 0.01 + eps;
[even, odd] = even odd bt 02301 (x,t, 'c');
6 x = 2x sin(2*pi*f*t) + square (2*pi*f*t);
Change inc = 0.01 + eps
[even, odd] = even odd bt 02301 (x,t, 'd');
7 x = cos (2*pi*f*t) + sin (2*pi*10*f*t);
Change inc = 0.021 ;[even, odd] = even odd bt o2301 (x,t, 'c');
8 x = cos (2*pi*f*t) + sin(2*pi*f*30*t);
Change inc = 0.02
EXPERIMENT NO – 6
Since sin(Q) = sin(Q + 2π) therefore the signal is periodic with the period
‘2π’.
A periodic signal in discrete time is defined similarly to continuous time
periodic signal. Specifically a discrete time periodic signal.
Specifically a discrete time signal x[n] is periodic with period ‘N’ where
‘N’ is positive integer, i:e if it is unchanged by a time shift of ‘N’ i:e.
X[n] = x[n + bN] Æ B (discrete time)
Where b = 0,1,2,3,4,-----------
FUNDAMENTAL PERIOD
The fundamental period is the smallest positive value of ‘T’(continuous)
or ‘N’ (discrete) for which the equations of periodicity held.
Consider the example of a sine function. In that case the signal is periodic on 2π,
4π, 6π and so on but the fundamental time period of the signal is 2π.
EXAMPLE # 1
t=0:0.01:6;
f=0.5;
x=sin(2*pi*f*t); hold on;
plot(t,x);
xL=[ 1/f 1/f];
yL=[1 1];
plot(xL, yL, 'r:')
hold off
HARMONICS:
Harmonics are integral multiples of a signal. Consider the signal
cos (wot). its harmonics are cos(2 wot), cos (3 wot)is the fundamental harmonic.
EXAMPLE # 2
inc = 0.01;
t = 0 : inc : 4;
f = 0.25;
w = 2 * pi * f ;
fundamental=sin(w *t);
harmaonic2=sin(2*w *t);
harmaonic3=sin(3*w *t);
harmaonic4=sin(4*w *t);
hold on;
plot(t,fundam ental,'ro');
plot(t,harm onic2,'k--');
plot(t,harm onic3,'m:');
plot(t,harm onic4,'y');
hold off
Example#3
t = 0 : 0.01: 6
f = 0.5
w = 2* pi * f
% plot function x(t)
Exercise:
1. Write MATLAB function for plotting the 5 harmonics of a signal x(t) =
cos(wt) with a fundamental frequency of f = 0.5 Hz. Take t = 0:0.01:2
2. What are the frequencies and Time periods at the all 5 harmonics in
questions 1
t = -2:0.01: 2. Plot all the 5 harmonics of the above function in same figure using
the subplot command. Subplot (511), subplot(512) --------
Subplot (513), Subplot(514), Subplot (515),
4. If the frequency of x(t) = sin(2πft) is f = o.2hz what will be the time period
of the 4rth harmonic of the above signal x(t). plot x(t) and its 4rth harmonic and
check the time period of 4rth harmonic in MATLAB. Do result agree with your
theoretical calculation of time period of 4rth harmonic?
Plot the function f(t) in Matlab upto n=5 take f=1Hz, t=0:0.01:4
EXPERIMENT NO – 7
SAMPLING:
A continuous time signal x (t) has its value defined for every value of
time ‘t’. on the other hand a discrete time signal has its value at regular fired
intervals. A continuous time signal can be represented by its values or samples at
points equally spaced in time.
Consider a continuous time signal x(t). This signal can be represented as a
discrete time signal having values of x(t)at ‘n’ number of intervals spaced by a
time interval ‘Ts’. We can’t obtain all the data in the signal by sampling it. Even if
we sample at a very high rate some data will still be lost but the results of course
will be very good.
NYQUIST CRITERIA:
If the greater the number of samples the greater the accuracy, then why
we don’t sample at a very high rate. There are two reasons for it. First of all high
number of samples require a lot of manipulation on the behalf of machine
software that we are using which results in move time. Secondly most of the vital
data that we need can easily be obtained from low sampling rates.
Therefore we need some criteria to define the rate at which the signals
should be sampled. This criterion is called the Nyquist criteria.
According to it, the sampling frequency must (at least) be twice the signal
frequency. When the sampling rate doesn’t satisfy the Nyquist. When the
sampling get the correct information about the signal.
Reducing the number of samples reduces information above original signal.
MATLAB is computer based simulation software. In MATLAB choosing a
very high sampling rate simulates analog signals. Normally the sampling
frequency chooses to be 10 times higher than the maximum frequency
component in a given signal.
Example#01:
f=50;
fs=10*50;
ts=1/fs;
t=1:ts:0.1;
x=sin(2*pi*f*t);
stem(t,x)
EXPLANATION:
This example generates a sinusoidal signal x(t) = sin wt = sin 2πft will f =
50 Hz. The sampling frequency ‘fs’ is chosen to be 10 times the signal frequency.
Note that in one complete cycle it contains 10 samples.
Example#02:
f=1;
w=2*pi*f;
a=4;
fs1=2*f; fs2=10*f; fs3=20*f; fs4=40*f;
ts1=1/fs1; ts2=1/fs2; ts3=1/fs3; ts4=1/fs4;
t1=0:ts1:a; t2=0:ts2:a; t3=0:ts3:a; t4=0:ts4:a;
x1=cos(w*t1); x2=cos(w*t2);
x13cos(w*t3); x4=cos(w*t4);
subplot(221); stem(t1,x1);
subplot(222); stem(t2,x2);
subplot(223); stem(t3,x3);
subplot(224); stem(t4,x4);
EXPLANATION:
The above example samples a cos(wt) signal of frequency 1 Hz With the
sampling frequencies 2Hz, 10Hz, 20Hz & to Hz and displays the four sampled
signals in & independent plots using the stem command. We can easily see that
as the closer and signal accuracy increases.
MANIPULATION:
We know that MATLAB treats all the inputs and outputs as
matrices/vector. Therefore for the manipulation of an audio signal, is no
different than altering the elements of a matrix. Avoid using loops in
manipulating a signal.
Example#03:
Make a folder in "c:" directory with name "soundlab" copy the file "testsound.wav" to the
"soundlab" folder.
[soundsig, fs, nbits]=wavread('c:\soundlab\testsound.wav');
left=soundsig(:,1);
right=soundig(:,2);
L=length(left);
half(left(1:2:L);
reverse=flipud(left);
wavplay(soundsig,fs);
EXPLANATION:
The ‘wavread’ function reads the “wav” file and returns the sampled
audio signal in variable “soundsig”, “fs” is the sampling frequency & “nbits”
represents the number of bits. The “wavplay” function plays the audio signal
with sampling frequency “fs”.
ECHO:
Example#04:
function[sigx, echosig, reflectedsig]=echobt031018(sig,fs,td,attn);
zer=zeros(td*fs,2);
sigx=[sig; zer];
refectedsig=[zer; sig*attn];
echosig=sigx+reflectedsig;
Example#05:
att=0.2;
td=1;
[signal, fs, nbits]= wavread('c:\soundlab\testsound.wav');
[signalx, echo, ref]=echobt031018(signal, fs, td,att);
wavplay(signalx,fs);
wavplay(echo, fs);
The coding in example # 5 produces a “achosignal” giving the reflected
sound an attenuation of 0.2 and time delay of 1 second. Next the
“warplay(signalx, fs” plays the original signal and “wavplayecho,fs” plays the
echo version of the original signal with sampling frequency “fs”.
EXAMPLE # 6:
Replace the last line in “example # 3” “wavplay (soundsignal,fs)” with
the following values. Run the “example # 3” for each value given below.
1 wavplay(sound signal, fs);
2 wavplay (sound signal, 0.5* fs);
3 wavplay (sound signal, 2 *fs);
4 wavplay (half, fs);
5 wavplay (half, 0.5*fs);
6 wavplay (half, 2*fs);
7 wavplay ( reverse, fs);
8 wavplay (reverse, o.5*fs);
9 wavplay (reverse, 2*fs);
Turn on your “speaker/headphones” and hear the result for all above values
numbered 1 through 9 by running the example # 3 & changing last line to the
above values separately & hearing the output.
2. Do the example # 6 and hear the output for all the values numbered 1 to 9.
Give explanation for each from 1 to 9 that what we hear? 3 what is the effect of
0.5fs and 2fs?
Exercise:
1. Consider a signal y(t) = sin(wt) where “w=2πf” Take the case when
f = 23 (Hertz). What should be the sampling frequency (fs) according to Nyquist
Criteria?
3. Consider a signal s(t) = 50 cos (20πt) + 10 cos (100πt) what is the maximum
frequency component in the above signal (Remember the relation cos wt = cos
2πft)
Fmax = Hz
What should be the sampling frequency according to the Nyquist Criteria in the
above signal “s(t)”?
+
Input Sig 2 td Output Sig
2
Reflected Sig
Where = attenuation
& td = Time Delay
EXPERIMENT NO - 8
If impulse response h[n] is given then we ear find the system output y[n] for any
input x[n] by the following relation.
+∞
y[ n ] = x[n] * h[n] = ∑
k = −∞
x[k] h [n-k]
+∞
= h[ n ] x[n] = ∑
k = −∞
h [k] x [n-k]
Than a= [1 1];
b= [1 2]
ab= conv(a,b) gives
ab= [1 3 2]
Therefor
a(s)*b(s)= S2 + 3S +2
Example 1:
x[ n ] = [1,1,2,1,2]
h[n] = [1,2,-1,1 ]
y[n]= [1,3,3,5,3,5,-1,2]
x =[1 1 2 1 2];
h =[1 2 -1 1];
y = conv [x,h];
y
H ( s ) = B(s) /A(s)
the names of the vectors are generally chosen to be num and den but any other
name can be used.
For example
H = 2s+3
s3+4s2+5
so num = [2 3]
den = [1 4 0 5]
Note that tall the coefficents must be included in the vector, even zero co –
efficients.
To find the zero, poles and gain of a transfer function from the vectors
‘num’ and ‘den’ which contain the co efficients of the numerator and
denominator polynomials type.
[z,p,k]= tf2zp(num,den)
The zeros are stored in z, poles are stored in p and the gain is stored in k
To find the numerator and denominator polynomials from z, p and k type.
This deconvolves vector A out of vector B. The result is returned in vector Q and
the remainder in vector R such that
B= conv (A,Q) + R
Returns the residues in column vector R, poles in column vector P and direct
terms in row vector K. K vector is empty if length (B) < length (A) I;e if H is in
the proper form.
Example # 2
H=s - 5 b = [1 -5]
S2-4s+3 a = [1 -4 3]
b = [1 -5]
a = [1 -4 3];
Therefore matlab makes the task of creating partial fractions much more easier.
EXAMPLE # 3
W e know e-at has Laplace 1/(s+a). We verify this from MATLAB using the
following code
Syms f t a;
f=exp (-1*t)
F=laplace(f)
F
EXAMPLE # 4
Laplace transform of t is 1/S2
Syms f t;
f=t;
F=laplace(t)
F
EXAMPLE # 5
Consider
H(s) = S -5 = B(s) = -1 + 2 .
S -4S+3 A(s) (S-3) (S-1)
2
EXAMPLE # 6
H(S)= S3+2S+1
S2-2S-1
This is an improper function we can convert this into proper function by
following method.
Quotient + Remainder . = q(s) + r(s) = H(s) = B(s)
Denominator a(s) A(s)
B=[ 1 0 2 1];
A=[1 -2 -1];
[q, r] = deconv (B, A);
Matlab gives q=[1 2]; and r= [0 0 7 3];applying formula
H(s)= (S+2) + (7S + 3) .
(S2-2S-1)
Thus this example shows how to use MATLAB to convert from an improper
transfer function to proper transfer function representation.
Example # 7
H(s)= (S3-4S2+3S + 6)
(S2-4S+1)
B= [1 -4 3 6];
A= [1 -4 3]
[r p k]=residue(b, a)
By using eq A1
H(s)=(1S +0) + 3 . + -3 .
(S-3) (S-1)
H(s)= S + 3 . - 3 .
(S-3) (S-1)
Example # 8
H(S)= 3S+1 .
S2+2S+5
B=[3 1];
A=[1 2 5];
[r, p, k]= residue(B,A)
This code gives
r=[1.5+0.5j 1.5- 0.5j]; p=[-1+2j -1-2j]; k=[ ]
syms f s
f=(3S +1 )/ (S2 +2S +5);
ilaplace(f)
EXPERIMENT NO – 9
Z TRANSFORM
When we work with continuous time signals & systems in the transform domain,
we use the Laplace Transform. When we work with discrete time signals we use
a different transform, called the Z transform. This transform is a function of the
complex variable ‘z’ which has real and imaginary parts.
Ö z = |z| <z
where |z| =e αT , <z= wT
Consider a discrete time system which is given an input f[n]. The Z transform of
this input signal is defined as F[z] and is given below
+∞
f(n) Z F(z)
-1 -2 -n
b(1) + b(2) z + b(3) z +……………….+ b(n+1) z
H(z) = ___________________________________________ -------------------(C)
-1 -2 -m
a(1) + a(2) z + a(3) z +……………….+ a(m+1) z
Example#1:
clc
syms a n;
xn=1^n;
% xn=a^n;
Xz=ztrans(xn);
Example # 2:
Z Transform of x [n] = ( 1/2 ) U[n] + ( - 1/3 ) U[n]
clc
syms n ;
xn=(1/2)^n + (-1/3)^n ;
Xz=ztrans(xn);
disp('X(z) = ');
[rXz,how]=simple(Xz);
pretty(rXz);
disp('X(z) = ');
pretty(simplify(Xz));
Example # 3:
X (z ) = 1 Æ Equation F
_____________________
-1 -1
( 1 – (1/4) z ) ( 1 – (1/2) z )
clc
syms a z;
xn=iztrans(Xz);
disp('x[n] = ') ; pretty(xn);
Example # 4:
Zero Pole Plot for Equation F
close all;
b=[1];
a=[1 (-3/4) (1/8) ];
zplane(b,a);grid;
title('algebric manipulation for evaluating vector " a "');
p1=[1 (-1/4)];
p2=[1 (-1/2)];
p3=conv(p1,p2);
b=1;a=p3;
figure;zplane(b,a);grid;
title('Using " conv " for evaluating vector " a "');
r=[(1/2) (1/4)];
p=poly(r);
b=1;a=p;
figure;zplane(b,a);grid;
title('Using " poly " for evaluating vector " a "');
Example # 5:
Partial Fraction Expansion for Equation G
clc;
b=1;
a=[1 (-3/4) (1/8)];
[r,p,k]=residuez(b,a)
Example # 6:
Finding z , p & k from b & a
-1 -2
( 1+2z+z )
X(z) = ___________________ Æ Equation L
-1 -2
( 1 – (3/2) z + (1/2) z )
b=[1 2 1];
a=[1 (-3/2) (1/2)];
[z,p,k]=tf2zp(b,a)
zplane(b,a)
Put the values of z , p & k from coding above into the Equation M to find an equivalent
expression for X ( z ) involving Zeros , Poles and Gain. Therefore we have the Equation L
reduced to Equation M
Exercise:
X(z) = z Æ Equation P
______________
2
(3 z – 4 z + 1)
A) Evaluate the values of the vectors ‘b’ and ‘a’ for Equation P using
“Equations H , I & J ”
B) Evaluate the Partial Fraction expansion for Equation P & reduce it to form
given by Equation K. Use the “residuez” command for this purpose
D) Compare the Locations for Poles and Zeros from the Graph in Part C with
the partial fraction expansion done in part B
EXPERIMENT NO – 10
FOURIER SERIES
The French mathematician fourier found that any periodic waveform, that is a
waveform that repeats itself after sometime, can be expressed as a series of
harmonically related sinusoids. i:e sinusoids whose frequencies are multiples of a
fundamentals frequency (or first harmonic). For example a series of sinosids with
frequencies MHZ, 2MHZ, 3MHZ and so on, contains the fundamental frequency
of of 1MHZ, a 2nd harmonic of 2MHZ, a third harmonic of 3MHZ, and so on. In
general any periodic waveform can be expressed as.
Therefore the general equation of a fourier series expansion of a signal x(t) can be
written as
t1+ T0
(C) => an =2/T0 § x(t) cos ( nwot) dt n=1, 2, 3, ….
t1
t1+ T0
(D) => bn =2/T0 § x(t) sin ( nwot) dt n=1, 2, 3, ….
t1
This if we have any periodic signal “x(t)” we can find the values of a0, an and ba
and put these values in equation A to get the fourier series expansion of a signal.
EXAMPLE # 1
FOURIER SERIES
X (t)
1
The founer series representation of the signal x(t) shown above can be written as
∞
x(t) =1/2 + 2/Π [ cos (w0t) – 1/3 cos(3w0t) +1/5 cos(5w0t) -1/7 cos (7w0t) +
1/9 cos (9w0 t) + ….]
Thus the waveform only contain the odd harmonics i:e 1w0, 3w0, 5w0, 7w0, 9w0
and so on. If we continue the equation (f) given above upper infinity we get the
ideal waveform x(t) as shown in the figure. If we discard some upper harmonics
we will get ripples in the waveform.
Save this function with the some name i:e “squarewavefourierseries”. This
function takes the harmonics in variable “har”. These are the number of
harmonics to be computed in the fourier series expansion. The variable “tmax”
contains the maximum time limit of the signal. The function returned
“fourierseries” variable which contains fourier series expansion and ‘t’ contains
the time.
Example # 2:
Close All;
tmax= 2*Pi;
[tx1,fx1] =squarewavefourierseries(2000, tmax);
Figure;
Plot(tx1,fx1); grid;
[tx2,fx2] =squarewavefourierseries(50, tmax);
Figure;
Plot(tx2,fx2); grid;
[tx3,fx3] =squarewavefourierseries(20, tmax);
Figure;
Plot(tx3,fx3); grid;
[tx4,fx4] =squarewavefourierseries(10, tmax);
Figure;
Plot(tx4,fx4); grid;
[tx5,fx5] =squarewavefourierseries(3, tmax);
Figure;
Plot(tx5,fx5); grid;
EXAMPLE # 3:
Function[t, fourierseries]= sawtoothfourierseries(har,tmax);
a=5;
fo=1/(2*pi);
wo= 2*pi*fo;
t= -tmax : 0.05 : tmax;
n=1: har;
ao=0;
an= zeros(1,length(n));
bn=(2*a)./(n.*n.*pi.*pi).*(sin(n*pi)-(n*pi).*cos(n*pi));
y=zeros(har,length(t));
for n=1:har
y(n,:)= an(n) * cos(n* wo*t)+ bn(n) * sin( n*wo*t);
end
fourierseries= ao+ sum(y,1);
Save this function with the some name i:e “sawtoothfourierseries”. This
function takes the harmonics in variable “har” and the max time limit in variable
tmax. These are the number of harmonics to be computed in the fourier series
expansion. The variable “tmax” contains the maximum time limit of the signal.
The function returned “fourierseries” variable which contains fourier series
representation for the sawtooth waveform. The ‘t’ variable is the axis for that
signal. As ‘n’ approaches to infinity, the waveform approaches to an ideal wave
form of sawtooth.
EXAMPLE # 4:
Close All;
tmax= 2*Pi;
[tx1,fx1] = sawtoothfourierseries (2000, tmax);
Figure;
Plot(tx1,fx1); grid;
[tx2,fx2] = sawtoothfourierseries (50, tmax);
Figure;
Plot(tx2,fx2); grid;
[tx3,fx3] = sawtoothfourierseries (20, tmax);
Figure;
Plot(tx3,fx3); grid;
[tx4,fx4] = sawtoothfourierseries (10, tmax);
Figure;
Plot(tx4,fx4); grid;
[tx5,fx5] = sawtoothfourierseries (3, tmax);
Figure;
Plot(tx5,fx5); grid;
[tx6,fx6] = sawtoothfourierseries (1, tmax);
Figure;
Plot(tx6,fx6); grid;
EXPERIMENT NO – 11
N − 1
j ( 2 π
∑
− ) k n
X ( K ) = x [ n ]e N
n = 0
N − 1
1 − j ( 2 π
∑
) kn
x[n ] = X (k )e N
N k = 0
2 π
Width =
N
In order to estimate DTFT of the signal, we may take DFT of the signal, while
taking large number of DFT points.
PROBLEM # 1:
Read the help of these Matlab functions understanding the input and output
parameters.
PROBLEM # 2:
inc = 0.001;
t = -0.2+inc:inc:0.2;
Plot the magnitude of DFT of Rectangular pulse having width 0.025 and defined
for the time given above. Use the Matlab functions “ rectpuls ” & “ fft ”.The DFT
should be centered around zero. Use function “ fftshift ” to shift zero-frequency
component to center of spectrum
PROBLEM # 3:
Consider a continuous time signal xc ( t ) = cos ( 4000 π t ) defined for the time
given below
Here To is the Time Period of xc ( t ). Plot the signal and the magnitude of DFT
for this cosine signal versus frequency values. Choose a value of “ N ” which is
higher than the signal length
Xc ( w ) = π ∂ [ ω – ωo ] + π ∂ [ ω + ωo ] .
Thus you will get the DFT graph having 2 impulses at +2000 Hertz and -2000
Hertz.
PROBLEM # 4:
Plot the Inverse DFT for problem 3.Graphs for Problem 3 and 4 are shown below
-1
-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5
Cosine Signal xt = cos(4000*pi*t) -3
x 10
300
200
100
0
-50 -40 -30 -20 -10 0 10 20 30 40 50
DFT of cosine ( Frequency Axis in KHz )
1
0.5
-0.5
-1
-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5
Inverse Discrete Fourier Transform IDFT -3
x 10
EXPERIMENT NO - 12
Once you lay out your GUI and set each component’s properties, using the tools in the
Layout Editor, you can program the GUI with the M-file Editor. Finally, when you press the Run
button on the toolbar, the functioning GUI appears outside the Layout Editor window.
GUIDE Toolset
For more information on the full set of GUIDE development tools, see the following sections:
•“Laying Out GUIs — The Layout Editor” on page 4-2 — add and arrange objects in the figure
window.
•“Aligning Components in the Layout Editor” on page 4-9 — align objects with respect to each
other.
•“Setting Component Properties — The Property Inspector” on page 4-14 — inspect and set
property values.
•“Viewing the Object Hierarchy — The Object Browser” on page 4-16 — observe a hierarchical
list of the Handle Graphics objects in the current
MATLAB session.
•“Creating Menus — The Menu Editor” on page 4-17 — create a menu bar or a context menu for
any component in your layout.
•“Setting the Tab Order — The Tab Order Editor” on page 4-27 — change the order in which
components are selected by tabbing.
When you select a template in the left pane, a preview of it appears in the right pane. Clicking
OK opens the template in the Layout editor. See “Using GUIDE Templates” on page 3-6 for more
information about GUIDE templates. The next section, “Getting Started with GUIDE” on page 1-
1, provides a detailed example of how to create a GUI using GUIDE.
NOTE: for details about GUI, refer to the PDF document, buildgui.pdf available
in LAB folder.
Example:
The matlab code below builds a GUI which helps the user to create some basic
signals using graphical interface. Copy the code to a M-file and save it as
comgui2. Running this code will generate the GUI.
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
t=-1:0.01:1;
f=1;
w=2*pi*f;
handles.t=t;
handles.sin=sin(w*t);
handles.cos=cos(w*t);
handles.sinc=sinc(w*t);
handles.square=square(w*t);
% --- Outputs from this function are returned to the command line.
function varargout = comgui2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
plot(handles.t,handles.sin);
plot(handles.t,handles.cos);
% --- Executes on button press in SquareButton.
function SquareButton_Callback(hObject, eventdata, handles)
% hObject handle to SquareButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
plot(handles.t,handles.square,'b*:');
plot(handles.t,handles.sinc);
EXPERIMENT NO – 13
INTRODUCTION TO SIMULINK - I
AMPLITUDE MODULATION
Amplitude Modulation
ts=1e-3;
a=2;
t=-2:ts:2;
fm=1;
fc=15;
wm=2*pi*fm;
wc=2*pi*fc;
message=sin(wm*t);
carrier=cos(wc*t);
am=(a+message).*carrier;
subplot(311);
plot(t,message);
xlabel('Message Signal');
subplot(312);
plot(t,carrier);
xlabel('Carrier Signal');
subplot(313);
plot(t,am);
xlabel('Amplitude Modulated (AM) Signal');
Example # 1
Co n sta n t
S i n e Wa ve
S co p e
C arrier Signal
S i n e Wa ve 1
AM = (A + message) * Carrier ÆA
EXPERIMENT NO – 14
Example # 1:
Wo = 1
We have to implement the above equation up till 9Wot and see the output in
simulink.
So we require 5 sine wave blocks and 1 constant block of value 1/2. “Cosine” is
implemented by giving “sine” a phase shift of
SIMULINK IMPLEMENTATION
Example # 2:
Wo = 1
fo = 1/To =
Wo = fo = 1
cos7wot, cos9wot
Simulink parameters