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, : the sound, then a copy D samples later scaled by a.
- The second stems are its inverse, 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?
30: the inverse's terms are at n = 3k. is still just above 1%, and is the first below, at n = 30. An echo at 0.9 would need 44 terms, 132 samples. A weak echo is easy to remove; a strong one takes a long inverse filter.
Name it
The tools
- Cross-correlation: , for every lag k where the two overlap, from to .
- Periods from autocorrelation: a periodic signal correlates strongly with itself shifted by one period. Skipping the lags where 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: . 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
- First sample.
- Peel off the echo. Each sample takes away 0.5 times the one before:
- 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
- Shape of the autocorrelation. times the number of overlapping samples.
- First negative value. It first goes negative past a quarter period, at k = 7.
- 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 for every lag k from to , 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 .
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?
Zero crossings depend on individual samples near zero, and noise moves those samples back and forth across zero, adding false crossings. The autocorrelation at a lag adds up products over the whole record, so the periodic part reinforces itself at a lag of one period while the noise, which is uncorrelated from sample to sample, contributes products of random sign that largely cancel. White noise adds its power only at lag 0, which the method skips. The longer the record, the more the sum averages the noise away, so the peak at the true period stands out clearly even when the waveform itself is hard to see.
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.