x[0]0.2x[1]−0.3x[2]0.1x[3]1x[4]0.9x[5]−1.1x[6]0.2x[7]−0.1× +r[0]–r[1]–r[2]–r[3]–r[4]–r[5]–1import numpy as np2p = np.array([1, 1, -1])3r = np.correlate(x, p)4lag = np.argmax(r)5np.convolve(x,p[::-1],'valid')p[0]1p[1]1p[2]−1
01234567-1-0.500.51nvaluex[n] AND p AT LAG lechoρ = 0.997012345-2-10123lag l (samples)r[l]CORRELATION r[l]peak r[3] = 3.0 at lag 3alikeopposite≈ 0same: x ∗ p[::−1]np.argmax(r) = 3
Coloured parts: what the current step is about; everything else rests in whiteBlocks: the received samples x[n], the sliding pattern p, the multiply-and-add chip, the result row r[l] and a Python code cardPlots: x[n] with p at the current lag, and the correlation r[l] lag by lagTokens carry the three products into the chip and their sum into r[l]Cross-correlation r[l] = Σ x[n]·p[n − l] of a received signal with the pattern [1, 1, −1]: −0.2, −1.2, 0.2, 3.0, −0.4, −0.8, largest at lag 3. The figure keeps only the lags where p fits wholly inside x (numpy's mode 'valid'). Mode 'full' also gives the partial overlaps at both ends.

Two signals

  1. A sonar sends a short known pattern p = [1, 1, −1]. The receiver records 8 samples x[n]. The echo is hidden somewhere in small noise. Where is it, and how alike is it to p?

Multiply and add

Slide it

Reading the result

Convolution and code

The working

# step 1/10 · question

pattern  p[n] = [1, 1, −1]        (3 samples)
received x[n] = [0.2, −0.3, 0.1, 1, 0.9, −1.1, 0.2, −0.1]
                                (8 samples)

question 1: where does p sit inside x?
question 2: how alike are they there?

one tool answers both: correlation

The pattern [1, 1, −1] is the Barker code of length 3. The received samples are made up for the example: the pattern at a delay of 3 samples plus small noise. Every number in the figure is computed from this model. Books differ in which signal they shift, so r[l] in one text can be r[−l] in another. Here r[l] = Σ x[n]·p[n − l], the convention of Proakis & Manolakis and of scipy.signal.correlate; np.correlate(x, p) gives the same values for real signals.