Docsity

MATLAB Tutorial - Signals and Systems - Lecture Notes | ECE 301, Study notes of Signals and Systems

Toggle sidebar
bg1
1
MATLAB TUTORIAL #2
I. MATLAB BASICS
First, it is important to recognize that there are two ways of representing signals in a computer, numeric and
symbolic. Symbolic implementations are briefly described at the end of this section.
Before proceeding, it is helpful to note that there are many MATLAB demos which are available to help you
quickly learn about MATLAB features. Simply type demo on the command line in the MATLAB command
window and select the demo you wish to see.
Numeric representation of signals
A numeric implementation represents a signal by samples of the signal. An example of a simple sine is
shown below
In MATLAB, these numbers are represented as a simple vector
Notice that there is no time reference for the component numbers of the vector. In other words, from the
vector itself, it is not possible to tell what time corresponds to the very first vector component which is 0.
The time reference is something that is known to the user according to the way the vector was created.
MATLAB code which generates this sequence is given below
>> t = [0:0.25:6];
>> x = sin(pi*t)
>> plot(x)
The time reference for the first component of is because that is the first component of the vector, .
Notice that the sine function normally operates on a scalar angle, however in this example we have placed a
vector in the argument of the sine function. MATLAB, by convention, will create a vector for the result by
using the function to operate on each component of the vector, .
Before proceeding, note that it is possible to get immediate help on any MATLAB function or command.
Typing
>> help plot
in the MATLAB command window will provide help information for the plot command.
It is possible to access any component of a vector. The statement
>> x(4) = -10;
changes the 4th element of the vector to the number -10. Try plotting the new vector to see the change.
MATLAB normally displays the result of a calculation immediately, however placing a semicolon at the end
of a statement suppresses the display. In the code above, the first statement has the output suppressed, but
x02120 2–1–2–02120 2–1–2–02120 2–1–0,,,,,,,,,,,,,,,,,,,,,,,[ ]=
x0t
t
xx
pf3
pf4
pf5

Partial preview of the text

Download MATLAB Tutorial - Signals and Systems - Lecture Notes | ECE 301 and more Study notes Signals and Systems in PDF only on Docsity!

MATLAB TUTORIAL

I. MATLAB BASICS

First, it is important to recognize that there are two ways of representing signals in a computer, numeric and symbolic. Symbolic implementations are briefly described at the end of this section.

Before proceeding, it is helpful to note that there are many MATLAB demos which are available to help you quickly learn about MATLAB features. Simply type demo on the command line in the MATLAB command window and select the demo you wish to see.

Numeric representation of signals

A numeric implementation represents a signal by samples of the signal. An example of a simple sine is shown below

In MATLAB, these numbers are represented as a simple vector

Notice that there is no time reference for the component numbers of the vector. In other words, from the vector itself, it is not possible to tell what time corresponds to the very first vector component which is 0. The time reference is something that is known to the user according to the way the vector was created. MATLAB code which generates this sequence is given below

t = [0:0.25:6]; x = sin(pi*t) plot(x)

The time reference for the first component of is because that is the first component of the vector,. Notice that the sine function normally operates on a scalar angle, however in this example we have placed a vector in the argument of the sine function. MATLAB, by convention, will create a vector for the result by using the function to operate on each component of the vector,.

Before proceeding, note that it is possible to get immediate help on any MATLAB function or command. Typing

help plot

in the MATLAB command window will provide help information for the plot command.

It is possible to access any component of a vector. The statement

x(4) = -10;

changes the 4th element of the vector to the number -10. Try plotting the new vector to see the change.

MATLAB normally displays the result of a calculation immediately, however placing a semicolon at the end of a statement suppresses the display. In the code above, the first statement has the output suppressed, but

x =[ 0 , 2 1, , 2 0, , – 2 , – 1 , – 2 , 0 , 2 1, , 2 0, , – 2 , – 1 , – 2 , 0 , 2 1, , 2 0, , – 2 , – 1 , 0 ]

x 0 t

t

x x

the second statement will show the result of the vector.

In MATLAB, the fundamental algebraic element is the vector. Even a scalar number can be thought of as a 1-component vector. The following code will divide 2 into 1

a = 1/

The result 0.5000 will show which is the numeric representation for 1/2. However the variable is essentially a 1-component vector.

MATLAB has several features which make generating a sequence of numbers easy. The first statement from the MATLAB code given above generates the vector

The statement generates a vector, indicated by the brackets and the numbers inside are separated by the colon, “:”. The three numbers represent, in order, [starting value : increment : ending value]. This is an easy way of generating time samples which can be used to create the sampled cosine function.

operators

Some of the algebraic operations that are allowed include. These operations will be performed in a normal “Matrix Algebra” sense. That is, if vector is a row vector and you type the statement

c = a * b

then variable needs to have a column dimension of 3. For example,

b = [1 2 0.5; 2 1 1]; a = [5 4 3] c = a * b

will be an accepted calculation. The variable is a row vector, the variable is a matrix, and so the resultant variable is a row vector.

However, there are many times you may wish to perform an operation between two vectors or matrices that are component-wise operations. For example, suppose you have two column vectors and and you want to multiply their respective components. Consider the following MATLAB code

x = [1 2 3]’ y = [1 1/2 1/3]’; z = x .* y

The prime to the right of the vector definition for variable performs a transpose operation which converts the [1 2 3] row vector into a column vector. Also, note that we put a semicolon at the end of the “y = ...” statement to suppress the output, just for practice.

Normally, the last statement would not be valid in a matrix algebra sense. That is, it is illegal to multiply a vector with a vector because the “inner” dimensions don’t match. However, the operation between the two vectors is not a regular multiply (asterisk), it has a dot preceding it. When a dot precedes an operation, that tells MATLAB to perform the operation on the two elements in a component-wise sense. But this does require that the two variables have the same dimensions. Try the following commands

x = [1 2 3]’

x

a

t =[ 0 0.25 0.5 0.75 1 1.25 1.5 1.75 2, , , , , , , , , … , 5.75 6, ]

[ …]

a 1 × 3

b

a 1 × 3 b 3 × 2 c 1 × 2

1 × 3 x y

x

3 × 1 3 × 1

Now from the MATLAB command window, type the whos command again. You will see all the variables exist that were created by the m -file. It is very important to note that when an m -file is executed, all the variables in the MATLAB session are available by the m -file and can be modified by the m -file. It is as if the m -file commands are being typed on the command line directly in the MATLAB command window.

Conditional Statements

The other basic construct is the conditional. Create a new m -file with the following statements

j = 0; for i = 1: if (i < 5) j = j + i; end end

This MATLAB function will add the numbers 1 through 4. The conditional statement is ended with the end statement as well as the for loop.

At this point, a trigonometric fourier series example will be presented followed by the MATLAB code used to generate the signal that is represented by the series.

Symbolic Processing

Before proceeding, however, a brief introduction to symbolic processing is given. Type the following commands in the MATLAB command window.

syms t x = sin(pi * t) ezplot(x)

You will see the sin function properly graphed. You can adjust the time scale so the graph looks similar to the graph generated above using a numeric representation of the sine function. Note that the variables and are now symbolic variables. Type whos to see that their class has changed to sym object.

II.TRIGONOMETRIC FOURIER SERIES EXAMPLE

Consider the following signal

It is possible to generate a trigonometric Fourier Series of which takes the form

t x

x t ( )

t

  • 1

x t ( )

x ˜^ ( ) t a 0 a (^) n cos( n ω 0 t ) n = 1

∞

∑ b^ n^ sin(^ n ω^0 t )

n = 1

∞

Since the series is periodic and is periodic, it is possible to find the coefficients so that and are identical for all time by picking the time interval over which the expansion is found to be the period of , that is. Remember that is always periodic with period where is the fundamental angular frequency (rad/sec) in the argument of the sin and cos functions. Therefore, we choose

To calculate the coefficients, a specific time period must be chosen. We will chose the time interval to calculate the coefficients which has the correct time-span of. The coefficients are calculated using the equations

Evaluating these integrals gives

which is the average, or DC, value of ,

which results because is an even function but is purely odd, and lastly

so the Trigonometric Fourier Series is

x t ( ) x t ( ) x ˜^ ( ) t

x t ( ) T 0 = 1 ⁄ 2 x ˜^ ( ) t ( 2 π) ⁄ ω 0 ω 0

ω 0 = 4 π

[ 0 1, ⁄ 2 ] T 0 = 1 ⁄ 2

a 0

T

--- (^) x t ( ) dt 0

1 ⁄ 2

a (^) n

T

--- (^) x t ( ) cos( n ω 0 t ) dt 0

1 ⁄ 2

= ∫ n =1 2, ,…

b (^) n

T

--- (^) x t ( ) sin ( n ω 0 t ) dt 0

1 ⁄ 2

= ∫ n =1 2, ,…

a 0 = 0

x t ( )

a (^) n = 0

cos ( n ω 0 t ) x t ( )

b (^) n

(^4 ⁄π)^ (^1 ⁄ n )

n even n odd

x ˜^ ( ) t

π

--- (^) sin ( 4 π t )^1 3

--- (^) sin [ 3 4( π t )]^1 5

--- (^) sin [ 5 4( π t )]^1 7

--- (^) sin [ 7 4( π t )]^1 9 = + + + + ---^ sin [ 9 4( π t )]+…