---
title: Correlation and energy spectra
heading: Finding a known signal in a received one, and what its spectrum says about it
eyebrow: A10 · Lesson 11 · Correlation and energy spectra
xp: 40
---

::: hook
A radar sends a short pulse and listens. The echo comes back weak, buried in noise, and delayed by the round trip to the target. A GPS receiver hunts for a satellite's code in a signal far below the noise floor. Both find what they're looking for the same way: slide a copy of the expected signal along the received one, multiply and add at every offset, and look for the offset where the match is best. That sliding product is **correlation**, a close cousin of lesson 10's convolution, and its Fourier transform turns out to be a product of spectra too.
:::

::: play
- The top curve is a rectangular pulse of width $T$ = 1 ms; the second is its autocorrelation, the pulse slid past itself, against the lag $\tau$ in milliseconds.
- The autocorrelation is a triangle twice as wide as the pulse, peaking at zero lag with the pulse's energy. Widen the pulse and the triangle widens with it: a long pulse can't mark time sharply.
:::

```sim plot
view: graph
x: tau = -3..3
y: v = -0.2..1.2
slider: T = 0.2..2 step 0.1 = 1
plot: step(T/2 - abs(tau)) | dashed | label: pulse of width T, centered
plot: step(T - abs(tau))*(1 - abs(tau)/T) | label: autocorrelation divided by its peak
readout: energy, peak of the autocorrelation (for unit height, ms) = T | digits: 3
```

::: predict
## A pulse of height $A$ and width $T$ is correlated with a copy of itself. What is the result's largest value, and where?

- [x] $A^2T$, the pulse's energy, at zero lag
- [ ] $A$, at zero lag
- [ ] $A^2T$, at a lag of $T$
- [ ] Zero everywhere, since a pulse can't correlate with itself

::: reveal
$A^2T$ at zero lag. With no offset the pulse lines up with itself, so the product is $A^2$ wherever the pulse is, and integrating gives $A^2T$: its energy. Any offset reduces the overlap. A signal always matches itself best when it isn't shifted, and the match at zero lag is its energy.
:::
:::

::: nameit
## Cross-correlation and autocorrelation

For energy signals (pulses, which have finite energy),

$$R_{xy}(\tau) = \int_{-\infty}^{\infty} x(t)\,y(t + \tau)\,dt$$

measures how much $y$, shifted back by $\tau$, looks like $x$. If $y$ is a delayed copy of $x$, $y(t) = a\,x(t - t_0)$, then $R_{xy}(\tau) = a\,R_{xx}(\tau - t_0)$: the correlation peaks at the delay. The **autocorrelation** $R_{xx}(\tau)$ has three properties:
- $R_{xx}(0) = \int x^2\,dt$ is the signal's energy;
- $|R_{xx}(\tau)| \le R_{xx}(0)$ for every lag (the Cauchy-Schwarz inequality);
- $R_{xx}(-\tau) = R_{xx}(\tau)$: it's even.

Correlation is convolution with one signal flipped: $R_{xy}(\tau) = x(-t) * y(t)$ evaluated at $\tau$.

## The spectrum of a correlation

Because flipping time conjugates a real signal's transform (lesson 10),

$$R_{xy}(\tau) \leftrightarrow X^*(f)\,Y(f) \qquad R_{xx}(\tau) \leftrightarrow |X(f)|^2$$

$|X(f)|^2$ is the **energy spectral density**: how a pulse's energy is spread over frequency. Its integral is the energy, which is lesson 4's Parseval theorem in another form. A pulse with a wide spectrum has a narrow autocorrelation peak, and the reverse.

## Delay, resolution and pulse compression

The correlation peak's width sets how precisely a delay can be measured and how close two echoes can be and still be told apart: roughly $1/B$ for a signal of bandwidth $B$. A plain pulse of width $T$ has $B \approx 1/T$, so a sharp peak needs a short pulse, which carries little energy. A **chirp**, a long pulse whose frequency sweeps over $B$, has a wide spectrum and a long duration, so its correlation peak is about $1/B$ wide while its energy grows with $T$. The **compression ratio** is the time-bandwidth product $TB$.
:::

::: example
## Find the autocorrelation of a rectangular pulse of height $A$ and width $T$.

::: solution
1. **Slide and overlap.** Sliding the pulse by $\tau$ with $0 \le \tau \le T$, the copies overlap for $T - \tau$, where the product is $A^2$.
2. **The autocorrelation.** It's
   $$\begin{aligned} R_{xx}(\tau) &= A^2(T - |\tau|) \\ &\quad\text{for } |\tau| \le T \end{aligned}$$
   and 0 otherwise: a triangle with peak $A^2T$, the energy.
3. **The peak.** For $A$ = 2 V and $T$ = 1 ms:
   $$\begin{aligned} R_{xx}(0) &= A^2T \\ &= (2\ \text{V})^2 \times 1\ \text{ms} \\ &= 0.004\ \text{V}^2\cdot\text{s} \end{aligned}$$
4. **A quarter-pulse offset.** It's down to
   $$\begin{aligned} R_{xx}(T/4) &= 0.004 \times 0.75 \\ &= 0.003\ \text{V}^2\cdot\text{s} \end{aligned}$$
5. **Check against the spectrum.** Its transform, $A^2T^2\operatorname{sinc}^2(fT)$, is $|X(f)|^2$ for the pulse's $X(f) = AT\operatorname{sinc}(fT)$, as the theory says.

**Answer:** a triangle $A^2(T - |\tau|)$ for $|\tau| \le T$, peaking at the energy $A^2T$: 0.004 V²·s for a 2 V, 1 ms pulse.
:::
:::

::: example
## A radar's echo, correlated with the transmitted pulse, peaks at a lag of 20 µs. How far away is the target? With a 1 µs pulse, how close can two targets be and still show as two peaks?

::: solution
1. **The range.** The pulse travels out and back at $3 \times 10^8$ m/s, so
   $$\begin{aligned} R &= \frac{c\tau}{2} \\ &= \frac{3 \times 10^8 \times 20 \times 10^{-6}}{2}\ \text{m} \\ &= 3\ \text{km} \end{aligned}$$
2. **Two targets.** Two targets give two triangles, each 2 µs wide at the base. They stay separate if their delays differ by more than about one pulse width, 1 µs.
3. **The range resolution.**
   $$\begin{aligned} \Delta R &= \frac{c \times 10^{-6}}{2} \\ &= 150\ \text{m} \end{aligned}$$
4. **Sonar.** Sonar uses the same arithmetic at 1500 m/s, where a 2 ms delay means 1.5 m.

**Answer:** the target is 3 km away, and two targets show as two peaks if their ranges differ by more than about 150 m.
:::
:::

::: example
## A radar wants 15 m range resolution but needs a pulse 100 µs long to carry enough energy. How can it have both?

::: solution
1. **The peak width needed.** 15 m of resolution needs a correlation peak this wide:
   $$\frac{2 \times 15\ \text{m}}{c} = 0.1\ \text{µs}$$
2. **The bandwidth needed.** That means a bandwidth of about
   $$\frac{1}{0.1\ \text{µs}} = 10\ \text{MHz}$$
3. **What a plain pulse has.** A 100 µs pulse at a single frequency has only about 10 kHz of bandwidth.
4. **Sweep the frequency.** Sweeping the frequency over 10 MHz during the 100 µs gives the long pulse a 10 MHz spectrum, so correlating the echo with the transmitted chirp compresses it to about 0.1 µs.
5. **The compression ratio.**
   $$\begin{aligned} TB &= 100\ \text{µs} \times 10\ \text{MHz} \\ &= 1000 \end{aligned}$$
   The radar gets the energy of a long pulse with the resolution of a short one.

**Answer:** sweep the pulse's frequency over 10 MHz (a chirp) and correlate the echo with it, which compresses the 100 µs pulse to about 0.1 µs, a ratio of 1000.
:::
:::

::: check
given: A = 2 | 3 | 5; T = 1 | 2 | 4; f = 0.1 | 0.25 | 0.5 | 0.8
answer: A^2*T*1e-3*(1 - f)
unit: V²·s
tolerance: 0.5%

## A rectangular pulse is {A} V high and {T} ms long. What is its autocorrelation, in V²·s, at a lag of {f} of its width?

::: right
Right. The overlap is $(1 - \given{f})T$ long at height $A^2$: $R = A^2T(1 - |\tau|/T)$ = {answer}.
:::

::: misses
- [A^2*T*1e-3] That's the peak, at zero lag. An offset reduces the overlap.
- [A*T*1e-3*(1 - f)] The product of the pulse with itself is $A^2$, not $A$.
- [A^2*T*(1 - f)] Convert the width to seconds.
- [any] Not yet. Multiply $A^2$ by the length of the overlap, $T - |\tau|$.
:::
:::

::: check
given: t = 2 | 20 | 200 | 1000
answer: 3e8*t*1e-6/2
unit: m
tolerance: 0.5%

## A radar's correlation peaks at a lag of {t} µs. How far away is the target?

::: right
Right. The signal travels out and back, so the range is $c\tau/2$ = {answer}.
:::

::: misses
- [3e8*t*1e-6] That's the round trip. Halve it.
- [any] Not yet. Multiply the delay by the speed of light and halve it for the round trip.
:::
:::

::: check
given: A = 2 | 3; T = 0.5 | 2 | 10
answer: A^2*T*1e-6
unit: V²·s
tolerance: 0.5%

## A rectangular pulse {A} V high and {T} µs long has energy spectral density $A^2T^2\operatorname{sinc}^2(fT)$. What is the integral of that density over all frequencies, in V²·s?

::: right
Right. By Parseval the integral is the pulse's energy, $A^2T$ = {answer}, the same as its autocorrelation at zero lag.
:::

::: misses
- [A^2*(T*1e-6)^2] That's the density's value at $f = 0$. Integrating over frequency brings in a factor of $1/T$: the sinc² has total area $1/T$.
- [A*T*1e-6] The energy goes as $A^2$.
- [any] Not yet. Use Parseval: the integral of $|X|^2$ is the energy $\int x^2\,dt$.
:::
:::

::: check
kind: parts
given: T = 0.1 | 2 | 10; B = 1 | 5 | 50

## A radar transmits a {T} µs pulse.

::: part
answer: 3e8*T*1e-6/2
unit: m
tolerance: 0.5%

### If it's a plain pulse, about how close can two targets be in range and still be told apart?

::: misses
- [3e8*T*1e-6] The resolution in range is half the pulse's length in space, because of the round trip.
- [any] Not yet. Two echoes merge when their delays differ by less than about $T$, which is $cT/2$ of range.
:::
:::

::: part
answer: T*B
tolerance: 0.5%

### If instead it's a chirp sweeping {B} MHz, what is its compression ratio?

::: misses
- [B/T] The compression ratio is the time-bandwidth product, $T \times B$.
- [any] Not yet. Multiply the duration by the bandwidth, in consistent units: µs times MHz is a pure number.
:::
:::

::: right
Right. The chirp's resolution is about $c/2B$ whatever its length, so a long chirp keeps both the energy and the resolution.
:::
:::

::: check
kind: python
given: d = 137 | 412 | 800; k = 1 | 2 | 3

## The starter builds a random ±1 code `x` of 255 samples and a received signal `y`: the code delayed by `d` = {d} samples, scaled by 0.5 and buried in noise of standard deviation 1. Using `np.correlate(y, x, mode="full")`, set `r` to the correlation, `lags` to the lag of each entry (from $-(N_x - 1)$ upward), and `delay` to the lag of the largest value. Then set `snr` to the peak divided by the standard deviation of `r` away from it (lags more than 300 samples from the peak).

```python starter
import numpy as np
rng = np.random.default_rng(k)
x = rng.choice([-1.0, 1.0], 255)
y = rng.normal(0, 1, 2000)
y[d:d + 255] += 0.5 * x
r = np.correlate(y, x, mode="full")
lags = np.arange(len(r))
delay = int(np.argmax(r))
snr = 0
print(delay, snr)
```

```python solution
import numpy as np
rng = np.random.default_rng(k)
x = rng.choice([-1.0, 1.0], 255)
y = rng.normal(0, 1, 2000)
y[d:d + 255] += 0.5 * x
r = np.correlate(y, x, mode="full")
lags = np.arange(len(r)) - (len(x) - 1)
i = int(np.argmax(r))
delay = int(lags[i])
far = np.abs(lags - delay) > 300
snr = r[i] / np.std(r[far])
print(delay, snr)
```

```python tests
import numpy as np
assert np.array_equal(lags, np.arange(len(r)) - 254), "In 'full' mode the first entry is lag -(len(x) - 1) = -254."
assert delay == d, f"The correlation should peak at the true delay, {d} samples."
assert snr > 4, "The peak should stand well clear of the noise: about 0.5·255 against a spread of about √255."
```

::: right
Right. Each sample of the echo is half the size of the noise, but the correlation adds 255 of them coherently while the noise adds only as $\sqrt{255}$, a gain of 16. The code is invisible in `y` and obvious in `r`, which is how GPS works far below the noise.
:::
:::

::: check
kind: derivation
vars: A = 0.5..3, T = 1..5, tau = 0..1

## Derive the autocorrelation of a rectangular pulse of height $A$ and width $T$ for a lag $\tau$ between 0 and $T$.

::: step
answer: T - tau

### Over what length of time do the pulse and its copy, shifted by $\tau$, overlap?
:::

::: step
answer: A^2*(T - tau)

### What is $R(\tau)$, the integral of their product?
:::

::: right
Right. A triangle peaking at $A^2T$ and reaching zero at $\tau = T$; for negative lags it's the mirror image.
:::
:::

::: check
kind: choice

## Why is a signal's autocorrelation always largest at zero lag?

- [x] By the Cauchy-Schwarz inequality, $\int x(t)x(t + \tau)\,dt \le \int x^2\,dt$, with equality only when the copies line up
- [ ] Because signals are always largest at $t = 0$
- [ ] Because the Fourier transform is largest at zero frequency
- [ ] It isn't: periodic signals peak at their period

::: right
Right. Shifting can only make the two copies agree less, so their product's integral can only fall. A periodic signal does reach the same height again at multiples of its period, but never exceeds the zero-lag value.
:::

::: misses
- [2] The signal can peak anywhere. It's the match with itself that's best at zero shift.
- [3] Many pulses have their largest spectrum away from zero frequency, yet their autocorrelation still peaks at zero lag.
- [4] A periodic signal's autocorrelation reaches its zero-lag value again at each period, but never exceeds it.
:::
:::

::: explain
## A radar designer wants both long range, which needs a lot of pulse energy, and fine range resolution. Explain why a plain pulse can't give both, and how a chirp does.

::: model
Range resolution comes from how narrow the echo's correlation peak is, which is about one over the signal's bandwidth. A plain pulse's bandwidth is about one over its length, so a sharp peak needs a short pulse, and a short pulse at a limited transmitter power carries little energy, which limits range. A chirp breaks the link: its length sets its energy, while its frequency sweep sets its bandwidth independently. Correlating the echo with the transmitted chirp compresses it into a peak about $1/B$ wide, so the radar gets the energy of a long pulse and the resolution of a short one. The improvement is the time-bandwidth product.
:::
:::

::: practice

::: check
given: t = 1 | 4 | 10
answer: 1500*t*1e-3/2
unit: m
tolerance: 0.5%

## A sonar's echo arrives {t} ms after the ping, in water where sound travels 1500 m/s. How far away is the target?

::: misses
- [1500*t*1e-3] The ping goes out and back: halve it.
- [any] Range is $v\tau/2$.
:::
:::

::: check
given: B = 1 | 10 | 100
answer: 3e8/(2*B*1e6)
unit: m
tolerance: 0.5%

## A radar signal has {B} MHz of bandwidth. About what range resolution can it reach?

::: misses
- [3e8/(B*1e6)] Halve it for the round trip: $c/2B$.
- [any] Range resolution is about $c/(2B)$.
:::
:::
:::
