ENع
→Discrete convolution
الدرس 17 · Discrete-time signals and systems

Discrete convolution, step by step

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

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

A linear, time-invariant digital system is described by one list of numbers: its impulse response h[n]. Feed an echo h = [1, 0.5, 0.25] the input x = [1, 2, 3] and work out the output two ways. First, every input sample makes its own scaled, shifted copy of h, and the copies add up. Then, one output at a time: flip h, slide it under x, multiply the lined-up pairs and add. Both give y = [1, 2.5, 4.25, 2, 0.75], the convolution sum y[n] = Σ x[k]·h[n − k]. Finally numpy does it in one line. It builds on "The impulse response of a digital system", "Discrete-time signals x[n]" and "System properties"; the continuous-time version is "Convolution and the impulse response".

ستتعلّم

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

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

The system: an echo h[n]

This digital system is an echo. Feed it one sample of 1, the impulse δ[n], and out comes 1, then 0.5, then 0.25. That list is its impulse response: h = [1, 0.5, 0.25].

δ[n] = 1 at n = 0, else 0

impulse in  ->  impulse response out
h[0] = 1
h[1] = 0.5
h[2] = 0.25
h[n] = 0 for every other n

more in "The impulse response of a
digital system" (it uses a different
example, h = [0.5, 0.3, 0.2])
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. The system: an echo h[n]

    This digital system is an echo. Feed it one sample of 1, the impulse δ[n], and out comes 1, then 0.5, then 0.25. That list is its impulse response: h = [1, 0.5, 0.25].

    δ[n] = 1 at n = 0, else 0
    
    impulse in  ->  impulse response out
    h[0] = 1
    h[1] = 0.5
    h[2] = 0.25
    h[n] = 0 for every other n
    
    more in "The impulse response of a
    digital system" (it uses a different
    example, h = [0.5, 0.3, 0.2])
  2. The input x[n]

    Now feed it a longer input: x = [1, 2, 3] at n = 0, 1, 2. Everywhere else x is 0. What comes out? Convolution answers that, one sample at a time.

    x[0] = 1
    x[1] = 2
    x[2] = 3
    x[n] = 0 for every other n
    
    a signal is a list of numbers:
    see "Discrete-time signals x[n]"
  3. Each input sample makes a copy of h

    The system is linear and time-invariant. So x[0] = 1 makes 1·h[n], x[1] = 2 makes 2·h[n − 1], one step later, and x[2] = 3 makes 3·h[n − 2]: three scaled, shifted echoes.

    input sample k  ->  x[k]·h[n − k]
    
    k = 0:  1·h[n]     = 1, 0.5, 0.25   at n = 0, 1, 2
    k = 1:  2·h[n − 1] = 2, 1, 0.5      at n = 1, 2, 3
    k = 2:  3·h[n − 2] = 3, 1.5, 0.75   at n = 2, 3, 4
    
    shift: time-invariance
    scale: linearity
    see "System properties"
  4. Add the copies: y = x ∗ h

    Linear also means the copies add. Stack them at every n and you get y = [1, 2.5, 4.25, 2, 0.75]. That sum is the convolution y[n] = Σ x[k]·h[n − k], written y = x ∗ h.

    y[n] = Σ_k x[k]·h[n − k]   (superposition)
    
    n = 0:  1                  = 1
    n = 1:  0.5 + 2            = 2.5
    n = 2:  0.25 + 1 + 3       = 4.25
    n = 3:  0.5 + 1.5          = 2
    n = 4:  0.75               = 0.75
    
    check: Σ y = Σ x · Σ h
           10.5 = 6 × 1.75
  5. Flip h and slide it under x

    Now compute one output at a time. Flip h to get h[−k]: the weights now read 0.25, 0.5, 1, with 1, the weight for the newest input, on the right. Slide it under x. Where it sits is the output time n.

    h[k]       weights 1, 0.5, 0.25 at k = 0, 1, 2
    h[−k]      the same, mirrored: k = 0, −1, −2
    h[n − k]   shifted right by n
    
    the weight h[0] = 1 sits under x[n]:
    the newest input counts the most
  6. n = 0: multiply the pairs, add

    At n = 0 only x[0] = 1 meets a weight, h[0] = 1. The other weights sit over zeros. Multiply each lined-up pair and add: y[0] = 1·1 = 1.

    y[0] = Σ_k x[k]·h[0 − k]
         = x[0]·h[0]
         = 1·1 = 1
    
    x[−1]·h[1] = 0·0.5 = 0
    x[−2]·h[2] = 0·0.25 = 0
  7. Slide one step: n = 1

    Slide the window one step right, to n = 1. Now two pairs line up: y[1] = 1·0.5 + 2·1 = 2.5. It is the same sum as before, read one output at a time.

    y[1] = x[0]·h[1] + x[1]·h[0]
         = 1·0.5 + 2·1 = 2.5
    
    the same number the stacked copies
    gave at n = 1
  8. n = 2: full overlap

    At n = 2 the window covers all of x: y[2] = 1·0.25 + 2·0.5 + 3·1 = 4.25. It is the largest output, because every input sample sits under a weight.

    y[2] = x[0]·h[2] + x[1]·h[1] + x[2]·h[0]
         = 1·0.25 + 2·0.5 + 3·1 = 4.25
    
    three pairs: the most overlap
  9. The window slides off the end

    Keep sliding. y[3] = 2·0.25 + 3·0.5 = 2, then y[4] = 3·0.25 = 0.75. After that no pairs meet, so y is 0. The output is 3 + 3 − 1 = 5 samples long.

    y[3] = x[1]·h[2] + x[2]·h[1] = 2
    y[4] = x[2]·h[2]            = 0.75
    y[5] = 0   (no pairs meet)
    
    length = N + M − 1 = 3 + 3 − 1 = 5
    y = [1, 2.5, 4.25, 2, 0.75]
  10. Convolution in Python

    In Python 3 with numpy, np.convolve(x, h) does every multiply and add for you. It prints the same five numbers: 1, 2.5, 4.25, 2 and 0.75. Its default "full" mode returns N + M − 1 values.

    import numpy as np
    x = np.array([1, 2, 3])
    h = np.array([1, 0.5, 0.25])
    y = np.convolve(x, h)
    print(y)
    
    [1.   2.5  4.25 2.   0.75]
    
    the same sum as two plain loops:
    y = [0] * (len(x) + len(h) - 1)
    for n in range(len(y)):
        for k in range(len(x)):
            if 0 <= n - k < len(h):
                y[n] += x[k] * h[n - k]
    
    (Python 3, numpy)

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