C912 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C9 · Lesson 12 · Correlation and convolution lab

Correlating, finding periods, and undoing a filter in Python

The last few lessons built a toolkit on paper: convolution, impulse responses, difference equations and correlation. In this lab you'll write the pieces that use them on real data: a full cross-correlation, a period finder that works through noise, a deconvolver that recovers an input from a filter's output, the inverse of an echo, and a matched-filter detector that finds every copy of a pulse in a noisy record.

  • The first stems are an echo, h[n]=δ[n]+aδ[n−D]: the sound, then a copy D samples later scaled by a.
  • The second stems are its inverse, hinv[n]=(−a)n/D at multiples of D: each term cancels the echo the previous term made.
  • Raise a toward 1: the inverse decays more and more slowly. Past 1, an echo louder than the sound itself, the inverse grows without bound and can't be used.

Predict

A recording has an echo 0.6 times as loud, 3 samples late. After how many samples has its inverse filter's response fallen below 1% of its first value?

Name it

The tools

  • Cross-correlation: rxy[k]=∑nx[n+k]y[n], for every lag k where the two overlap, from −(Ly−1) to Lx−1.
  • Periods from autocorrelation: a periodic signal correlates strongly with itself shifted by one period. Skipping the lags where rxx is still positive from the main peak, the next highest point is the period. Noise adds mostly to lag 0, so this works where watching for zero crossings fails.
  • Deconvolution: if y = x * h and h[0] ≠ 0, the convolution sum can be run backward one sample at a time: x[n]=(y[n]−∑k≥1h[k]x[n−k])/h[0]. It's the inverse filter, applied as a recursion.
  • Matched-filter detection: correlate with the known pulse, and report each lag where the result is a local maximum above a set fraction of the pulse's energy.

Worked example

Deconvolve y = [1, 2.5, 4, 5.5, 2] by h = [1, 0.5].

Show the solution
  1. First sample. x[0]=1/1=1
  2. Peel off the echo. Each sample takes away 0.5 times the one before: x[1]=(2.5−0.5×1)/1=2x[2]=4−0.5×2=3x[3]=5.5−0.5×3=4
  3. Check. Indeed [1, 2, 3, 4] * [1, 0.5] = [1, 2.5, 4, 5.5, 2]. The last sample of y isn't needed: it's a check.

Answer x = [1, 2, 3, 4].

Worked example

The autocorrelation of a 25-sample-period sine is computed over 200 samples. Where are its first negative value and its next peak?

Show the solution
  1. Shape of the autocorrelation. r[k]≈12cos⁡(2πk/25) times the number of overlapping samples.
  2. First negative value. It first goes negative past a quarter period, at k = 7.
  3. Next peak. It peaks again at k = 25, a full period. The peak at 50 is lower, because fewer samples overlap at larger lags.

Answer the first negative value is at k = 7, and the next peak is at k = 25.

Check

An echo of 0.6, 2 samples late. At what sample does its inverse's response first fall to 1% of its start or below?

Check

Write xcorr(x, y): the cross-correlation rxy[k]=∑nx[n+k]y[n] for every lag k from −(len(y)−1) to len(x)−1, as a list in that order.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write period(x): compute the autocorrelation for lags 0 to len(x) // 2 − 1, skip lags until it first goes negative, and return the lag of the highest value after that.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write deconvolve(y, h): given y = x * h with h[0] ≠ 0, return x, which has len(y) − len(h) + 1 samples.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write echo_inverse(a, D, N): the first N samples of the inverse of the echo δ[n]+aδ[n−D].

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write detect(y, pulse, threshold): correlate y with the pulse at every lag k where the pulse fits (0 to len(y) − len(pulse)), and return the lags whose value is at least threshold times the pulse's energy and is the largest within len(pulse) − 1 lags either side.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does the autocorrelation find a period in noise more reliably than counting zero crossings?

Practice

Keep practicing

One kind of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.