Lecture 14: Fourier Series Examples • Last time: If we represent a function f (t) that is periodic on T as a fourier series: f (t) = A0 +
∞ X n=1
2πn 2πn t + Bn sin t An cos T T
(1)
then we derived formulas for the amplitudes of the sinusoids in the fourier series: 2ZT 2πn Bn = f (t) sin t dt (2) T 0 T 2πn 2ZT f (t) cos t dt (3) An = T 0 T 1ZT f (t)dt (4) A0 = T 0 • As an example, we found the amplitudes for the square wave: f (t) =
F0 0≤t≤π −F0 π < t ≤ 2π repeat
(5)
Here were the amplitudes we found: A0 = 0 An = 0 0 Bn = 2F (1 − (−1)n ) πn This last one can be simplified a bit to read: Bn = 0 for n even and 0 Bn = 4F for n odd. πn • So our Fourier series for the square wave can be written: f (t) =
∞ X n=1, n odd
4F0 sin nt nπ
(6)
• Today we will investigate how this sum gives us the square wave. We will also try to develop some intuition about why A0 and An were 0 in this example, and we will do another example. 1
Plotting the Fourier series for the square wave • The python program fourier squarewave.py plots the square wave defined in equation (5) over 2 whole periods and also plots the fourier series in equation (6) from n = 1 to N where you can specify N . Although the fourier series only exactly represents the square wave when N = ∞, the fact that our coefficients Bn are proportional to n−1 suggests that the higher n terms will be small relative to the lower n terms. • First lets start by setting N = 1. This means we are only taking the first sinusoid in the sum. Here is a plot of the results: 1.5 1.0
Fourier series for square wave with 1 components Square wave Fourier series
f(t)
0.5 0.0 0.5 1.0 1.50.0
0.5
1.0
1.5
2.0 time/pi
2.5
3.0
3.5
4.0
Although the single term doesn’t do well in getting the small details right, it does do well in getting the general large scale structure right. For example, notice that the square wave and the fourier series are both position in the same regions and negative in the same regions. However we do notice that the single sinusoid underestimates the magnitude of f (t) in three regions during a single period: near 0, π, and 2π and overestimates it in 2 regions in a single period: around π/2 and 3π/2. In order to ‘fix’ these under and over estimates (which are shorter in scale than our first sinusoid), we will need a sinusoid with a higher frequency. • So lets run the python program again with N = 2 to include the first and second terms in the sum. Here is what we get:
2
1.5 1.0
Fourier series for square wave with 2 components Square wave Fourier series
f(t)
0.5 0.0 0.5 1.0 1.50.0
0.5
1.0
1.5
2.0 time/pi
2.5
3.0
3.5
4.0
Compared to the previous picture we are doing a little better here. Adding this second sinusoid kind of ‘flattened’ the first sinusoid near its maximum and minimum and therefore made it closer in resemblance to the square wave. Also notice that the number of regions where over and under estimates occur increases, but the width of the regions decreases (i.e. the ‘errors’ are getting smaller in scale. For example, in a single period, I count 6 regions where the magnitude of the function is underestimated and 4 regions where it is overestimated (compare this to the 3 regions of overestimation and 2 regions of underestimation when N = 1). • As we continue to increase N , this trend with continue. The superposition of sinusoids will continue to ‘flatten’ where they should and the scale of the errors will decrease. For example, here is the plot when N = 10 (I recommend trying others yourself): 1.5 1.0
Fourier series for square wave with 10 components Square wave Fourier series
f(t)
0.5 0.0 0.5 1.0 1.50.0
0.5
1.0
1.5
2.0 time/pi
2.5
3.0
3.5
3
4.0
• We are doing a pretty good job now of representing the square wave with sinusoids. Notice that there are distinct places where the errors are worst: at t = 0, π and 2π. Notice these are the regions where the square wave abruptly changes value (there is a discontinuity at these places). Because sinusoids are SOOOO good at being continuous and forever differentiable, it is actually hard for them to represent discontinuities, which is why you get the largest errors at the discontinuities. Also notice that the amplitudes of the error in these locations is NOT decreasing (although it is elsewhere). This is known as the “Gibbs Phenomenon” and happens near discontinuities when using Fourier series. • Lets try a big N value, say N = 100. Then we get: 1.5 1.0
Fourier series for square wave with 100 components Square wave Fourier series
f(t)
0.5 0.0 0.5 1.0 1.50.0
0.5
1.0
1.5
2.0 time/pi
2.5
3.0
3.5
4.0
Now we really are doing well except for near the discontinuities. However, notice that the WIDTH of the errors near the discontinuities continue to shrink (even if their height doesn’t). This means they can’t affect the dynamics much. In the limit that N → ∞, the width is 0 and therefore, they don’t affect anything at all. • Just for fun, and because we would never do it without a computer program, lets try N = 10000:
4
1.5 1.0
Fourier series for square wave with 10000 components Square wave Fourier series
f(t)
0.5 0.0 0.5 1.0 1.50.0
0.5
1.0
1.5
2.0 time/pi
2.5
3.0
3.5
4.0
Notice that it now looks like the Gibbs phenomenon is gone, but this is just because our time increments are larger than the width of the Gibbs error, so we don’t see it (the program misses plotting the points where the Gibbs phenomena occur). • You can use this python program to plot the Fourier series components of any function as long as you know the formula for the amplitudes of the sinusoids.
Some Intuition About the Amplitudes • You might be wondering if it was just a coincidence that the A0 and An amplitudes were 0 for the square wave. In fact, it was not a coincidence and there is an easy way to predict whether a whole set of amplitudes (like all the An ’s or all the Bn ’s will be 0 for specific functions (which is nice because then you don’t have to do the integral to find that all the coefficients are 0). • It turns out that it all depends on the symmetry of the function. Specifically on whether the function is “odd” or “even” about t = T /2 (i.e. about the midpoint of the integral bounds). So first lets define these concepts: • An EVEN function about the point T /2 satisfies: f (t) = f (T − t) for all t in 0 → T . An ODD function about the point T /2 satisfies f (t) = −f (T − t) for all t in 0 → T . Its also possible that a function is neither even nor odd, in which case, we will just say it is “asymmetric”. 5
• You are actually very familiar with even and odd functions. Sines are all odd functions and cosines are all even functions. For example, for trig functions with period 2π: cos(θ) = cos(2π − θ)
and
sin(θ) = − sin(2π − θ)
• It turns out that if you are trying to find the Fourier series for an ODD function, you only need the ODD sinusoids (i.e. the sines) to do it. The even ones will just mess stuff up because they don’t have the right symmetry. Similarly, if you are trying to find the Fourier series for an EVEN function, you only need the EVEN sinusoids (i.e. the cosines) to do it. • Notice that the square wave we did is an ODD function. This means we only needed the odd sinusoids to represent it. This is why the An ’s were all 0. • What about A0 ? Lets look at the definition of A0 more closely: 1ZT f (t)dt A0 = T 0
(7)
If I asked you to calculate the AVERAGE of f (t) over its period, you would do so using exactly this formula for A0 . So the A0 coefficient represents the average of the function. This makes sense because it is the coefficient of the 0th frequency sinusoid which is a constant, so essentially, its trying to approximate the function with a single constant. If you only have a single constant to approximate a function, the best constant would be the average of the function. • So in our square wave example, because the average of the function was 0, it makes sense that A0 = 0. If you notice right off the bat that the average of your function is 0, then A0 = 0. If you notice the average of your function is something else, then A0 is whatever that something else is. • Lets do another example to make sure we understand the process for calculating Fourier coefficients.
6
Another Fourier Series example: The Sawtooth Wave • Consider the function f (t) = t for 0 ≤ 2π and then repeats. So the function looks like this over 2 periods: Sawtooth
7 6 5
f(t)
4 3 2 1 00.0
0.5
1.0
1.5
2.0 time/pi
2.5
3.0
3.5
4.0
• Lets calculate the Fourier series coefficients: Starting with A0 : A0 =
1ZT 1 Z 2π 1 1 2 2π 1 f (t)dt = tdt = t = (2π)2 = π T 0 2π 0 2π 2 0 4π
Note: We could have figured this out by inspection by noting that the average of the function is π. Now for An : 2 Z 2π 2ZT f (t) cos ntdt = t cos ntdt An = T 0 2π 0 Using integration by parts: "
#
Z 2π 2π 1 t 1 An = sin nt − sin ntdt π n n 0 0
2π 1 1 = 0 + 2 cos nt =0 π n 0 "
#
It might seem strange that An = 0 but if you remove the average value from the function (i.e. shift the function down by its average value 7
everywhere) then what you are left with is indeed an ODD function, and hence we only need ODD fourier components so An = 0 except for the A0 term we found above. Finally calculating Bn : 2 Z 2π 2ZT f (t) sin ntdt = t sin ntdt Bn = T 0 2π 0 Using integration by parts: "
#
Z 2π 2π 1 −1 t Bn = − cos nt − cos ntdt π n n 0 0
2π 2 1 1 2π =− cos(2πn) − 0 − 2 sin nt =− π n n n 0 #
"
• So our Fourier series expansion of the sawtooth wave is: f (t) = π +
∞ X −2
n
n=1
sin nt
(8)
• We can use the python program fourier sawtooth.py to make similar plots as we did above for the square wave: • N = 0 component: If we ONLY include the A0 term, then we get the average right but none of the details: 7 6
Fourier series for Sawtooth with 0 components Sawtooth Fourier series
5
f(t)
4 3 2 1 00.0
0.5
1.0
1.5
2.0 time/pi
2.5
3.0
3.5
8
4.0
• N = 1 component (plus A0 ): getting the greater than and less than the average regions right: 7 6
Fourier series for Sawtooth with 1 components Sawtooth Fourier series
5
f(t)
4 3 2 1 00.0
0.5
1.0
1.5
2.0 time/pi
2.5
3.0
3.5
4.0
• N = 2 component (plus A0 ): fixing some of the over and under estimates: 7 6
Fourier series for Sawtooth with 2 components Sawtooth Fourier series
5
f(t)
4 3 2 1 00.0
0.5
1.0
1.5
2.0 time/pi
2.5
3.0
3.5
4.0
• and some more N ’s below. Notice that the Gibbs phenomenon occurs at the discontinuity in this example as well:
9
7
7 6
5
5
4
4
3
3
f(t)
f(t)
6
Fourier series for Sawtooth with 10 components Sawtooth Fourier series
2
2
1
1
0
0
10.0
7 6
0.5
1.0
1.5
2.0 time/pi
2.5
3.0
3.5
4.0
Fourier series for Sawtooth with 10000 components Sawtooth Fourier series
5
f(t)
4 3 2 1 00.0
0.5
1.0
1.5
2.0 time/pi
2.5
3.0
3.5
10
4.0
10.0
Fourier series for Sawtooth with 100 components Sawtooth Fourier series
0.5
1.0
1.5
2.0 time/pi
2.5
3.0
3.5
4.0