Wednesday, 22 April 2015

Fourier series and square wave approximation

Fourier series is one of the most intriguing series I have met so far in mathematics. Roughly speaking it is a way to represent a periodic function using combinations of sines and cosines. There are many other fascinating topics such as the Laplace and Fourier transforms but I am new to complex analysis and techniques so I’ll go step by step!

There are many reasons why I find Fourier series fascinating but primarily, I like the fact of approximating functions using the sines, cosines and complex exponentials because of the bond between these functions.

Now, what are the ingredients we need to approximate a function with a Fourier series?

1) A periodic bounded function, with period T
2) The function should be integrable over the period

If the above conditions are met, then we can write the function as follows


Note that this is an infinite sum, the more terms you add, better the approximation. As far as the coefficients are concerned, you can calculate them using the formulas below.





For this simple test, I chose the square wave function, which could be interpreted as an on and off signal. The aim of this simple test was to check how good is the approximation. Using only 10 armonics (n=10) this is the result I got:


I would say it is pretty accurate, Python took only a few seconds to calculate it. Furthermore you can use more complex periodic functions and found similar amazing results. Here are some useful resources I used:

If you have time, perhaps you could try plot the sawtooth wave


In the square wave Wikipedia page there are other kind of wave functions, perhaps in the future I’ll try them out too. Below is the Python code I used to plot the square wave.




  1. What is "n_max" in the code given above for square wave approximation

    1. Hi, n_max is the number of harmonics used.

  2. This comment has been removed by the author.