A105 of 28
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A10 · Lesson 5 · Lab: Fourier series in code

Coefficients, synthesis, Gibbs and Parseval by computer

Lessons 1 to 4 found Fourier coefficients by integrating by hand. A computer does those integrals numerically for any waveform, including measured ones with no formula. In this lab you'll compute a square wave's coefficients, rebuild it from its harmonics and measure the Gibbs overshoot that never goes away, confirm Parseval's theorem by summing ten thousand terms, compute total harmonic distortion, and filter the square wave harmonic by harmonic.

  • The curve is the sum of a ±1 square wave's first N odd harmonics, ∑4πnsin⁡(2πnt).
  • Add harmonics: the edges sharpen and the ripple crowds toward them, but the first overshoot stays at about 9% of the jump.
  • That's the Gibbs phenomenon: a finite sum can't follow a jump, and the overshoot's height doesn't shrink, only its width.

Predict

The partial sums of a ±1 square wave's series overshoot the jump. As you add more and more harmonics, the peak of the overshoot:

Name it

Coefficients by integration

For a period T sampled finely, the sine coefficients are

bn=2T∫0Tx(t)sin⁡2πntTdt

and the trapezoid rule (np.trapezoid) evaluates the integral. For a measured waveform the same sum works on the samples, and that's what an FFT does efficiently for all n at once.

Synthesis

Adding ∑bnsin⁡(2πnt/T) up to N terms rebuilds the waveform. Smooth waveforms converge quickly; ones with jumps converge slowly and show the Gibbs overshoot of about 9% of the jump.

Power and distortion

Parseval's theorem: the mean square is a02+∑an2+bn22. Total harmonic distortion compares everything above the fundamental with the fundamental:

THD=∑n≥2An2A1

For a square wave it's π28−1=48.3%.

Filtering harmonic by harmonic

A linear filter multiplies each harmonic by |H(nf0)| and shifts its phase. Low-pass filtering a square wave cuts the higher harmonics more, rounding the corners.

Worked example

A ±1 V square wave at 1 kHz passes through an RC low-pass with its corner at 1 kHz. How do the 3rd harmonic and the fundamental compare at the output?

Show the solution
  1. At the input. The 3rd harmonic is 13 of the fundamental.
  2. The filter's gains. The filter passes the fundamental with gain 12 and the 3rd with 110.
  3. At the output. V3V1=13×1/101/2=130.2=0.149 The output looks much more like a sine than the input does, though a single pole is a gentle filter.

Answer at the output the 3rd harmonic is 0.149 of the fundamental, down from a third at the input.

Worked example

What is the THD of a ±1 square wave?

Show the solution
  1. The harmonics. They are 4πn for odd n, and the total mean square is 1.
  2. The fundamental's share. 12(4π)2=8π2=0.811
  3. The harmonics' share. The rest is in the harmonics: 1−0.811=0.189
  4. The THD. THD=0.1890.811=π28−1=0.483
  5. What it means. A function generator's square output is 48% distortion by this measure, which is why "THD" is only meaningful for something meant to be a sine.

Answer the THD is 0.483, or 48%.

Check

A ±1 square wave at 1 kHz passes through an RC low-pass with its corner at 2 kHz. What is the ratio of the output's 3rd harmonic to its fundamental?

Check

A ±1 square wave is rebuilt from its odd harmonics up to and including 5. What RMS error remains?

Check

For a ±1 square wave of period 1, sampled at 200 001 points, compute b1, b2 and b3 by the trapezoid rule and set b. (The wave is np.sign(np.sin(2 * np.pi * t)).)

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Sum the first 200 odd harmonics (n up to 399) of the ±1 square wave on 40 001 points over 0≤t≤0.02 and set peak to the largest value.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Check Parseval's theorem: set total to ∑bn22 over the square wave's odd harmonics up to n = 20 001, which should approach the mean square, 1.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write thd(amps) returning the total harmonic distortion of a list of harmonic amplitudes (fundamental first). Set t_sq for the square wave's odd harmonics up to n = 9999 (with zeros for the even ones), and exact to π2/8−1.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Filter the square wave through an RC low-pass with its corner at the fundamental: multiply each odd harmonic n (up to 999) by 11+n2. Set ratio3 to the output's 3rd harmonic over its fundamental and thd_out to the output's THD.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why doesn't the Gibbs overshoot shrink as you add more harmonics, even though the series converges?

Practice

Keep practicing

4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.