Chapter 4
Sinusoids
Most analog signals are either sinusoids, or a combination of sinusoids (or can be
approximated as a combination of sinusoids). This makes combinations of sinusoids
especially interesting. It is easy to add sinusoids together; pressing keys on a piano
or strumming a guitar adds several sinusoids together (though they do decay, unlike
the sinusoids we usually study). In this chapter, we will investigate sinusoids and
see how they can be added together to model signals. The goal is to gain a better
understanding of sinusoids and get ready for the Fourier transform.
4.1
Review of Geometry and Trigonometry
Greek letters are nothing to be afraid of. Mathematics uses variables, much like we
do in computer programming. These variables are one letter, which allows things to
be described concisely. Greek letters are often used simply because we tend to run
out of our alphabetic letters. Also, there are letters that are good to avoid, like l,
and o, since they look too much like the numbers 1 and 0.
Consider a right triangle (see Figure 4.1). We know, thanks to Pythagoras (or
at least his school [15]) that a2 + b2 = c2 , so if we happen to know only two sides, we
can calculate the third1 . Also, with this information, we can figure out the angles
θ1 and θ2 . Since it is a right triangle, the third angle is 90 degrees by definition.
The cosine function gives us the ratio of the length of the adjacent side (a) to
the length of the hypotenuse (c). In other words,
cos(θ1 ) =
1
a
.
c
According to [16] the Babylonians knew this, but the Greeks were first to prove it.
133
134
DSP Using MATLAB and Wavelets
θ2
c
b
θ1
a
Figure 4.1: A right triangle.
The sine function gives us the ratio of the length of the opposite side (b) to the
length of the hypotenuse (c). In other words,
b
sin(θ1 ) = .
c
The cos−1 and sin−1 functions (also called arccos and arcsin) give us the angle
θ1 from the ratio of the sides.
4.2
The Number π
It is impossible to talk about sine and cosine functions without talking about the
constant π, the ratio of a circle’s diameter to its circumference (see Figure 4.2). You
probably remember the equation:
circumference = 2πr, where r is the circle’s radius.
Length
around is
2π r
−r
+r
Figure 4.2: An example circle.
Sinusoids
135
Pi (π) is an irrational number. No matter how many digits you calculate it out
to be, someone else could go a digit further. To date, no repeating pattern has been
found, so unlike rational numbers, you cannot represent π in a compact form. For
example, with a rational number such as 13 , you could write it as 0.33 to indicate
that it repeats forever when represented as a decimal number, or at least it can be
represented as one number (1) divided by another (3).
Ancient peoples had various approximations for π. The Egyptians used 256/81
for π, while the Babylonians calculated with 3 81 [17]. We will use the approximation
of 3.1415927 for π.
We will specify sinusoid components with the format
a × cos(2πf t + φ)
where a is the amplitude, f is the frequency, and φ is the phase angle. The variable
t represents time. Sine could be used in place of cosine, if we add π/2 to the phase
angle, since cos(θ) = sin(θ + π/2).
Amplitude (a), phase shift (φ) (also called the phase angle), and frequency completely specify a sinusoid. Amplitude describes a quantity without assigning it units.
Magnitude is also a unitless description of a quantity, with one difference: amplitude can be positive or negative, while magnitude is always positive [11]. |x(t)|
refers to magnitude, and as you may expect, the abs function in MATLAB returns
the magnitude. Many people in DSP use magnitude and amplitude interchangeably,
for good reason. Viewing a sinusoid component as a phasor, where we draw it in
its initial position without concern about rotation with time, we would draw the
phasor along the positive x-axis, then rotate it counterclockwise according to the
phase angle. A negative length means we would draw the phasor along the negative
x-axis, then rotate (again counterclockwise) according to the angle. However, this
is exactly the same as drawing the length along the positive x-axis, and rotating it
an additional 180 degrees. In other words, −a × cos(2πf t + φ) is exactly the same
as a × cos(2πf t + φ + π). This is why we typically use a positive value for the
amplitude. The main exception is for the DC component, where frequency equals
zero. The phase angle also is assumed to be zero, and thus the amplitude is the
only value left to determine the sign of the DC component.
We use f to represent the cyclic frequency, also called simply frequency, though
sometimes the radian frequency (ω) is used for convenience. It relates to f with the
equation ω = 2πf . The units of f are in Hz, cycles per second, named for Heinrich Hertz (1857–1894), a German physicist whose experiments confirmed Maxwell’s
electromagnetic wave theories [18]. Since the unit Hz comes from a person’s name,
the first letter should always be capitalized.
136
DSP Using MATLAB and Wavelets
4.3
Unit Circles
A unit circle is of particular interest. For a unit circle, the radius r equals 1.
Therefore, the circumference, or length of the circle if it were cut and unrolled, is
2π. As a result, we can talk about an angle in terms of the length of the arc between
two line segments joined at a point. We call this angle radians (rad for short). In
Figure 4.3, angle θ is only a fraction of the circle’s circumference, therefore, it is a
fraction of 2π. Figure 4.4 shows the arc lengths on a unit circle for several common
angles.
θ
Figure 4.3: An angle specified in radians.
No doubt you are also familiar with an angle measured in degrees. When dealing
with sinusoidal functions (such as sine, cosine, etc. ), it is important to know whether
the function expects the argument to be in degrees or radians. On a scientific
calculator, you are likely to have a button labeled DRG, to allow you to specify
degrees or radians, though the default is probably degrees. For MATLAB, C/C++
and Java, the arguments to the sin() and cos() functions are assumed to be radians.
To convert between degrees and radians, we recall that one complete rotation
around the unit circle’s edge is 2π radians, or 360 ◦ . To convert from degrees to
radians, therefore, we multiply by 2π/360. To convert from radians to degrees, we
multiply by 360/(2π). For example, suppose the angle shown in Figure 4.3 is 30 ◦ ,
(30)(2π/360) = 60π/360
= π/6 radians.
Converting π/6 back to degrees can be done in a similar fashion:
Sinusoids
π/2
π
2π
3π/2
Figure 4.4: An angle specified in radians.
137
138
DSP Using MATLAB and Wavelets
(π/6)(360/(2π)) = 360π/(12π)
= 360/12
= 30◦ .
Quiz: What is larger, cos(0) or cos(1, 000, 000)? At first glance, one unfamiliar
with the cosine function might assume that the latter is larger, simply because the
argument is larger. But since sinusoids repeat, there is no reason for this to be the
case. In fact, one might remember that cos(0) is 1, and that cos(any number) always
generates a value between +1 and −1. Therefore, cos(0) is going to be greater than
or equal to cos(any number). In fact, cos(1, 000, 000) ≈ 0.9368, correct? If not, it is
likely due to your calculator being in degrees mode! Since it was not specified that
the angle is in degrees, one should assume that it is in radians.
Another thing to consider when dealing with sinusoid functions is that they are
repetitive. The angle specified by 0 radians is the same as that specified by 2π
radians. Thus, you can always add or subtract 2π to/from an angle. In fact, you
can add or subtract any integer multiple of 2π to or from an angle. Sometimes this
makes things easier.
4.4
Principal Value of the Phase Shift
A sinusoid stretches out infinitely in both directions. If we want to view it in terms
of a time axis, we can use the positive peaks of the sinusoid to relate to our time
axis. Figure 4.5 shows an example sinusoid, cos(2π60t), with a frequency of 60 Hz.
1
, or 0.0167 seconds.
Therefore, the period of time before this sinusoid repeats is 60
Vertical lines show where one repetition starts and ends, at t = −0.0167/2 and t =
+0.0167/2. This corresponds to arguments of −π and +π for the cos function, that
1
is, let t = ( 60
)( −1
2 ) to make cos(2π60t) equal cos(2π60(1/60)(−1/2)) = cos(−π).
But WHICH peak do we normally use? In Figure 4.5 it is very easy, but a
sinusoid function often has a nonzero phase angle. Since it repeats every 2π, we
could pick any peak that we want to classify the signal. This means that there will
always be a positive peak between -π and +π, in other words, −π < φ ≤ +π where
φ is the phase shift [6].
So why not use the positive peak closest to zero? This makes a lot of sense, and
we give this peak a special name: we say it is the principal value of the phase shift.
To find this peak, we can always add/subtract 2π from the phase shift, until it is in
the range we desire.
Sinusoids
139
amplitude
1
0.5
0
−0.5
−1
−0.05
−0.04
−0.03
−0.02
−0.01
0
time
0.01
0.02
0.03
0.04
0.05
Figure 4.5: A 60 Hz sinusoid.
4.5
Amplitudes
Usually, amplitude values are positive, and for good reason. Consider two sinusoids
of the same frequency, x1 = a × cos(2πf t + φ1 ) and x2 = −a × cos(2πf t + φ2 ). We
know that the minimum and maximum values for the cosine function are -1 and +1.
Therefore, x1 varies between +a and −a, and x2 varies between −a and +a. Given
that the two sinusoids have the same frequency, they must look a lot alike. In fact,
the only difference between them is their phase angles, a difference of π. That is,
φ2 = φ1 + π. This makes a lot of sense considering
the conversion of a complex value
p
2
of x + jy to polar form r∠θ: the equation ( x + y 2 ) leads to a positive value for
r. A vector with a negative length would be the same as a vector with a positive
length, rotated a half-revolution, as demonstrated in Figure 4.6.
imaginary axis
imaginary axis
j
j
a
θ
real axis
θ
θ
real axis
−a
Figure 4.6: A vector of −a at angle θ = a at angle (θ + π).
Signals are often represented as a sum of sinusoids. When these sinusoids are
related, we call the signal a harmonic.
140
DSP Using MATLAB and Wavelets
4.6
Harmonic Signals
Real-world signals can be decomposed into a set of sinusoids added together. A
harmonic signal is composed of several sinusoids added together, each with an integer
multiple of some base frequency, called the fundamental frequency. For example, the
following signal is a harmonic:
x(t) = 0.4 cos(2π15t + π/5) + 0.7 cos(2π30t) + cos(2π45t − π/4).
Signal x(t) is of the form
a1 × cos(2π(1)f0 t + φ1 ) + a2 × cos(2π(2)f0 t + φ2 ) + a3 × cos(2π(3)f0 t + φ3 )
where, for x(t), a1 = 0.4, a2 = 0.7, a3 = 1, φ1 = π/5, φ2 = 0, φ3 = −π/4, and
f0 = 15 Hz. Any signal of this form is a harmonic.
If an amplitude value is 0, then the corresponding sinusoid will not contribute
to the function. In other words, 0 × cos(anything) evaluates to 0. If we have a
function like
x2 (t) = 0.1 cos(2π100t − π/6) + 1.3 cos(2π300t + π) + 0.5 cos(2π400t + 2π/3)
then x2 (t) is a harmonic with a1 = 0.1, a2 = 0, a3 = 1.3, a4 = 0.5, φ1 = −π/6,
φ2 = 0, φ3 = π, φ4 = 2π/3, and f0 = 100 Hz.
The following MATLAB code is a function to plot harmonic signals.
%
% Given magnitudes, phases, and the fundamental frequency,
%
plot a harmonic signal.
%
% Usage: plotharmonic(freq, mag, phase)
%
% where freq = the fundamental frequency (a single value),
%
mag = the amplitudes (a list),
%
phase = the phases (a list).
%
function plotharmonic(freq, mag, phase)
num_points = 200;
% Number of points per repetition
% Check parameters
if (size(mag) ~= size(phase))
Sinusoids
141
sprintf('Error - magnitude and phase must be same length')
end
% We want this for 2 repetitions, and num_points per rep.
step = 2/(freq*num_points);
t = 0:step:2*(1/freq);
clear x;
x = 0;
for i=1:length(mag)
x = x + mag(i)*cos(2*pi*i*freq*t + phase(i));
end
my_title = sprintf('Harmonic signal: %d sinusoids',length(mag));
plot(t,x);
title(my_title);
xlabel('time (sec)');
ylabel('Amplitude');
For example, the following commands plot x 2 (t) from above. It is assumed that
the code from above is saved with “plotharmonic.m” as the filename.
Mag = [ 0.1 0 1.3 0.5];
Phase = [ -pi/6 0 pi 2*pi/3];
plotharmonic(100, Mag, Phase);
% Fundamental freq. is 100 Hz
The resulting plot can be seen in Figure 4.7.
One additional piece of information is if there is a vertical shift of the signal.
That is, what if the graph shown in Figure 4.7 was not centered around the x-axis?
This can be accomplished by adding a constant to the signal. For example, if we add
100 units to the signal, it would look the same, but it would be centered around the
line y = 100. This piece of information can be added to the above harmonic signal
format, simply by including the term a 0 cos(2π(0)f0 t + φ0 ). Starting the multiples
of f0 at zero means that the first component is simply a constant. There is no
need to specify a φ0 term, since cos(φ0 ) would just be a constant, and a0 already
specifies this. Therefore, we will assume φ 0 is zero. This term is often called the
DC component, for Direct Current.
Example:
For the signals below, say which ones are harmonic and which are not. If they
142
DSP Using MATLAB and Wavelets
Harmonic signal with 4 sinusoids
2
1.5
1
Amplitude
0.5
0
−0.5
−1
−1.5
−2
0
0.002
0.004
0.006
0.008
0.01
0.012
Time (sec)
0.014
Figure 4.7: A harmonic signal.
0.016
0.018
0.02
Sinusoids
143
are harmonic, say what the fundamental frequency is.
x1 (t) = 2 cos(2π7t) + 3 cos(2π35t)
x2 (t) = 2 cos(2π 2 t) + 3 cos(2πt)
x3 (t) = 2 cos(2π7t + π) + 3 cos(2πt − π/4)
Answer:
To be harmonically related, each sinusoid must have a frequency that is an
integer multiple of some fundamental frequency. That is, they should be in the
following form. Remember that k is an integer.
x(t) =
N
X
ak cos(2πkf0 t + φk )
k=1
So we put the above signals in this form, and see if the k values are integers,
which they must be for the sinusoids to be harmonically related. Looking at the
arguments of the cosine function for x 1 , let k1 and k2 be values of k.
2π7t = 2πk1 f0 t + φk , Let φk = 0
f0 = 7/k1
2π35t = 2πk2 f0 t + φk , Let φk = 0
f0 = 35/k2
Substituting in f0 , we get
35/k2 = 7/k1
k1 /k2 = 7/35
k1 /k2 = 1/5
So if k1 = 1, then k2 = 5
f0 = 35/k2 = 35/5 = 7 Hz.
Therefore, x1 (t) has integer k values, and is harmonic. x 3 (t) is, too. x2 (t)
does not have integer k values (due to the frequency of π) and is, therefore, not
harmonic. The third signal, x3 , is a bit tricky because the frequency components
are not lined up as we may expect.f0 = 1 Hz, a0 = 1, ak = {3, 0, 0, 0, 0, 0, 2}, and
φk = {−π/4, 0, 0, 0, 0, 0, π}.
144
DSP Using MATLAB and Wavelets
We could consider harmonic a signal with a DC component, too. Without loss
of generality, we can have index k start at 0, implying that we have a frequency
component of 0 ∗ f0 , or 0 Hz. Since designating a value for the phase angle would
only result in the amplitude a0 being multiplied by a constant, we should set the
phase angle to 0. Thus, a0 completely specifies the DC component.
Representing a signal that has discontinuities, such as a square wave, with a sum
of harmonically related sinusoids is difficult. Many terms are needed [19]. Called
Gibbs’ phenomenon, we cannot perfectly represent a discontinuous signal as a sum
of sinusoids, but our approximation will have overshoot, undershoot, and ripples.
Fourier analysis works best with a periodic signal. We implicitly expect that the
first and last sample are linked, that if we were to take one more reading of the
signal, that this reading would be the same as the very first sample, or at least a
value that we have seen before. Whether or not this N + 1 value matches value 0
depends upon how many samples we have taken. Also, we assume that a sufficient
number of samples has been recorded, given by the sampling period. If the signal
truly is periodic, and we have a good sampling rate, and we take exactly one period’s
worth of samples, then the sample at N + 1 would be equal to sample 0.
Sometimes we do not have a periodic signal. In this case, we may have a discontinuity between the last sample and the first. For example, consider the signal
below, Figure 4.8:
Figure 4.8: A short signal.
If we treat this signal as being periodic, then we expect that it repeats forever,
and that we can copy and paste this signal to itself, such as in Figure 4.9.
Here we see that there is a discontinuity in the middle, and this is difficult for a
Fourier series to handle (remember Gibb’s phenomenon). Thus, we should consider
the Fourier analysis to give us an approximation of the original signal. We would
expect that the approximation is not as good around a discontinuity as it is for the
rest of the signal. We call this difficulty edge effects.
Even with edge effects, Fourier analysis will return a good approximation to the
Sinusoids
145
Figure 4.9: The short signal, repeated.
original signal. In fact, any nonrandom signal can be approximated with Fourier
analysis. If we take a random signal and find the Fourier series of it, we will find
that it approximates the signal for the values that we have seen. However, we may
be tempted to use this to predict future values of the signal. If it is truly random,
though, any matching between the signal and a prediction would be coincidence.
4.7
Representing a Digital Signal as a Sum of
Sinusoids
A digital signal can be represented as a sum of sinusoids. The following three figures
show how this works. We can take a digital signal and find the Fourier transform of
it (the Fourier transform is covered a bit later in this book). The important thing
to understand is that the Fourier transform gives us a frequency-domain version of
the signal: a list of amplitudes and phase angles corresponding to a harmonically
related set of sinusoids.
We explore the fundamental frequency of these sinusoids later; for the present
discussion, we will ignore it. The timing of these simulated samples determines the
real fundamental frequency, so we will just stick to sample numbers instead of time
for the x-axis of the plots.
Refer to Figure 4.10. Here we see the original signal, a list of discrete sample
points. Under it, we have a graph of a signal made by adding several sinusoids (8,
to be precise). Notice how this composite signal passes through the same points as
the original digital signal. If we were to read values from this composite signal with
the same timing as the sampling of the digital signal, we would have the same data.
In Figure 4.10, we see that the sum of sinusoids representation is continuous.
But we should be careful not to conclude that the underlying analog signal, which we
are trying to represent as a set of discrete points, actually looks like the composite
146
DSP Using MATLAB and Wavelets
original
8
7
6
5
4
3
2
0
1
2
3
4
5
6
7
5
6
7
sum of sinusoids
8
7
6
5
4
3
2
0
1
2
3
4
Figure 4.10: A digital signal (top) and its sum of sinusoids representation (bottom).
Sinusoids
147
signal. We may not know what it looks like, and the hill we have between sample 2
and sample 3 (as well as between sample 3 and sample 4) could mislead us.
The next two graphs, Figures 4.11 and 4.12, show the sinusoids that we added
together to get the composite one. Yes, the first one shown is a sinusoid of zero
frequency! The asterisks show the values that would be found if the sinusoids were
sampled with the same timing as the original signal. Notice how the number of
sinusoids (8) returned from the Fourier transform matches the number of original
points (8).
6
4
2
0
1
2
3
4
5
6
7
0
1
2
3
4
5
6
7
0
1
2
3
4
5
6
7
0
1
2
3
4
5
6
7
1
0
−1
1
0
−1
amplitude
1
0
−1
sample
Figure 4.11: The first four sinusoids in the composite signal.
As an exercise, let’s approximate the value of the second-to-last sample. In
Figure 4.11, we see that sample #6 of each sinusoid has the following approximate
values: 4.9, 0.3, -0.1, and 0.7. Turning our attention to Figure 4.12, we see this
next-to-last sample’s values are roughly: 0.4, 0.7, -0.1, and 0.3. If we add these up
together, we get 7.1. Looking back at the original Figure 4.10, we see that the value
for sample #6 is right around 7. In fact, the only reason we did not get exactly
the same number as in the first figure was due to the error induced by judging the
148
DSP Using MATLAB and Wavelets
graphs by eye.
0.5
0
−0.5
0
1
2
3
4
5
6
7
0
1
2
3
4
5
6
7
0
1
2
3
4
5
6
7
0
1
2
3
4
5
6
7
1
0
−1
1
0
−1
amplitude
1
0
−1
sample
Figure 4.12: The last four sinusoids in the composite signal.
In a similar exercise, we can note the maximum values for each of the sinusoids
in Figures 4.11 and 4.12. These are: 4.88, 0.86, 0.76, 0.81, 0.38, 0.81, 0.76, and
0.86. They correspond to the amplitudes of each of the sinusoids, the a k values
that we would need for a harmonic representation. The other pieces of information
(besides the fundamental frequency) would be the phase angles. These are a bit
more difficult to read off of a graph, but we can make approximations for them.
Remembering that cos(0) = 1, we notice that the value returned from the cosine
function decreases as the angle gets larger, until it reaches −1 at π, then it increases
until it gets to 1 again at 2π. We can use this observation to get a rough idea about
the phases. Looking at the second graph of Figure 4.11, we see that the sinusoid
has its maximum point right at sample 0. The maximum point would correspond
to the amplitude, or more exactly a1 × cos(2π1f0 t + φ1 ) when the argument to the
cosine function is 0, as it would be at time 0. So we would have 2π1f 0 0 + φ1 = 0,
or simply φ1 = 0. Now, to find the next phase angle, φ 2 . At sample 0 (third
Sinusoids
149
graph on Figure 4.11), we notice that our value would be about 0.1. Solving for φ 2 ,
a2 × cos(2π1f0 t + φ2 ) at time 0 ≈ 0.1. This means that a2 × cos(φ2 ) ≈ 0.1, and we
already have an approximation of a2 as 0.8. So we can solve to find:
0.8 cos(φ2 ) ≈ 0.1
0.1
cos(φ2 ) ≈
0.8
0.1
φ2 ≈ arccos
0.8
φ2 ≈ 83◦
46π
.
100
But there could be a problem: how do we know that the sinusoid will increase at the
next time step? To put this another way, we know that the cosine function returns
the same value for θ as it would for 2π − θ, so which value should we use for our
phase?
To solve this problem, we borrow from calculus (in a very painless way!). We
have a graphical representation for the sinusoid that we are after, and we can tell
just by looking at the graph if it increases or decreases. In the case of the third graph
on Figure 4.11, we see that it increases. If we add a little bit to the phase angle,
simulating a very small time increment, the value returned by a 2 ×cos((a little bit)+
φ2 ) should be a little more than a2 × cos(φ2 ). Let’s use this to test our value for φ 2 .
46π
= 0.1003
0.8cos
100
46π
0.8cos 0.01 +
= 0.0923
100
φ2 ≈
But this shows that the sinusoid would decrease, contrary to our graph. Therefore,
we know that the phase angle must be our previous estimate negated, or −46π/100.
We can (and should) double-check our work by applying the same trick as above.
−46π
0.8cos
= 0.1003
100
46π
0.8cos 0.01 −
= 0.1082
100
We see that the next values increase, just as we observe from the graph. We can
150
DSP Using MATLAB and Wavelets
continue in like fashion for the rest of the graphs.
The program below demonstrates the sum-of-sinusoids concept. It does not
require any parameters, since it makes the “original” signal randomly. This program
finds the frequency-domain representation X of the random signal x, then uses the
frequency-domain information to get the original signal back again (as the array
my sum of sins). We see that, when run, this program calculates the error between
the original signal and the reconstructed one, an error so small that it is due only
to the precision used.
% sum_of_sins.m
% Show a random signal as a sum of sins
%
% Make our x signal
x = round(rand(1,8)*10);
Xsize = length(x);
% Get FFT of x
X = fft(x);
Xmag = abs(X);
Xphase = angle(X);
% Show the freq-domain info as sum of sinusoids
% Find the IFFT (the hard way) part 1
n=0:Xsize-1; %m=0:Xsize-1;
% Do the sinusoids as discrete points only
s0 = Xmag(1)*cos(2*pi*0*n/Xsize + Xphase(1))/Xsize;
s1 = Xmag(2)*cos(2*pi*1*n/Xsize + Xphase(2))/Xsize;
s2 = Xmag(3)*cos(2*pi*2*n/Xsize + Xphase(3))/Xsize;
s3 = Xmag(4)*cos(2*pi*3*n/Xsize + Xphase(4))/Xsize;
s4 = Xmag(5)*cos(2*pi*4*n/Xsize + Xphase(5))/Xsize;
s5 = Xmag(6)*cos(2*pi*5*n/Xsize + Xphase(6))/Xsize;
s6 = Xmag(7)*cos(2*pi*6*n/Xsize + Xphase(7))/Xsize;
s7 = Xmag(8)*cos(2*pi*7*n/Xsize + Xphase(8))/Xsize;
% Redo the sinusoids as smooth curves
t = 0:0.05:Xsize-1;
smooth0 = Xmag(1)*cos(2*pi*0*t/Xsize + Xphase(1))/Xsize;
smooth1 = Xmag(2)*cos(2*pi*1*t/Xsize + Xphase(2))/Xsize;
smooth2 = Xmag(3)*cos(2*pi*2*t/Xsize + Xphase(3))/Xsize;
smooth3 = Xmag(4)*cos(2*pi*3*t/Xsize + Xphase(4))/Xsize;
Sinusoids
151
smooth4 = Xmag(5)*cos(2*pi*4*t/Xsize + Xphase(5))/Xsize;
smooth5 = Xmag(6)*cos(2*pi*5*t/Xsize + Xphase(6))/Xsize;
smooth6 = Xmag(7)*cos(2*pi*6*t/Xsize + Xphase(7))/Xsize;
smooth7 = Xmag(8)*cos(2*pi*7*t/Xsize + Xphase(8))/Xsize;
% Find the IFFT (the hard way) part 2
my_sum_of_sins = (s0+s1+s2+s3+s4+s5+s6+s7);
smooth_sum = smooth0 + smooth1 + smooth2 + smooth3;
smooth_sum = smooth_sum + smooth4 + smooth5 + smooth6 + smooth7;
% Show both discrete points and smooth curves together
xaxis1 = (0:length(smooth0)-1)/20; % for 8 points
xaxis2 = (0:length(s0)-1);
figure(1);
subplot(4,1,1); plot(xaxis1, smooth0, 'g', xaxis2, s0,
subplot(4,1,2); plot(xaxis1, smooth1, 'g', xaxis2, s1,
subplot(4,1,3); plot(xaxis1, smooth2, 'g', xaxis2, s2,
subplot(4,1,4); plot(xaxis1, smooth3, 'g', xaxis2, s3,
ylabel('amplitude');
xlabel('sample');
figure(2);
subplot(4,1,1); plot(xaxis1, smooth4, 'g', xaxis2, s4,
subplot(4,1,2); plot(xaxis1, smooth5, 'g', xaxis2, s5,
subplot(4,1,3); plot(xaxis1, smooth6, 'g', xaxis2, s6,
subplot(4,1,4); plot(xaxis1, smooth7, 'g', xaxis2, s7,
ylabel('amplitude');
xlabel('sample');
'r*');
'r*');
'r*');
'r*');
'r*');
'r*');
'r*');
'r*');
% Show the original versus the reconstruction from freq-domain
figure(3);
subplot(2,1,1); plot(0:Xsize-1, x, 'bd');
title('original');
min_y = floor(min(smooth_sum));
max_y = ceil(max(smooth_sum));
axis([0, Xsize-1, min_y, max_y]); % Make the axes look nice
xaxis1 = (0:length(smooth_sum)-1)/20;
xaxis2 = (0:length(s0)-1);
subplot(2,1,2);
152
DSP Using MATLAB and Wavelets
plot(xaxis1, smooth_sum, 'b', xaxis2, my_sum_of_sins, 'rd');
title('sum of sinusoids');
Error = my_sum_of_sins - x
We assume that the signal contains only real information. In general, this is
not the case. However, since we started with a signal of all real values, we will
end up with a signal of all real values, since we do not change the information, we
only transform it from the time-domain to the frequency-domain, then transform
it back. The Fourier transform results in a complex signal, though the original
signal was real. To reconcile this, we simply treat the original real signal as if it
were complex, but the imaginary part is zero. The signal returned from the inversetransform must also have an imaginary part of zero for all values. The program
performs the inverse-transform in a sense, but it does not take the complex part into
consideration, as it would have to do if the original signal were complex. In other
words, the program treats each sinusoid as if it were X mag [k] cos(2πkf0 t+Xangle [k]),
when in fact each sinusoid is (from Euler’s formula) X mag [k] cos(2πkf0 t+Xangle [k])+
jXmag [k] sin(2πkf0 t + Xangle [k]). We are able to get away with this simplification
since we know in advance that the complex parts, j sin(◦), must cancel each other
out.
4.8
Spectrum
A spectrum is the name given to the frequency plot of a signal. The spectrum is
a graph of the frequencies that make up a signal, with their amplitudes shown in
relation to each other. A separate graph shows the phases for each sinusoid.
Previously, in Figure 4.10, we saw that a digital signal can be represented by
a sum of sinusoids. These sinusoids can then be graphed individually, as shown in
Figures 4.11 and 4.12. Sometimes this sinusoid information provides insight into the
signal. For example, if the original signal is music, then the sinusoids tell us about
the instruments that are being played. If the original signal was of a person talking,
with a high-pitched extraneous sound such as a train whistle, we might be able to
remove the higher-frequency whistle.
Plotting the information given to us by the sinusoids results in a spectrum. Continuing with the example from the last section, we see that one piece of information
we are missing is the fundamental frequency. As stated before, we need to know how
much time there is between samples before we can know the frequencies. But we will
do our best without this. We see that the sinusoids repeat an integral amount in each
graph, from 0 repetitions (0 frequency) up to 7 repetitions. That is, the sinusoids repeat with such a frequency that sample #8, if plotted, would be the same as sample
Sinusoids
153
#0. As the number of repetitions gets larger, so does the corresponding frequency.
Suppose, for the sake of argument, that the original signal was sampled at a rate of
125 milliseconds per sample, so we read each sample 125 milliseconds after the last.
The second graph, on Figure 4.11, shows that the sinusoid repeats once every 1000
milliseconds, or once per second (it takes another 125 milliseconds before it gets to
sample #8, which is identical to sample #0). Thus, it has a frequency of 1 Hz.
Similarly, we see that the next graph has a frequency of 2 Hz, then 3 Hz for the next
graph, etc., up to 7 Hz for the last graph of Figure 4.12. For the remainder of this
chapter, we assume that we have this fundamental frequency of 1 Hz. Recalling our
amplitudes {4.88, 0.86, 0.76, 0.81, 0.38, 0.81, 0.76, 0.86} from the previous section,
and getting the corresponding phases in degrees {0, 22, −81, −56, 0, 56, 81, −22}, we
can plot this information as follows, Figure 4.13. In the top part of the figure, we
see a frequency magnitude plot, since it shows the positive amplitudes versus the
frequencies. The bottom part of Figure 4.13 shows a frequency phase plot, since it
shows the phase angles for each corresponding sinusoid frequency.
4.88
amplitude
0.86
0.86
0.76 0.81 0.81 0.76
0.38
0
1
2
3
4
5
6
7
81
phase
(degrees)
frequency
(Hz)
56
0
0
22
1
0
2
3
−56
−81
4
5
6
7
−22
frequency
(Hz)
Figure 4.13: Frequency magnitude spectrum and phase angles.
One should observe the pattern of the spectrum: for the frequency magnitude
plot, we see a mirror image centered around 4 Hz. On the phase plot, again the
pattern centers around 4 Hz, but we have a flipped as well as mirrored image.
So far we have seen how to get the spectrum from the amplitudes and phases
returned by the Fourier transform. To find the spectrum of an abstract signal, such
as a mathematical model, use the inverse Euler’s formula. Here we have only an
154
DSP Using MATLAB and Wavelets
introduction; this topic is covered extensively in Chapter 7, “The Number e.” For
example, to find and plot the magnitude spectrum of x(t) = 2 + 2 cos(2π(200)t), we
put it in terms of cosine functions.
x(t)
=
=
2 + 2 cos(2π200t)
2 cos(0) + 2 cos(2π200t)
Next, use inverse Euler’s formula:
cos(φ) = (ejφ + e−jφ )/2.
Notice how this results in two frequency components: one at the frequency given,
and one at the negative frequency. For the spectrum, this implies that the range of
frequencies will be different from what we saw with the previous example. Actually,
the information stays the same, only our spectral plot has different, but equivalent,
frequency values. This is explained in Chapter 6, “The Fourier Transform.”
x(t)
=
=
=
2(ej0 + e−j0 )/2 + 2(ej2π(200)t + e−j2π(200)t )/2
2(1 + 1)/2 + (2/2)(ej2π(200)t + e−j2π(200)t )
2 + ej2π(200)t + e−j2π(200)t
This results in a magnitude of 2 at frequency 0 Hz, and a magnitude of 1 at
frequencies 200 Hz and -200 Hz. This is shown in the magnitude plot, Figure 4.14.
2
1
−300
−200
1
−100
0
100
200
300
Hz
Figure 4.14: Spectrum plot: magnitude of x(t) = 2 + 2 cos(2π(200)t).
Example:
For the signal x(t) below,
x(t) = 4 cos(2π100t + 3π/4) + 2 cos(2π200t) + 5 cos(2π300t − 3π/5)
draw the magnitude spectrum and the phase spectrum of this signal.
Sinusoids
155
Answer:
x(t) =
+
+
4 j(2π100t+3π/4) 4 −j(2π100t+3π/4)
e
+ e
2
2
2 j(2π200t) 2 −j(2π200t)
e
+ e
2
2
5 j(2π300t−3π/5) 5 −j(2π300t−3π/5)
e
+ e
2
2
x(t) = 2 ej2π100t ej3π/4 + 2 e−j2π100t e−j3π/4
+ ej2π200t + e−j2π200t
+ 2.5 ej2π300t e−j3π/5 + 2.5 e−j2π300t ej3π/5
Pulling the frequencies out { 100, −100, 200, −200, 300, −300 }, and marking the
corresponding magnitudes {2, 2, 1, 1, 2.5, 2.5}, gives us the graph of Figure 4.15. For
each frequency, we also note the phases, { 3π/4, −3π/4, 0, 0, −3π/5, 3π/5}. The
phase angles are shown in Figure 4.16.
2.5
2.5
2
2
1
−300
1
−200
−100
0
100
200
300
Hz
300
Hz
Figure 4.15: Spectrum plot: magnitudes.
3π/4
3π/5
0
−300
−200
0
−100
−3π/4
0
100
200
−3π/5
Figure 4.16: Spectrum plot: phase angles.
156
DSP Using MATLAB and Wavelets
4.9
Summary
Real-world signals have sinusoid components, unless they are truly random, in which
case they can still be approximated with a sum of sinusoids. Since all analog signals
can be represented (or at least approximated) with a sum of sinusoids, we can view
a signal in terms of the frequencies that comprise it. Given a sum-of-sinusoids
equation for a signal, we use the inverse Euler’s formula to convert to a sum of
complex exponentials, such as (a1 /2)ej2πf1 t ejφ1 + (a1 /2)ej2π(−f1 )t ej(−φ1 ) . From this
form, we can make a plot of the amplitudes (a 1 /2, a1 /2) versus the frequencies (f1 ,
−f1 ), as well as a plot of the phase angles versus the frequencies. Together, we call
this information the spectrum, a view of the signal in the frequency-domain.
We will see how to get the frequency-domain information from a signal of only
time-domain points using the Fourier transform, in Chapter 6.
4.10
Review Questions
1. What are the minimum and maximum values for x(t)?
x(t) = 3 cos(2π100t + π/6)
2. Given sin(θ) = cos(θ−π/2), if x(t) = 3 sin(2π100t+π/6), what is this function
in terms of cos?
3. The analog signal, x(t), is:
x(t) = 3 cos(2π2000t + π/4) + 2 cos(2π5000t) + cos(2π11000t − π/7)
a. plot each frequency component (in the time-domain) separately
b. graph this function (in the time-domain)
c. represent x(t) in terms of a fundamental frequency, amplitudes and phases.
4. The analog signal, x(t), is:
x(t) = 3 cos(2π3000t) + cos(2π1000t + π/3) + 4 cos(2π5000t − π/3)
Represent x(t) in terms of a fundamental frequency, amplitudes and phases.
5. What is the difference between amplitude and magnitude? What does |x(t)|
mean?
6. DC component specifies the “shift of the signal”
. (f = 0)
Sinusoids
Phase-shift specifies shift of signal
157
. (φ)
7. What is another name for a plot of the frequency information of a signal?
8. Which signal, a square wave or a triangular wave, is easier to approximate
with a series of harmonically related sinusoids? Why?
9. Why are amplitudes always positive?
P
10. Since x(t) below is in the form a0 + N
k=1 ak cos(2π(k)f0 t + φk ), what are the
values for N , ak , φk , and f0 ? What special name do we give this signal x(t)?
x(t) = 4 + 3 cos(2π(1)20t + π/2) + 7 cos(2π(2)20t − 2π/3)
© Copyright 2026 Paperzz