---
title: Capacitor and inductor measurements lab
heading: Fitting time constants, the energy lost in charge sharing, impedance sweeps, PWM ripple and an inductor's saturation, in Python
eyebrow: C2 · Lesson 13 · Measurements lab
xp: 50
---

::: hook
Lessons 1 to 11 gave capacitors and inductors simple laws and then complicated them: real parts have ESR and ESL, energy goes missing when capacitors share charge, a PWM filter never quite settles, and inductors saturate. On the bench these show up as data: a decaying trace, an impedance curve, a current ramp that steepens. In this lab you'll turn such data into numbers, and check the formulas from the lessons against simulations that don't assume them.
:::

::: play
- The curve is a real capacitor's impedance against frequency: its capacitance dominates at low frequency, its ESR at the bottom of the dip, and its ESL above.
- The dip, at $\frac{1}{2\pi\sqrt{LC}}$, is the self-resonant frequency, and its depth is the ESR.
- Change the ESL, as a different package or longer leads would, and watch the useful range shrink. Step 3 finds the dip by a sweep.
:::

```sim plot
view: graph
x: f = 1000..1000000000 Hz log
y: z = 0.001..1000 Ω log
slider: C = 1..1000 = 100 nF log
slider: L = 0.1..10 = 1 nH log
slider: R = 1..100 = 10 mΩ log
plot: sqrt((R/1000)^2 + (2*pi*f*L*1e-9 - 1/(2*pi*f*C*1e-9))^2) | label: impedance magnitude | samples: 600
marker: f = 1/(2*pi*sqrt(L*1e-9*C*1e-9)) | label: self-resonance
readout: self-resonant frequency, MHz = 1/(2*pi*sqrt(L*1e-9*C*1e-9))/1e6 | digits: 4
```

::: predict
## A 10 µF capacitor at 10 V is connected to an empty 10 µF capacitor through a resistor. Does the energy lost depend on the resistor?

- [ ] Yes: a small resistor loses almost nothing
- [ ] Yes: a large resistor loses almost everything
- [x] No: half the energy is lost for any resistance
- [ ] No energy is lost, since charge is conserved

::: reveal
Half is lost for any resistance. A small resistor carries a huge current briefly; a large one a small current for a long time; and $\int i^2R\,dt$ comes out the same, $\frac{C_1C_2V^2}{2(C_1 + C_2)}$. Step 2 integrates the current numerically for very different resistors to show it.
:::
:::

::: nameit
## Fitting an exponential

A decay $v = V_0e^{-t/\tau}$ becomes a straight line in $\ln v$: slope $-1/\tau$. Fitting a line to $\ln v$ against t uses every sample, not just two, so noise averages down. Points near zero, where noise dominates the logarithm, should be left out.

## Charge sharing

When a capacitor at V charges another through R, the current is $\frac VRe^{-t/\tau}$ with $\tau = R\frac{C_1C_2}{C_1 + C_2}$, and the energy dissipated is $\int i^2R\,dt = \frac{C_1C_2V^2}{2(C_1 + C_2)}$, independent of R.

## Impedance of a real capacitor

A capacitor with ESR R and ESL L has $|Z| = \sqrt{R^2 + (\omega L - \frac{1}{\omega C})^2}$. A logarithmic sweep finds the minimum at $\frac{1}{2\pi\sqrt{LC}}$, with $|Z|$ = R there.

## PWM ripple exactly

A PWM wave of amplitude V, duty D and period T through an RC settles into a sawtooth whose exact peak-to-peak value is

$$\Delta v = V\frac{(1 - e^{-DT/\tau})(1 - e^{-(1 - D)T/\tau})}{1 - e^{-T/\tau}},$$

which tends to $V\frac{D(1 - D)T}{\tau}$ when τ ≫ T.

## Inductance from a current ramp

With a known voltage across an inductor, its current ramps at $\frac{di}{dt} = \frac{V}{L(i)}$. Differencing a measured ramp gives L at each current, and where the slope steepens, the core is saturating.
:::

::: example
## A capacitor discharges through a resistor; samples of its voltage, with noise, fall from 5 V. How do you get τ from them?

::: solution
1. **Keep the clean samples.** Use the samples above about 10% of the start, where noise is small compared with the signal.
2. **Fit a line to their logarithms.** Take the logarithm of each sample and fit a straight line against time:
   $$\ln v = \ln V_0 - \frac t\tau$$
   The slope is $-\frac1\tau$ and the intercept $\ln V_0$.
3. **How close it gets.** With 100 samples over two time constants and 10 mV of noise, the fit lands within about 1% of the true τ.
4. **Compare with one point.** Reading τ from a single point, the time to fall to 37%, uses one noisy sample and an eyeballed crossing.
5. **Where it's used.** The fit is how an automated tester measures a capacitor's value through a known resistor.

**Answer:** fit a straight line to the logarithm of the samples above about 10% of the start; its slope is $-\frac1\tau$, which gives τ within about 1% here.
:::
:::

::: example
## Integrate the loss when 10 µF at 10 V charges an empty 10 µF through 10 Ω, and through 1 kΩ.

::: solution
1. **With 10 Ω.** τ = 50 µs and the current starts at 1 A.
2. **With 1 kΩ.** τ = 5 ms and the current starts at 10 mA.
3. **Integrate the loss.** Integrating $i^2R$ numerically over ten time constants gives 250 µJ in both cases, matching
   $$\begin{aligned} E &= \frac{C_1C_2V^2}{2(C_1 + C_2)} \\ &= \frac{10 \times 10 \times 100}{2 \times 20}\ \mu\text{J} \\ &= 250\ \mu\text{J} \end{aligned}$$
4. **Compare with the stored energy.** That's half of the 500 µJ stored at the start.
5. **How to avoid it.** Only adding an inductor, as switched-mode converters do, avoids the loss, by moving charge without a large voltage across a resistance.

**Answer:** 250 µJ is lost with either resistor, half of the 500 µJ stored at the start.
:::
:::

::: example
## How exact is the ripple estimate $V\frac{D(1 - D)T}{\tau}$ for a 5 V, 25% PWM wave with τ = 100T, 10T and 3T?

::: solution
1. **Exact ripple.** The exact formula gives 9.375 mV, 93.7 mV and 312 mV.
2. **Estimated ripple.** The estimate gives 9.375, 93.75 and 312.5 mV.
3. **Compare.** Even at τ = 3T the estimate is only 0.2% high.
4. **Why it works.** The estimate assumes the capacitor's voltage barely changes during a period, so its current stays nearly constant through each phase.
5. **Why it works so well.** The errors in the on and off phases nearly cancel, so the approximation holds surprisingly well and is good enough for design work.

**Answer:** very close: the estimate is at most 0.2% high, even at τ = 3T.
:::
:::

::: check
kind: python

## Step 1. Write `fit_tau(t, v, floor)` returning τ from a least-squares line through $\ln v$ against t, using only samples with v > `floor`.

```python starter
import numpy as np

def fit_tau(t, v, floor):
    return 1.0

t = np.linspace(0, 2e-3, 100)
print(fit_tau(t, 5 * np.exp(-t / 1e-3), 0.5))
```

```python solution
import numpy as np

def fit_tau(t, v, floor):
    t = np.asarray(t, float)
    v = np.asarray(v, float)
    keep = v > floor
    slope, _ = np.polyfit(t[keep], np.log(v[keep]), 1)
    return float(-1 / slope)

t = np.linspace(0, 2e-3, 100)
print(fit_tau(t, 5 * np.exp(-t / 1e-3), 0.5))
```

```python tests
import numpy as np
t = np.linspace(0, 2e-3, 100)
assert abs(fit_tau(t, 5 * np.exp(-t / 1e-3), 0.5) - 1e-3) < 1e-9, "Clean data must give τ exactly."
rng = np.random.default_rng(7)
v = 5 * np.exp(-t / 1e-3) + rng.normal(0, 0.01, t.size)
assert abs(fit_tau(t, v, 0.5) / 1e-3 - 1) < 0.01, f"Noisy data should give τ within 1%; got {fit_tau(t, v, 0.5)}"
```

::: right
Right. The floor keeps the logarithm of noise out of the fit.
:::
:::

::: check
kind: python

## Step 2. Write `share_loss(C1, C2, V, R)` returning the energy dissipated in R when C1 at V charges an empty C2, by integrating $i^2R$ numerically (trapezoid rule, 100 000 points over ten time constants).

```python starter
def share_loss(C1, C2, V, R):
    return 0.0

print(share_loss(10e-6, 10e-6, 10, 10), share_loss(10e-6, 10e-6, 10, 1000))
```

```python solution
import numpy as np

def share_loss(C1, C2, V, R):
    tau = R * C1 * C2 / (C1 + C2)
    t = np.linspace(0, 10 * tau, 100001)
    i = V / R * np.exp(-t / tau)
    trap = np.trapezoid if hasattr(np, "trapezoid") else np.trapz
    return float(trap(i * i * R, t))

print(share_loss(10e-6, 10e-6, 10, 10), share_loss(10e-6, 10e-6, 10, 1000))
```

```python tests
a = share_loss(10e-6, 10e-6, 10, 10)
b = share_loss(10e-6, 10e-6, 10, 1000)
assert abs(a - 250e-6) < 1e-7 and abs(b - 250e-6) < 1e-7, f"Both should lose 250 µJ; got {a}, {b}"
c = share_loss(10e-6, 1e-6, 5, 100)
assert abs(c - 10e-6 * 1e-6 * 25 / (2 * 11e-6)) < 1e-9, "Unequal capacitors follow C1C2V²/2(C1 + C2)."
```

::: right
Right. The resistor sets how fast the energy is lost, never how much.
:::
:::

::: check
kind: python

## Step 3. Write `impedance(f, C, L, R)` returning |Z| of a capacitor with ESL L and ESR R, and `srf(C, L, R)` returning the frequency of minimum |Z| from a sweep of 20 001 log-spaced points between 1 kHz and 10 GHz.

```python starter
def impedance(f, C, L, R):
    return 1.0

def srf(C, L, R):
    return 1e6

print(srf(100e-9, 1e-9, 0.01))
```

```python solution
import numpy as np

def impedance(f, C, L, R):
    w = 2 * np.pi * np.asarray(f, float)
    return np.sqrt(R**2 + (w * L - 1 / (w * C)) ** 2)

def srf(C, L, R):
    f = np.logspace(3, 10, 20001)
    return float(f[np.argmin(impedance(f, C, L, R))])

print(srf(100e-9, 1e-9, 0.01))
```

```python tests
import math
f0 = srf(100e-9, 1e-9, 0.01)
assert abs(f0 / 15.915e6 - 1) < 1e-3, f"100 nF with 1 nH resonates at 15.9 MHz; got {f0}"
assert abs(float(impedance(f0, 100e-9, 1e-9, 0.01)) - 0.01) < 1e-4, "At resonance |Z| is the ESR."
assert float(impedance(1e3, 100e-9, 1e-9, 0.01)) > 1000, "At 1 kHz the capacitance dominates: about 1.6 kΩ."
```

::: right
Right. Above the dip the capacitor is really an inductor, which is why decoupling uses several sizes in parallel.
:::
:::

::: check
kind: python

## Step 4. Write `pwm_ripple(V, D, T, tau)` returning the exact steady-state peak-to-peak ripple of an RC-filtered PWM wave, by iterating the on and off exponentials for many periods until it settles.

```python starter
def pwm_ripple(V, D, T, tau):
    return V * D * (1 - D) * T / tau

print(pwm_ripple(5, 0.25, 1, 100))
```

```python solution
import math

def pwm_ripple(V, D, T, tau):
    a = math.exp(-D * T / tau)
    b = math.exp(-(1 - D) * T / tau)
    v = 0.0
    for _ in range(int(50 * tau / T) + 50):
        top = V + (v - V) * a
        v = top * b
    return top - v

print(pwm_ripple(5, 0.25, 1, 100))
```

```python tests
import math
def exact(V, D, T, tau):
    a = math.exp(-D * T / tau); b = math.exp(-(1 - D) * T / tau); c = math.exp(-T / tau)
    return V * (1 - a) * (1 - b) / (1 - c)
for tau in (100, 10, 3):
    r = pwm_ripple(5, 0.25, 1, tau)
    assert abs(r - exact(5, 0.25, 1, tau)) < 1e-9, f"τ = {tau}: expected {exact(5, 0.25, 1, tau)}, got {r}"
assert abs(pwm_ripple(5, 0.25, 1, 100) / 9.375e-3 - 1) < 1e-4, "At τ = 100T the estimate is almost exact."
```

::: right
Right. Iterating the two exponentials until they repeat is the same as solving for the periodic steady state.
:::
:::

::: check
kind: python

## Step 5. Write `inductance_curve(t, i, V)` returning `(i_mid, L)`: the inductance at each step of a measured current ramp, from $L = \frac{V\Delta t}{\Delta i}$, at the midpoint currents.

```python starter
import numpy as np

def inductance_curve(t, i, V):
    return np.asarray(i), np.full(len(i), 1e-5)

print(inductance_curve([0, 1e-6, 2e-6], [0, 0.5, 1.0], 5))
```

```python solution
import numpy as np

def inductance_curve(t, i, V):
    t = np.asarray(t, float)
    i = np.asarray(i, float)
    L = V * np.diff(t) / np.diff(i)
    return 0.5 * (i[:-1] + i[1:]), L

print(inductance_curve([0, 1e-6, 2e-6], [0, 0.5, 1.0], 5))
```

```python tests
import numpy as np
im, L = inductance_curve([0, 1e-6, 2e-6], [0, 0.5, 1.0], 5)
assert np.allclose(L, 1e-5), "A straight ramp of 0.5 A/µs at 5 V is 10 µH."
# A saturating inductor, L(i) = 10 µH / (1 + (i/2 A)^4), ramped at 5 V.
L0, Isat, V = 10e-6, 2.0, 5.0
dt = 1e-9
t, i = [0.0], [0.0]
while i[-1] < 3.0:
    i.append(i[-1] + V * dt / (L0 / (1 + (i[-1] / Isat) ** 4)))
    t.append(t[-1] + dt)
im, L = inductance_curve(t, i, V)
assert abs(L[np.argmin(abs(im - 0.2))] / L0 - 1) < 0.01, "At low current it's the full 10 µH."
k = np.argmin(abs(L - L0 / 2))
assert abs(im[k] - 2.0) < 0.02, f"It falls to half at the saturation current, 2 A; got {im[k]}"
```

::: right
Right. The ramp steepens where the core saturates, and the curve shows exactly where.
:::
:::

::: check
kind: parts
given: v = 3 | 5; s = 1 | 2; t = 0.5 | 2

## A capacitor's voltage falls from {v} V to {s} V in {t} ms.

::: part
answer: t/ln(v/s)
unit: ms
tolerance: 0.5%

### What's its time constant?

::: misses
- [t/(v - s)] Decay is exponential: $\tau = \frac{t}{\ln(v_0/v)}$.
- [any] $\tau = \frac{\Delta t}{\ln(v_1/v_2)}$.
:::
:::

::: part
answer: t/ln(v/s)*ln(v/0.05)
unit: ms
tolerance: 0.5%

### How long from the start until it reaches 50 mV?

::: misses
- [any] $\tau\ln\frac{v_0}{0.05}$.
:::
:::
:::

::: check
given: a = 1 | 10; b = 1 | 22; V = 5 | 12
answer: a*b/(a + b)*V^2/2
unit: µJ
tolerance: 0.5%

## A {a} µF capacitor at {V} V charges an empty {b} µF one through a resistor. How much energy does the resistor dissipate?

::: right
Right. $\frac{C_1C_2V^2}{2(C_1 + C_2)}$, whatever the resistance.
:::

::: misses
- [a*V^2/2] That's all the initial energy; some stays in the capacitors.
- [any] Initial energy minus final.
:::
:::

::: check
given: V = 3.3 | 5; D = 0.2 | 0.5; t = 2 | 5
answer: V*(1 - exp(-D/t))*(1 - exp(-(1 - D)/t))/(1 - exp(-1/t))*1000
unit: mV
tolerance: 0.5%

## A {V} V PWM wave at {D} duty passes through an RC with τ = {t} periods. What's its exact peak-to-peak ripple?

::: right
Right. $V\frac{(1 - e^{-DT/\tau})(1 - e^{-(1 - D)T/\tau})}{1 - e^{-T/\tau}}$.
:::

::: misses
- [any] Track the charging and discharging exponentials over one period.
:::
:::

::: check
given: V = 5 | 12; d = 0.2 | 1; L = 4.7 | 22
answer: V/(d*1e6)/(L*1e-6)
unit: µs
tolerance: 0.5%

## A {L} µH inductor has {V} V across it. How long does its current take to rise by {d} A?

::: right
Right. $\Delta t = \frac{L\Delta i}{V}$.
:::

::: misses
- [any] $\Delta t = \frac{L\Delta i}{V}$.
:::
:::

::: explain
## Why does fitting a straight line to the logarithm of a decay give τ more reliably than timing when the voltage reaches 37%?

::: model
The 37% point uses one sample and an estimate of exactly where the trace crosses a level, so any noise on that sample, or on the starting value it's referred to, goes straight into τ. Taking the logarithm turns the whole exponential into a straight line, and a least-squares fit uses every sample along it; random errors partly cancel, so the slope, and τ, come out far more precisely. The fit also shows whether the decay really is a single exponential: if the logarithm curves, there's a second time constant, such as dielectric absorption in the capacitor, which a single crossing would miss.
:::
:::

::: practice

::: check
given: v = 4 | 9; s = 0.5 | 2; t = 1 | 10
answer: t/ln(v/s)
unit: ms
tolerance: 0.5%

## τ for a decay from {v} V to {s} V in {t} ms?

::: misses
- [any] $\frac{t}{\ln(v_0/v)}$.
:::
:::

::: check
given: a = 4.7 | 47; b = 10 | 100; V = 3.3 | 24
answer: a*b/(a + b)*V^2/2
unit: µJ
tolerance: 0.5%

## Charge-sharing loss, {a} µF at {V} V into {b} µF?

::: misses
- [any] $\frac{C_1C_2V^2}{2(C_1 + C_2)}$.
:::
:::
:::
