I want my Arduino project to listen to the ambient music and sync its light show output to the beat.

It seems that doing BPM detection properly is fiendishly difficult in software, but if your music is sufficiently doofy in nature and you use a little bit of hardware you can cheat and get away with it.

So my plan is to hook a mic/opamp breakout board to a simple 1st order passive low-pass filter and sample the input on an interrupt every 5ms or so. Audacity tells me a bass beat is around 15ms long, so every 5ms should be plenty to make sure I don't, er, "miss a beat".

If my low-pass'd sample is over a volume threshold, I'll record the time since the last beat, do some kind of weighted average calculation over the past few samples and that will allow me to predict how many ms until the next beat (some other software trickery might be necessary to account for bass drops and breakbeats.)

Firstly, do I have roughly the right idea?

Secondly, can anyone tell me what order-of-magnitude values I'll need for the resistor and capacitor? I figure I want my cutoff frequency to be something like 500-1000Hz. I also gather the resistance of the rest of my circuit counts but I'm not sure where to measure that resistance across. A little fiddling with this tool gives me answers that look reasonable (500Hz = 330Ω/1uF) but I don't know enough to really know.

An example schematic showing where I measure the resistance of the existing circuit and where the low pass filter goes in the context of the rest would be exceedingly helpful.

  • \$\begingroup\$ I don't think your BPM detection is going to work very well. You should try simulating it on a computer first. \$\endgroup\$
    – endolith
    Dec 20 '10 at 15:49
  • \$\begingroup\$ I hadn't thought of that but it'd be easy to do, good tip. \$\endgroup\$ Dec 20 '10 at 16:22
  • 3
    \$\begingroup\$ You can do it with LTspice. Save your audio file as a WAV file, and point a voltage source in your schematic at this file. Simulation will take a LONG time, probably at least 4x the length of your audio file. However, this is still quite speedy compared with the "build/code/compile/program/test" sequence. \$\endgroup\$ Dec 20 '10 at 22:02

A fair share of the spectral content of most music is below 500-1000 Hz, and with a fairly weak first-order filter falloff of 6 dB/octave, your waveform will be relatively unaffected. A much higher-order filter would be required, preferably at a much lower cutoff (440 Hz is concert A, bass hits should be way below that), with envelope detection (a diode and cap).

As you don't care about the actual fidelity of the audio, a passive filter should suffice, though the order required may significantly reduce your amplitude and reduce the effective resolution.


Just to reiterate other's concerns, the main problem of just filtering audio like this is that if you have some very loud hit (e.g. cymbal crash), it will still go through your LPF (low-pass filter) and give you spikes that you may interpret wrong. Another problem would be inability to cope with much dynamic range (music getting softer/louder); though you may be able to add some variable threshold.

As you've correctly stated, this approach will work best with Techno, but that still might not be very good.

  • \$\begingroup\$ Derp. I completely misread the frequency graph I made, I'm looking more at 50-60 Hz. Maybe I want a band pass filter instead? \$\endgroup\$ Dec 20 '10 at 16:35
  • \$\begingroup\$ @Rob, a low-pass filter would be fine. Audio will already be bandlimited to 20-20k. Added some more to my answer as well \$\endgroup\$
    – Nick T
    Dec 20 '10 at 18:26
  • \$\begingroup\$ I don't see why a cymbal crash would show up as a peak under my 60Hz cutoff though. Are there significant low-end harmonics down there even for cymbals? \$\endgroup\$ Dec 21 '10 at 17:47
  • 1
    \$\begingroup\$ @Rob, there are some, but the main point is that they're relatively loud, and unless you have a fairly high-order LPF, they may get through (and other loud hits) and still have a high enough amplitude to get confused with a bass thump. \$\endgroup\$
    – Nick T
    Dec 21 '10 at 18:13
  • \$\begingroup\$ Ah, I see—you're saying the critical thing is having a sharp cutoff at my target frequency. I'm not too proud to use an opamp IC with a higher-order low pass filter included, maybe with a trimpot to adjust the cutoff frequency. Now to see if it exists... \$\endgroup\$ Dec 22 '10 at 15:02

I hate linking to wikipedia for answers, but here is a good explanation: http://en.wikipedia.org/wiki/Low-pass_filter under Electronic low-pass filters

I do have some concerns with your plan though. A first order filter will give you a drop off of −6 dB per octave. This means that if you set your cut off frequency at 500Hz, you will only have a 6db drop off by 1khz. In music there is a lot going on in the sub 5khz frequency that you will still be passing through a decent amount. The digital filtering (weighted average calculation) you are doing may help you out, but not sure.

You can get a better frequency response by going to a higher order filter. Either way I think you will be much happier with an opamp low pass filter. You can also look into some digital filtering, its nothing to be scared of.

  • \$\begingroup\$ 500 Hz to 5000 kHz is 1 decade, not octave (would be 500-1000) \$\endgroup\$
    – Nick T
    Dec 20 '10 at 15:13
  • \$\begingroup\$ Opps it's 20db per decade 6db per octave, changed it. \$\endgroup\$
    – Kellenjb
    Dec 20 '10 at 15:23
  • \$\begingroup\$ I did read the Wikipedia entry, but it does get very theoretical very quickly which I find clouds my practical understanding. Do op-amp low pass filters exist as discrete parts or do I have to make my own? \$\endgroup\$ Dec 20 '10 at 16:26
  • \$\begingroup\$ @Robert - You can buy them as discrete parts, but it will probably be better for this special purpose just to make your own. Op amps come in (mostly) single, dual, or quad packages. You need to put in the capacitors and resistors to make a filter, and don't forget to power the opamp. \$\endgroup\$ Dec 20 '10 at 22:14

agreed on substance of music happening above the base range, so why not send two signals, run lpf on one, then run a hard gate on the other. That way you capture both rhythmic bass and peak sounds. :)

  • 2
    \$\begingroup\$ Some more details in your answer would be appreciated. Defining "lpf" and "hard gate" would probably be useful. \$\endgroup\$ Oct 17 '12 at 9:15

If you expect to recover a 200 Hz waveform (down in the bass), you need to sample MINIMALLY at 400 Hz for the ideal case, but 450 Hz would be better. If your filter isn't good ( and it probably won't be), you will have aliasing which will screw up your detection algorithms.

You NEED your sample frequency to be at least double the cutoff frequency of your filter to avoid aliasing. The cruddier the filter, the faster beyond that mathematical ideal you'll need to sample. With an IDEAL brick wall 500Hz filter, you need to sample at 1ms intervals.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.