Skip to content

Sensors & Signal Processing

How to remove a drifting baseline from a sensor signal

To remove a slow drift from a sensor signal and keep the quick changes, subtract a low-pass from the reading: fast = reading - slow, where slow is an exponential filter following the reading. Whatever the low-pass keeps, the subtraction throws away, so the result is a high-pass filter with the same cutoff. Put the cutoff between the drift and the signal you want.

Subtract the slow part

slow = slow + (reading - slow) * 0.05;
fast = reading - slow;

slow follows the drifting baseline, and fast is whatever rides on top of it. If the reading is 640 and slow is 600, fast is 40. Add the two parts back together and you get the reading exactly, slow + (reading − slow) = reading, so nothing is lost: the signal is just split into a slow part and a fast part.

A lighthouse at dawn

A light sensor watches a lighthouse flash once a second, each flash about 100 counts brighter than the gaps, while dawn lifts the whole sky from about 200 to over 700 in 8 seconds. A fixed line at 230 catches the first flash, then the brightening sky climbs past it and every moment looks like a flash: it sees 1 of 8. A line at fast > 40 rides along on the sky and sees all 8.

Live experiment

Spot the flashes through the dawn

A light sensor watches a lighthouse flash once a second while the dawn sky brightens by about 500. Try a fixed line, the fast part reading − slow, and the dips slow − reading, and count what each sees: 1 of 8 flashes, all 8, or the 9 gaps.

Try it yourself: free lesson

“Subtract the slow part” is lesson 4 of 6 in Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 16 before it first.

Choose the cutoff

cutoff ≈ alpha × sample rate ÷ 2π

A fingertip pulse sensor sees each heartbeat, about 1.2 Hz, riding on breathing at about 0.2 Hz. A cutoff between them, about 0.6 Hz, removes the breathing and keeps the beats: alpha 0.04 at 100 samples a second. Set it far too low, alpha 0.002 for about 0.03 Hz, and the breathing passes straight through with the beats, pushing some of them under the line.

The lag leaves an offset

slow follows the drift a little late, so while the baseline is rising, fast sits slightly above zero. That's why the line goes at 40 rather than just above 0. A higher cutoff tracks a fast drift more closely, at the cost of shrinking the slower parts of the signal you wanted to keep.

Where you'll meet it

Heart-rate monitors and pulse oximeters remove breathing and finger movement this way. ECG machines filter out baseline wander with a cutoff typically between 0.05 and 0.5 Hz. An oscilloscope's AC coupling does the same job with a capacitor, and a burglar alarm's motion sensor reacts to changes in heat, not to a warm room.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Subtract the slow part” is lesson 4 of 6 in Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 16 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3The sky brightens slowly from 200 to 700. After a high-pass, the sky's part of the reading…
    Show the answer

    Answer: Stays near 0 the whole time. slow follows the brightening sky, so reading − slow takes it away. Only quick changes, like the flashes, are left.

    • Why not “Flashes”? The sky changes slowly and steadily. Only the lighthouse flashes.
    • Why not “Doubles”? A high-pass removes slow changes; it doesn't amplify them.
    • Why not “Rises from 200 to 700”? That's the raw reading. slow follows the sky, so reading − slow takes it away.
  2. Question 2 of 3A heartbeat at 1.2 Hz rides on breathing at 0.2 Hz. Which cutoff lets the beat through and removes the breathing?
    Show the answer

    Answer: About 0.6 Hz: between the two. A high-pass removes what's below its cutoff and keeps what's above. Put it between the breathing and the heartbeat.

    • Why not “5 Hz”? 5 Hz is above the heartbeat too, so the beats would be removed along with the breathing.
    • Why not “Any cutoff”? A high-pass keeps what's above its cutoff and removes what's below, so the cutoff must sit between them.
    • Why not “0.05 Hz”? 0.05 Hz is below the breathing, so the breathing would pass straight through with the beats.
  3. Question 3 of 3Low-pass plus high-pass, added together, give back…
    Show the answer

    Answer: The original reading. slow + (reading − slow) = reading. Together they split the signal into its slow part and its fast part, with nothing lost.

    • Why not “Twice the reading”? The two parts don't overlap: together they make up the reading exactly once.
    • Why not “Only the noise”? The high-pass alone keeps the fast part. Adding the slow part back restores everything.
    • Why not “Nothing”? Each part keeps something. slow + (reading − slow) is the reading itself.
Keep practising: free lesson

“Subtract the slow part” is lesson 4 of 6 in Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 16 before it first.

Questions people ask

How do you remove a DC offset from a signal in code?

Subtract a slow running average: keep slow = slow + (x − slow) × alpha and use x − slow. That's a first-order high-pass filter. It removes the steady offset and any drift slower than its cutoff.

What is baseline drift?

A slow wander in a sensor's reading that isn't part of the signal you want, caused by temperature, movement, breathing or a changing background. It's removed with a high-pass filter whose cutoff sits below the signal's frequencies.

Is a reading minus its moving average a high-pass filter?

Yes. Whatever the average keeps, the subtraction removes, so the result passes the frequencies the average blocks. With an exponential average the cutoff is about alpha × sample rate ÷ 2π.

Every topic