DSP from Scratch · Lesson 4

How Much Coffee Is Still in You at Midnight? The Impulse Response

One measurement after one cup predicts any day of coffee. The same idea predicts what any system does.

  • 13 min video
  • 10 min read
  • Lab and checks
Watch on YouTube
Chapters17 parts, tap one to jump there

In one sentence

One tap in, one answer out, and that answer predicts everything.

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.

Here is a strange fact. If you drink a coffee at seven in the evening, about half of its caffeine is still in your body at midnight. That is like drinking half a coffee right before bed. And if you had two more coffees earlier that day, the total at midnight is almost a whole cup.

How can we possibly work that out? With one simple idea: measure what happens after one single cup, and you can predict any day of coffee. In signal processing this idea is called the impulse response. Engineers use it to test speakers, train wheels and bridges; here we learn it with your own body.

By the end you will be able to answer this: you drink a coffee at 9 am, 2 pm and 7 pm. How much caffeine is still in you at midnight?

Quick recap

In lesson 1 we saw that a digital signal is a list of measurements taken at regular moments, like a nurse writing down your temperature every hour. Today our list is the caffeine in your body, measured in cups: one cup means the caffeine from one ordinary cup of coffee.

One cup: watch the caffeine fade

Start with a single cup at 9 am. Its caffeine quickly soaks into your blood: one cup’s worth. Then your body starts clearing it out, slowly. For a typical adult, about half is gone after five hours, so let us check every five hours: at 2 pm, 7 pm and midnight.

  • At 2 pm, half a cup is left.
  • At 7 pm, half of that: a quarter.
  • At midnight, half again: an eighth of a cup. Fifteen hours later, it still has not gone.
00.5119 am½2 pm¼7 pm⅛midnight
One cup at 9 am. Every five hours, half of what was left is gone: 1, ½, ¼, ⅛. Fifteen hours later it still hasn't gone. This shape is the only measurement we need.

So one cup gives this curve: 1, ½, ¼, ⅛. Tall at first, then fading. Keep your eye on this shape: it is the only measurement we will ever need.

Real people differ: some clear caffeine in three hours, others take eight or more. But the idea works the same for everyone, so we will stick with five. (You can change it in the lab.)

Two simple rules

What if you drink two cups at once, or have your coffee later in the day? Do we need a new test for every possibility? No. Your body follows two simple rules, and they do all the work.

Rule 1: twice the coffee gives twice the caffeine, at every check. A double coffee gives 2, 1, ½, ¼: the same shape, just twice as tall. Half a cup gives the same shape, half as tall. And when coffees overlap, their caffeine simply adds up; your blood does not care which cup it came from.

Rule 2: the same coffee later gives the same curve, just later. A coffee at 2 pm gives 1, ½, ¼, ⅛, starting at 2 pm. The whole curve simply slides along the clock.

Rule 1: twice the coffee, twice the caffeine

A double coffee at 9 am: 2, 1, ½, ¼. Same shape, twice as tall.

01229 am12 pm½7 pm¼mid.

Rule 2: a later coffee gives the same curve, later

One cup at 2 pm: 1, ½, ¼, starting at 2 pm.

0129 am12 pm½7 pm¼mid.
Two rules your body follows. Sizing and sliding the one-cup curve is all it takes.

Half a cup. From 2 pm to midnight is two checks, and after two checks one cup leaves a quarter. Twice a quarter is a half.

A whole day of coffee

Now the big question: coffees at 9 am, 2 pm and 7 pm. Here is the trick: deal with each cup on its own. Each one makes its own copy of the curve, starting at the moment you drink it.

  • The 9 am cup: 1, ½, ¼, ⅛.
  • The 2 pm cup: the same curve, starting one check later.
  • The 7 pm cup: one check later again.

Now add them up at each check:

Check9 am cup2 pm cup7 pm cupTotal
9 am11
2 pm½11½
7 pm¼½11¾
midnight⅛¼½⅞
00.511.5219 am1½2 pm1¾7 pm⅞midnight
9 am cup2 pm cup7 pm cup
Each cup brings its own copy of the curve, starting when you drink it. Stack the copies at each check. At midnight: ⅛ + ¼ + ½ = ⅞ of a cup, and the biggest piece is the 7 pm cup.

At midnight: 18+14+12=78\frac{1}{8} + \frac{1}{4} + \frac{1}{2} = \frac{7}{8} of a cup. Almost a whole coffee, still in you as you go to bed. And look where most of it comes from: the 7 pm cup, which on its own leaves half a cup at midnight. That is why an evening coffee keeps you awake.

One measurement predicts them all

Here is the surprising part. We never tested three cups. We never looked inside your body. We measured one cup, once. Everything else came from copying that one curve:

  1. sized to the cup,
  2. slid to the moment you drank it,
  3. and added up.

And it works for any coffee day at all: a cup here, half a cup there, a double at lunch. One measurement predicts them all.

The real names

Engineers use these ideas every day, so let us give them their real names.

  • Anything that takes a signal in and gives a signal out is called a system. Here, your body is the system. Its input is your coffee, a list of how many cups you drink at each moment; its output is the caffeine in your blood.
  • A single, short input, like one cup at one moment, is called an impulse. Think of it as one quick tap.
  • What comes out after that single tap is called the impulse response, often written h[n]h[n]. Your body’s impulse response, checked every five hours, is 1,12,14,181, \tfrac{1}{2}, \tfrac{1}{4}, \tfrac{1}{8}.

The two rules have names too:

  • Twice the input gives twice the output, and overlapping inputs simply add: that is called linear.
  • The same input later gives the same output later: that is called time-invariant.

When the rules break

The rules really must hold. Turn a small speaker up too far and it starts to crackle: it cannot move any further, so twice the input no longer gives twice the output. It is not linear, and the copies will not add up to the right answer.

And if someone keeps turning a volume knob while you test, a tap now and a tap later get different answers. That system is not time-invariant.

The same idea with sound

In a computer, a sound is a list of numbers, one for each moment. So the simplest tap is a list with a single 1 and zeros everywhere else:

1, 0, 0, 0, …1,\ 0,\ 0,\ 0,\ \dots

It is called the unit impulse: “unit” because its size is one. Played as a sound, it is a single sharp click.

Here is a simple system: a tiny program that smooths out jumpy numbers. Each output is the average of the newest number and the one just before it. Writing x[n]x[n] for the input and y[n]y[n] for the output, as in lesson 1:

y[n]=x[n]+x[n−1]2y[n] = \frac{x[n] + x[n-1]}{2}

where x[n−1]x[n-1] means “the number one step before x[n]x[n]”.

Tap it with the unit impulse. At the first step the newest number is 1, with nothing before it, so the average is ½. At the next step it is 0 and 1: ½ again. After that, only zeros. So its impulse response is

h[n]=12, 12h[n] = \tfrac{1}{2},\ \tfrac{1}{2}

Your body’s response fades slowly; this one is over in two steps. Every system has its own impulse response, a bit like a fingerprint.

Now a real input: 2, then 4. Treat each number as a tap.

  • The 2 makes a copy twice as tall: 1, 1.
  • The 4 makes a copy four times as tall, one step later: 2, 2.
  • Add them up: 1, then 1+2=31 + 2 = 3, then 2. The output is 1, 3, 2.
copy of the 211copy of the 422output (added)132step 0step 1step 2
Each input number is a tap. Size the impulse response (½, ½) to the tap, slide it to the tap's step, then add: 1, 3, 2.

Check it the long way, by averaging: 2 and 0 give 1; 4 and 2 give 3; 0 and 4 give 2. Exactly the same.

3, 4, 1. The 6 makes 3, 3; the 2 makes 1, 1, one step later. Added: 3, then 3+1=43 + 1 = 4, then 1.

The one-tap test in real life

  • Speakers. Engineers play a single click through a new speaker and record what comes out. That recording is the speaker’s impulse response, and it tells them how the speaker will sound with any song.
  • Train wheels. Railway workers once tapped every train wheel with a hammer and listened. A healthy wheel rings clearly; a cracked wheel sounds dull. The crack changes the system, so it changes the impulse response. Engineers still tap aircraft panels and bridges for the same reason.
  • Medicine. Scientists measure the level in the blood after a single dose, then add up copies of that curve to plan a schedule, like one tablet every eight hours, that keeps the level high enough to work but never too high. Exactly our coffee trick.

Three common mistakes

Recap

  • A system turns an input signal into an output signal.
  • Give it a single tap, and what comes out is its impulse response.
  • If the system is linear and time-invariant, every input is a row of taps. Each tap brings a copy of the impulse response, sized to the tap and slid to its moment. Add the copies and you have the output.

Say the main idea aloud once: one tap in, one answer out, and that answer predicts everything.

Next, lesson 5 turns copy, slide and add into one quick recipe for any input. It is called convolution.

Interactive lab

Plan a day of coffee, then feed the smoother

Tap the hours you drink coffee and watch each cup bring its own copy of the one-cup curve. Then type any input into the two-point smoother and see the copies add up.

1. Plan a day of coffee

Each colour is one cup’s own copy of the curve. The bar at each hour is all the copies stacked.

2. Feed the smoother

Type any input. Each number brings its own copy of ½, ½; the copies are added, then checked by averaging.

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

    What is an impulse response?

    Question 1: pick one answer

  2. 2

    A system’s impulse response is 1, ½, ¼. You tap it with a size of 4. If the system is linear, what comes out?

    Question 2: pick one answer

  3. 3

    Half the caffeine is gone every five hours. You drink a double coffee (two cups’ worth) at 7 pm. How much caffeine from it is still in you at midnight?

  4. 4

    A system gives three times the output when you double the input. Can you predict it by sizing, sliding and adding copies of its impulse response?

    Question 4: pick one answer

  5. 5

    The two-point smoother has impulse response ½, ½. The input is 4, then 6. What comes out?

    Question 5: pick one answer

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.