---
title: AC power and harmonics lab
heading: Real and apparent power from sampled waveforms, sizing correction capacitors, neutral currents, THD by FFT and true power factor, in Python
eyebrow: C3 · Lesson 21 · AC power lab
xp: 50
---

::: hook
Lessons 16 to 20 computed power from phasors: P, Q and S from a voltage, a current and the angle between them, then power-factor correction, three-phase loads and distorted waveforms. A power analyzer doesn't see phasors. It samples voltage and current thousands of times a cycle and computes everything from the samples. In this lab you'll do the same: average the instantaneous power, size a correction capacitor, add three phase currents, take a waveform apart with an FFT, and split a distorted load's power factor into its two causes.
:::

::: play
- The curves are a sinusoidal voltage, a current lagging it by the slider's angle, and their product, the instantaneous power, all over one cycle.
- The power swings at twice the line frequency. Its average is P = VI cos φ; where it dips below zero, energy flows back to the source.
- At 90° the average is zero: pure reactive power, energy sloshing back and forth with none consumed. The readouts give P and Q for 230 V and 5 A RMS.
:::

```sim plot
view: graph
x: t = 0..1
y: p = -2..2
slider: a = 0..90 step 1 = 30 °
plot: sqrt(2)*cos(2*pi*t) | label: voltage, per unit | samples: 300
plot: sqrt(2)*cos(2*pi*t - a*pi/180) | color: ch2 | label: current, per unit | samples: 300
plot: 2*cos(2*pi*t)*cos(2*pi*t - a*pi/180) | color: ch3 | label: instantaneous power, per unit | samples: 300
marker: p = cos(a*pi/180) | label: average power
readout: P, W = 230*5*cos(a*pi/180) | digits: 4
readout: Q, var = 230*5*sin(a*pi/180) | digits: 4
```

::: predict
## A rectifier draws a current with 80% THD whose fundamental is exactly in phase with the voltage. What's its power factor?

- [ ] 1, since the current is in phase
- [x] About 0.78
- [ ] About 0.2
- [ ] 0.8, the THD

::: reveal
About 0.78. Only the fundamental carries real power with a sinusoidal voltage, but every harmonic adds to the RMS current, and so to the apparent power. The distortion factor is $\frac{1}{\sqrt{1 + \text{THD}^2}} = \frac{1}{\sqrt{1.64}}$ = 0.781, and with no phase shift that's the whole power factor. Step 5 computes both parts from samples.
:::
:::

::: nameit
## Power from samples

Over a whole number of cycles, real power is the average of the instantaneous product, $P = \overline{v(t)\,i(t)}$, and the RMS values are the square roots of the averaged squares. Apparent power is $S = V_{rms}I_{rms}$ and the power factor $\frac PS$. These definitions hold for any waveform, sinusoidal or not.

## Sizing a correction capacitor

To move a load of real power P from power factor $\text{pf}_1$ to $\text{pf}_2$, the capacitors must supply $Q_C = P(\tan\phi_1 - \tan\phi_2)$, and a capacitor at RMS voltage V and angular frequency ω supplies $\omega CV^2$:

$$C = \frac{P(\tan\phi_1 - \tan\phi_2)}{\omega V^2}.$$

## Three phase currents

In a four-wire wye, the neutral carries the phasor sum of the three phase currents, at 0°, −120° and +120°. Balanced currents cancel; for unequal in-phase currents a, b and c, $|I_n| = \sqrt{a^2 + b^2 + c^2 - ab - bc - ca}$.

## Harmonics by FFT

An FFT of exactly an integer number of cycles puts each harmonic in its own bin. The amplitude of harmonic n is twice the magnitude of bin n·(cycles) divided by the number of samples, and

$$\text{THD} = \frac{\sqrt{\sum_{n\ge2}A_n^2}}{A_1}.$$

## True power factor

With a sinusoidal voltage, the power factor of a distorted current splits into a **displacement** factor, cos φ₁ of the fundamental's phase, and a **distortion** factor, $\frac{I_{1,rms}}{I_{rms}} = \frac{1}{\sqrt{1 + \text{THD}^2}}$. Their product equals $\frac PS$.
:::

::: example
## A power analyzer samples 230 V RMS and a 5 A RMS current lagging by 30°. What does it report?

::: solution
1. **Real power.** Averaging $v(t)i(t)$ over whole cycles gives
   $$\begin{aligned} P &= 230 \times 5 \times \cos30° \\ &= 996\ \text{W} \end{aligned}$$
2. **Apparent power.** The RMS values are 230 V and 5 A, so
   $$\begin{aligned} S &= 230 \times 5 \\ &= 1150\ \text{VA} \end{aligned}$$
3. **Power factor.**
   $$\begin{aligned} \text{pf} &= \frac{P}{S} \\ &= 0.866 \end{aligned}$$
4. **Reactive power.**
   $$\begin{aligned} Q &= \sqrt{S^2 - P^2} \\ &= 575\ \text{var} \end{aligned}$$
5. **No phase angle needed.** The analyzer never computed a phase angle: the average of the product does it implicitly.
6. **Why that matters.** That's why the same method works for a dimmer or a switch-mode supply, where no single angle describes the current.

**Answer:** it reports P = 996 W, S = 1150 VA, a power factor of 0.866 and Q = 575 var.
:::
:::

::: example
## A 10 kW load at power factor 0.7 runs from 230 V, 50 Hz. What capacitance corrects it to 0.95?

::: solution
1. **Tangents of the two angles.**
   $$\begin{aligned} \tan\phi_1 &= 1.020 \\ \tan\phi_2 &= 0.329 \end{aligned}$$
2. **Reactive power to cancel.**
   $$\begin{aligned} Q_C &= P(\tan\phi_1 - \tan\phi_2) \\ &= 10\ \text{kW} \times 0.6915 \\ &= 6.92\ \text{kvar} \end{aligned}$$
3. **Capacitance.**
   $$\begin{aligned} C &= \frac{6920}{2\pi \times 50 \times 230^2} \\ &= 416\ \mu\text{F} \end{aligned}$$
4. **In practice.** That's a large AC capacitor; in practice it's a bank of smaller ones, switched in steps as the load changes, so the plant never swings to a leading power factor at light load.

**Answer:** 416 µF.
:::
:::

::: example
## A wye of resistive loads draws 10, 8 and 6 A in its three phases. What does the neutral carry?

::: solution
1. **Add the phasors.** For $10\angle0°$, $8\angle{-120°}$ and $6\angle120°$:
   $$\begin{aligned} |I_n|^2 &= 100 + 64 + 36 \\ &\quad - 80 - 48 - 60 \\ &= 12 \end{aligned}$$
2. **Take the square root.**
   $$\begin{aligned} |I_n| &= \sqrt{12} \\ &= 3.46\ \text{A} \end{aligned}$$
3. **Unbalance.** Unbalance of a few amps leaves only a few amps in the neutral.
4. **Harmonics.** Harmonics are another matter: third harmonics from the three phases are in phase with each other, so they add in the neutral instead of cancelling, which is why buildings full of computers once overheated their neutral wires.

**Answer:** the neutral carries 3.46 A.
:::
:::

::: check
kind: python

## Step 1. Write `powers(v, i)` for samples spanning whole cycles, returning `(P, Vrms, Irms, S, pf)`.

```python starter
import numpy as np

def powers(v, i):
    return 0.0, 0.0, 0.0, 0.0, 1.0

t = np.arange(1000) / 1000 / 50
w = 2 * np.pi * 50
print(powers(325.27 * np.cos(w * t), 7.0711 * np.cos(w * t - np.pi / 6)))
```

```python solution
import numpy as np

def powers(v, i):
    v = np.asarray(v, float)
    i = np.asarray(i, float)
    P = float(np.mean(v * i))
    Vr = float(np.sqrt(np.mean(v * v)))
    Ir = float(np.sqrt(np.mean(i * i)))
    S = Vr * Ir
    return P, Vr, Ir, S, P / S

t = np.arange(1000) / 1000 / 50
w = 2 * np.pi * 50
print(powers(325.27 * np.cos(w * t), 7.0711 * np.cos(w * t - np.pi / 6)))
```

```python tests
import numpy as np
t = np.arange(1000) / 1000 / 50
w = 2 * np.pi * 50
P, V, I, S, pf = powers(230 * np.sqrt(2) * np.cos(w * t), 5 * np.sqrt(2) * np.cos(w * t - np.pi / 6))
assert abs(P - 995.93) < 0.05 and abs(V - 230) < 1e-6 and abs(I - 5) < 1e-6, f"Expected 996 W, 230 V, 5 A; got {P}, {V}, {I}"
assert abs(pf - np.cos(np.pi / 6)) < 1e-9, "The power factor is cos 30° = 0.866."
```

::: right
Right. Averaging the product over whole cycles gives the real power for any waveform.
:::
:::

::: check
kind: python

## Step 2. Write `pf_capacitor(P, V, f, pf1, pf2)` returning the correcting capacitance in µF.

```python starter
def pf_capacitor(P, V, f, pf1, pf2):
    return 100.0

print(pf_capacitor(10e3, 230, 50, 0.7, 0.95))
```

```python solution
import math

def pf_capacitor(P, V, f, pf1, pf2):
    tan = lambda pf: math.sqrt(1 - pf * pf) / pf
    Qc = P * (tan(pf1) - tan(pf2))
    return Qc / (2 * math.pi * f * V * V) * 1e6

print(pf_capacitor(10e3, 230, 50, 0.7, 0.95))
```

```python tests
assert abs(pf_capacitor(10e3, 230, 50, 0.7, 0.95) - 416.10) < 0.05, f"10 kW from 0.7 to 0.95 at 230 V, 50 Hz needs 416 µF; got {pf_capacitor(10e3, 230, 50, 0.7, 0.95)}"
assert abs(pf_capacitor(10e3, 230, 60, 0.7, 0.95) / pf_capacitor(10e3, 230, 50, 0.7, 0.95) - 50 / 60) < 1e-12, "At 60 Hz less capacitance does the same job."
assert pf_capacitor(5e3, 400, 50, 0.9, 0.9) == 0.0, "No correction needed, no capacitor."
```

::: right
Right. The capacitance falls with frequency and with the square of the voltage, which is why correction is cheaper at higher voltages.
:::
:::

::: check
kind: python

## Step 3. Write `neutral_current(a, b, c)` returning the magnitude of the phasor sum of in-phase currents a, b and c on phases at 0°, −120° and +120°.

```python starter
def neutral_current(a, b, c):
    return abs(a - b) + abs(b - c)

print(neutral_current(10, 8, 6))
```

```python solution
import cmath, math

def neutral_current(a, b, c):
    s = a + b * cmath.exp(-2j * math.pi / 3) + c * cmath.exp(2j * math.pi / 3)
    return abs(s)

print(neutral_current(10, 8, 6))
```

```python tests
import math
assert abs(neutral_current(10, 8, 6) - math.sqrt(12)) < 1e-9, f"10, 8 and 6 A leave √12 = 3.46 A; got {neutral_current(10, 8, 6)}"
assert neutral_current(7, 7, 7) < 1e-9, "Balanced currents cancel."
assert abs(neutral_current(10, 0, 0) - 10) < 1e-9, "One loaded phase sends all its current back on the neutral."
```

::: right
Right. The phasor sum handles any unbalance, and the closed form follows from it.
:::
:::

::: check
kind: python

## Step 4. Write `thd(x, cycles, nmax)` for samples spanning exactly `cycles` periods, returning the THD from FFT harmonic amplitudes 2 to `nmax`.

```python starter
import numpy as np

def thd(x, cycles, nmax):
    return 0.0

n = np.arange(4000)
s = np.sin(2 * np.pi * 4 * n / 4000)
print(thd(s + 0.3 * np.sin(3 * 2 * np.pi * 4 * n / 4000), 4, 20))
```

```python solution
import numpy as np

def thd(x, cycles, nmax):
    X = np.fft.rfft(np.asarray(x, float))
    A = 2 * np.abs(X) / len(x)
    harm = [A[k * cycles] for k in range(1, nmax + 1) if k * cycles < len(A)]
    return float(np.sqrt(sum(h * h for h in harm[1:])) / harm[0])

n = np.arange(4000)
s = np.sin(2 * np.pi * 4 * n / 4000)
print(thd(s + 0.3 * np.sin(3 * 2 * np.pi * 4 * n / 4000), 4, 20))
```

```python tests
import numpy as np, math
n = np.arange(4000)
ph = 2 * np.pi * 4 * n / 4000
x = np.sin(ph) + 0.3 * np.sin(3 * ph) + 0.1 * np.sin(5 * ph)
assert abs(thd(x, 4, 20) - math.sqrt(0.1)) < 1e-9, f"Harmonics of 0.3 and 0.1 give THD √0.1 = 0.316; got {thd(x, 4, 20)}"
sq = np.sign(np.sin(2 * np.pi * (np.arange(100000) + 0.5) / 1000))
assert abs(thd(sq, 100, 499) - 0.4824) < 0.003, f"A square wave's THD to the 499th harmonic is about 0.482; got {thd(sq, 100, 499)}"
```

::: right
Right. With an integer number of cycles each harmonic lands in one bin, so no window is needed.
:::
:::

::: check
kind: python

## Step 5. Write `pf_parts(v, i, cycles)` returning `(displacement, distortion, total)`: cos of the phase between the voltage's and current's fundamentals (from the FFT), $\frac{I_{1,rms}}{I_{rms}}$, and P/S from the samples.

```python starter
def pf_parts(v, i, cycles):
    return 1.0, 1.0, 1.0

import numpy as np
n = np.arange(2000)
ph = 2 * np.pi * 2 * n / 2000
print(pf_parts(np.cos(ph), np.cos(ph - 0.5) + 0.8 * np.cos(3 * ph), 2))
```

```python solution
import numpy as np

def pf_parts(v, i, cycles):
    v = np.asarray(v, float)
    i = np.asarray(i, float)
    V1 = np.fft.rfft(v)[cycles]
    I1 = np.fft.rfft(i)[cycles]
    disp = float(np.cos(np.angle(V1) - np.angle(I1)))
    i1rms = 2 * abs(I1) / len(i) / np.sqrt(2)
    dist = float(i1rms / np.sqrt(np.mean(i * i)))
    total = float(np.mean(v * i) / (np.sqrt(np.mean(v * v)) * np.sqrt(np.mean(i * i))))
    return disp, dist, total

n = np.arange(2000)
ph = 2 * np.pi * 2 * n / 2000
print(pf_parts(np.cos(ph), np.cos(ph - 0.5) + 0.8 * np.cos(3 * ph), 2))
```

```python tests
import numpy as np, math
n = np.arange(2000)
ph = 2 * np.pi * 2 * n / 2000
d, s, t = pf_parts(np.cos(ph), np.cos(ph - 0.5) + 0.8 * np.cos(3 * ph), 2)
assert abs(d - math.cos(0.5)) < 1e-9, "The displacement factor is cos 0.5 rad."
assert abs(s - 1 / math.sqrt(1.64)) < 1e-9, "80% THD gives a distortion factor of 0.781."
assert abs(t - d * s) < 1e-9, "The total power factor is their product."
```

::: right
Right. Correction capacitors fix only the displacement part; the distortion part needs filters or an active front end.
:::
:::

::: check
kind: parts
given: V = 120 | 230; I = 2 | 10; a = 20 | 45

## A load draws {I} A RMS from {V} V RMS, lagging by {a}°.

::: part
answer: V*I*cos(a*pi/180)
unit: W
tolerance: 0.5%

### What real power does it take?

::: misses
- [V*I] That's the apparent power.
- [any] $VI\cos\phi$.
:::
:::

::: part
answer: V*I*sin(a*pi/180)
unit: var
tolerance: 0.5%

### What reactive power?

::: misses
- [any] $VI\sin\phi$.
:::
:::
:::

::: check
given: P = 5 | 20; V = 230 | 400; a = 0.65 | 0.8; f = 50 | 60
answer: P*1e3*(sqrt(1 - a^2)/a - sqrt(1 - 0.95^2)/0.95)/(2*pi*f*V^2)*1e6
unit: µF
tolerance: 0.5%

## A {P} kW load at power factor {a} runs from {V} V, {f} Hz. What capacitance corrects it to 0.95?

::: right
Right. $C = \frac{P(\tan\phi_1 - \tan\phi_2)}{\omega V^2}$.
:::

::: misses
- [any] Find the kvar, then $C = \frac{Q_C}{\omega V^2}$.
:::
:::

::: check
given: a = 12 | 20; b = 6 | 15; c = 3 | 10
answer: sqrt(a^2 + b^2 + c^2 - a*b - b*c - c*a)
unit: A
tolerance: 0.5%

## A wye of resistive loads draws {a}, {b} and {c} A. What does the neutral carry?

::: right
Right. The phasor sum at 0°, −120° and +120°.
:::

::: misses
- [a - c] The phases are 120° apart; add them as phasors.
- [any] $\sqrt{a^2 + b^2 + c^2 - ab - bc - ca}$.
:::
:::

::: check
given: h = 0.2 | 0.5 | 0.9
answer: 1/sqrt(1 + h^2)
tolerance: 0.5%

## A load's current has THD {h} (as a fraction), with its fundamental in phase with the voltage. What's its power factor?

::: right
Right. $\frac{1}{\sqrt{1 + \text{THD}^2}}$: harmonics add current without adding power.
:::

::: misses
- [1 - h] The distortion factor is $\frac{1}{\sqrt{1 + \text{THD}^2}}$.
- [any] Distortion factor times displacement factor.
:::
:::

::: explain
## Why do harmonic currents lower the power factor even when they're perfectly in phase with the voltage?

::: model
With a sinusoidal supply voltage, only the current at the same frequency can carry real power: the product of a 50 Hz voltage and a 150 Hz current averages to zero over a cycle, whatever their phases. But the harmonic currents still flow through every wire and transformer, so they add to the RMS current and to the apparent power VI. The ratio P/S therefore falls by the factor $\frac{I_1}{I_{rms}} = \frac{1}{\sqrt{1 + \text{THD}^2}}$. A capacitor can't fix this, because it only supplies current at the fundamental; the cure is to make the load draw a sinusoidal current, which is what active power-factor-correction circuits do.
:::
:::

::: practice

::: check
given: V = 230 | 400; I = 1 | 20; a = 10 | 60
answer: V*I*cos(a*pi/180)
unit: W
tolerance: 0.5%

## Real power: {V} V, {I} A, lagging {a}°?

::: misses
- [any] $VI\cos\phi$.
:::
:::

::: check
given: a = 5 | 16; b = 9 | 12; c = 2 | 7
answer: sqrt(a^2 + b^2 + c^2 - a*b - b*c - c*a)
unit: A
tolerance: 0.5%

## Neutral current for {a}, {b}, {c} A?

::: misses
- [any] $\sqrt{a^2 + b^2 + c^2 - ab - bc - ca}$.
:::
:::
:::
