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

clip_image002[12]

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.

clip_image002[10]

clip_image002[16]

clip_image002[14]

clip_image004

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:

figure_1

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:

http://en.wikipedia.org/wiki/Fourier_series
http://en.wikipedia.org/wiki/Square_wave

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

figure_1

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.

 

Enjoy!

9 comments:

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

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

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi there!
    Good stuff, thank you!
    I'm currently learning to translate mathematical notation to working algorithms for my master's thesis, so your code was a first step and a huge help.

    Just one question that came up though: If you cycle from 1 to 10 in line 56, don't you get 9 harmonics instead of 10? Not trying to be a smartass here btw, I'm just trying to grasp this.. ^^
    And I'll definitely remember the modulo trick from line 41, too. ;-)

    ReplyDelete
    Replies
    1. You get 9 armonics in the cycle but before you have got a0, so it's 10 armonics in total.

      Delete
  4. This make my eyes bleed:

    if one == 1:
    one = -1
    negativeOne = 1
    else:
    one = 1
    negativeOne = -1

    ReplyDelete
  5. I used your code, but I wrote a shorter square wave function that handles the discontinuities more consistently (unless I missed something):
    def squareWave(x):
    global T
    x=x/(T/2)
    if math.remainder(math.ceil(x),2):
    return 1
    else:
    return -1

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. could someone help me with the code for f(t) = t^2+2t+3 please?

    ReplyDelete