Notes on Fourier series
The trigonometric Fourier series is a beautiful mathematical theory that shows how to decompose a periodic function into an infinite sum of sinusoids. These are my notes on the subject, with some examples and the connection to linear algebra in Hilbert space. Let’s assume that is a well-behaved 2L -periodic [1] function and that we can find coefficients a_n and b_n such that: Then we say that the Fourier series on the right-hand side converges to . We’ll talk more about the assumptions mentioned above and convergence in the next section. Note that when n=0 , the sum becomes just ; therefore it’s customary to write the series starting with n=1 , with a separate constant component (which is the function's average over one period). To make computations nicer, this constant is typically called a_0 / 2 , so: Our goal is to find the coefficients a_n and b_n that satisfy this equation. We’ll do this in three steps. Step 1: Integrate both sides of the equation between -L and L [2] . Per Appendix A, all integrals within the sum are zero, so we’re left with: And thus we find : Step 2: Multiply both sides by cos\frac{m\pi x}{L} ( m is a positive integer constant) and integrate between -L and L . Looking at the right-hand side, the first integral is zero per Appendix A, and the last integral is zero per Appendix B. We’re left with: Per Appendix B, the integral on the right is zero for all n\neq m , and L for n=m . Therefore, we can write: Recall that m is an arbitrary integer, just like ; for consistency, we’ll replace m by and isolate a_n : Step 3: Hopefully it’s clear where this is going now; multiply both sides by sin\frac{m\pi x}{L} and integrate between -L and L . Using a very similar reasoning to step 2, we’ll end up with: We’ve just found a way to calculate all the coefficients of our Fourier series for : The previous section discusses Fourier series for a function that is well-behaved - but what does that mean? The full answer would lead us deep into analysis, which I’d like to avoid here. So I’ll keep it brief. We typically assume that is square integrable , which is denoted as L^2 . Moreover, we assume that the function is piecewise smooth : each segment of the function has continuous derivatives. A very simple example of a piecewise smooth function is f(x)=|x| . Another is the triangular wave function used in the example below. These conditions hold for pretty much any reasonable function we want to approximate using Fourier series, so they aren’t a serious burden. For a function that satisfies these conditions, it’s guaranteed to have a Fourier series that pointwise converges to it. This means that at every continuous point of , the Fourier series converges to it exactly; at every jump point, the Fourier series converges to the mid-point of the jump. Sometimes, additional properties of the function can help us simplify the Fourier series for it. If f_e(x) is an even function , then we know that: Because the function inside the integral is odd, and integrating an odd function over a symmetric interval results in 0. Therefore, the Fourier series for such f_e(x) is a cosine series : With coefficients and a_n given as before. Similarly if f_o(x) is an odd function, then its and a_n are 0, and its Fourier series is a sine series : So far we’ve been talking about 2L -periodic functions that can be faithfully represented by Fourier series. But what if we have a non-periodic function defined on a finite interval? E.g. suppose we have f(x)=x on the interval [0,L] . Can we approximate it with a Fourier series? Yes! First, we have to make a choice of how to extend the function to the negative interval [-L,0] . Then, we simply repeat the function every 2L - this is called a periodic extension . Note that the Fourier series calculation only cares about the range [-L,L] . The resulting series will approximate the generated periodic function in its entirety, and in particular will also converge to it in the [0,L] interval (except maybe the endpoints, depending on the mode of extension). There are several natural ways to extend a function defined on [0,L] into the interval [-L,0] [3] : Here’s an example of extending our sample function f(x)=x onto the full interval [-L,L] and then repeating it periodically every 2L : Note that the Fourier series for these extended functions will be different. However, they will all converge to in the interval [0,L] . Typically, even and odd extensions have the benefit of producing either cosine or sine series, correspondingly (as discussed in the previous section). We’ve seen that Fourier series work well for periodic functions and also non-periodic functions defined on a finite domain (because we can extend these periodically). But what about aperiodic functions defined on the entire real line? This is where we’ll have to leave Fourier series behind and move on to their generalization - the Fourier transform ; this will be a topic for a separate post. Let’s take the following triangular function t(x) [4] : t(x) is periodic with period 4. We can define it by starting with a formula on the interval [0,2] : Then making an odd extension into [-2,0] and repeating it periodically. Now we can go ahead to calculate its Fourier coefficients. Since this function is odd, we know that we’ll get a sine series , as a_n are going to be 0 for all . Let’s calculate b_n ; in our case L=2 (half the period). Since t(x) is odd and so is the sine, we’re integrating an even function over a symmetric interval. Therefore, we only have to integrate on the positive half of the range and multiply the result by two: Let’s set k=\frac{n\pi}{2} : And split up the integral for the different segments of t(x) : The first integral, by the method described in Appendix C: The second integral can also be split into two: The first of these is trivial to calculate; the second can once again use Appendix C. After some tedious but straightforward calculations [5] we’ll get: Adding I_1+I_2 , we get: Now let’s substitute k=\frac{n\pi}{2} back. This makes sin(2k) zero because the sine of an integer multiple of \pi is always zero: We have b_n , so the Fourier series for our t(x) is: Note that for even values of , sin \frac{n\pi}{2} is zero, so only the odd terms remain: Here’s an interactive chart showing how the series t(x) converges to our triangular function. You can set the number of terms in the Fourier series and see the effect (red line). Note that all even coefficients are zero so it will look the same for as for n-1 when is odd. We’ve written the Fourier series for as follows so far: We can rewrite this in a somewhat more compact form, using a single sinusoid with a configurable phase at each : Based on Appendix D, q_n and \theta_n can be computed as follows: When Fourier series are used in the context of signal processing, this formulation is easier to reason about because it represents the magnitude and phase shift of each harmonic of in the frequency domain [6] It should not come as a surprise that the Fourier series, being a combination of trigonometric functions, can also be represented with complex exponential functions. Specifically, we’ll show that our can be approximated as follows: Let’s calculate C_n . We proceed in a manner similar to before, by multiplying both sides of the equation by e^{-im\pi x/L} and taking an integral in the range [-L,L] : By Appendix A, the sum elements are all zero when n\neq m . When n=m , we get: Therefore, renaming m to (since it’s just an arbitrary integer constant): We’ve found an alternative formulation to Fourier series, using complex exponentials instead of trigonometric functions. While this was a direct derivation, another way to achieve the same result is to use the Euler Formula to derive: And substitute these into the original Fourier series formula. I’ll leave this as an exercise for the diligent reader; eventually, the result will be the same. Moreover, it’s possible to show a direct correspondence between a_n , b_n and C_n , for n>0 : Note that C_{-n}=C_n^* when both a_n and b_n are real (which is the case for a real-valued ). This helps explain why the complex formulation has negative frequencies in the sum; when the function is actually real, each negative frequency is paired up with a positive frequency and the result is real [7] : So, for a real function we only need to account for positive frequencies: We can take it further. C_n is a complex number, so let’s represent it in polar form as C_n=\frac{q_n}{2} e^{i\theta_n} (the factor of half will make sense soon). Then: And substituting back into the sum: This is precisely the compact formulation from the previous section! The most beautiful aspect of Fourier theory is that it doesn’t just happen to work by chance, and is deeply connected to linear algebra. Please read my post on Hilbert space before proceeding. The space of real-valued square integrable functions L^2 forms a Hilbert space, in which we can define the inner product (assuming real functions): We’ve demonstrated that the family of functions: Are all mutually orthogonal, because their pairwise inner products are zero! We’ve also shown that any function in L^2 can be represented as a weighted sum of these functions: So these functions form a basis for L^2 . When we think of these functions as vectors (in an infinite Hilbert space), much of what we did in this post starts feeling like "normal" linear algebra. For example, when we have a set of basis vectors and we want to know how to represent some vector in this basis, we usually find the coefficients by projecting it onto the basis. E.g. with a basis vector e_1 , the coefficient of : Similarly, when we calculate the coefficient b_n for some function , we project onto the basis vector sin\frac{n\pi x}{L} by calculating: From Appendix B, we know that the denominator is L , and we’ve just denoted: Which should look familiar! This is the core linear-algebra idea behind Fourier series: the functions 1 , cos\frac{n\pi x}{L} , and sin\frac{n\pi x}{L} play the role of orthogonal basis vectors, while the Fourier coefficients are coordinates of in this basis. The integral formulas for a_n and b_n are not mysterious tricks; they are projections, just like dot products with basis vectors in ordinary Euclidean space. Fourier series therefore let us decompose a function into independent orthogonal directions, much like decomposing a vector into its , , and z components. For any integer n\neq 0 and an arbitrary constant L, we have: Using these, we can calculate the integral of a complex exponential function for an integer n\neq 0 : We’ll start with the product of two sines, for any positive integers m and : Using the trigonometric identity for a product of sines, we can write: Now let’s focus on two different scenarios, m\neq n and m=n . If m\neq n , then each of the integrals constituting ss are 0 (see on Appendix A), so ss=0 . If m=n , then the second integral is still 0, but the first one isn’t: We can use exactly the same approach to show that: One more variant to cover: Since sine is an odd function and cosine is an even function, their product is an odd function. And the integral of an odd function over a symmetric interval is 0 (see this post for more details ). Let’s calculate the indefinite integral: For some constant k . We’ll use integration by parts: Here u=x , so du=dx . Also dv=sin(kx) , so v=-\frac{cos(kx)}{k} . Putting it together: Let’s take a general sinusoid with magnitude q , frequency and phase : We’re going to show that s(x) can be represented as a sum of a sine and a cosine with no phase. This is related to my earlier post on the sum of same-frequency sinusoids . Let’s start by expanding s(x) using a trigonometric identity: Now we’ll denote: a=q\cdot cos(\theta) and b=-q\cdot sin(\theta) , so: We have a and b in terms of q and , but what about the other way around? Let’s take the equations: Square both of them and add together: Now we’ll take the equations for b and a and divide one by the other: Where the atan2 function is careful to take into account the sign of both numerator and denominator. Also it’s worth mentioning that is determined up to additions of 2\pi . To conclude, for any q , and : With the aforementioned conversion formulas for a , b . The trigonometric Fourier series is a beautiful mathematical theory that shows how to decompose a periodic function into an infinite sum of sinusoids. These are my notes on the subject, with some examples and the connection to linear algebra in Hilbert space. Coefficients of Fourier series Let’s assume that is a well-behaved 2L -periodic [1] function and that we can find coefficients a_n and b_n such that: \[f(x)=\sum_{n=0}^{\infty}\left(a_n cos\frac{n\pi x}{L}+b_n sin\frac{n\pi x}{L}\right)\] Then we say that the Fourier series on the right-hand side converges to . We’ll talk more about the assumptions mentioned above and convergence in the next section. Note that when n=0 , the sum becomes just ; therefore it’s customary to write the series starting with n=1 , with a separate constant component (which is the function's average over one period). To make computations nicer, this constant is typically called a_0 / 2 , so: \[f(x)=\frac{a_0}{2}+\sum_{n=1}^{\infty}\left(a_n cos\frac{n\pi x}{L}+b_n sin\frac{n\pi x}{L}\right)\] Our goal is to find the coefficients a_n and b_n that satisfy this equation. We’ll do this in three steps. Step 1: Integrate both sides of the equation between -L and L [2] . \[\int_{-L}^{L}f(x)dx=\int_{-L}^{L}\frac{a_0}{2}dx+\sum_{n=1}^{\infty}\bigg (\int_{-L}^{L}a_n cos\frac{n\pi x}{L}dx+\int_{-L}^{L}b_n sin\frac{n\pi x}{L}dx\bigg )\] Per Appendix A, all integrals within the sum are zero, so we’re left with: \[\int_{-L}^{L}f(x)dx=\int_{-L}^{L}\frac{a_0}{2}dx=\bigg[\frac{x\cdot a_0}{2}\bigg]_{-L}^{L}=a_0\cdot L\] And thus we find : \[a_0=\frac{1}{L}\int_{-L}^{L}f(x)dx\] Step 2: Multiply both sides by cos\frac{m\pi x}{L} ( m is a positive integer constant) and integrate between -L and L . \[\begin{aligned} \int_{-L}^{L}f(x)cos\frac{m\pi x}{L}dx&=\int_{-L}^{L}\frac{a_0}{2}cos\frac{m\pi x}{L}dx\\ &+\sum_{n=1}^{\infty}\bigg (\int_{-L}^{L}a_n cos\frac{n\pi x}{L}cos\frac{m\pi x}{L}dx+\int_{-L}^{L}b_n sin\frac{n\pi x}{L}cos\frac{m\pi x}{L}dx\bigg ) \end{aligned}\] Looking at the right-hand side, the first integral is zero per Appendix A, and the last integral is zero per Appendix B. We’re left with: \[\int_{-L}^{L}f(x)cos\frac{m\pi x}{L}dx=\sum_{n=1}^{\infty}\int_{-L}^{L}a_n cos\frac{n\pi x}{L}cos\frac{m\pi x}{L}dx\] Per Appendix B, the integral on the right is zero for all n\neq m , and L for n=m . Therefore, we can write: \[\int_{-L}^{L}f(x)cos\frac{m\pi x}{L}dx=a_m\cdot L\] Recall that m is an arbitrary integer, just like ; for consistency, we’ll replace m by and isolate a_n : \[a_n=\frac{1}{L}\int_{-L}^{L}f(x)cos\frac{n\pi x}{L}dx\] Step 3: Hopefully it’s clear where this is going now; multiply both sides by sin\frac{m\pi x}{L} and integrate between -L and L . Using a very similar reasoning to step 2, we’ll end up with: \[b_n=\frac{1}{L}\int_{-L}^{L}f(x)sin\frac{n\pi x}{L}dx\] We’ve just found a way to calculate all the coefficients of our Fourier series for : \[f(x)=\frac{a_0}{2}+\sum_{n=1}^{\infty}\left(a_n cos\frac{n\pi x}{L}+b_n sin\frac{n\pi x}{L}\right)\] Where: \[\begin{aligned} a_0&=\frac{1}{L}\int_{-L}^{L}f(x)dx\\ a_n&=\frac{1}{L}\int_{-L}^{L}f(x)cos\frac{n\pi x}{L}dx\\ b_n&=\frac{1}{L}\int_{-L}^{L}f(x)sin\frac{n\pi x}{L}dx \end{aligned}\] Conditions on f and convergence of Fourier series The previous section discusses Fourier series for a function that is well-behaved - but what does that mean? The full answer would lead us deep into analysis, which I’d like to avoid here. So I’ll keep it brief. We typically assume that is square integrable , which is denoted as L^2 . Moreover, we assume that the function is piecewise smooth : each segment of the function has continuous derivatives. A very simple example of a piecewise smooth function is f(x)=|x| . Another is the triangular wave function used in the example below. These conditions hold for pretty much any reasonable function we want to approximate using Fourier series, so they aren’t a serious burden. For a function that satisfies these conditions, it’s guaranteed to have a Fourier series that pointwise converges to it. This means that at every continuous point of , the Fourier series converges to it exactly; at every jump point, the Fourier series converges to the mid-point of the jump. Cosine and Sine series Sometimes, additional properties of the function can help us simplify the Fourier series for it. If f_e(x) is an even function , then we know that: \[b_n=\frac{1}{L}\int_{-L}^{L}f(x)sin\frac{n\pi x}{L}dx=0\] Because the function inside the integral is odd, and integrating an odd function over a symmetric interval results in 0. Therefore, the Fourier series for such f_e(x) is a cosine series : \[f_e(x)=\frac{a_0}{2}+\sum_{n=1}^{\infty}a_n cos\frac{n\pi x}{L}\] With coefficients and a_n given as before. Similarly if f_o(x) is an odd function, then its and a_n are 0, and its Fourier series is a sine series : \[f_o(x)=\sum_{n=1}^{\infty}b_n sin\frac{n\pi x}{L}\] Fourier series for a non-periodic function defined on an interval So far we’ve been talking about 2L -periodic functions that can be faithfully represented by Fourier series. But what if we have a non-periodic function defined on a finite interval? E.g. suppose we have f(x)=x on the interval [0,L] . Can we approximate it with a Fourier series? Yes! First, we have to make a choice of how to extend the function to the negative interval [-L,0] . Then, we simply repeat the function every 2L - this is called a periodic extension . Note that the Fourier series calculation only cares about the range [-L,L] . The resulting series will approximate the generated periodic function in its entirety, and in particular will also converge to it in the [0,L] interval (except maybe the endpoints, depending on the mode of extension). There are several natural ways to extend a function defined on [0,L] into the interval [-L,0] [3] : Direct periodic repetition: we simply repeat every L : f(x+L)=f(x)\ \forall x . Even extension: f(|x|) Odd extension: when x\ge 0 and -f(-x) when x<0 .