Sensors & Signal Processing
How to measure loudness with an envelope follower
An envelope follower turns a sound into one number for how loud it is. Take the size of each sample, fabs(x), which is called rectifying, then smooth it. The average of a swing is zero, but the average of its size is its loudness. For quick sounds like claps, follow the peaks instead: jump up at once, then fade slowly.
Why not just smooth it?
A microphone's reading, with its middle taken off, swings above and below 0 hundreds of times a second. It spends as long below 0 as above, so smoothing x gives 0, for a whisper and a shout alike.
Rectify, then smooth
For a steady note, the result settles at about 0.64 of the peak, which is 2 ÷ π. Humming that peaks at 60 reads about 38; singing that peaks at 300 reads about 191. The number goes up in proportion with the swing.
Fast attack, slow release
float size = fabs(x);
if (size > env) env = size; // attack: jump to the new peak
else env = env * 0.995; // release: fade a little each sample
At 4000 samples a second, × 0.995 halves env in about 35 ms. One clap, over in about 30 ms with samples jumping wildly inside it, becomes one smooth bump.
Live experiment
Rectify, then smooth
A recording of quiet, then humming, then singing. Smooth x and the meter reads 0 throughout; smooth fabs(x) and it reads about 38 and 191; smooth x × x and the singing reads 25 times the humming; fabs(x) unsmoothed follows every wiggle.
“Follow the loudness” is lesson 5 of 6 in Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 17 before it first.
Make it a clap switch
Flip a lamp as env rises past an upper line, 250, and don't allow another flip until env has dropped below 100. Talking peaks at about 150 and a clap at about 550, so 250 catches claps and ignores talk. The samples inside one clap cross 250 dozens of times; the envelope crosses it once.
Peak, average or RMS
A peak follower answers "how high did it go?"; the average of fabs(x) answers "how big, on average?". RMS, the square root of the average of x², measures power: 0.71 of the peak for a sine. Smoothing x × x without the square root exaggerates loud sounds, so five times the swing reads 25 times as loud. Level meters, voice-activated recorders and the compressors that keep a radio station's loudness even each choose one of these.
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.
“Follow the loudness” is lesson 5 of 6 in Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 17 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: 250. Above anything talking reaches, below what a clap reaches.
- Why not “50”? 50 is far below talking, so every word would cross it.
- Why not “100”? 100 is below talking's 150 peaks, so talking would switch it too.
- Why not “600”? 600 is above a clap's 550, so no clap would ever reach it.
Show the answer
Answer: env stays above 250 for many samples: the lamp would flip on every one. An edge, not a level, once more. The flip happens once, and loud stays set until env has fallen below 100.
- Why not “It's the same thing”? A level is true for many samples in a row; an edge happens once. Flipping on the level flips on every one.
- Why not “It saves battery”? Both versions read the same sensor just as often. The difference is how many times one clap flips the lamp.
- Why not “Claps are too quiet”? A clap's envelope reaches about 550, well past 250. The question is flipping once, not hearing it.
Show the answer
Answer: The average of a swing is 0; the average of its size isn't. Smooth x and you get 0 for any sound. Smooth fabs(x) and you get its loudness.
- Why not “It removes the bass”? fabs keeps every frequency. Removing bass takes a high-pass filter.
- Why not “It's faster”? It costs a little time, if anything. It's there because smoothing x alone gives 0.
- Why not “fabs makes it louder”? fabs changes the sign of the negative samples, not their size.
“Follow the loudness” is lesson 5 of 6 in Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 17 before it first.
Questions people ask
What is an envelope follower?
A circuit or piece of code that tracks the outline, the envelope, of a signal's swings: rectify, then smooth or hold the peaks. Its output rises and falls with the loudness and ignores the individual waves.
How do I measure sound level with an Arduino?
Read the microphone module's analog output quickly, subtract its middle with a slow running average, take the absolute value of what's left and smooth it. For decibels, take 20 × log₁₀ of the result against a reference level.
What are attack and release?
Attack is how fast an envelope follower rises when the sound gets louder; release is how fast it falls when it gets quieter. A fast attack catches the start of a clap, and a slower release keeps one sound as one smooth bump.
Related topics
- AC voltage and RMS explainedPeak, period, frequency and RMS: the numbers that describe an AC sine wave.
- How an exponential moving average filter worksNudge a running value a fraction of the way toward each reading.
- What is hysteresis, and how does it stop flicker?On below one line, off above another: the gap that stops a noisy reading flickering.
- First-order vs second-order low-pass filters, in codeOne filter passes a tenth of a wave ten times past its cutoff. Two in a row pass a hundredth.