Correlation: how alike two signals are
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Correlation measures how alike two signals are, one sample at a time: line them up, multiply each pair, add. Slide one signal along the other and you get one number per shift (lag): the cross-correlation. Follow a sonar-style search: a known pattern p = [1, 1, −1] is hidden in 8 noisy received samples, and the correlation peaks at lag 3, the echo's delay. You will normalize the score to a value between −1 and 1, see why correlation is convolution without the flip, and do it all with np.correlate in Python. It builds on the lessons on discrete-time signals and discrete convolution.
Two signals: where is the pattern?
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?
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اقرأ الدرس خطوة بخطوة
Two signals: where is the pattern?
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?
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: correlationLine them up and multiply
Put p under the first three samples of x. Multiply each lined-up pair: 0.2·1, −0.3·1 and 0.1·(−1). Where both have the same sign, the product is positive. Opposite signs give a negative product.
lag l = 0: p[m] sits under x[m] x[0]·p[0] = 0.2·1 = 0.2 x[1]·p[1] = −0.3·1 = −0.3 x[2]·p[2] = 0.1·(−1) = −0.1 same sign -> product > 0 opposite signs -> product < 0Add: one number per position
Add the three products: 0.2 − 0.3 − 0.1 = −0.2. This multiply-and-add is the dot product. One number now says how alike p and this part of x are. Store it as r[0].
r[0] = 0.2·1 + (−0.3)·1 + 0.1·(−1) = −0.2 multiply pairs, then add: the dot product of two lists big and positive -> alike near zero -> unrelated negative -> opposite shapeSlide one sample, repeat
Shift p one sample to the right: this shift is the lag, l = 1. Multiply the new pairs and add again: r[1] = −1.2. Negative: here x leans the opposite way to p.
lag l: p is shifted l samples right r[1] = (−0.3)·1 + 0.1·1 + 1·(−1) = −1.2 r[l] = Σ x[n]·p[n − l] = Σ x[m + l]·p[m] (m = 0, 1, 2)Every lag gives one value
Keep sliding until p reaches the end of x. Eight samples and a three-sample pattern give 8 − 3 + 1 = 6 lags. The list r[l] is the cross-correlation of x and p.
r[0] = 0.2·1 + (−0.3)·1 + 0.1·(−1) = −0.2 r[1] = (−0.3)·1 + 0.1·1 + 1·(−1) = −1.2 r[2] = 0.1·1 + 1·1 + 0.9·(−1) = 0.2 r[3] = 1·1 + 0.9·1 + (−1.1)·(−1) = 3.0 r[4] = 0.9·1 + (−1.1)·1 + 0.2·(−1) = −0.4 r[5] = (−1.1)·1 + 0.2·1 + (−0.1)·(−1) = −0.8 lags where p fits: 8 − 3 + 1 = 6 r = [−0.2, −1.2, 0.2, 3.0, −0.4, −0.8]The peak finds the echo
The biggest value is r[3] = 3.0: at lag 3, p lines up with the echo. So the echo arrived 3 samples after we started listening. Radar, sonar and GPS find delays this way.
largest r[l]: r[3] = 3.0 -> the echo starts at x[3] x[3..5] = [1, 0.9, −1.1] p = [1, 1, −1] delay in time = lag × Ts (one sample period Ts per lag) this search is called a matched filterReading the number
Large and positive (r[3] = 3.0): same shape. Negative (r[1] = −1.2): leaning the opposite way. Close to zero (r[2] = 0.2): unrelated. The size also grows with loudness, so compare values from the same signals only.
r[3] = 3.0 alike r[1] = −1.2 opposite lean r[2] = 0.2 about unrelated double x -> every r[l] doubles the raw number mixes shape and sizeNormalize: a score from −1 to 1
Divide by the sizes of both lists: ρ = Σa·b / (√Σa² · √Σb²). The result always lies between −1 and 1. At lag 3, ρ = 0.997: almost the same shape. Loudness no longer matters.
a = x[3..5] = [1, 0.9, −1.1] b = p = [1, 1, −1] Σa·b = 3.0 √Σa² = √3.02, √Σb² = √3 ρ = 3.0 / (√3.02 · √3) = 0.997 ρ = 1 same shape (one is a scaled copy) ρ = 0 no match ρ = −1 same shape, upside down (Cauchy–Schwarz keeps |ρ| ≤ 1)Correlation is convolution, unflipped
Convolution flips one signal, then slides, multiplies and adds. Correlation slides without the flip. So correlating with p equals convolving with p reversed: [−1, 1, 1]. Same numbers, −0.2, −1.2, 0.2, 3.0, −0.4, −0.8.
convolution: y[n] = Σ x[k]·h[n − k] (h flipped) correlation: r[l] = Σ x[n]·p[n − l] (no flip) flip p first: q = p[::-1] = [−1, 1, 1] convolving with q flips it back to p np.convolve(x, p[::-1], 'valid') -> [-0.2 -1.2 0.2 3. -0.4 -0.8] (same as r)In Python: np.correlate
With numpy, np.correlate(x, p) slides, multiplies and adds for every lag where p fits (mode 'valid'). np.argmax(r) gives the index of the largest value: 3, the echo's delay in samples.
import numpy as np x = np.array([0.2, -0.3, 0.1, 1, 0.9, -1.1, 0.2, -0.1]) p = np.array([1, 1, -1]) r = np.correlate(x, p) >>> r array([-0.2, -1.2, 0.2, 3. , -0.4, -0.8]) >>> np.argmax(r) np.int64(3) (Python 3 with numpy; scipy.signal.correlate does the same and can use the FFT)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What does correlation do at one lag l?
r[l] = Σ x[n]·p[n − l]: shift, multiply the pairs that line up, add. One lag gives one number.
02A pattern p = [1, −1] lines up with the samples [2, −1]. What is the correlation value there?
2·1 + (−1)·(−1) = 2 + 1 = 3. Both pairs have the same sign, so both products are positive.
03Samples are 1 ms apart. The cross-correlation of a received signal with the sent pattern peaks at lag 7. What is the delay?
Each lag is one sample period, so the delay is 7 × 1 ms = 7 ms.
04The normalized correlation of two lists is ρ = −1. What does that mean?
ρ = ±1 only when one list is a scaled copy of the other (Cauchy–Schwarz). The minus sign means the scale factor is negative.
05How does correlation differ from convolution?
Both slide, multiply and add, but convolution uses h[n − k] (flipped). Correlating with p equals convolving with p reversed: np.convolve(x, p[::-1], 'valid') gives [-0.2 -1.2 0.2 3. -0.4 -0.8], the same as np.correlate(x, p).
المسرد
- Cross-correlation r[l]
- For each lag l, the sum of the products of one signal with the other shifted by l samples: r[l] = Σ x[n]·p[n − l].
- Lag
- How many samples one signal is shifted against the other before multiplying and adding.
- Dot product
- Multiply two equal-length lists pair by pair and add the products; one value of the correlation is a dot product.
- Normalized correlation ρ
- The dot product divided by the sizes of both lists, √Σa² · √Σb²; always between −1 and 1, so loudness does not matter.
- Matched filter
- A detector that correlates a received signal with a known template to find where (and whether) the template occurs.
- Barker code
- A short ±1 pattern, such as + + − for length 3, whose correlation with shifted copies of itself stays small; used in radar and in 802.11b Wi-Fi (length 11).
- np.correlate
- numpy's correlation: c_k = Σ a[n+k]·conj(v[n]); its default mode 'valid' keeps only the lags where v fits wholly inside a.
الملاحظات والمصادر
- 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.
- 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.
- J. G. Proakis, D. G. Manolakis, Digital Signal Processing: Principles, Algorithms, and Applications, Pearson Prentice Hall, 2007, ch. 2 (correlation of discrete-time signals) (Internet Archive record)
- A. V. Oppenheim, A. S. Willsky, S. H. Nawab, Signals and Systems, 2nd ed., Pearson (ch. 2: the convolution sum)
- A. V. Oppenheim, R. W. Schafer, J. R. Buck, Discrete-Time Signal Processing, 2nd ed. (course text listed on MIT OCW 6.341 readings)
- B. P. Lathi, R. A. Green, Linear Systems and Signals, Oxford University Press, 2018 (Internet Archive record)
- MIT OpenCourseWare 6.003 Signals and Systems (D. Freeman, Fall 2011)
- NumPy documentation: numpy.correlate (c_k = Σ a[n+k]·conj(v[n]), default mode 'valid')
- NumPy documentation: numpy.argmax (index of the first maximum)
- SciPy documentation: scipy.signal.correlate (z[k] = Σ x[l]·conj(y[l − k]); direct or FFT method)
- Cross-correlation (Wikipedia): searching a long signal for a shorter known feature; relation to convolution
- Matched filter (Wikipedia): correlating a known template with an unknown signal, as in radar
- Barker code (Wikipedia): length 3 = + + −; off-peak autocorrelation magnitude at most 1
- Cauchy–Schwarz inequality (Wikipedia): (Σ u·v)² ≤ Σu² · Σv², equality only for scaled copies
اختبار يتاح بعد الخطوة الأخيرة.