DSP from Scratch · Lesson 5

Convolution Explained Simply: Flip, Slide, Multiply, Add

The most used recipe in signal processing, done in your head with one cup of coffee.

  • 14 min video
  • 11 min read
  • Lab and checks
Watch on YouTube
Chapters14 parts, tap one to jump there

In one sentence

Flip the impulse response, slide it, multiply each input by the weight under it, and add.

Watch first, then read. The video shows every idea moving. These notes write out each step so you can check the maths at your own pace. Press any ▶ time button to jump the video to that moment.

Drink one coffee every five hours, for a whole week, and the caffeine in your body never passes two cups. Not after ten coffees. Not after thirty.

Why? Because only your last few coffees really count. The recipe that tells you how much each one counts is called convolution. It is the same recipe that blurs the background of a photo, and makes a song sung in a bedroom sound as if it were sung in a cathedral. All it takes is one cup of coffee and four simple moves.

Quick recap

From lesson 4: your body clears about half its caffeine every five hours. So one cup, checked every five hours, leaves 1, ½, ¼, ⅛. That fading list is your body’s impulse response: what comes out after a single tap.

For coffees at 9 am, 2 pm and 7 pm we drew a copy of that list for each cup and added them up: ⅞ of a cup at midnight. That works, but it is slow. A song is 44,100 numbers every second (lesson 3); one copy for every number? There is a quicker way.

Turn it around: look back

Instead of following each cup forward, stand at midnight and look back at the cups you drank.

  • The 7 pm cup is one check old, so half of it is left.
  • The 2 pm cup is two checks old: a quarter is left.
  • The 9 am cup is three checks old: an eighth.

Add them: 18+14+12=78\frac{1}{8} + \frac{1}{4} + \frac{1}{2} = \frac{7}{8}. The same answer as last time, and we did not draw a single curve.

So the impulse response also tells you how much a past cup still counts, by its age. A cup drunk right now counts fully: 1. One check old: ½. Two checks: ¼. Three checks: ⅛. We call these numbers weights. (Strictly, it keeps halving, 116\tfrac{1}{16}, 132\tfrac{1}{32} and so on; those are tiny, so for now we keep just four weights.)

The strip and the recipe

Write the four weights on a strip of paper and lay it under the clock. On a clock line older times sit on the left, so the oldest weight, ⅛, goes on the left and the newest weight, 1, goes on the right.

Look at the strip: ⅛, ¼, ½, 1. That is the impulse response backwards. It is flipped, and that is not a maths trick: the oldest cups have faded the most, so their small weights end up on the left.

clock9 am2 pm7 pmmidnight
cups drunk1110
strip (weights)⅛¼½1
multiply⅛¼½0

add: ⅛ + ¼ + ½ + 0 = ⅞ of a cup at midnight

The strip is the impulse response 1, ½, ¼, ⅛ written backwards, because older cups sit on the left of the clock. Slide its 1 under the moment you want, multiply, add.

Run it along the whole day, and keep sliding even after the last cup:

MomentCups under the stripTotal
9 am1 × 11
2 pm1 × ½ + 1 × 11½
7 pm1 × ¼ + 1 × ½ + 1 × 11¾
midnight1 × ⅛ + 1 × ¼ + 1 × ½⅞
5 am1 × ⅛ + 1 × ¼⅜
10 am1 × ⅛⅛

Exactly the numbers we got last lesson by drawing copies, and the caffeine outlasts the coffee.

Three cups and a strip of four weights gave six answers. That is a handy rule:

output length=input length+impulse response length−1=3+4−1=6\text{output length} = \text{input length} + \text{impulse response length} - 1 = 3 + 4 - 1 = 6

Cups can be different sizes too: a double coffee counts double, two times the weight under it.

¾ of a cup. The 7 pm cup sits over ½: ½. The empty 2 pm slot gives nothing. The double at 9 am sits over ⅛: 2×18=142 \times \frac{1}{8} = \frac{1}{4}. Then 12+14=34\frac{1}{2} + \frac{1}{4} = \frac{3}{4}.

A week of coffee in one line

Back to the week: one cup every five hours, again and again. Slide the strip to any moment late in the week. How many cups are over it? Only four. Everything older is off the end of the strip. So at every check it is the same sum:

1+12+14+18=1781 + \tfrac{1}{2} + \tfrac{1}{4} + \tfrac{1}{8} = 1\tfrac{7}{8}

Drink your tenth cup or your thirtieth: the answer does not change. And the tiny leftovers we dropped? Each one covers half of the gap that is left to 2, so the total creeps closer and closer to two cups but never gets past it.

00.511.522 cups: never passed111½21¾31⅞45678910111⅞12
with four weightswith every tiny leftover added· numbers along the bottom: which coffee
From the fourth coffee on, the strip always covers the same four cups: 1 + ½ + ¼ + ⅛ = 1⅞. The tiny leftovers each cover half the gap that is left, so the true level creeps towards 2 but never passes it.

That is the power of the strip. However long the coffee diary, a week or a year, each answer needs just four multiplications and one sum. That is why even the tiny chip in a pair of earbuds can run a recipe like this on every number of a song, as it plays.

The real name: convolution

Flip the impulse response, slide it, multiply, add: that is called convolution. We write it with a star: the output equals the input, star, the impulse response.

y=x∗hy = x * h

In lesson 1 we wrote x[n]x[n] for the input at step nn. Call the impulse response hh and the output yy. Then one answer reads

y[n]=h[0] x[n]+h[1] x[n−1]+h[2] x[n−2]+⋯y[n] = h[0]\,x[n] + h[1]\,x[n-1] + h[2]\,x[n-2] + \cdots

In words: the first weight times the newest input, plus the second weight times the input one step ago, plus the third weight times the input two steps ago, and so on. That is our strip: the first weight, h[0]h[0], always goes with the newest input.

Books squeeze all of that into one line with the Greek letter Σ\Sigma (sigma), which just means “add them all up”, and a letter kk that counts how many steps back you are looking:

y[n]=∑kh[k] x[n−k]y[n] = \sum_{k} h[k]\, x[n-k]

Do not let it scare you: it is still flip, slide, multiply, add.

And last lesson’s way, one copy per input, sized, slid and added, is convolution too. The copies follow each input forward; the strip looks back from each output. Same sum, same answer: use whichever is easier.

An echo, and why the flip matters

Shout across a valley and you hear your voice, then a quieter copy a moment later: an echo. Here is the simplest echo as a system: the sound goes straight through, and a copy, half as loud, comes back two steps later. Tap it with a click and you get 1, 0, ½. That is its impulse response.

Feed it a short sound: 4, then 2. Flip the impulse response onto a strip, ½, 0, 1, and slide:

  • step 0: 1×4=41 \times 4 = 4
  • step 1: 1×2+0×4=21 \times 2 + 0 \times 4 = 2
  • step 2: the input has ended, but the ½ reaches back to the 4: 12×4=2\frac{1}{2} \times 4 = 2
  • step 3: 12×2=1\frac{1}{2} \times 2 = 1

The output is 4, 2, 2, 1: your sound, 4, 2, then its echo, 2, 1, the same shape half as loud, two steps later. Exactly what an echo should do.

Now forget to flip. Lay the strip down the wrong way round, 1, 0, ½, and slide it. You get 2, 1, 4, 2: the quiet copy comes first. An echo you would hear before you even shouted.

Flipped: 4, 2, 2, 1

Your sound, then its echo: half as loud, two steps later.

4step 02step 12step 21step 3

Forgot to flip: 2, 1, 4, 2

The quiet copy comes first: an echo before the shout.

2step 01step 14step 22step 3
the soundthe echo
The input 4, 2 through the echo system 1, 0, ½. Only the flipped strip puts the echo after the sound.

6, 0, 5, 0, 1. At step 2 the new 2 arrives just as the echo of the 6 comes back: 2+3=52 + 3 = 5. The echo lands on top of the next sound, and they simply add.

Convolution in real life

  • The cathedral. Engineers pop a balloon inside a real cathedral and record what comes back: thousands of echoes, slowly fading. That recording is the cathedral’s impulse response. Take a dry recording made in a room with no echo, convolve it with that impulse response, and it sounds as if it were played right there. Music apps call this convolution reverb; reverb is the wash of echoes a room adds.
  • Photos. A photo is a grid of tiny dots called pixels, and each pixel is a number: how bright it is. To blur it, slide a strip like ¼, ½, ¼ along each row, then down each column. Each new pixel becomes a mix of itself and its neighbours: the same recipe, in two directions.
  • The 7-day average on a news chart. A strip of seven equal weights, each 17\frac{1}{7}, sliding along the days. It smooths away the jumpy days, just like last lesson’s smoother.

Three common mistakes

Recap

  • To find the output at any moment, look back.
  • Flip the impulse response onto a strip, slide it to that moment, multiply each input by the weight under it, and add. That is convolution: y[n]=∑kh[k] x[n−k]y[n] = \sum_k h[k]\,x[n-k].
  • It gives the same answer as last lesson’s copies, and it explains why caffeine levels off, why an echo comes after the shout, and how a photo gets blurred.

Say it aloud once: flip, slide, multiply, add.

Next, lesson 6 feeds a system pure sine waves, and finds out why the same system lets a deep hum through but dims a high hiss.

Interactive lab

Flip, slide, multiply, add

Step the strip along any input, one output at a time. Try the coffee, the echo and the smoother, or type your own numbers. Then forget to flip and hear the echo come first.

Sign in to open the lab

It is free. Signing in with Google also keeps your progress and your scores.

Sign in with Google

Lesson 1 of every course is open without signing in.

Check yourself

Did it stick?

Work each one out on paper first. Every question has a full worked answer.

  1. 1

    In y[n]=h[0] x[n]+h[1] x[n−1]+h[2] x[n−2]+⋯y[n] = h[0]\,x[n] + h[1]\,x[n-1] + h[2]\,x[n-2] + \cdots, which input does the first weight h[0]h[0] go with?

    Question 1: pick one answer

  2. 2

    The impulse response is 1, ½. The input is 4, 2. What comes out?

    Question 2: pick one answer

  3. 3

    An input of 6 numbers goes into a system whose impulse response has 4. How many numbers come out?

  4. 4

    The echo system has impulse response 1, 0, ½. The input is 2, 0, 4. What comes out?

    Question 4: pick one answer

  5. 5

    Half the caffeine is gone every five hours. You drink one cup at 9 am, nothing at 2 pm, and a double coffee at 7 pm. How many cups of caffeine are in you at midnight? (A decimal is fine.)

Sign in to open the checks

It is free. Signing in with Google also keeps your progress and your scores.

Sign in with Google

Lesson 1 of every course is open without signing in.

Free weekly email

Get the next lesson by email.

The week's new lessons, the daily facts in one place, one thing to try in the lab and one puzzle. Free.

No spam. One click to leave.