Chuyển đến nội dung

Bài 11 · LTI Systems and Convolution

The Convolution Sum

Compute the discrete-time convolution sum three ways, by the flip-and-slide method, by a table, and in Python, and learn the output length and edge effects.

The convolution sum is the formula that turns an input and an impulse response into an output:

y[n] = (x * h)[n] = sum over k of x[k] h[n - k].

It looks abstract, but you can calculate it by hand with a few reliable procedures, and you can code it in five lines. This lesson shows the flip-and-slide picture, the shifted-copies picture, a table method, and numerical checks.

Picture 1: flip, slide, multiply, add

To get one output sample y[n]:

  1. Flip h[k] to get h[-k].
  2. Slide it by n to get h[n - k].
  3. Multiply point by point with x[k].
  4. Add all the products.

Repeat for each n. The overlap between x[k] and the moving flipped h changes as n grows, which makes the output rise, plateau and fall.

flowchart LR
    A["h[k]"] -->|"flip"| B["h[-k]"]
    B -->|"slide by n"| C["h[n-k]"]
    C -->|"multiply with x[k], sum"| D["y[n]"]

Do this for every n, and the sequence of sums is the output signal.

Python Flip and slide, one output at a time

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Picture 2: shifted copies

The same sum, read column by column: every input sample x[k] launches a scaled, delayed copy of h. Adding the copies gives y. For x = [1, 2, 3] and h = [1, 1, 1, 1]:

k contribution n = 0..5
0 1 * h starting at 0 1 1 1 1 0 0
1 2 * h starting at 1 0 2 2 2 2 0
2 3 * h starting at 2 0 0 3 3 3 3
sum 1 3 6 6 5 3

The output is [1, 3, 6, 6, 5, 3]. This method is fast for hand calculations with short signals.

Output length

If x has Lx samples and h has Lh samples (both starting at n = 0), the output has Lx + Lh - 1 samples. The first output index is the sum of the starting indices and the last is the sum of the end indices. Convolving x on n = 2..5 with h on n = -1..1 gives y on n = 1..6.

Python Shifted copies and the table method

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Special inputs

  • Convolving with delta[n] gives the signal back: x * delta = x.
  • Convolving with delta[n - k0] delays: x * delta[n - k0] = x[n - k0].
  • Convolving with a step u[n] produces a running sum.
  • Two decaying exponentials a^n u[n] and b^n u[n] (with a != b) give (a^(n+1) - b^(n+1)) / (a - b) for n >= 0.
Python Convolution with delta, delayed delta and step

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Writing your own convolution

A direct implementation uses two loops and costs Lx * Lh multiplications. NumPy's convolve does the same in C. For long signals, FFT-based methods (scipy.signal.fftconvolve) are much faster.

Python Convolution from scratch against the library

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Forgetting to flip h (that computes a correlation, not a convolution).
  • Getting the output length wrong; remember Lx + Lh - 1.
  • Using the wrong starting index when the signals do not start at zero.
  • Mixing up x * h (convolution) with x h (point-wise product).

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. x has 5 samples and h has 3 samples. How many samples does x * h have?

    Output length is Lx + Lh - 1 = 5 + 3 - 1 = 7.
  2. 2. x[n] * delta[n - 3] equals:

    Convolving with a shifted impulse shifts the signal by the same amount.
  3. 3. What is [1, 1] * [1, 1]?

    Copies [1, 1] at 0 and [1, 1] at 1 overlap: 1, 1+1, 1.
  4. 4. Which step distinguishes convolution from correlation?

    Convolution reverses h; correlation does not.

Summary

  • y[n] = sum of x[k] h[n - k]: flip, slide, multiply, add.
  • Equivalent view: sum of scaled, delayed copies of h, one per input sample.
  • Output length is Lx + Lh - 1; the start index is the sum of the start indices.
  • Convolving with a delta returns the signal; with a delayed delta, a delayed signal.
  • np.convolve and scipy.signal.fftconvolve compute it numerically.

Next: The Convolution Integral

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.