---
title: "Inverter lab: PWM spectra, space vectors, LCL filters and multilevel outputs"
heading: Generating sinusoidal PWM and measuring its spectrum, leg duty cycles for space-vector modulation, an LCL filter's exact attenuation and the damping trade-off, and a cascaded H-bridge's phase-shifted carriers
eyebrow: S11 · Lesson 38 · Inverter lab
xp: 60
---

::: hook
Lessons 33 to 37 claimed a lot about inverters: that sinusoidal PWM puts no low-order harmonics in the output, that space-vector modulation is the same as adding a common-mode offset, that an LCL filter attenuates as the square of frequency above its resonance, that phase-shifted cells push the ripple up by a factor of 2k. Each claim is easy to test numerically. In this lab you'll generate the switching waveforms sample by sample and measure them.
:::

::: play
- The curve is an LCL filter's grid current against the bridge voltage, relative to a single inductor of the same total, in decibels: L₁ = 2 mH, L₂ = 0.5 mH, C = 10 µF, with a damping resistor $R_d$ in series with C on the slider.
- With no damping the response spikes at 2.52 kHz; a few ohms flattens the spike.
- Damping costs attenuation at high frequency: at 10 kHz, 23.4 dB with none, 19.0 dB with 2.1 Ω.
:::

```sim plot
view: graph
x: f = 0.5..50 log
y: A = -30..30
slider: R = 0.1..10 = 2.1
plot: 20*log(sqrt((1 + (2*pi*f*1e3*R*10e-6)^2)/((1 - (f*1e3/2516.46)^2)^2 + (2*pi*f*1e3*R*10e-6)^2))) | label: relative response, dB
```

::: predict
## Adding a damping resistor equal to a third of the capacitor's reactance at resonance (2.1 Ω here) tames the LCL filter's peak. What does it cost at the 10 kHz switching frequency?

- [ ] Nothing
- [x] About 4 dB of attenuation
- [ ] About 20 dB
- [ ] It improves attenuation

::: reveal
Above the resonance the resistor's impedance competes with the capacitor's falling reactance, so the capacitor no longer shunts the ripple as well: attenuation falls from 23.4 to 19.0 dB at 10 kHz, and the resistor dissipates the ripple current flowing through it. That's why active damping, feeding back the capacitor current, is preferred in larger inverters. Task 3 computes both numbers.
:::
:::

::: nameit
## What the lab computes

- **Sinusoidal PWM**: a leg is high when the reference $m\sin\theta$ exceeds a triangle carrier at $m_f$ times the fundamental. Sampled finely over one period, the output's DFT shows the fundamental at m (in units of $V_{dc}/2$) up to m = 1, and harmonics only around multiples of $m_f$.
- **Space-vector duties**: for phase references $v_x = \frac{m}{\sqrt3}\cos(\theta - k\cdot120°)$ in units of $V_{dc}$, the leg duties $d_x = \frac12 + v_x - \frac{\max v + \min v}{2}$ center the zero vectors exactly as lesson 35's dwell times do.
- **LCL filter**: with $Z_C = R_d + 1/sC$ and the grid as a short,
$$\frac{i_2}{v_i} = \frac{1}{s(L_1 + L_2) + s^2L_1L_2/Z_C}$$
which for $R_d$ = 0 is exactly $|1 - (f/f_\text{res})^2|$ times smaller than one inductor's response.
- **Cascaded H-bridges**: each cell compares $+m\sin\theta$ and $-m\sin\theta$ with its carrier (unipolar); cell j's carrier is shifted by j/(2k) of a carrier period.
:::

::: check
kind: python

## Write `spwm(m, mf, N)`: N samples (at the midpoints of N equal steps) of one fundamental period of a half-bridge leg's output, ±1 (in units of $V_{dc}/2$), comparing $m\sin\theta$ with a triangle carrier that is +1 at θ = 0 and has $m_f$ periods per fundamental period.

```python starter
import math

def spwm(m, mf, N):
    return [math.copysign(1.0, math.sin(2 * math.pi * (n + 0.5) / N)) for n in range(N)]
```

```python solution
import math

def triangle(x):
    x %= 1.0
    return 1 - 4 * x if x < 0.5 else -3 + 4 * x

def spwm(m, mf, N):
    out = []
    for n in range(N):
        t = (n + 0.5) / N
        out.append(1.0 if m * math.sin(2 * math.pi * t) > triangle(mf * t) else -1.0)
    return out
```

```python tests
import cmath, math
def H(x, h):
    N = len(x)
    return abs(sum(x[n] * cmath.exp(-2j * math.pi * h * (n + 0.5) / N) for n in range(N))) * 2 / N
x = spwm(0.8, 21, 21 * 400)
assert abs(H(x, 1) - 0.8) < 0.003, "The fundamental is m."
assert max(H(x, h) for h in (2, 3, 5, 7, 9)) < 0.005, "No low-order harmonics."
assert H(x, 21) > 0.7, "The carrier's own harmonic is large."
y = spwm(1.5, 21, 21 * 400)
assert 1.1 < H(y, 1) < 4 / math.pi and H(y, 3) > 0.1, "Overmodulated: less than m, and low harmonics appear."
```

::: right
Right. Below m = 1 the switching moves all the distortion up around the carrier, where a small filter removes it.
:::
:::

::: check
kind: python

## Write `svm_duties(m, theta_deg)`: the three leg duty cycles for space-vector modulation with centered zero vectors.

```python starter
import math

def svm_duties(m, theta_deg):
    t = math.radians(theta_deg)
    return [0.5 + m / 2 * math.cos(t - k * 2 * math.pi / 3) for k in range(3)]
```

```python solution
import math

def svm_duties(m, theta_deg):
    t = math.radians(theta_deg)
    v = [m / math.sqrt(3) * math.cos(t - k * 2 * math.pi / 3) for k in range(3)]
    offset = (max(v) + min(v)) / 2
    return [0.5 + x - offset for x in v]
```

```python tests
import math
da, db, dc = svm_duties(0.8, 20)
assert abs((da - dc) - 0.8 * (math.sin(math.radians(40)) + math.sin(math.radians(20)))) < 1e-12, "d_a − d_c is T1 + T2: 0.788 of the period."
assert abs((db - dc) - 0.8 * math.sin(math.radians(20))) < 1e-12, "d_b − d_c is T2."
for th in range(0, 360, 7):
    d = svm_duties(0.6, th)
    want = 0.6 / math.sqrt(3) * (math.cos(math.radians(th)) - math.cos(math.radians(th) - 2 * math.pi / 3))
    assert abs((d[0] - d[1]) - want) < 1e-12, "The line-to-line average equals the reference's."
assert all(-1e-12 <= x <= 1 + 1e-12 for th in range(360) for x in svm_duties(1.0, th)), "m = 1 just fits."
assert any(x > 1 for th in range(360) for x in svm_duties(1.05, th)), "Beyond m = 1 a duty exceeds 1."
```

::: right
Right. The min-max offset is the common-mode voltage that space-vector modulation adds; the motor never sees it, and it lets the line-to-line voltage reach the full bus.
:::
:::

::: check
kind: python

## Write `lcl_ratio(L1, L2, C, Rd, f)`: the magnitude of $i_2/v_i$ times $\omega(L_1 + L_2)$, so that 1 means "the same as one inductor", and `lcl_peak(L1, L2, C, Rd)`: the largest ratio over a log sweep from 100 Hz to 100 kHz.

```python starter
def lcl_ratio(L1, L2, C, Rd, f):
    return 1.0

def lcl_peak(L1, L2, C, Rd):
    return 1.0
```

```python solution
import math

def lcl_ratio(L1, L2, C, Rd, f):
    s = 2j * math.pi * f
    Zc = Rd + 1 / (s * C)
    return abs(s * (L1 + L2)) / abs(s * (L1 + L2) + s * s * L1 * L2 / Zc)

def lcl_peak(L1, L2, C, Rd):
    return max(lcl_ratio(L1, L2, C, Rd, 10 ** (2 + 3 * k / 20000)) for k in range(20001))
```

```python tests
import math
r = lcl_ratio(2e-3, 0.5e-3, 10e-6, 0, 10e3)
assert abs(-20 * math.log10(r) - 23.40) < 0.01, "Undamped: 23.4 dB at 10 kHz."
r2 = lcl_ratio(2e-3, 0.5e-3, 10e-6, 2.108, 10e3)
assert abs(-20 * math.log10(r2) - 19.03) < 0.01, "With 2.1 Ω: 19.0 dB."
assert abs(lcl_peak(2e-3, 0.5e-3, 10e-6, 2.108) - 3.199) < 0.01, "The damped peak: 3.2 times."
assert lcl_peak(2e-3, 0.5e-3, 10e-6, 0.5) > 12, "Light damping leaves a sharp peak."
```

::: right
Right. A third of the capacitor's reactance at resonance holds the peak to about 10 dB while giving up about 4 dB at the switching frequency.
:::
:::

::: check
kind: python

## Write `chb(k, mf, m, N)`: N samples (at step midpoints) of one fundamental period of a cascaded H-bridge phase with k cells, in units of one cell's DC voltage, using unipolar PWM against triangle carriers with $m_f$ periods per fundamental period, cell j's carrier shifted by j/(2k) of a carrier period.

```python starter
import math

def chb(k, mf, m, N):
    return [k * m * math.sin(2 * math.pi * (n + 0.5) / N) for n in range(N)]
```

```python solution
import math

def triangle(x):
    x %= 1.0
    return 1 - 4 * x if x < 0.5 else -3 + 4 * x

def chb(k, mf, m, N):
    out = []
    for n in range(N):
        t = (n + 0.5) / N
        r = m * math.sin(2 * math.pi * t)
        s = 0
        for j in range(k):
            c = triangle(mf * t + j / (2 * k))
            s += (1 if r > c else 0) - (1 if -r > c else 0)
        out.append(s)
    return out
```

```python tests
import cmath, math
def H(x, h):
    N = len(x)
    return abs(sum(x[n] * cmath.exp(-2j * math.pi * h * (n + 0.5) / N) for n in range(N))) * 2 / N
x = chb(5, 10, 0.9, 2000)
assert set(x) <= set(range(-5, 6)), "The output takes the 11 levels −5 to 5."
assert abs(H(x, 1) - 4.5) < 0.02, "Fundamental k·m = 4.5."
assert max(H(x, h) for h in (19, 21, 39, 41, 59, 61)) < 0.03, "Each cell's own ripple groups cancel."
assert max(H(x, h) for h in (97, 99, 101, 103)) > 0.08, "The ripple appears near 2k·mf = 100."
```

::: right
Right. Five cells each switching at ten times the fundamental give an output whose first ripple is at the hundredth harmonic.
:::
:::

::: explain
## Why do phase-shifted carriers move a cascaded H-bridge's ripple to $2k$ times the carrier frequency?

::: model
Each unipolar cell's output contains ripple groups around twice its carrier frequency and its multiples. Shifting cell j's carrier by j/(2k) of a carrier period shifts that cell's ripple at twice the carrier frequency by j/k of a full cycle, so the k cells' ripple components at that frequency are spread evenly around the circle and sum to zero. The same happens at every multiple of twice the carrier frequency except multiples of 2k, where the shifts become whole cycles and the components add. The fundamental is unaffected because every cell follows the same reference, so the output keeps the full voltage with ripple only near 2k times the carrier frequency.
:::
:::
