Generation of Signals in Matlab.

advertisement
Generation of signals in Matlab
HTE - 27.10.2014
1. Basics
From notes entitled, “ECE 373_FT_Classification of signals_June 2013_HTE”, available on course
webpage, ece373.cankaya.edu.tr, we know that signals can be examined in several categories. Here
we initially concentrate on sinusoidal ones, these are
sc  t   As cos  2 f s t 

Cosine form
ss  t   As sin  2 f s t 

Sine form
se  t   As exp  j 2 f s t   As cos  2 f s t   j sin  2 f s t   
se  t   As exp  js  t   , se  t   As , s  t   2 f s t
Complex form
(1.1)
The plots of the sinusoidal functions of (1.1) are displayed in Figs. 1.1 and 1.2.
sc ( t ) = Ascos(2 fst)
s ( t ) = A sin(2 f t)
s
s
s
A
s
t = 1 / 2fs
2 f t = 
s
t
0
t = 1 / 4f
s
2 f t =  / 2
s
t = 2 / fs
2 f t = 4
-As
s
Fig. 1.1 Plots of sc  t   As cos  2 f st  and ss  t   As sin  2 f st  against time.
ECE 108 - HTE Ekim 2014 Sayfa 1
(t)
)
| se ( t ) | = | Ac |
t
0
t = 1 / fs
2 f t = 2
s
Fig. 1.2 Plot of se  t   As exp  j 2 f st  against time.
As seen from Fig. 1.1, sine and cosine functions follow each other, with an angular difference of
 / 2 . This is commonly expressed as, “the sine and cosine functions have a phase difference of
 / 2 ”. On the other hand, the exponential form, i.e., se  t   As exp  j 2 f st  can only be plotted in
a three dimensional picture as shown in Fig. 2.1, where
se  t  is split into magnitude, i.e.,
se  t   As and phase s  t   2 f st parts.
From (1.1), taking the cosine form, we identify the indivisual terms as
sc  t   As cos  2 f s t 
As : Amplitude coefficient
,
f s : Frequency ,
t : Time variable
(1.2)
From (1.2), we can say that the function, sc  t  , as indicated in its argument in the round brackets is
dependent on time, hence, to plot sc  t  , we fix As and f s and vary time to obtain graphical outputs
displayed in Fig. 1.1. Note that there may be cases where As and f s will vary with time as well. In
such cases, it is possible to arrive at probability density function (pdf) and Fourier transform of sc  t  .
Presently we concentrate on the case where the variation of sc  t  is with time. Thus to plot the
curve of sc  t  , we can write the following Matlab m code
t = 0:1e-3:1;
%%%% defining the time range
fs = 2;
%%%% defining frequency
Ac = 1;
%%%% defining amplitude coefficient
sct = Ac*cos(2*pi*fs*t); %%%% defining s_c(t)
figure(1) %%%%% plotting
plot(t,sct,sitil(1,:),'LineWidth',2.5);
axis ([0 max(t) min(sct)*1.1 max(sct)*1.1]);
set(gcf,'Color','White');set(gca,'FontSize',18);
ECE 108 - HTE Ekim 2014 Sayfa 2
As seen from the above piece of code, we initially define a time range, in the above case, this is from
0 to 1 (seconds). Then allow sc  t  to complete two cycles, by choosing f s  2 (Hz). For our
illustrations, As is unimportant, so we set it to unity. A more detailed rundown is offered below
starting time
increment
end time
t  0 : 103 : 1
, Number of samples : 1/10 3  1  1001
f s  2 Hz
, Number of samples per cycle
500
(1.3)
So we may test what would happen, if we were to choose different increment or frequency. Firstly by
taking t  0 :102 :1 , we plot sc  t   As cos  2 f st  against time, getting the graph given in Fig. 1.3.
s ( t ) = A cos(2 f t)
c
s
s
A
s
t
0
t=1/f
s
2 f t = 2
s
-A
s
Fig. 1.3 Plot of sc  t   As cos  2 f st  with time increment of 102 .
Using (1.3), we can calculate that the number of samples in Fig. 1.3 is 101 or the number of samples
per cycle is approximately 50, thus the curve in Fig. 1.3 still looks line a sinusoidal waveform. In Fig.
1.4, we have increased the increment to 101 . In Fig. 1.4, it is easy to say that the increment of 101
or approximately 5 samples per unit cycle of cosine waveform does no lead to a proper
representation of the signal. As we shall see in “ECE 373_FT_Classification of signals_June
2013_HTE”, the minimum number of samples that allows the reconstruction of a time continuous
(analogue) signal is given by the Nyquist rate which corresponds to twice its highest frequency in that
signal. In the above case, that is 2f s  4 samples . In practice, we choose slightly higher sampling
rates.
ECE 108 - HTE Ekim 2014 Sayfa 3
s ( t ) = A cos(2 f t)
c
s
s
t
0
Fig. 1.4 Plot of sc  t   As cos  2 f st  with time increment of 101 .
Now we examine the variation against, frequency , f s , when the other settings are kept the same.
For this, we increase the frequency to f s  4 Hz , where the related plot is given in Fig. 1.5.
s ( t ) = A cos(2 f t)
c
s
s
Third
cycle
Second
cycle
First
cycle
First
cycle
t
0
t=1/f
s
2 f t = 2
s
t = 2 / fs
2 fst = 4
t = 3 / fs
2 fst = 6
t=4/f
s
2 f t = 8
s
Fig. 1.5 Plot of sc  t   As cos  2 f st  with f s  4 Hz .
2. Generating sine wave signals with the help of Simulink
In Matlab, the above described procedure of generating sinusoidal signals with m file commands is
not unique. Another way is using Simulink blocks of Matlab. In this method, we open a “model” file
from File → New menu of Matlab command window. Then from Start → Toolboxes → DSP System →
Block Library → Sources, we select, “Sine Wave” block and paste in the model file opened, similarly
from Start → Simulink → Commonly Used Blocks, we select, “Scope” block and paste in the model
ECE 108 - HTE Ekim 2014 Sayfa 4
file opened. By making a link from the output of “Sine Wave” block to the input of “Scope” block we
arrive at the construction illustrated in Fig. 2.1.
Fig. 2.1 Model file containing Sine Wave and Scope blocks.
After preparing the model file shown in Fig. 2.1, we must do the parameter settings. Initially we
double click on Sine Wave block, then the menu shown in Fig. 2.2 props up.
ECE 108 - HTE Ekim 2014 Sayfa 5
Fig. 2.2 Popup screen for adjusting the parameters of Sine Wave block.
As seen from Fig. 2.2, we have adjusted the parameters of the sine wave in the model file to be the
same as those in the Matlab m code of section 1.
Next we manage the settings of the Scope in the model file of Fig. 2.1. Again double clicking on the
Scope, setting from the Parameter (second button from the top left) panel, Number of axis to 1, Time
Range to 1, Sample time to 1/1000, then setting Simulation stop time to 1 from the top menu of the
model file, we can run start the run by pressing start simulation button ( ). When the run is
terminated, double clicking on the scope, we get the sine waveform shown in Fig. 2.3.
Fig. 2.3 Sine waveform obtained from model file of Fig. 2.1.
We see that the sine wave of Fig. 2.3 is the same as the one exhibited in Fig. 1.1.
Exercise 2.1 : Explain how to transform the model file of Fig. 2.1 so that it generates cosine waveform
instead of sine by referring to the menu in Fig. 2.2.
There is another sine wave generator placed in Start → Simulink → Sources. After inserting this
generator into the model file of Fig. 2.1, we have the following
ECE 108 - HTE Ekim 2014 Sayfa 6
Fig. 2.4 Two sine wave generators in the same model file.
To add another input channel to the scope in Fig. 2.4, we have used the Parameter button of the
Scope. The setting panel of the sine wave generator at the bottom of Fig. 2.4 is slightly different from
the top one, as shown in Fig. 2.5.
By applying the appropriate setting to the lower sine wave generator in Fig. 2.4 and leaving the upper
one as written in Fig. 2.2, then running the model file, we get the scope output given in Fig. 2.6.
Exercise 2.1 : Explain what settings are applied in the panel of Fig. 2.5 to obtain the lower scope
output shown in Fig. 2.6.
ECE 108 - HTE Ekim 2014 Sayfa 7
Fig. 2.5 The setting panel of the sine wave generator at the bottom of Fig. 2.4.
ECE 108 - HTE Ekim 2014 Sayfa 8
Fig. 2.6 Combined scope output from the model file of Fig. 2.4.
3. Generating other signals with the help of Simulink
We can use Simulink blocks to generate other signal types as well. One such example is displayed in
Fig. 3.1. Signal Generator is taken from Start → Simulink → Sources.
Fig. 3.1 An example for generation of other signal types.
ECE 108 - HTE Ekim 2014 Sayfa 9
The menu panel appearing after the double clicking on the Signal Generator is displayed in Fig. 3.2.
Fig. 3.2 Menu panel for Signal Generator of Fig. 3.1.
The waveform bar in the menu panel of Fig. 3.2 offers the options of sine, square, sawtooth and
random. By selecting square and sawtooth for the upper and lower signal generators and running the
model file, we get the following the scope view shown in Fig. 3.3.
ECE 108 - HTE Ekim 2014 Sayfa 10
Fig. 3.3 Scope view from the model file of Fig. 3.1.
Exercise 3.1 : Write the mathematical expressions for the waveforms of Fig. 3.3.
Solution : By calling the square waveform sq (t ) and the sawtooth waveform sh (t )and observing the
the time range of the signals in Fig. 3.2, we can write for individual time intervals
sq (t ) = - 1 for 0 £ t < 0.25 , sq (t ) = 1 for 0.25 £ t < 0.5
sq (t ) = - 1 for 0.5 £ t < 0.75 , sq (t ) = 1 for 0.75 £ t < 1
sh (t ) = - 4t + 1 for 0 £ t < 0.5 , sh (t ) = - 4 (t - 0.5)+ 1 for 0.5 £ t < 1
(3.1)
It is quite easy to conclude from Fig. 3.3 and (3.1) that sq (t ) and sh (t )have a (common) period of
Ts = 0.5 . Assuming that sq (t ) and sh (t )extend along time axis from - ¥ to + ¥ with a period of
Ts = 0.5 , then (3.1) can be generalized as
ECE 108 - HTE Ekim 2014 Sayfa 11
¥
sq (t ) =
å a U éët - 0.5(n - 1)T ùû
n= - ¥
n
q
s
ïì - 1 n odd
an = {- 1, 1}= ïí
, U q (t ) =
ïïî + 1 n even
ïíïì 1 for 0 £ t £ 0.25Ts
ïïî 0 elsewhere
¥
sh (t ) =
å U éët - (n - 1)T ùû
n= - ¥
h
s
ìï 2t
ïï + 1 for 0 £ t £ Ts
U h (t ) = í Ts
ïï
elsewhere
ïïî 0
(3.2)
For clarification, graphical representations of the expressions in (3.2) are replotted in Fig. 3.4.
sq ( t )

1
n=1
n=0

n=2
t
0
n = -1
n=3
n=4
-1

sh ( t )

1
n=1
n = -1
n=2
t
0
-1
0.5T
s
Ts
2T
s
Fig. 3.4 Illustration of graphs displayed in Fig. 3.3 and (3.2).
Signal generator given in Fig. 3.1 is also able to generator random signals (with uniform amplitude
distribution). When “Waveform” option in the menu panel of Fig. 3.2 is set to “random” (both for
Signal Generator and Signal Generator1), we get the graphical outputs displayed in Fig. 3.5.
It is not so reasonable to write mathematical expressions for the waveforms of Fig. 3.5, indicating
explicit amplitude variations against time. Due to their statistical nature however, it is possible to
(partially) determine the probability density functions (without normalization) in the form of a
histogram. For this purpose, we connect what is called “simout” blocks as shown in Fig. 3.6. This
way, we are able to extract the sample values of the waveforms of Fig. 3.5 to Matlab workspace as
arrays. From there by applying the following Matlab command
hist(simout,30) %%% Matlab command to plot the probability density function in the form of
histogram
We get the histogram given in Fig. 3.7. As noted from the horizontal and vertical axes of Fig. 7 the
sample values are distributed between -1 and +1 almost uniformly. Fig. 3.7 is plotted by using the
sample value of 1/10000. If this is further lowered we will acquire a smoother uniform distribution
than the one illustrated in Fig. 3.7.
ECE 108 - HTE Ekim 2014 Sayfa 12
Fig. 3.5 Graphical outputs when waveform is set to random in the menu panel of Fig. 3.2.
Fig. 3.6 Model file with outputs of signal generators channelled to Matlab workspace via “simout”
and “simout1” blocks.
ECE 108 - HTE Ekim 2014 Sayfa 13
400
350
300
250
200
150
100
50
0
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Fig. 3.7 Histogram (probability density function) of simout from Fig. 3.6.
It is possible to drive the model files (of Matlab) and their parameter settings from an m file (of
Matlab). Such a listing is given below. Note that parameter name are retrieved by right clicking on
the block and then selecting Properties → Block Annotations and scrolling down on Block property
tokens and finding the appropriate label for the sought parameter, for instance “Waveform”.
echo off;clear;clc;close all;
%%%%% Setting the parameters as strings
WF = 'square';As = '1';Fs = '2';WF1 = 'sawtooth';ST = '1/10000';
%%%% Setting the parameters in the blokcs of Signal Genberator and Signal
%%%% Generator1
set_param('WaveformGen/Signal
Generator','Waveform',WF,'Amplitude',As','Frequency',Fs);
set_param('WaveformGen/Signal
Generator1','Waveform',WF1,'Amplitude',As','Frequency',Fs);
%%%% Setting the sample time in Scope1
set_param('WaveformGen/Scope1','SampleTime',ST);
sim('WaveformGen');%%% Runing the model file
hist(simout,30)%%%% Ploting the histogram of amplitude distribution (pdf)
set(gcf,'Color','w');set(gca,'FontSize',16)
These notes are based on
1) MATLAB m files.
2) My own Lecture Notes.
ECE 108 - HTE Ekim 2014 Sayfa 14
Download