Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Follow publication

Member-only story

Representing a Square Wave With a Fourier Series and Python

--

Suppose you want to make a periodic wave — maybe it’s for a music synthesizer or something. Whatever you need the wave for, it turns out the easiest wave to make is a sine (or cosine) wave. You can get a sine wave by taking only the vertical component of a point on a circle rotating at a constant angular velocity. Like this.

Yes, I made this animation with Glowscript — here is the code in case you want it. But what if you want to make a different period function. Let’s say you want to make a square wave. It looks like this.

Well, here is the cool thing. You can make this square wave with a bunch of sine and cosine functions. Yup. This is the whole idea behind the Fourier series.

If I have a period function f(t), then the following is true.

This is an infinite sum of sines and cosines with different frequencies and different amplitudes. The frequencies are easy — just integer multiples of ω. For the amplitudes, they can be calculated as:

Here τ is the period of the periodic function where:

--

--

Written by Rhett Allain

Physics faculty, science blogger of all things geek. Technical Consultant for CBS MacGyver and MythBusters. WIRED blogger.

No responses yet

Write a response