ENع
→Correlation
الدرس 20 · Discrete-time signals and systems

Correlation: how alike two signals are

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 20 · تفاعلي
01 / 10

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. Line 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 < 0
  3. Add: 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 shape
  4. Slide 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)
  5. 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]
  6. 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 filter
  7. Reading 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 size
  8. Normalize: 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)
  9. 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)
  10. 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)

اختبار يتاح بعد الخطوة الأخيرة.