Lab 1RT
Lab 1RT
Lab 1RT
1
1. Basic MATLAB functions and plots
A basic cosine wave takes the form
x[n] = A cos(ω0 n + φ),
and is described completely by the parameters A (the amplitude),
ω0 (the frequency), and φ (the phase).
(a) Generate and plot each of the following sequences:
π
x1 [n] = sin n , 0 ≤ n ≤ 25
17
π
x2 [n] = sin n , −15 ≤ n ≤ 25
17
π
x3 [n] = sin 3πn + , −10 ≤ n ≤ 10
2
π
x4 [n] = cos √ n , 0 ≤ n ≤ 50.
23
In each case the horizontal axis should extend only over the
range indicated (see the axes command), and the axes should be
labelled accordingly (see xlabel and ylabel). Each sequence
should be displayed using stem.
It is important to be able to generate plots in MATLAB — they
are often required in reports, research papers, and theses. Other
MATLAB constructs that are useful are:
i. Variants of the plotting commands that allow multiple plots
on the same axes. For example, plot(n,s1,’k-’,n,s2,’g:’)
will plot the signal s1 as a solid black line, and s2 as a dotted
green line. Most other plot commands accept similar
arguments. The legend command is useful for labelling these
plots.
ii. The subplot command for placing multiple plot axes on the
same figure.
iii. The print command can be used for saving figures to file, for
later inclusion into reports. For example, the command
2
print(gcf, ’-depsc2’, ’outfile.eps’) will save an
Encapsulated PostScript representation of the current figure,
and print(gcf, ’-dpng’, ’outfile.png’) will save the
current figure in PNG format.
(b) Write a general MATLAB function that will generate a
finite-length sinusoid. The function will need five input
arguments: three for the parameters, and two more to indicate
the first and last n index of the signal. The function should
return a column vector that contains the values of the sinusoid.
Test the function by plotting the results for various choices of
the input parameters.
(c) Modify the function in part (b) to return two results: a vector of
indices over the range n, and the values of the signal.
(d) Hand in: Two plots, on the same figure (using subplot), of the
functions
2π
x5 [n] = 3 cos n
10
2π π
x6 [n] = 2 cos n+
12 4
over the range −10 to 15.
where h[n] is the filter impulse response. A very simple (and bad, as
3
you will discover later) lowpass filter has the impulse response
1 0≤n≤6
h[n] = 7
0 otherwise.
Because h[n] is non-zero only over a limited range, the limits of the
convolution sum can be narrowed in this case:
6
X
y[n] = h[k]x[n − k].
k=0
4
To characterise linear time-invariant systems, we are interested in
their response to the complex exponential sequences ejωn for
different values of ω. Since Matlab deals with complex numbers as a
matter of course, we can generate portions of such sequences easily
using the exp command and a given value of ω.
A problem that arises is that such sequences take on non-zero
values for all n, both positive and negative. For computational
purposes some truncation is necessary, so instead we consider input
sequences of the form
x[n] = u[n]ejωn .
5
is to consider a value of n large enough so that the transients
have settled — now the ratio y[n]/x[n] should give you the
required complex value.)
(c) Repeat for ω = 2π/5 and ω = 4π/5. In total you should now
have three values that describe the frequency response of the
filter to three different frequencies. Convince yourself again that
the filter exhibits lowpass behaviour.
4. More frequency response
Formally, the response of the system to the input x[n] = ejωn is
given by
∞
X ∞
X
y[n] = h[k]x[n − k] = h[k]ejω(n−k)
k=−∞ k=−∞
∞ ∞
!
X X
= h[k]ejωn e−jωk = h[k]e−jωk ejωn
k=−∞ k=−∞
= H(ejω )ejωn .
6
limited range, so the limits of the sum in this expression become
finite. Compare the results to the values obtained for A and φ
for all cases in the previous question.
(b) Hand in: If you didn’t already do so, write a Matlab function
that takes in a filter impulse response h[n] and a frequency ω,
and returns the value of H(ejω ) under the assumptions
described above. The function declaration may look as follows:
function H = freqresp(h, omega)
where h is a vector of values h = (h[1], h[2], . . . , h[p])T , ω is a
scalar frequency, and H is a scalar return value. Use this
function to find the frequency response H for a range of
frequencies ω = 0, 0.01, . . . , 4π. Plot the magnitude abs(H) as a
function of ω. On a separate set of axes, plot the phase
angle(H) as a function of ω. Interpret the results you obtain,
and note the periodicity of the frequency response. What effect
will the system have on an input complex exponential with
frequency ω = π/2?
(c) Do you see from the previous plots why the system is a bad
lowpass filter? Note the large sidelobes. Now plot the magnitude
and phase response (also over the range 0 to 4π) of the filter
with modified impulse response
1
(n + 1) 0≤n≤3
16
1
ht [n] = 16 (7 − n) 4≤n≤6
0 otherwise.
7
with 100 elements, using the command
n = randn(100,1);
Plot the signal on a set of axes.
(b) Now filter the signal with one of the lowpass filters used before.
Plot the result on another set of axes, and compare your two
plots. The input noise signal has equal power at all frequencies,
while the output signal has a spectrum determined by the filter
frequency response.
(c) Clearly, if we have a low-frequency signal corrupted by noise, a
lowpass filter can increase our signal-to-noise ratio. Generate a
noisy signal
sn = sin((1:100)’/10) + randn(100,1);
and plot it both before and after filtering. Notice the reduction
in noise?