---
title: Impulse response and convolution in practice
heading: Predicting any output from one measurement
eyebrow: A10 · Lesson 24 · Impulse response
xp: 50
---

::: hook
Hit a linear system with one very short, sharp pulse and record what comes out: that recording, the impulse response, completely describes the system. Room acoustics are measured this way, by popping a balloon; so are loudspeakers, cables and control loops. Any other input's output is then a convolution, which a computer does in one line. In this lesson you'll go between the impulse response, the transfer function and the output, and compute a convolution in NumPy.
:::

::: play
- A pulse of 1 V lasting $T$ goes into an RC low-pass with time constant $\tau$. The output rises toward 1 V while the pulse is on, then decays.
- Make the pulse much shorter than $\tau$. The output becomes a small, sharp rise and a slow fall: almost the impulse response itself, scaled by the pulse's area.
- Make the pulse much longer than $\tau$. Now the output copies the pulse, with rounded edges.
:::
:::

```sim plot
view: graph
x: t = 0..8 ms
y: v = -0.05..1.1 V
slider: tau = 0.2..3 step 0.1 = 1 ms
slider: T = 0.1..5 step 0.1 = 2 ms
plot: 1 - step(t - T) | label: input pulse | color: ch3
plot: (1 - exp(-t/tau))*(1 - step(t - T)) + (1 - exp(-T/tau))*exp(-(t - T)/tau)*step(t - T) | label: output | samples: 1200
readout: output at the end of the pulse = 1 - exp(-T/tau) | digits: 4
```

::: predict
## A filter's impulse response has an area of 2 under it. What does its output settle to for a 1 V step input?

- [ ] 1 V
- [x] 2 V
- [ ] 0 V
- [ ] It depends on the impulse response's shape

::: reveal
2 V. A step is a steady stream of tiny impulses, so the step response is the running integral of the impulse response, and it settles at the total area. That area is also $H(0)$, the DC gain.
:::
:::

::: nameit
## Three descriptions of one system

| Domain | Description | Output |
|---|---|---|
| time | impulse response $h(t)$ | $y = h * x = \int_0^th(t - s)x(s)\,ds$ |
| Laplace | transfer function $H(s) = \mathcal{L}\{h\}$ | $Y(s) = H(s)X(s)$ |
| frequency | frequency response $H(j\omega)$ | each sinusoid scaled by $H(j\omega)$ |

The step response is $\int_0^th$, or $\frac{H(s)}{s}$ in Laplace, and its final value is $H(0) = \int_0^\infty h\,dt$.

For an RC low-pass, $H(s) = \frac{1}{1 + s\tau}$ and $h(t) = \frac{1}{\tau}e^{-t/\tau}$.

## Convolution on a computer

Sample both signals at spacing $\Delta t$; then

$$y(n\Delta t) \approx \Delta t\sum_k h(k\Delta t)\,x((n - k)\Delta t)$$

which is `np.convolve(x, h) * dt`, truncated to the length you want. The error shrinks with $\Delta t$: this is the rectangle rule (A6), so expect errors of order $\Delta t/\tau$.

## Short and long inputs

A pulse much shorter than the system's time constant acts like an impulse of the same area. A pulse much longer than it passes through nearly unchanged. In between, the output is a rounded copy, which is the "rise time" of a scope or a data link.
:::

::: example
## A 1 V, 2 ms pulse enters an RC low-pass with $\tau$ = 1 ms. Find the output at 2 ms and 4 ms.

::: solution
1. **During the pulse.** The output is a step response, so at 2 ms
   $$1 - e^{-2} = 0.865\ \text{V}$$
2. **After the pulse.** That value decays, so at 4 ms
   $$\begin{aligned} v &= 0.865e^{-(4 - 2)/1} \\ &= 0.117\ \text{V} \end{aligned}$$
3. **Check numerically.** A numerical convolution with $\Delta t$ = 10 µs gives 0.860 and 0.116.

**Answer:** 0.865 V at 2 ms and 0.117 V at 4 ms.
:::
:::

::: example
## A system has $H(s) = \dfrac{2}{(s + 1)(s + 2)}$. What is its impulse response, and its step response's final value?

::: solution
1. **Partial fractions.**
   $$\begin{aligned} H(s) &= \frac{2}{(s + 1)(s + 2)} \\ &= \frac{2}{s + 1} - \frac{2}{s + 2} \end{aligned}$$
2. **Invert.**
   $$h(t) = 2e^{-t} - 2e^{-2t}$$
3. **The final value.** The step response settles at
   $$\begin{aligned} H(0) &= \frac{2}{2} \\ &= 1 \end{aligned}$$
4. **Check.** That's also the area under $h$:
   $$\int_0^\infty h\,dt = 2 - 1$$

**Answer:** $h(t) = 2e^{-t} - 2e^{-2t}$, and the step response settles at 1.
:::
:::

::: example
## Find the impulse response of $H(s) = \dfrac{1}{s^2 + 2s + 5}$ and its peak.

::: solution
1. **The impulse response.**
   $$h(t) = \tfrac12e^{-t}\sin 2t$$
2. **Find the peak.** Its derivative is zero where $\tan 2t = 2$, first at
   $$\begin{aligned} t &= \tfrac12\tan^{-1}2 \\ &= 0.554\ \text{s} \end{aligned}$$
3. **The peak value.**
   $$\begin{aligned} h &= \tfrac12e^{-0.554}\sin(1.107) \\ &= 0.5 \times 0.575 \times 0.894 \\ &= 0.257 \end{aligned}$$
4. **After the peak.** It then rings, each swing $e^{-\pi/2} = 0.21$ times the size of the one before.

**Answer:** $h(t) = \tfrac12e^{-t}\sin 2t$, which peaks at 0.257 at $t = 0.554$ s.
:::
:::

::: check
given: tau = 0.5 | 2; T = 1 | 3
answer: 1 - exp(-T/tau)
unit: V
tolerance: 0.5%

## A 1 V pulse lasting {T} ms enters an RC low-pass with $\tau$ = {tau} ms. What is the output at the end of the pulse?

::: right
Right. The step response at $T$: $1 - e^{-T/\tau}$ = {answer}.
:::

::: misses
- [exp(-T/tau)] That's how far it still has to go.
- [any] Not yet. The step response $1 - e^{-t/\tau}$ at $t = T$.
:::
:::

::: check
given: tau = 1 | 2; T = 1 | 2; t = 3 | 5
answer: (1 - exp(-T/tau))*exp(-(t - T)/tau)
unit: V
tolerance: 0.5%

## The same kind of pulse ({T} ms, $\tau$ = {tau} ms) has ended. What is the output at $t$ = {t} ms?

::: right
Right. The value at $T$, decaying: {answer}.
:::

::: misses
- [exp(-(t - T)/tau)] Start the decay from where the output was at $T$, not from 1 V.
- [any] Not yet. $(1 - e^{-T/\tau})e^{-(t - T)/\tau}$.
:::
:::

::: check
given: K = 2 | 6; a = 1 | 2; b = 3 | 4
answer: K/(a*b)
tolerance: 0.5%

## A system has $H(s) = \dfrac{\given{K}}{(s + \given{a})(s + \given{b})}$. What does its step response settle to?

::: right
Right. $H(0) = \frac{K}{ab}$ = {answer}.
:::

::: misses
- [K] Evaluate $H$ at $s = 0$.
- [any] Not yet. The DC gain, $H(0)$.
:::
:::

::: check
given: A = 1 | 5; d = 1 | 10; tau = 100 | 1000
answer: A*d/tau
unit: V
tolerance: 1%

## A {A} V pulse lasting {d} µs enters an RC low-pass with $\tau$ = {tau} µs, much longer. About what peak does the output reach?

::: right
Right. A short pulse acts like an impulse of area $Ad$, and the peak of $\frac{Ad}{\tau}e^{-t/\tau}$ is $\frac{Ad}{\tau}$ = {answer}.
:::

::: misses
- [A] The pulse is far too short for the output to reach {A} V.
- [any] Not yet. Area of the pulse times the impulse response's peak, $1/\tau$.
:::
:::

::: check
given: N = 100 | 1000; M = 50 | 300
answer: N + M - 1
tolerance: 0.01%

## `np.convolve` of arrays of length {N} and {M} returns how many samples?

::: right
Right. $N + M - 1$ = {answer}: every overlap from first touch to last.
:::

::: misses
- [N + M] One less: the two arrays overlap first in one sample.
- [any] Not yet. Count the shifts with any overlap.
:::
:::

::: check
kind: python

## Compute the output of an RC low-pass ($\tau$ = 1 ms) to a 1 V, 2 ms pulse by convolution: sample at `dt = 1e-5` s for 6 ms, build `x` and `h = exp(-t/tau)/tau`, and set `y = np.convolve(x, h)[:len(t)] * dt`.

```python starter
import numpy as np
dt = 1e-5
tau = 1e-3
t = np.arange(0, 6e-3, dt)
x = np.zeros_like(t)
h = np.zeros_like(t)
y = np.zeros_like(t)
print(y[200], y[400])
```

```python solution
import numpy as np
dt = 1e-5
tau = 1e-3
t = np.arange(0, 6e-3, dt)
x = (t < 2e-3).astype(float)
h = np.exp(-t / tau) / tau
y = np.convolve(x, h)[:len(t)] * dt
print(y[200], y[400])
```

```python tests
import numpy as np
assert len(y) == len(t), "Keep y the same length as t."
e2 = 1 - np.exp(-2)
assert abs(y[200] - e2) / e2 < 0.02, f"At 2 ms the output should be about {e2:.3f} V; got {y[200]:.3f}."
e4 = e2 * np.exp(-2)
assert abs(y[400] - e4) / e4 < 0.02, f"At 4 ms it should be about {e4:.3f} V; got {y[400]:.3f}."
assert abs(np.sum(h) * dt - 1) < 0.01, "The impulse response's area, its DC gain, should be 1."
```

::: right
Right. Within about 1% of the exact answer; halving `dt` halves the error, as the rectangle rule predicts.
:::
:::

::: check
kind: parts
given: a = 1 | 2; w = 2 | 4

## $H(s) = \dfrac{1}{(s + \given{a})^2 + \given{w}^2}$.

::: part
answer: atan(w/a)/w
unit: s
tolerance: 0.5%

### When does its impulse response first peak?

::: misses
- [pi/(2*w)] The decay shifts the peak earlier: $\tan(wt) = \frac wa$.
- [any] Differentiate $\frac1we^{-at}\sin wt$.
:::
:::

::: part
answer: 1/(a^2 + w^2)
tolerance: 0.5%

### What is its step response's final value?

::: misses
- [any] $H(0)$.
:::
:::
:::

::: check
kind: derivation
vars: tau = 0.5..3, T = 0.5..3

## A unit pulse of length $T$ enters an RC low-pass with time constant $\tau$.

::: step
answer: 1 - exp(-T/tau)

### What is the output at the end of the pulse?
:::

::: step
answer: (1 - exp(-T/tau))*exp(-T/tau)

### What is it a further $T$ later?
:::

::: right
Right. Charging during the pulse, then plain decay: the step response minus a delayed copy.
:::
:::

::: explain
## Why does a very short pulse produce an output shaped like the impulse response, whatever the pulse's own shape?

::: model
The output is the convolution $\int h(t - s)x(s)\,ds$. If the pulse lasts much less than the time over which $h$ changes, $h(t - s)$ is nearly the same for every $s$ inside the pulse, so it can be pulled out of the integral: the output is about $h(t)\int x(s)\,ds$, the impulse response times the pulse's area. The pulse's detailed shape washes out. That's why a balloon pop or a hammer tap can measure an impulse response.
:::
:::

::: practice

::: check
given: tau = 2..20 int
answer: 1/tau
tolerance: 0.5%

## What is the peak of the RC impulse response $\frac{1}{\tau}e^{-t/\tau}$, for $\tau$ = {tau} ms, in 1/ms?

::: misses
- [tau] It's $1/\tau$ at $t = 0$.
- [any] At $t = 0$.
:::
:::

::: check
given: K = 3 | 8; a = 2 | 5
answer: K/a
tolerance: 0.5%

## What is the area under the impulse response $\given{K}e^{-\given{a}t}$?

::: misses
- [K] Integrate: $\frac{K}{a}$.
- [any] $\int_0^\infty Ke^{-at}dt$.
:::
:::

::: check
given: e = 2 | 0.8; k = 2 | 4
answer: e/k
tolerance: 1%

## A rectangle-rule convolution has an error of {e}% at some step $\Delta t$. Its error is proportional to $\Delta t$. What error do you expect with a step {k} times smaller, in percent?

::: misses
- [e/k^2] The rectangle rule's error is first order: proportional to $\Delta t$, not $\Delta t^2$.
- [any] Divide by {k}.
:::
:::
:::
