Topic : How to do beat detection
Author : Ilia Yordanov
Page : 1

How to do beat detection


Hey all!

I recently had to program a beats analyzer. What it does is to count beats in a given music file. And it worked pretty well after all, so here is how you can do it... I consider you have good understanding of DSP algorithms.

   First, let's define beat- a beat is considered when we have noticable higher sound. Usually, the beats are the drums, but they can also be guitar and else... but most of all, the beats are the drums, because the drums sound with much higher amplitude than the rest of the instruments.

   To do beat detection, you should do a few things... first of all, you should decide for what kind of music you want it- pop,roc,dance,classical,country,jazz... As a matter of fact, beat detection (often written as BPM which means Beats Per Minute) can't be accurately applised on classical music, for the simple reason they have almost no beats... and for sure- no tempo... yes, to detect beats, you need to have some tempo- constantly repeating part of the music. Actually, that's the use to know the BPM of a song- this shows you how fast it is. DJs use this mostly.
So, once you decided what kind of music you want to apply it, then you can do this:
1) Create a band-pass filter and apply it on the sound. From my tests, it showed out that for ROCK/POP/METAL, the best frequency domain to keep is: 40Hz-500Hz. For RAP/TECHNO and more drummy music- 40Hz-80Hz or 40Hz-100Hz (maximum!!!). Values below 40Hz are unnecessary usually, but if you want, you can make it from 20Hz too... it won't do big difference.
For other genres- you must do tests to see the frequency domain of the instrument you want to consider as beat producing (just like drums are beat producing for RAP/TECHNO)
2) Now, that you have filtered the beats-producing instrument, you should analyze the sound. For 44100 samples per second sound, you could analyze for 1024 or 2048 samples at a time.
What you should do is to calculate the average amplitude of sound of buffer (of n*1024 samples length, where n is recommended to be 1,2,3 and maximum 4!). Once you get this value, keep it in a temporary variable. Then analyze the next bunch of data, and compare the two values. If the second one is much higher than the first one- count beat, if not- don't count beat.

Some improvements:
1) You can add a variable C which's value you can modify to set the precision for comparison between two buffers.
2) Some songs start with beat. In this cases, you have high amplitude buffer and then low amplitude buffer, so this won't be counted as beat. So, to know for existance of such a beat, just analyze the first K buffers of the song seperately, and compare just the opposite way- if the current buffer is smaller then the previous- we HAD beat, so countt it. I recommend K to be 2,3 or maximum 4.
3) I suggest you add some other filter- do not only compare the current buffer to the previous, but compare it also the the average amplitude of the last P buffers. Where P can be somewhere between 10 and 15, depending on the song. This way, you get more precise results!


A better way:
The better way to count beats is to use FFT to divide the sound into the frequency/amplitude domain, so you can analyze several frequency domains at a time. Of course, lower frequencies are with higher priority. This though, takes more CPU time and is harder to code. The buffers comparison technique is the same though.

So, this is VERY SHORT description for DSP maniacs, how to count beats in a song.
P.S. I coded all this stuff and added some functions to analyze the song so I can do better filtering and comparison. I coded it in C++, but it was for commercial product, so I can't release any of the code, sorry!

Page : 1