MATLab Tutorial #5 PDF
MATLab Tutorial #5 PDF
MATLab Tutorial #5 PDF
MATLAB Tutorial #5
x(t)
h(t)
y(t)
Using the convolution method, we can find the output to a corresponding input by evaluating the
convolution integral:
y(t) =
h( )x(t )d =
x( )h(t )d
This project describes the various methods for evaluating the convolution integral and finding
the impulse response using MATLAB.
Convolving Two Functions
The conv function in MATLAB performs the convolution of two discrete time (sampled)
functions. The results of this discrete time convolution can be used to approximate the
continuous time convolution integral above. The discrete time convolution of two sequences,
h(n) and x(n) is given by:
y(n) = h( j)x(n j)
j
If we multiply this sum by the time interval, T, between points in the sequence it will
approximate the value of the integral above. In other words:
y(t) =
h( )x(t )d T h( j)x(n j)
j
For example, in order to convolve the two pulses shown below, we begin by representing the
pulses as vectors x and h.
h(t)
x(t)
1
2
T=0.1;
t=0:T:2;
h=(t>0) - (t>2);
x=(t>0) - (t>1);
Note that the result is very close to what we expect for the convolution of these two pulses
however the figure is slightly off. We would expect the trapezoid to begin exactly at t = 0 and
end exactly at t = 3. Repeat the commands above, but reduce T to a smaller interval, 0.01 for
example. You should now observe that the results are more accurate. Remember that we are
using a discrete time sequence to approximate the continuous time functions. Thus, the closer
together the values, the better we expect the approximation to be.
Now we will repeat the example above, but change the impulse response to:
2
h(t) = et /2 u(t)
This is done with the following MATLAB commands:
>> h=exp(-t/2);
>> y=T*conv(h,x);
>> plot(0:T:4, y)
and results in the following output:
Note that the result is only correct in the interval 0 t 2, or for the first 201 points. This is due
to the fact that the exponential function, which extends to infinity is approximated by a finite
duration exponential from 0 t 2. In general, if the two functions being convolved are
represented by N points each, only the first N points of the result are accurate, if either of the
functions is non-zero beyond the sampling region. In the first example, both pulses drop to zero
outside of the sampling region so the result is accurate for all t. If we want accurate results over
a wider range in the second example we just need to extend the range over which both functions
are sampled.
Determining the Impulse Response for a Linear System
In class we found the impulse response for a system described by the linear differential equation:
ak
k
dk y
djx
=
b
j dt j
dt k
j
t=0:.01:4;
b= [1];
a = [1 5 6];
h=impulse(b,a,t);
plot(t,h)
Note that the result of the convolution is only accurate for 0 t 4, since this is the time
interval for which both the impulse response and input are specified. We can plot that portion
only using:
>> plot(0:.01:4, y(1:401))
Set the simout block to output its data to an array. Double click on the pulse generator and set
the parameters to those shown in the figure below.
Note that the pulse width is 10% of the period or 0.5 seconds. The amplitude is set to be the
reciprocal of this pulse width so that the pulse has an area of one. Set the simulation time to 5
seconds so that the system only receives a single pulse. Run the simulation and return to the
command line window. Use the following MATLAB commands to plot the impulse response
given above (blue) and the output of the simulation (red).
>>
>>
>>
>>
h=exp(-2*tout)-exp(-3*tout);
plot(tout, h)
hold on
plot(tout,simout, 'r')
Note that they are close but not the same. Repeat the simulation, but this time change the pulse
width to be 5% of the period and increase the pulse amplitude to be 1/(.05*5) to maintain a pulse
area of one. The output is shown by the green curve.
Finally reduce the pulse width to 1% of the period and increase the pulse amplitude to 1/(.01*5).
The cyan curve shown below is now output.
Note that as the pulse width decreases, approaching an impulse function, the output of the
simulated system approaches the known impulse response of the system.