Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Chapter 2

Basic Signals and Systems

• A large part of this chapter is taken from: C.S. Burrus, J.H. McClel- (IIR) filter is investigated.
lan, A.V. Oppenheim, T.W. Parks, R.W. Schafer, and H. W. Schüssler:
Computer-based exercises for signal processing using Matlab. Prentice
Hall, 1994. 2.1 Signals

Overview The basic signals used often in digital signal processing are the
Overview MATLAB is an ideal software tool for studying digital sig-
unit impulse signal δ[n], exponentials of the form an · u[n], sine waves, and
nal processing (DSP). Its language has many functions that are commonly
their generalization to complex exponentials. The following projects are di-
needed to create and process signals. The plotting capability of MATLAB
rected at the generation and representation of these signals in MATLAB.
makes it possible to view the results of processing and gain understanding
Since the only numerical data type in MATLAB is the M × N matrix, sig-
into complicated operations. In this chapter we present some of the basics
nals must be represented as vectors: either M × 1 matrices if column vec-
of DSP in the context of MATLAB. At this point, some of the exercises
tors, or 1 × N matrices if row vectors. In MATLAB all signals must be finite
are extremely simple so that familiarity with the MATLAB environment can
in length. This contrasts sharply with analytical problem solving, where a
be acquired. Generating and plotting signals is treated first, followed by the
mathematical formula can be used to represent an infinite-length signal (e.g.,
operation of difference equations as the basic class of linear time-invariant
a decaying exponential, an · u[n]).
Systems. An important part of this chapter is understanding the role of the
numerical computation of the Fourier transform (DTFT). Since MATLAB is A second issue is the indexing domain associated with a signal vector. MAT-
a numerical environment, we must manipulate samples of the Fourier trans- LAB assumes by default that a vector is indexed from 1 to N, the vector
form rather than formulas. We also examine the signal property called group length. In contrast, a signal vector is often the result of sampling a signal
delay. The sampling process is studied to show the effects of aliasing and over some domain where the indexing runs from 0 to N − 1; or, perhaps, the
the implementation of various reconstruction schemes. Finally, a filtering sampling starts at some arbitrary index that is negative, e.g., at −N. The in-
method to produce zero-phase response with an infinite impulse response formation about the sampling domain cannot be attached to the signal vector

2-1
2.1. SIGNALS 2-2

containing the signal values. Instead, the user is forced to keep track of this 1

information separately. Usually, this is not a problem until it comes time to


0.8
plot the signal, in which case the horizontal axis must be labelled properly.
0.6
A final point is the use of MATLAB ’s vector notation to generate signals. A
significant power of the MATLAB environment is its high-level notation for 0.4
vector manipulation; for loops are almost always unnecessary. When creating
signals such as a sine wave, it is best to apply the sin() function to a vector 0.2

argument, consisting of all the time samples. In the following projects, we


0
treat the common signals encountered in digital signal processing:
−0.2
impulses, impulse trains, exponentials, and sinusoids.
−0.4

Background Reading Oppenheim and Schafer (1989), Chapter 2, Sec- −0.6


tions 2.0 and 2.1.
−0.8

−1
2.1.1 Project 1: Basic Signals 0 5 10 15 20 25 30

Figure 2.1: Plotting a discrete-time signal with stem()


This project concentrates on the issues involved with generation of some
basic discrete-time signals in MATLAB. Much of the work centers on using
internal MATLAB vector routines for signal generation. In addition, a sample
MATLAB function will be implemented. Notice that the n = 0 index must be referred to as nn(1), due to MATLAB’s
indexing scheme; likewise, sinus(1) is the first value in the sinusoid. When
plotting the sine wave we would use the stem() function, which produces
Hints Plotting discrete-time signals is done with the stem() function1 in the discrete-time signal plot commonly seen in DSP books (see Fig. 2.1):
MATLAB. The following MATLAB code will create 31 points of a discrete-
time sinusoid.
stem(nn, sinus);
nn = 0:30; % vector of time indices
sinus = sin(nn/2+1);
The first vector argument must be given in order to get the correct n-axis. For
1 In MATLAB versions previous to version 4, this function was denoted as comb(). comparison, try stem(sinus) to see the default labeling.

Digital Video & Audio Processing (R.Mester) DVAP Exercises Exc1,V2.0 23-November-2002
2.1. SIGNALS 2-3

Exercise 1.1 The weights are A` ; if they are all the same, the impulse train is periodic
with period P.
Basic Signals: Impulses The simplest signal is the (shifted) unit impulse
Generate and plot a periodic impulse train whose period is P = 5 and
signal:  whose length is 50.
1 n = n0
δ[n − n0 ] = (2.1) Start the signal at n = 0. Try to use one or two vector operations rather
0 n 6= n0
To create an impulse in MATLAB, we must decide how much of the signal than a for loop to set the impulse locations. How many impulses are
is of interest. If the impulse δ[n] is going to be used to drive a causal LTI contained within the finite-length signal?
system, we might want to see the L points from n = 0 to n = L − 1. If we
c. The following MATLAB code will produce a repetitive signal in the
choose L = 31, the following MATLAB code will create an ’impulse’:
vector x:
L = 31;
nn = 0 : (L-1) ; x = [0; 1; 1; 0; 0;0] * ones (1,7);
imp = zeros(L, 1); x = x(:);
imp(1) = 1; size(x) %<--- return the signal length

Notice that the n = 0 index must be referred to as imp (1) , due to MAT- Plot x to visualize its form; then give a mathematical formula similar to
LAB’s indexing scheme. eq.2.2 to describe this signal.

a. Generate and plot the following sequences. In each case the horizontal
(n) axis should extend only over the range indicated and should be la- Exercise 1.2
beled accordingly. Each sequence should be displayed as a discrete-time
signal using stem(). Basic Signals: Sinusoids Another very basic signal is the cosine wave. In
x1 [n] = 0.9 · δ[n − 5] for 1 ≤ n ≤ 20 general, it takes three parameters to describe a real sinusoidal signal com-
pletely: amplitude (A), frequency (ω0 ), and phase (Φ).
x2 [n] = 0.8 · δ[n] for −15 ≤ n ≤ 15
x3 [n] = 1.5 · δ[n − 333] for 300 ≤ n ≤ 350 x[n] = A · cos(ω0 n + Φ) (2.3)
x4 [n] = 4.5 · δ[n + 7] for −10 ≤ n ≤ 0

b. The shifted impulses, δ[n − n0 ], can be used to build a weighted impulse a. Generate and plot each of the following sequences. Use MATLAB’s
train, with period P and total length M · P: vector capability to do this with one function call by taking the co-
sine (or sine) of a vector argument. In each case, the horizontal (n)
M−1
axis should extend only over the range indicated and should be labeled
s[n] = ∑ Ak δ[n − kP] (2.2)
accordingly. Each sequence should be displayed as a sequence using
k=0

Digital Video & Audio Processing (R.Mester) DVAP Exercises Exc1,V2.0 23-November-2002
2.1. SIGNALS 2-4

stem(). In general, a continuous-time sinusoid is given by the following mathemati-


cal formula:
π
x1 [n] = sin ·n for 0 ≤ n ≤ 25 s(t) = A · cos(2π f0t + φ) (2.4)
17
π where A is the amplitude, f0 is the frequency in Hertz, and φ is the initial
x2 [n] = sin · n for −15 ≤ n ≤ 25
17  phase. If a discrete-time signal is produced by regular sampling of s(t) at a
 π rate of fs = 1/T , we get
x3 [n] = sin 3πn + ) for −10 ≤ n ≤ 10
2  
f0
π s[n] = s(t)|t=nT = A · cos (2π f0 T n + φ) = A · cos 2π n + φ

(2.5)
x4 [n] = cos √ · n for 0 ≤ n ≤ 50 fs
23
Comparison with formula 2.3 for a discrete-time sinusoid, x[n] = A ·
Give a simpler formula for x3 [n] that does not use trigonometric func- cos(ω0 n + φ), shows that the normalized radian frequency is now a scaled
tions. Explain why x4 [n] is not a periodic sequence. version of f0 , ω0 = 2π( f0 T ).

b. Write a MATLAB function that will generate a finite-length sinusoid. a. From formula 2.4 for the continuous-time sinusoid, write a function
The function will need a total of five input arguments: three for the that will generate samples of s(t) to create a finite-length discrete-time
parameters and two more to specify the first and last n index of the signal. This function will require six inputs: three for the signal param-
finite-length signal. The function should return a column vector that eters, two for the start and stop times, and one for the sampling rate
contains the values of the sinusoid. Test this function by plotting the (in Hertz). It can call the previously written MATLAB function for the
results for various choices of the input parameters. In particular, show discrete-time sinusoid. To make the MATLAB function correspond to
how to generate the signal 2 sin(πn/11) for −20 ≤ n <≤ 20. the continuous-time signal definition, make the units of the start and
stop times seconds, not index number. Use this function to generate a
c. Modification: Rewrite the function in part (b) to return two arguments: sampled sinusoid with the following definition:
a vector of indices over the range of n, and the values of the signal.
Signal freq = 1200 Hz
Sampling freq = 8 kiloHz
Exercise 1.3 Initial Phase 45 deg
Starting Time = 0 sec
Amplitude = 50
Sampled Sinusoids Often a discrete-time signal is produced by sampling Ending Time = 7 millisec
a continuous-time signal such as a constant-frequency sine wave. The rela-
tionship between the continuous-time frequency and the sampling frequency Make two plots of the resulting signal: one as a function of time t (in
is the main point of the Nyquist-Shannon sampling theorem, which requires milliseconds), and the other as a function of the sample index n used in
that the sampling frequency be at least twice the highest frequency in the tn = nT . Determine the length of the resulting discrete-time signal and
signal for perfect reconstruction. the number of periods of the sinusoid included in the vector.

Digital Video & Audio Processing (R.Mester) DVAP Exercises Exc1,V2.0 23-November-2002
2.1. SIGNALS 2-5

b. Show by mathematical manipulation that sampling a cosine at the times over a finite range. This sum is known in closed form:
3
tn = n + T will result in a discrete-time signal that appears to be a L−1
4 1 − aL
sine wave when f = 1/T . Use the function from part (a) to generate ∑ an = 1−a
for a 6= 1 (2.6)
n=0
a discrete-time sine wave by changing the start and stop times for the
sampling. Use the function from part (a) to generate an exponential and then sum
it up, compare the result to formula 2.6.

Exercise 1.4 c. One reason the exponential sequence occurs so often in DSP is that
time shifting does not change the character of the signal. Show that the
Basic Signals: Exponentials The decaying exponential is a basic signal in finite-length exponential signal satisfies the shifting relation:
DSP because it occurs as the solution to linear constant-coefficient difference
y[n] = a · y[n − 1] over the range 1 ≤ n ≤ L − 1 (2.7)
equations.
by comparing the vectors y(2 : L) and a * y (1 : L- 1). When
a. Study the following MATLAB function to see how it generates a shifting finite-length signals in MATLAB, we must be careful at the
discrete-time exponential signal. Then use the function to plot the ex- endpoints because there is no automatic zero padding.
ponential x[n] = (0.9)n over the range n = 0, 1, 2, . . . 20. d. Another way to generate an exponential signal is to use a recursive for-
mula given by a difference equation. The signal y[n] = an · u[n] is the
function y = genexp( b, n0, L ) solution to the following difference equation when the input, x[n], is an
%GENEXP generate an exponential signal: b^n impulse:
% usage: Y = genexp( B, NO, L )
% B input scalar giving ratio between terms y[n] − a · y[n − 1] = x[n] initial condition: y[−1] = 0 (2.8)
% NO starting index (integer)
% L length of generated signal Since the difference equation is assumed to recurse in a causal manner
% Y output signal Y(1:L) (i.e., for increasing n), the initial condition at n = −1 is necessary. In
if( L <= 0 ) MATLAB the function filter() will implement a difference equation.
error(’GENEXP: length not positive’) Use filter() to generate the same signal as in part (a) (i.e., a = 0.9).
end
nn = n0 + [1:L]’ - 1; %---vector of indices
y = b .^ nn;
end

b. In many derivations, the exponential sequence an · u[n] must be summed

Digital Video & Audio Processing (R.Mester) DVAP Exercises Exc1,V2.0 23-November-2002
2.1. SIGNALS 2-6

2.1.2 Project 2: Complex-Valued Signals 1


REAL PART

This project centers on the issues involved with representing and generating 0.5
complex-valued signals. Although in the real world, signals must have real
values, it is often extremely useful to generate, process, and interpret real- 0

valued signal pairs as complex-valued signals. This is done by combining


−0.5
the signals into a pair, as the real and imaginary parts of a complex number,
and processing this pair with other complex-valued signals using the rules of
−1
complex arithmetic. Use of signal pairs in this way is an important part of 0 5 10
INDEX (n)
15 20 25

many signal processing systems, especially those involving modulation.


IMAG PART
1
Complex exponentials are a class of complex signals that is extremely im-
portant because the complex amplitude (phasor notation) provides a concise 0.5
way to describe sinusoidal signals. Most electrical engineering students are
familiar with phasors in connection with ac circuits or power systems, but 0
their use in radar, wave propagation, and Fourier analysis is just as signifi-
cant (although the term phasor is not always used). −0.5

−1
0 5 10 15 20 25
Hints In MATLAB, the functions real() and imag() will extract the INDEX (n)

real and imaginary parts of a complex number. When plotting a complex


vector, the defaults for plot and stem() can be confusing. If z is com- Figure 2.2: Plotting real and imaginary parts of a discrete-time signal with
plex, then plot (z) will plot the imaginary part versus the real part; and subplot.
plot (n, z) will plot the real part of z versus n. However, stem(z) will
just plot the real part. If you want to view simultaneous plots of the real and
imaginary parts, the subplot (211) and subplot (212) commands prior
to each stem() command will force the two plots to be placed on the same
screen, one above the other. See Fig. 2.2, which was created using the fol- title(’REAL PART’), xlabel(’INDEX (n)’)
lowing code: subplot (212)
stem (nn, imag (xx))
title(’IMAG PART’), xlabel(’INDEX (n)’)
nn = 0:25;
xx = exp(j*nn/3); %--- complex exponential
subplot (211)
stem (nn, real (xx))

Digital Video & Audio Processing (R.Mester) DVAP Exercises Exc1,V2.0 23-November-2002
2.1. SIGNALS 2-7

Exercise 2.1 only over the range indicated and should be labeled accordingly.
π  π 
x1 [n] = 3 sin n + j4 cos n 0 ≤ n ≤ 20
Complex Exponentials The real exponential notation can be extended to 7  π7 
complex-valued exponential signals that embody the sine and cosine signals. x2 [n] = sin n −15 ≤ n ≤ 25
These signals form the basis of the Fourier transform. π 17
π
x3 [n] = 1.1n cos n+ 0 ≤ n ≤ 50
11  4 
π
a. In MATLAB a complex signal is a natural extension of the notation in x4 [n] = 0.9n cos n −10 ≤ n ≤ 20
Exercise 1.4. Thus the parameter a can be taken as a complex number 11
to generate these signals. Recall Euler’s formula for the complex expo- For each signal, determine the values of amplitude and phase constants
nential (in a form that gives a signal): that have to be used in G; also the angle and magnitude of z0 .
d. These same complex exponentials can be generated by first-order dif-
x[n] = (z0 )n = exp ((ln z0 + j6 z0 ) n) = rn exp jθn = rn (cos θn + j sin θn) ference equations (using filter()):
(2.9)
where z0 = r · e jθ = r6 θ. Use this relationship to generate a complex y[n] = z0 · y[n − 1] + x[n]. (2.11)
exponential with z0 = 0.96 45o . Plot the real and imaginary parts of x[n] The filter coefficient, z0 = re jθ , is a complex number. The ratio between
over the range 0 < n < 20. Notice that the angle of z0 controls the fre- successive terms in the sequence is easily seen to be z0 ; but the correct
quency of the sinusoids. amplitude and phase must be set by choosing a complex amplitude for
the impulse which drives the difference equation (i.e., x[n] = G · δ[n]).
b. For the signal in part (a) make a plot of the imaginary part versus the
Use filter() to create the same signals as in part (c). Verify by plot-
real part. The result should be a spiral. Experiment with different angles
ting both the real and imaginary parts of y[n] and comparing to the sig-
for θ − a smaller value should produce a better picture of a spiral.
nals generated via exp().
c. Equation 2.9 is not general enough to produce all complex exponentials. e. In the first-order difference equation 2.11, let yR [n] and yI [n] denote the
What is missing is a complex constant to scale the amplitude and phase real and imaginary parts of y[n]. Write a pair of real-valued difference
of the sinusoids. This is the so-called phasor notation: equations expressing yR [n] and yI [n] in terms of yR [n − 1], yR [n − 1], x[n]
and r, cos θ, and sin θ.
G · zn0 = Ae jθ rn e j(θn) = Arn e j(θn+φ) = Arn [cos(θn + φ) + j sin(θn + φ)]
(2.10) f. Write a MATLAB program to implement this pair of real equations, and
where G = Ae = A φ is the complex amplitude of the complex expo-
jφ 6 use this program to generate the impulse response of equation 2.11 for
1
nential. Generate and plot each of the following sequences. Convert the r = and θ = 0, and θ = π/4. For these two cases, plot the real part of
sinusoids to complex notation; then create the signal vector using exp. 2
the impulse responses obtained. Compare to the real part of the output
If the signal is purely real, it should be generated by taking the real part from the complex recursion 2.11.
of a complex signal. In each plot, the horizontal (n) axis should extend

Digital Video & Audio Processing (R.Mester) DVAP Exercises Exc1,V2.0 23-November-2002

You might also like