---
title: Resonant and repetitive control
heading: The internal model principle: zero steady-state error for sinusoids and periodic disturbances
eyebrow: S9 · Lesson 21 · Resonant and repetitive control
xp: 50
---

::: hook
Lesson 10 showed that an integrator in the loop drives a constant error to zero: the controller contains a model of a constant, a pole at $s = 0$. A grid-tied inverter must track a 50 or 60 Hz sinusoid exactly, a disk drive must follow a track that wobbles once per revolution, and a power supply must reject ripple at the line frequency and its harmonics. An integrator can't do those perfectly, but the same idea extends: put a model of the sinusoid, a pair of poles at $\pm j\omega_0$, inside the controller, and the loop has infinite gain at exactly that frequency. A delay line in positive feedback does it for every harmonic at once. That's the **internal model principle**, and it's why proportional-resonant and repetitive controllers are standard in power electronics.
:::

::: play
- The magnitude of a proportional-resonant controller, $C(s) = K_p + K_r\dfrac{s}{s^2 + \omega_0^2}$ with $f_0$ = 50 Hz, in dB against frequency. It's flat at $K_p$ except for a sharp peak, infinite in the ideal form, at 50 Hz.
- A practical version adds damping, $K_r\dfrac{2\omega_cs}{s^2 + 2\omega_cs + \omega_0^2}$, to widen the peak and tolerate grid-frequency drift; the dashed curve shows it with $\omega_c$ chosen by the slider.
:::

```sim plot
view: graph
x: f = 10..500 log
y: G = -10..60
slider: c = 0.5..20 step 0.5 = 5
plot: 20*log(abs(1 + 100*2*pi*f/((2*pi*50)^2 - (2*pi*f)^2 + 1e-9))) | samples: 2000 | label: ideal PR, dB
plot: 20*log(sqrt((1 + 100*2*c*(2*pi*f)^2*2*c/(((2*pi*50)^2 - (2*pi*f)^2)^2 + (2*c*2*pi*f)^2))^2 + (100*2*c*2*pi*f*((2*pi*50)^2 - (2*pi*f)^2)/(((2*pi*50)^2 - (2*pi*f)^2)^2 + (2*c*2*pi*f)^2))^2)) | samples: 2000 | dashed | color: ch2 | label: damped PR, dB
```

::: predict
## A PI current controller in a grid inverter has a gain of 20 dB at 50 Hz in its loop. How large is the steady-state error tracking the 50 Hz reference, roughly?

- [ ] Zero
- [x] About 9% of the reference
- [ ] About 1%
- [ ] About 50%

::: reveal
With loop gain $L$ = 10 at 50 Hz, the tracking error is $1/|1 + L|$ ≈ 1/11 ≈ 9% (less if the phase helps, more if it hurts). An integrator gives infinite gain only at DC. A resonant term with poles at ±j2π·50 gives infinite loop gain at 50 Hz and drives that error to zero, which is why grid-tied inverters use proportional-resonant control.
:::
:::

::: nameit
## The internal model principle

To reject or track a signal with zero steady-state error, the loop must contain a model of that signal's generator. A constant comes from $1/s$, so the loop needs an integrator. A sinusoid at $\omega_0$ comes from $\dfrac{s}{s^2 + \omega_0^2}$, so the loop needs poles at $\pm j\omega_0$: infinite gain there, so $|1/(1 + L)|$ = 0 at exactly $\omega_0$.

## Proportional-resonant control

$$C(s) = K_p + K_r\frac{s}{s^2 + \omega_0^2}$$

In a rotating (dq) frame a sinusoid becomes a constant and an ordinary PI controller does the job; the PR controller is its equivalent in the stationary frame. Because an ideal resonance is infinitely narrow, a small frequency drift loses most of the benefit, so practical PR controllers use a damped form with bandwidth $\omega_c$, and add resonant terms at selected harmonics (3rd, 5th, 7th).

## Repetitive control

A periodic disturbance of period $T$ contains every harmonic of $1/T$. Its generator is a delay line in positive feedback: in discrete time with $N = T/T_s$ samples per period,

$$\frac{1}{1 - z^{-N}}$$

which has poles at all $N$ roots of unity: every harmonic $kf_s/N$ up to the Nyquist frequency. Placed in the loop (with a low-pass filter $Q(z)$ in the feedback for robustness, and phase-lead compensation), it learns the periodic error from cycle to cycle and cancels it. It needs $N$ samples of memory and converges over several periods.

## Stability

Infinite gain at a frequency is safe only if the loop's phase there allows it. The resonant gain $K_r$ is limited by phase margin near $\omega_0$, and a repetitive controller's $Q$ filter limits how high the learned harmonics go, trading perfect rejection for robustness against unmodeled high-frequency dynamics.
:::

::: example
## A loop has a gain of 10 at 50 Hz with a phase of −120°. What fraction of a 50 Hz reference appears as error, and what would a resonant term do?

::: solution
1. **Loop gain as a complex number.**
   $$\begin{aligned} L &= 10e^{-j120°} \\ &= -5 - 8.66j \end{aligned}$$
2. **Return difference.**
   $$1 + L = -4 - 8.66j$$
   with magnitude 9.54.
3. **Error.**
   $$1/9.54 = 10.5\%$$
   of the reference, in magnitude.
4. **Add a resonant term.** Adding $K_rs/(s^2 + \omega_0^2)$ makes $|L|$ infinite at exactly 50 Hz, so the error there is zero.
5. **If the grid drifts.** At 49.5 Hz the error is merely small, which is why damped resonances are used.

**Answer:** 10.5% of the reference appears as error, and a resonant term at 50 Hz makes it zero there.
:::
:::

::: example
## A repetitive controller rejects a 50 Hz periodic disturbance in a converter sampled at 10 kHz. How many samples of memory does it need, and how many harmonics can it address?

::: solution
1. **Memory.** One location for each sample in a period:
   $$\begin{aligned} N &= 10\,000/50 \\ &= 200 \end{aligned}$$
2. **Harmonics.** The poles of $1/(1 - z^{-200})$ sit at every multiple of 50 Hz, so it addresses up to
   $$N/2 = 100$$
   harmonics, up to the 5 kHz Nyquist frequency.
3. **In practice.** The $Q$ filter limits it to a few kilohertz.

**Answer:** 200 samples of memory, addressing up to 100 harmonics (in practice the Q filter limits it to a few kilohertz).
:::
:::

::: example
## A grid's frequency drifts to 50.5 Hz. A damped PR controller with $K_r$ = 100 and bandwidth $\omega_c$ = 5 rad/s is tuned to 50 Hz. How much gain does its resonant term give at 50.5 Hz, compared with its peak?

::: solution
1. **The damped term.** It peaks at $K_r$ = 100 at $\omega_0$:
   $$\frac{2\omega_cK_rs}{s^2 + 2\omega_cs + \omega_0^2}$$
2. **The two frequencies.**
   $$\begin{aligned} \omega &= 2\pi \times 50.5 \\ &= 317.3\ \text{rad/s} \\ \omega_0 &= 314.2\ \text{rad/s} \end{aligned}$$
3. **The denominator at ω.** Its real and imaginary parts:
   $$\begin{aligned} \omega_0^2 - \omega^2 &= -1984 \\ 2\omega_c\omega &= 3173 \end{aligned}$$
4. **Magnitude.**
   $$\begin{aligned} \text{gain} &= \frac{100 \times 3173}{\sqrt{1984^2 + 3173^2}} \\ &= 85 \end{aligned}$$
   That's still 39 dB, against 40 dB at the peak.
5. **Compare an undamped resonance.** It would give only about
   $$\frac{K_r\omega}{|\omega_0^2 - \omega^2|} = 16$$
   so the damping keeps the gain high across the drift.

**Answer:** 85 (39 dB) at 50.5 Hz, against 100 (40 dB) at the peak; an undamped term would give only about 16.
:::
:::

::: check
given: L = 5 | 10 | 30 | 100; p = -90 | -120 | -150
answer: 1/sqrt((1 + L*cos(p°))^2 + (L*sin(p°))^2)*100
tolerance: 0.5%

## At the reference frequency, a loop has gain {L} and phase {p}°. What percentage of the reference appears as tracking error?

::: right
Right. $1/|1 + L|$ = {answer}%.
:::

::: misses
- [1/(1 + L)*100] Use the complex loop gain: the phase matters.
- [any] Not yet. $|1/(1 + Le^{j\varphi})|$.
:::
:::

::: check
given: fs = 5 | 10 | 20; f0 = 50 | 60 | 400
answer: fs*1e3/f0
tolerance: 0.5%

## A repetitive controller sampled at {fs} kHz rejects a disturbance with a {f0} Hz period. How many samples of memory does its delay line need?

::: right
Right. $N = f_s/f_0$ = {answer}.
:::

::: misses
- [f0/(fs*1e3)] It's samples per period: $f_s/f_0$.
- [any] Not yet. One period in samples.
:::
:::

::: check
given: N = 100 | 200 | 400
answer: N/2
tolerance: 0.5%

## A repetitive controller's delay line holds N = {N} samples. Up to how many harmonics of the fundamental do its poles cover, to the Nyquist frequency?

::: right
Right. Poles at every $kf_s/N$, up to $f_s/2$: {answer} harmonics.
:::

::: misses
- [N] Beyond the Nyquist frequency the poles mirror the lower ones.
- [any] Not yet. Up to $f_s/2$, that's $N/2$ multiples of $f_s/N$.
:::
:::

::: check
given: Kr = 50 | 100 | 300; wc = 2 | 5 | 10; df = 0.2 | 0.5 | 1
answer: Kr*2*wc*2*pi*(50 + df)/sqrt(((2*pi*50)^2 - (2*pi*(50 + df))^2)^2 + (2*wc*2*pi*(50 + df))^2)
tolerance: 0.5%

## A damped PR controller with $K_r$ = {Kr} and $\omega_c$ = {wc} rad/s is tuned to 50 Hz. What is its resonant term's gain at {df} Hz above 50 Hz?

::: right
Right. $K_r \cdot 2\omega_c\omega/|\omega_0^2 - \omega^2 + 2j\omega_c\omega|$ = {answer}.
:::

::: misses
- [Kr] That's the peak, at exactly 50 Hz.
- [any] Not yet. Evaluate the damped resonant term's magnitude at $\omega$.
:::
:::

::: check
kind: choice

## Why can a PI controller in a synchronous (dq) reference frame track a 50 Hz current with zero error, while the same PI in the stationary frame can't?

- [x] In the dq frame rotating at 50 Hz, the sinusoid becomes a constant, which the integrator's pole at zero tracks exactly
- [ ] The dq transformation filters out the error
- [ ] PI controllers are only stable in rotating frames
- [ ] The dq frame doubles the controller's gain

::: right
Right. The integrator's internal model is a constant; transforming to the rotating frame turns the 50 Hz signal into one. A PR controller is the stationary-frame equivalent.
:::

::: misses
- [2] The transformation changes the signal's appearance, not the error itself.
- [3] Stability depends on the loop, not the frame.
- [4] Gain isn't the point; the model of the signal is.
:::
:::

::: check
kind: python
given: a = 0.2 | 0.5; N = 20 | 50; g = 0.5 | 0.9

## Simulate repetitive control. A plant $y[n] = u[n] + d[n]$ (a direct feedthrough for simplicity) suffers a periodic disturbance $d[n] = \sin(2\pi n/N) + a\sin(6\pi n/N)$ with period `N` = {N}. The controller learns with $u[n] = u[n - N] - g\,e[n - N]$, where $e = y$ (the reference is zero) and `g` = {g}, starting from $u = 0$ for the first $N$ samples. Run 40 periods and set `rms_first` and `rms_last`, the RMS error over the first and last periods.

```python starter
import math
rms_first = rms_last = 1.0
```

```python solution
import math
n_tot = 40 * N
d = [math.sin(2 * math.pi * n / N) + a * math.sin(6 * math.pi * n / N) for n in range(n_tot)]
u = [0.0] * n_tot
e = [0.0] * n_tot
for n in range(n_tot):
    if n >= N:
        u[n] = u[n - N] - g * e[n - N]
    e[n] = u[n] + d[n]
rms = lambda xs: math.sqrt(sum(x * x for x in xs) / len(xs))
rms_first = rms(e[:N])
rms_last = rms(e[-N:])
```

```python tests
import math
nt = 40 * N
d = [math.sin(2 * math.pi * n / N) + a * math.sin(6 * math.pi * n / N) for n in range(nt)]
u = [0.0] * nt; e = [0.0] * nt
for n in range(nt):
    if n >= N: u[n] = u[n - N] - g * e[n - N]
    e[n] = u[n] + d[n]
r = lambda xs: math.sqrt(sum(x * x for x in xs) / len(xs))
assert abs(rms_first - r(e[:N])) < 1e-12 and abs(rms_last - r(e[-N:])) < 1e-12, "Compute the RMS errors over the first and last periods."
assert rms_last < 1e-6 * rms_first, "The periodic error should be learned away, every harmonic at once."
```

::: right
Right. Each period the controller subtracts a fraction $g$ of the last period's error, so the error shrinks by $(1 - g)$ per period, for every harmonic at once: the delay line is a model of any signal with that period.
:::
:::

::: explain
## A grid-tied inverter with a PI current controller in the stationary frame shows a few percent of current error at 50 Hz and visible 5th and 7th harmonic distortion. Explain why, and what to change, using the internal model principle.

::: model
A PI controller contains a model only of constants (its integrator's pole at zero), so it can't drive a 50 Hz error to zero; with finite loop gain at 50 Hz, a few percent of the reference remains as error, and the grid's 5th and 7th voltage harmonics act as disturbances the loop only partly rejects. The internal model principle says the controller must contain the generators of those signals: resonant terms $K_rs/(s^2 + \omega^2)$ at 50, 250 and 350 Hz, damped to tolerate grid-frequency drift, which give very high loop gain at exactly those frequencies. Alternatively, transforming to a dq frame rotating at 50 Hz turns the fundamental into a constant for an ordinary PI, with extra frames or resonant terms for the harmonics, or a repetitive controller handles every harmonic of 50 Hz at once. Each added resonance must be checked for phase margin.
:::
:::

::: practice

::: check
given: L = 3 | 50; p = -100 | -140
answer: 1/sqrt((1 + L*cos(p°))^2 + (L*sin(p°))^2)*100
tolerance: 0.5%

## What percentage tracking error results from a loop gain of {L} at {p}°?

::: misses
- [any] $1/|1 + Le^{j\varphi}|$.
:::
:::

::: check
given: fs = 8 | 50; f0 = 60 | 100
answer: fs*1e3/f0
tolerance: 0.5%

## How many delay-line samples does repetitive control need for a {f0} Hz period at {fs} kHz?

::: misses
- [f0/(fs*1e3)] Samples per period: $f_s/f_0$.
- [any] $f_s/f_0$.
:::
:::
:::
