REVIEW ON MATLAB
EXPERIMENT 1
INTRODUCTION ON SIGNALS AND SYSTEMS
1.0 OBJECTIVES:
After completing this section you will be able to :
1.1 Generate basic signals.
1.2 Differentiate type of signaling generated.
2.0 TOOLS
The MATLAB Signal Processing Toolbox.
3.0 M-FILES
It is useful for a large number of commands. We can write and save our commands in script files called M-Files. When an M-File is run, MATLAB sequentially executes the commands found in the file. The advantage of having M-File is that commands are saved and can be easily modified without retyping the entire list of commands.
Example 3.0.1
Start by creating an M-File file:
Type this text in new M-File
t = linspace(1,3,200) %---- to create 200 points between 1 to 3
x1 = t. ^ 2; %---- signal x1,
x2 = sin ( 10.*t); %---- signal x2,
x = x1.*x2; %---- Product of signals.
[pic]
Then, Save As ( to (C:) Directory ) as Revp1.m (as File name)
Example 3.0.2
Consider the signal,
x(t) = t2 sin (10t), 0 ≤ t ≤ 3 ___________ (i)
We would like to plot this signal. To generate the signal in (i), two intermediate signals are defined.
x1(t) = t2 , and x2(t) = sin (10t) ___________ (ii)
Then the signal in (i) is the product of the signals in (ii).
Solution to the above example;
clear
% generate signal;
type Revp1 .m % recall from m.files
plot (t,x) % Plot Signal
xlabel (‘time’) % Label x-axis
ylabel (‘x’) % Label y-axis
title (‘Product of Function’) % Puts a title on the plot
[pic]
[pic]
3.1 OVERLAY PLOTS
There are three different ways of generating overlay plots in MATLAB:
the plot, hold, and line commands
Plot command
For example, If we have three sets of data, [pic] and [pic],
the command plot [pic];
Plots [pic], with a solid line, [pic], with a dotted line, and [pic] as uncorrected points marked by small circles ‘o’, all on the same graph.
Consider the three signals below and using plot command to generate overlay plots.
[pic] , [pic] and [pic]
Plot 100 points vector, sets the x-axis from 0 to 5 and y-axis from -1 to 5.
Example 3.1.1
>> clear
>> t = linspace (0, 2.*pi, 100); % generate vector t
>> y1 = sin (t); y2 = t;
>> y3 = t-(t.^3)./6 + (t.^5)/120;
>> plot (t, y1, t, y2, ‘--‘, t, y3, ‘o’)
>> axis ( [0 5 -1 5] ) % Zoom in new axis limits
>> xlabel (‘t’)
>> ylabel (‘Approximations of sin (t)’)
>> title (‘Fun with sin (t)’)
>> text (3.5,0, ‘sin (t)’)
>> gtext (‘Linear approximation’)
>> gtext (‘First 3 terms’)
>> gtext (‘in Taylor Series’)
“gtext” writes the specified string at a location clicked with the mouse in the graphics window. So, after hitting return at the end of gtext command, go to the graphic window and click a location.
Example 3.1.2
[pic] , 0 ≤ x ≤ 20 , using plot command
x = 0:0.1:20; % create vector x
y = exp(0.1.*x).*sin(x); % calculate y
plot (x,y) % plot x vs. y
xlabel (‘Time (t) in seconds’) % label x-axis
ylabel (‘The Response Amplitude in mm’) % label y-axis
title (‘A Simple 2-D Plot’) % put a title
Example 3.1.3
[pic] , 0 ≤ x ≤ 20 , using fplot
fplot (‘exp(0.1.*x).*sin(x)’,[0,20])
xlabel (‘x’),ylabel(‘f(x)=e^{x/10}sin(x)’)
title (‘A function plotted with fplot’)
Example 3.1.4
In this example, the magnitude of “F(Z)” is manipulated by MATLAB program.
F (Z) = [pic]
along the path Z = x + j2 ; -6 ≤ x ≤ 4. Create 100 points between -6 to 4.
Solution Example 3.1.4
>> clear
>> x = linspace (-6 , 4,100) ; % Create a 100 point independent variable
>> Z = x + 2i ; % now the range of Z has been defined
>> F = 2*(Z+4)./(Z-1); % Calculate the function for each value of Z
>> plot (x, 180*angle (F)/pi) % plot phase in degrees
>> grid
>> xlabel (‘x’)
>> ylabel (‘phase in degrees’)
3.2 INTRODUCTION ON SIGNALS
❖ A signal is formally defined as a function of one or more variables that conveys information on the nature of a physical phenomenon.
❖ The function which is chosen to describe a signal is called the representation of the signal.
❖ A signal that depends on the discrete variable n and that model a physical variable that evolves in discrete time is called a discrete-time signal.
❖ A signal that depends on the continuous variable t and that model a physical variable that evolves in continuous time is called a continuous-time signal.
❖ A system is formally defined as an entity that manipulates one or more signals to accomplish a function, thereby yielding new signals.
❖ The interaction between a system and its associated signal is illustrated schematically in figure below;
Input signal Output Signal
❖ If the input signal and output signals of a system are continuous time signal, then the system is called a continuous-time system.
❖ If the input signal and output signals of a system are discrete-time signals, then the system is called a discrete-time system.
3.2.1 EVEN AND ODD SIGNALS
Even and odd signals bear some important symmetry properties. Under
reversal of independent variable, these signals either remain the same (even
signal) or get reflected or flipped (odd signal) about the horizontal axis.
Equations or definitions (3.1) and (3.2) mathematically express these properties for both continuous and discrete time cases.
Even Signals: x(t) = x(-t), x[n] = x[-n] (3.1)
Odd Signals: x(t) = -x(-t), x[n] = -x[-n] (3.2)
To explore even and odd signals in continuous time, let us create one
even and one odd signal. The even one will be a cosine and the odd will be a
sine, both will have frequency of 1 Hz, defined over a time axis extending from
-1 to 1 with a sampling interval of 0.01s, each totaling 201 points or samples.
» f=1;
» t=-1:0.01:1;
» xe=cos(2*pi*f*t);
» xo=sin(2*pi*f*t);
We can now see what happens to our functions under time reversal. We
first examine the even one, and then the odd one.
» subplot(2,2,1);plot(t,xe);
» subplot(2,2,2);plot(-t,xe);
» subplot(2,2,3);plot(t,xo);
» subplot(2,2,4);plot(-t,xo);
What do you observe? How do you interpret the pictures you got?
3.2.2 PERIODIC SIGNALS
Periodic signals can be generated by square waves and triangular waves. To generate a square waves signals, use the basic command:
A*square(w0*t + rho);
A is an amplitude, w0 is a fundamental frequency measured rad/sec and rho is a duty
cycle for which the signal is positive.
Example
Use the following set of commands for generating:
(i) Square waves
A = 1 and Period, T = 0.2s * f = 1/T ; ( = 2πf = 2(/T
>> A = 1; % Amplitude
>> w0 = 10*pi; % Fundamental frequency
>> rho = 0.5; % Duty cycle
>> t = 0: .001 : 1; % To create 1000 time vector points
>> sq = A*square(w0*t + rho);
>> plot (t,sq)
>> axis ( [ 0 1 -1.1 1.1 ] ) % Sets graph axes: axis([xstart xend ystart yend])
Explanation: The plot command is used to view the square waves : the command plot draws lines connecting the successive values of the signal and thus gives the appearance of a continuous-time signal.
(ii) Triangular waves
A*sawtooth (w0*t,w);
>> A = 1;
>> w0 = 10*pi;
>> w = 0.5;
>> t = 0: .001 : 1;
>> tri = A*sawtooth(w0*t,w);
>> plot (t,tri)
Explanation: The plot command is used to view the triangular waves : the command plot draws lines connecting the successive values of the signal and thus gives the appearance of a continuous-time signal.
To visualize a discrete-time signal, used stem command .Specifically, stem (n,x) depicts the data contained in vector x as a discrete-time signal at the time values defined by n. The vector n and x must have compatible dimensions.
Example
The discrete-time square wave is generated by using the following commands;
>> A = 1;
>> omega = pi/4;
>> n = -10: 10; % Starting point from -10 to 10
>> x = A*square(omega*n);
>> stem (n,x)
3.2.3 EXPONENTIAL SIGNALS
Exponential signal is written as x(t) = Beat , where both B and a are parameters. The parameter B is the amplitude of the exponential signal measured at time t = 0. Depending on whether the other parameter a is positive or negative, its identify two special cases:
• Decaying exponential, for it which a < 0, B*exp (-a*t);
• Growing exponential, for it which a > 0, B*exp (a*t);
Example
>> B = 5;
>> a = 6;
>> t = 0: .001: 1;
>> x = B*exp (-a*t); % Decaying exponential
>> plot (t,x)
Explanation: The plot command is used to view the decaying exponential signal.
3.2.4 SINUSOIDAL SIGNALS
A*cos (w0*t + phi) ; for continuous
A*sin (w0*t + phi); (replace ‘t’ with ‘n’ for discrete)
These two commands are basically to generate the sinusoidal signals. A cosine signal of amplitude A, frequency w0 (measured in radians per second), and phase angle phi (in radians) is obtained by using the command.
Example
% generate of a sinusoidal sequence for discrete signal
>> n = 0:40;
>> f = 0.1;
>> phase = 0;
>> A=1.5;
>> arg = (2*pi*f*n) – phase;
>> x = A*cos(arg);
>> clf; % clear old graph
>> stem (n,x); % plot the generated sequence
>> axis ([0 40 -2 2]);
>> grid;
>> title (‘Sinusoidal Sequence’);
>> xlabel (‘Time Index n ‘);
>> ylabel (‘Amplitude’);
>> axis;
3.2.5 STEP, IMPULSE, AND RAMP FUNCTIONS
Step Function
A unit-amplitude step function is generated by unity
u = [zeros (1, 50), ones (1, 50)];
Discrete-time Impulse Function
Delta = [Zeros (1, 49), 1, zeros (1, 49)];
Ramp Function
To generate a ramp sequence, we simply write
ramp = 0: .1: 10
Example
Try these commands and observe the output
x(t) = A , 0≤ ׀t׀ ≤ 0.5
0 ׀t׀ > 0.5
>> t = -1: 1/500 : 1 % time running from 1 second to 1 second
>> u1 = [ zeros (1,250) , ones ( 1, 751)]; % beginning at t = -0.5 second
>> u2 = [ zeros (1,751) , ones ( 1, 250)]; % begin at t = 0.5
>> u = u1 – u2; % produce a rectangular pulse of unit amplitude and unit
duration centered on the origin.
>> plot(t,u)
Example
Try this command and generate the signal as shown in Figure 1 below;
>> dt = 0.05; % time increment for plotting
>> t1 = [-1 : dt : -dt]; % first interval
>> x1 = ones (size (t1)); % signal on first interval
>> t2 = [0: dt : 1]; % second interval
>> x2 = 1-t2; % signal on 2nd interval
>> t3 = [1+dt : dt : 2]; % third signal
>> x3 = 2*ones(size(t3)); % signal on 3rd interval
>> t4 = [ 2+dt: dt: 4]; % fourth interval
>> x4 = (t4-3); % signal on 4th interval
>> t = [ t1, t2, t3, t4]; % total time interval
>> x = [ x1, x2, x3, x4]; % total signal
>> plot (t,x)
Figure 1
3.3 COMMANDS RELATED SIGNALS TO SYSTEMS
The following commands are organized by topics in signals and systems. Each of these commands has a number of options that extend its usefulness.
3.4 PLOTTING
Plotting is a basic skill in MATLAB that will be used frequently. Basically, a signal can be represented by a vector that represents values of the signal at specified point in time.
3.4.1 Simple plotting commands
The simple 2D plotting commands include
plot Plot in linear coordinates as a continuous function
stem Plot in linear coordinates as discrete samples
loglog Logarithmic x and y axes
semilogx Linear y and logarithmic x axes
semilogy Linear x and logarithmic y axes
bar Bar graph
errorbar Error bar graph
hist Histogram
3.4.2 Customization of plots
There are many commands used to customize plots by annotations, titles, axes labels, etc.
A few of the most frequently used commands are
xlabels Labels x-axis
ylabel Labels y-axis
title Puts a title on the plot
grid Adds a grid to the plot
gtext Allows positioning of text with the mouse
text Allows placing text at specified coordinates of the plot
axis Allows changing the x and y axes
figure Create a figure for plotting
figure (n) Make figure number n the current figure
hold on Allows multiple plots to be superimposed on the same axes
hold off Release hold on current plot
close (n) Close figure number n
subplot (a,b,c) Create an a x b matrix of plots with c the current figure
orient Specify orientation of a figure
String modifiers can be used to change color, data point, and line styles.
|Colors |Point style |Line style |
|b blue |. point |- solid |
|g green |o circle |: dotted |
|r red |x x-mark |-. dashdot |
|c cyan |+ plus |-- dashed |
|m magenta |* star |(none) no line |
|y yellow |s square | |
|k black |d diamond | |
| |v triangle (down) | |
| |^ triangle (up) | |
| |< triangle (left) | |
| |> triangle (right) | |
| |p pentagram | |
| |h hexagram | |
| |
| |
3.5 Polynomials
Polynomials arise frequently in systems theory. MATLAB represents polynomials as row vectors of polynomial coefficients. For example, the polynomial s2 + 4s – 5 is represented in MATLAB by the polynomial >> p = [1 4 -5]. The following is a list of the more important commands for manipulating polynomials.
roots(p) Express the roots of polynomial p as a column vector.
polyval(p,x) Evaluate the polynomial p at the values contained in the vector x.
conv(p1,p2) Compute the product of the polynomials p1 and p2
deconv(p1,p2) Compute the quotient of p1 divided by p2
poly2str(p,’s’) Display the polynomial as an equation in s
( asn+bsn-1+csn2+……+……)
poly(r) Compute the polynomial given a column vector of roots r
TASKS OF THE DAY
1. Plot the following signal on the same graph and determine whether it’s odd or even signal. -2 ≤ t ≤ 2
* use subplot command
(i) x(t)=4t
(ii) x(t)=5cos(3t)
(iii) x(t)=5sin(3t)
2. Plot the following signals on the same graph and identify whether it’s periodic or non periodic signal.
(i) x(t) = cos (10 πt + 8 sin (πt)), 0 ≤ t ≤ 2 for 200 points vectors
(ii) x(t) = cos (10 πt + 8 ( t/4) ) ,
3. Consider the signal
x(t) = A sin (t) + B sin (ωt + θ )
A = 0.9 ; B = 2.5 ; 0 ≤ t ≤ 1
frequency = 1 Hz .
Write a M-File to plot this signal for phase θ = 0, π/2, π, 3π/2, and 2π in one graph. Investigate the waveforms obtained.
Use string argument to differentiate the signal and “gtext” function
4. This exercise examines the properties of the exponential and sinusoidal signals in both continuous and discrete forms.
(a) The Continuous Signal, x(t) = 2eat
(i) For values of ‘a’ equal to 0.5,1.0 and 1.5, plot the corresponding signals all on the same graph using a timescale -3 to +3.
(ii) Repeat (i) for values of ‘a’ equal to -0.5,-1.0 and -1.5, again using the time scale -3 to +3.
Use ‘subplot’ to show both two plots on one screen
(b) The Discrete Signal, x(n) = 2eanT with T = 0.25
(i) For values of ‘a’ equal to 0.5, 1.0 and 1.5, plot the corresponding signals using the command ‘stem’ over a range of ‘n’ from n = -12 to n = +12. Plot the three signal in one graph.
5.
[pic]
Figure 2
Consider the signal in Figure 2:
(a) Find representation for this signal.
(b) Plot this signal using MATLAB.
-----------------------
fplot (‘function’,[Xmin Xmax])
H
y (t)
x (t)
[pic]
[pic]
[pic]
[pic]
LABORATORY MODULE
EXPERIMENT 1
INTRODUCTION ON SIGNALS & SYSTEMS
ENT 281
SIGNALS & SYSTEMS
Semester I (2011/2012)
LECTURER : DR ABU HASSAN ABDULLAH
PLV : MRS. NORLAILI SAAD, MRS NOR AZILA SHOIB
MR ERDY SULINO, MRS SHARIFAH NURUL HUSNA
TECHNICIAN: MR MOHD AL HAFFIZ
Student’s Particular:
|Name: |
|Matric. No: |
|Date: |
|Group: |
|Signature: |
School of Mechatronic Engineering
Kolej Universiti Kejuruteraan Utara Malaysia
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.