$29
Objective and Background
This assignment has four basic objectives:
To continue to remind you that you know how to program in C or C++.
To give you some practice in converting a mathematical description of an idea into a computer imple-mentation of the idea.
To provide practice and experience in convolution.
To provide a little more practice in nding the complete solution of di erential equations.
In this assignment you will write a program in C or C++ to perform numerical convolution of causal signals through causal systems.
As you recall, the formula for continuous-time convolution is given by
Z t
y(t) =
f( )h(t )d
0
This was arrived at by taking the following limit (see p.120 in the text):
1
X
y(t) = lim
f(m )h(t m )
(1)
!0 m=0
If we de ne as the sampling interval T , (1) can be written as
n
X
y(nT ) = T f(mT )h(nT mT )
m=0
We can change notation to indicate sampled signals by realizing that y(nT ) can be written y[n]. This results in the sum
n
X
y[n] = T f[m]h[n m]
m=0
The initial part of this programming assignment is to write a function which will compute the convolu-tion sum of two sequences f[n] and h[n]. The convolution sum is de ned as
n
X
y[n] =f[m]h[n m] f[n] h[n]
(2)
m=0
Note that the sum is not scaled by T .
Assignment
For the following exercises, turn in your programs, your analytical computations, your plots, and your comments and observations, as appropriate. The plots should be clearly labeled.
1
Write a subroutine (function) that will convolve two sequences using (2). You might want to use a function description like
leny = conv(double *f1, int len1, double *f2, int len2, double *y)
which would convolve the sequence in the array f1 having len1 points with the sequence in the array f2 having len2 points. The result of the convolution is returned in the array y, and the number of points in the convolution is returned in leny.
Test your program by convolving the following functions:
f1 f1
f1 f2
f1 f3
f2 f3
f1 f4
where the sequences are described by the C/C++ arrays
f1[] = {0,1,2,3,2,1};
len1 = 6;
f2[] = {-2,-2,-2,-2,-2,-2,-2};
len2 = 7;
f3[] = {1,-1,1,-1};
len3 = 4;
f4[] = {0,0,0,-3,-3};
len4 = 5;
Remember that C/C++ arrays start with an index of 0.
Plot these functions. Verify that the convolution is working as it should.
Do the convolutions by hand. Also, compute the same convolutions using the Matlab function conv.
Compare the results from the three methods (they better all be the same!).
Using the results from Program #1 (which computed the zero-input response), nd the total solution to the di erential equation
(D3 + 5D2 + 12D + 15)y(t) = (D + 0:5)f(t)
with initial conditions y(0) = 3, y(0) = 2, y(0) = 1 and f(t) = sin(3 t)u(t). Use T = 0:001, and let 0 t 10. You will need to incorporate part of Program #1 into your new program to complete this part.
Using paper-and-pencil analysis, nd the impulse response of the system h(t). Then compute and plot its sampled values h[k] = h(kT ). You may use Laplace transform methods if you want.
Find the sampled values of the input function f[k] = f(kT ). Plot these values.
The zero-state solution is the scaled convolution T (f[k] h[k]).
The zero-input solution is found using Program #1.
The total solution is the sum of the zero-state solution and the zero-input solution. (You will somehow need to incorporate the data from Program #1 in with the data from this program to get the total solution.)
Compute numerically and plot the total solution.
Find an analytical solution to the DE and plot it.
Compare the analytical and the numerical solution. (Comment)
Note that the solution to the system output in 3 above settles to a \steady-state" solution after a few seconds, where the signal form continues unchanged. Your plots should indicate this. What is the steady-state output amplitude? Why does this happen?
2
Hints and helps
Limits As for many convolution problems, the hardest part of the convolution is getting the limits of summation correct. Pay very close attention to the upper and lower limits of summation.
Number of points Also pay close attention to the number of points which appear in the convolved output.
Two nested for loops The basic structure for convolution is simple:
for(k = 0; k <= upper limit on k; k++) {
sum = 0;
for(m = lower limit on m; m <= upper limit on m; m++) { sum += f1[m]*f2[k-m];
}
y[k] = sum;
}
All you really need to do (which will require some care and understanding; you are are on your own for this!) is to nd the lower and upper limits of the two for loops.