33 Functions as vectors
Starting with Chapter ?sec-vectors, we have been working with the dot product, an operation that combines two vectors to produce a scalar.
- length:
- included angle:
- projection onto
:
We used such operations to solve the target problem: finding the best approximation of a vector
As early as Block 1, we constructed functions as a linear combination of other functions, for example:
We will revisit the idea of linear combinations of functions using our new tools of length, included angle, and projection. To do this, we need to have a definition of the dot product suitable for application to functions.
33.1 Dot product for functions
Given two functions,
Suppose that our two functions are
Length:
Length:
Included angle:
Since , the angle is 45 degrees.
Project
The projection of
Thus,
The left panel of Figure 33.1 shows the functions
33.2 Sinusoids as vectors
The sinusoid is our fundamental model of periodic phenomena. To get started with using sinusoids as vectors, we will start with a simple setting: a single sinusoid of a specified frequency.
Figure 33.2 shows three sinusoids all with the same frequency, but shifted somewhat in time:
Since we have a dot product for functions, we can treat each of the three sinusoids as a vector. For instance, consider the length of waveforms A and B and the included angle between them.
## vector lengths
<- Integrate(waveA(t) * waveA(t) ~ t, bounds(t=0:1)) %>% sqrt()
lengthA
lengthA## [1] 0.7071068
<- Integrate(waveB(t) * waveB(t) ~ t, bounds(t=0:1)) %>% sqrt()
lengthB
lengthB## [1] 0.7071068
<- Integrate(waveC(t) * waveC(t) ~ t, bounds(t=0:1)) %>% sqrt()
lengthC
lengthC## [1] 0.7071068
## dot products
<- Integrate(waveA(t) * waveB(t) ~ t, bounds(t=0:1))
dotAB
dotAB## [1] -3.984443e-18
<- Integrate(waveA(t) * waveC(t) ~ t, bounds(t=0:1))
dotAC
dotAC## [1] -0.1545085
<- Integrate(waveB(t) * waveC(t) ~ t, bounds(t=0:1))
dotBC
dotBC## [1] -0.4755283
The cosine of the included angle
/ (lengthA * lengthB)
dotAB ## [1] -7.968886e-18
Since
The graphical presentation of orthogonality between waveforms A and B is easier to appreciate if we plot out the dot product itself: the integral of waveform A times waveform B. Figure 33.3 shows this integral using colors, blue for positive and orange for negative. The integral is zero, since the positive (blue) areas exactly equal the negative (orange) areas.
In contrast, waveform A is not orthogonal to waveform C, and similarly for waveform B. ?fig-AC-BC shows this graphically: the positive and negative areas in the two integrals do not cancel out to zero.
We can project waveform C onto the 2-dimensional subspace spanned by A and B. Since waveforms A and B are orthogonal, This can be done simply by projecting C onto each of A and B one at a time. Here’s a calculation of the scalar multipliers for A and for B and the model vector (that is, the component of C in the A-B subspace):
<- dotAC / lengthA^2
A_coef <- dotBC / lengthB^2
B_coef <- makeFun(A_coef*waveA(t) + B_coef*waveB(t) ~ t)
mod_vec # length of mod_vec
Integrate(mod_vec(t)*mod_vec(t) ~ t, bounds(t=0:1)) %>% sqrt()
## [1] 0.7071068
You can see that the length of the model vector is the same as the length of the vector being projected. This means that waveform C lies exactly in the subspace spanned by waveforms A and B.
A time-shifted sinusoid of frequency
Consider the function
<- makeFun(17.3 * sin(2*pi*5*(t-0.02)) ~ t)
g <- makeFun(sin(2*pi*5*t) ~ t)
sin5 <- makeFun(cos(2*pi*5*t) ~ t)
cos5 <- Integrate(g(t) * sin5(t) ~ t, bounds(t=0:1)) /
A_coef Integrate(sin5(t) * sin5(t) ~ t, bounds(t=0:1))
A_coef## [1] 13.99599
<- Integrate(g(t)*cos5(t) ~ t, bounds(t=0:1)) /
B_coef Integrate(cos5(t) * cos5(t) ~ t, bounds(t=0:1))
B_coef## [1] -10.16868
The amplitude of
sqrt(A_coef^2 + B_coef^2)
## [1] 17.3
The time delay involves the ratio of the two coefficients:
atan2(B_coef, A_coef) / (2*pi*5)
## [1] -0.02
For our purposes here, we will need only the Pythagorean sum and will ignore the time delay.
?fig-cello-seg (top) shows the waveform of a note played on a cello. The note lasts about 1 second. The bottom panel zooms in on the waveform, showing 82 ms (that is, 0.082 s).
The whole note starts with a sharp “attack,” followed by a long period called a “sustain,” and ending with a “decay.” Within the sustain and decay, the waveform is remarkably repetitive, seen best in the bottom panel of the figure.
If you count carefully in the bottom panel, you will see that the waveform completes 9 cycles in the 0.082 s graphical domain. This means that the period is 0.082 / 9 = 0.0091 s. The frequency
In modeling the cello waveform as a linear combination of sinusoids, the frequencies we use ought to respect the period of the cello vibration. Figure 33.4 shows the original waveform as well as the projection of the waveform onto a sinusoid with a frequency of 109.76 Hz. The figure also shows the residual from the projection, which is simply the original waveform minus the projected version.
The sinusoid with
As the number of harmonics increases, the approximation gets better and better.
Until now, all the plots of the cello waveform have been made in what’s called the time domain. That is, the horizontal axis of the plots has been time, as seems natural for a function of time.
The decomposition into sinusoids offers another way of describing the cello waveform: the frequency domain. In the frequency domain, we report the amplitude and phase of the projection onto each frequency, plotting that versus frequency. Figure 33.6 shows the waveform in the frequency domain.
From the amplitude graph in Figure 33.6, you can see that only a handful of frequencies account for almost all of the signal. Thus, the frequency domain representation is in many ways much more simple and compact than the time domain representation.
The frequency domain description is an important tool in many fields. As you will see in Block 6, models of many kinds of systems, from the vibrations of buildings during an earthquake, aircraft wings in response to turbulence, and the bounce of a car moving over a rutted road have a very simple form when stated in the frequency domain. Each sinusoid in the input (earthquake shaking, air turbulence, rutted road) gets translated into the same frequency sinusoid in the output (building movement, wing bending, car bound): just the amplitude and phase of the sinusoid is altered.
The construction of the frequency domain description from the waveform is called a Fourier Transform, one of the most important techiques in science.
33.3 Exercises
Exercise 33.03
Consider these functions/vectors on the domain
(that is, ) (that is, )
Plot out each of the functions on the domain. How many complete cycles does each function complete as
goes from 0 to 1?What is the length of each function?
All of the functions are mutually orthogonal except one. Which is the odd one out? (Hint: If the dot product is zero, the vectors are orthgonal.)
Exercise 33.05
Here is a square-wave function:
<- makeFun(ifelse(abs(t) <= 0.5, 1, 0) ~ t)
sq_wave slice_plot(sq_wave(t) ~ t, bounds(t=-1:1))
Find the projection of the square wave onto each of these functions. Use the domain
Hint: To find the scalar multiplier of projecting
<- Integrate(g(t)*f(t) ~ t, bounds(t=-1:1)) /
A Integrate(f(t)*f(t) ~ t, bounds(t=-1:1))
Then the projection of
Write down the scalar multiplier on each of the 8 functions above.
If you calculated things correctly, this is the linear combination of the 8 functions that best matches the square wave.