---
title: "Harmonics lab: line currents, power factor and rectifiers"
heading: Measuring harmonics and THD from one period of samples, separating displacement from distortion in the power factor, the six-pulse bridge with commutation overlap, the twelve-pulse bridge's cancellation, and simulating a capacitor-input rectifier
eyebrow: S11 · Lesson 32 · Harmonics lab
xp: 60
---

::: hook
A power analyzer on a rectifier's input samples the line voltage and current and reports P, S, power factor, THD and a bar chart of harmonics. Lessons 29 to 31 derived those quantities for idealized waveforms. In this lab you'll write the analyzer, check it against the lessons' examples, then point it at waveforms that have no tidy formula: a twelve-pulse rectifier's stepped current and a capacitor-input rectifier's narrow charging pulses.
:::

::: play
- The curve is the distortion factor of a current made of rectangular pulses centered on the voltage peaks, against their total width per half cycle (lesson 29's first example): $\frac{2\sqrt2}{\pi}\sin(\theta/2)/\sqrt{\theta/\pi}$.
- A full 180° square wave reaches 0.900; pulses 60° wide give 0.780.
- Narrow pulses, like a capacitor-input rectifier's, push the factor below 0.5 even though the current is perfectly in phase with the voltage.
:::

```sim plot
view: graph
x: t = 10..180
y: D = 0..1
plot: 2*sqrt(2)/pi*sin(t*pi/360)/sqrt(t/180) | label: distortion factor
```

::: predict
## A 230 V, 50 Hz bridge rectifier charges 330 µF and feeds a 400 Ω load (about 250 W). What power factor does it present to the line?

- [ ] About 0.95
- [ ] About 0.78
- [x] About 0.42
- [ ] About 0.1

::: reveal
The capacitor holds the output within 21 V of the 325 V peak, so the diodes conduct only from 69.3° to 91.4° of each half cycle: 22° wide pulses peaking at 12.7 A to deliver the load's 0.79 A average current. The line current is 2.59 A RMS for 248 W, so PF = 248/(230 × 2.59) = 0.417. Task 4 finds this by simulation; it's the reason regulations push anything above about 75 W toward PFC.
:::
:::

::: nameit
## What the lab computes

- **Harmonics from samples**: for N samples $x_n$ covering exactly one period, the DFT $X_h = \sum x_ne^{-j2\pi hn/N}$ gives each harmonic's RMS value $\sqrt2|X_h|/N$ (and the DC term $|X_0|/N$). THD is the RMS of harmonics 2 and up over the fundamental's.
- **Power terms**: $P = \frac1N\sum v_ni_n$, $S = V_\text{rms}I_\text{rms}$, PF = P/S; the displacement factor is the cosine of the angle between the voltage and current fundamentals, and the distortion factor $I_{1,\text{rms}}/I_\text{rms}$.
- **Six-pulse bridge with overlap**: $V_{dc} = \frac{3\sqrt2}{\pi}V_{LL}\cos\alpha - \frac3\pi\omega L_sI_d$, with the overlap angle μ from
$$\cos\alpha - \cos(\alpha + \mu) = \frac{2\omega L_sI_d}{\sqrt2V_{LL}}$$
- **Twelve-pulse bridge**: the Y-fed bridge's line current is a 120° block; the Δ-fed bridge's, seen at the primary, is a stepped wave $[b(\theta - 30°) - b(\theta - 150°)]/\sqrt3$, built from the same block b. Their sum has no 5th or 7th harmonic.
- **Capacitor-input rectifier**: while the diodes conduct, the capacitor follows $|v_g|$ and the line supplies $C\,d|v_g|/dt + |v_g|/R$; conduction stops when that would go negative, at $\theta = \pi - \arctan(\omega RC)$, and the capacitor then discharges through R until the rising line catches it.
:::

::: check
kind: python

## Write `harmonics(x)`: the RMS value of each harmonic h = 0, 1, …, N/2 − 1 of one period of N samples (h = 0 is the DC value), and `thd(x)`.

```python starter
def harmonics(x):
    return [0.0] * (len(x) // 2)

def thd(x):
    return 0.0
```

```python solution
import cmath, math

def harmonics(x):
    N = len(x)
    out = []
    for h in range(N // 2):
        X = sum(x[n] * cmath.exp(-2j * math.pi * h * n / N) for n in range(N))
        out.append(abs(X) / N if h == 0 else math.sqrt(2) * abs(X) / N)
    return out

def thd(x):
    a = harmonics(x)
    return math.sqrt(sum(v * v for v in a[2:])) / a[1]
```

```python tests
import math
N = 256
x = [math.sqrt(2) * (5 * math.cos(2 * math.pi * n / N - math.pi / 6) + 2 * math.cos(6 * math.pi * n / N) + 1.5 * math.cos(10 * math.pi * n / N)) + 0.4 for n in range(N)]
a = harmonics(x)
assert abs(a[1] - 5) < 1e-9 and abs(a[3] - 2) < 1e-9 and abs(a[5] - 1.5) < 1e-9, "5, 2 and 1.5 A RMS."
assert abs(a[0] - 0.4) < 1e-9, "The DC term is the plain average."
assert abs(thd(x) - 0.5) < 1e-9, "THD = √(4 + 2.25)/5 = 0.5."
```

::: right
Right. With exactly one period of samples, each harmonic lands in its own DFT bin with no leakage.
:::
:::

::: check
kind: python

## Write `power_terms(v, i)` for one period of samples: return (P, S, PF, displacement factor, distortion factor).

```python starter
def power_terms(v, i):
    return 0.0, 0.0, 1.0, 1.0, 1.0
```

```python solution
import cmath, math

def power_terms(v, i):
    N = len(v)
    P = sum(a * b for a, b in zip(v, i)) / N
    Vr = math.sqrt(sum(a * a for a in v) / N)
    Ir = math.sqrt(sum(b * b for b in i) / N)
    V1 = sum(v[n] * cmath.exp(-2j * math.pi * n / N) for n in range(N))
    I1 = sum(i[n] * cmath.exp(-2j * math.pi * n / N) for n in range(N))
    disp = math.cos(cmath.phase(V1) - cmath.phase(I1))
    dist = math.sqrt(2) * abs(I1) / N / Ir
    return P, Vr * Ir, P / (Vr * Ir), disp, dist
```

```python tests
import math
N = 360
v = [120 * math.sqrt(2) * math.cos(2 * math.pi * n / N) for n in range(N)]
i = [math.sqrt(2) * (5 * math.cos(2 * math.pi * n / N - math.pi / 6) + 2 * math.cos(6 * math.pi * n / N) + 1.5 * math.cos(10 * math.pi * n / N)) for n in range(N)]
P, S, PF, disp, dist = power_terms(v, i)
assert abs(P - 519.6) < 0.1 and abs(S - 670.8) < 0.1, "Lesson 29's example: 520 W and 671 VA."
assert abs(PF - 0.7746) < 1e-3 and abs(disp - 0.8660) < 1e-4 and abs(dist - 0.8944) < 1e-4, "PF 0.775 = 0.894 × 0.866."
```

::: right
Right. The power factor splits cleanly into a phase part and a shape part only when the voltage is sinusoidal; then the current's harmonics carry no power.
:::
:::

::: check
kind: python

## Write `six_pulse(VLL, alpha_deg, Ls, Id, f)` returning (Vdc, overlap angle μ in degrees), and `twelve_pulse_current(theta_deg)`: the primary line current of a twelve-pulse rectifier for $I_d$ = 1, the Y bridge's block plus the Δ bridge's stepped wave.

```python starter
def six_pulse(VLL, alpha_deg, Ls, Id, f):
    return 0.0, 0.0

def twelve_pulse_current(theta_deg):
    return 0.0
```

```python solution
import math

def six_pulse(VLL, alpha_deg, Ls, Id, f):
    w = 2 * math.pi * f
    a = math.radians(alpha_deg)
    Vdc = 3 * math.sqrt(2) / math.pi * VLL * math.cos(a) - 3 / math.pi * w * Ls * Id
    c = math.cos(a) - 2 * w * Ls * Id / (math.sqrt(2) * VLL)
    return Vdc, math.degrees(math.acos(c)) - alpha_deg

def block(th):
    th %= 360
    if 30 < th < 150:
        return 1.0
    if 210 < th < 330:
        return -1.0
    return 0.0

def twelve_pulse_current(theta_deg):
    return block(theta_deg) + (block(theta_deg - 30) - block(theta_deg - 150)) / math.sqrt(3)
```

```python tests
import cmath, math
Vdc, mu = six_pulse(480, 30, 0.2e-3, 100, 60)
assert abs(Vdc - 554.1) < 0.1, "Lesson 30's example: 554 V."
assert abs(mu - 2.46) < 0.01, "About 2.5° of overlap."
N = 1440
x = [twelve_pulse_current((n + 0.5) * 360 / N) for n in range(N)]
H = lambda h: abs(sum(x[n] * cmath.exp(-2j * math.pi * h * (n + 0.5) / N) for n in range(N)))
assert H(5) < 1e-9 * H(1) and H(7) < 1e-9 * H(1), "No 5th or 7th."
assert abs(H(11) / H(1) - 1 / 11) < 0.002, "The 11th remains, at about 1/11."
```

::: right
Right. The Δ bridge's 5th and 7th harmonics arrive inverted relative to the Y bridge's, while their fundamentals add.
:::
:::

::: check
kind: python

## Write `cap_rectifier(Vrms, f, C, R)`: the steady state of an ideal bridge rectifier into C with load R, returning (power factor, peak-to-peak ripple in volts, conduction angle per half cycle in degrees). Any accurate method will do: an exact event solution or fine time steps.

```python starter
def cap_rectifier(Vrms, f, C, R):
    return 0.9, 0.0, 180.0
```

```python solution
import math

def cap_rectifier(Vrms, f, C, R):
    Vp = Vrms * math.sqrt(2)
    w = 2 * math.pi * f
    a = w * R * C
    toff = math.pi - math.atan(a)
    g = lambda p: math.sin(p) - math.sin(toff) * math.exp(-(p + math.pi - toff) / a)
    lo, hi = 0.0, math.pi / 2
    for _ in range(100):
        m = (lo + hi) / 2
        if g(m) < 0:
            lo = m
        else:
            hi = m
    ton = (lo + hi) / 2
    n = 4000
    h = (toff - ton) / n
    I2 = P = 0.0
    for k in range(n):
        th = ton + (k + 0.5) * h
        i = Vp * (w * C * math.cos(th) + math.sin(th) / R)
        I2 += i * i * h
        P += Vp * math.sin(th) * i * h
    Ir = math.sqrt(I2 / math.pi)
    PF = P / math.pi / (Vrms * Ir)
    return PF, Vp - Vp * math.sin(ton), math.degrees(toff - ton)
```

```python tests
PF, dv, cond = cap_rectifier(230, 50, 330e-6, 400)
assert abs(PF - 0.4168) < 0.003, "About 0.417."
assert abs(dv - 21.0) < 0.2 and abs(cond - 22.08) < 0.3, "21 V of ripple, 22° of conduction."
PF2, dv2, cond2 = cap_rectifier(120, 60, 1000e-6, 50)
assert abs(PF2 - 0.4914) < 0.003 and abs(cond2 - 32.45) < 0.3, "A heavier load conducts longer: PF 0.49."
```

::: right
Right. A bigger capacitor lowers the ripple but narrows the pulses further, so the power factor gets worse, not better.
:::
:::

::: explain
## Why does a capacitor-input rectifier have a poor power factor even though its current is in phase with the voltage?

::: model
Power factor is real power divided by RMS voltage times RMS current, and it suffers from both phase shift and waveform shape. The capacitor holds the output near the line's peak, so the diodes conduct only for a short interval near each peak, and the whole half cycle's charge has to flow in that interval as a tall, narrow pulse. A narrow pulse has a much larger RMS value than a sinusoid carrying the same average power, and the excess lives in harmonics that deliver no power from a sinusoidal voltage. So the displacement factor is near one, but the distortion factor, and with it the power factor, is around 0.4 to 0.6.
:::
:::
