Go Down

Topic: Faster Filter, Analog or Digital (Read 2395 times) previous topic - next topic

rickso234

Which kind of filter, analog or digital, would yield a faster response with a minimum number of sampled cycles needed? I'm trying to detect a bass drum in music and want fast identification of the drum hit, and my choices are active analog filter or FIR/IIR digital filters.

The period of a 50 Hz sine is 20 msec. Is an entire cycle needed before a frequency detection can be realized or will a partial cycle provide enough information for detection of the frequency? My feeling is that an analog filter will provide some output before one cycle completes but a digital filter needs at least one cycle to provide enough information to identify the frequency.

pjrc

Neither will give any meaningful output with less than 1 cycle of the passband signal.

With any filter, analog or digital, the more selective it is, the sharper it transitions from passband to stopband, the longer it will take to respond.

rickso234

So then, like most other things, there is no free lunch.

Does "selective" refer more to the number of filter poles or, for a narrow bandpass, the Q?

DVDdoug

#3
Jul 05, 2017, 05:14 pm Last Edit: Jul 05, 2017, 05:35 pm by DVDdoug
A 50Hz band-pass filter or >50Hz* low-pass filter will pass a single-cycle 50Hz signal.  (A kick drum is more than one cycle, although it does decay.)    It doesn't matter if it's digital or analog.  A basic R-C filter may be just as good as a more-advanced filter for your purposes.    If you use an analog filter, you can connect-up your computer speakers or sound system and listen to what you're getting.   (The Arduino doesn't have a DAC so you can't listen to an Arduino digital filter.)

Of course, it's cheaper to make a digital filter and a more-advanced digital filter (sharper cutoff, etc.) is just a matter of more software.   But you can run-out of processing power, and if you're processor is doing lots of filtering that takes-away from other things you might want to do at the same time.

If you use an analog filter (or no filter at all), a peak detector (AKA "envelope follower") may be helpful because you Arduino only has to read the "loudness" at maybe 10 samples-per-second instead of reading the "waveform" at thousands of samples-per-second.  That frees-up your processor to do other things, but since the peak detector removes all of the frequency-information, you can't use a digital filter.


Of course, there is often bass guitar in music so you can't generally isolate the kick drum.    And, "the beat" may be dominated by higher frequencies. 

There are beat detection algorithms and you might want to research that.    I don't think beat detectors are sas good as the human brain, but I assume  (good)  beat detectors are "intelligent", or that they use some "fuzzy logic".   i.e. When you tap your foot to the beat (or dance), you don't wait for the beat before tapping your foot...   You "get into the rhythm", and anticipate the next beat.   You might also need some automatic level detection because some songs have a stronger bass drum than others, and some songs are just louder than others, etc.



....I've made a very-crude beat detector (for lighting effects).   My non-backed-up computer crashed and I lost the code.   But, here's how it works -  I use a peak detector (with no filter**) for all of my lighting effects.   The beat detector uses the previous peak as a reference, it then has a delay of about 200mS before it can be triggered again.   After the delay, another equally-loud beat can be detected immediately, and then the "sensitivity" starts increasing over the following several-milliseconds so the next peak/beat doesn't have to be exactly  as loud as the previous one.    It isn't perfect, but for my purposes it's "more interesting" than a perfect 1-2-3-4 beat detector. 

My beat detector could be improved by using a bandpass filter of about 3-4Hz on the envelope,  3-4 beats per second) and it could be improved even more by making a filter that "tunes" to the actual beat it finds, and maybe even better it I make it anticipate the next couple of beats...    But, that's more work and think it's more interesting if my lights to react dynamically to the sound, and only a couple of my effects use beat detection anyway.






* The cutoff frequency of a high-pass or low-filter is defined as the -3dB point.  So a "50Hz low-pass filter" will loose some 50hz signal.   If you want to get the full 50Hz signal, you need a higher cut-off frequency, depending on the steepness of the filter.   

** The use of a peak detector means that it's impossible to do any digital filtering, and all of my effects work form the loudness or "beat".     (And, they all automatically calibrate to a moving-average of the loudness or a moving-average of the peak.)




P.S.
One of my lighting effects is "the worlds simplest effect", and it sort-of reacts to "the beat".  I save one a reading from the peak detector (envelope follower) once per second and I make a 20-second moving average.      At the same time I'm reading the peak detector in a fast-loop while, comparing to the average.  When the reading is above average the light comes-on, and it's otherwise off.   The light is on about half the time and off about half the time, blinking to the music.

In the real-world I have more than one light and they can all go on & off together, or I can toggle some-on while the others are off, or I can trigger a random pattern, etc., making this simplest effect into multiple, more-interesting effects.

... Of course, that can be done without the peak detector circuit...   You just have to make your software "read the loudness".

Southpark

#4
Jul 05, 2017, 11:22 pm Last Edit: Jul 05, 2017, 11:25 pm by Southpark
The period of a 50 Hz sine is 20 msec. Is an entire cycle needed before a frequency detection can be realized or will a partial cycle provide enough information for detection of the frequency? My feeling is that an analog filter will provide some output before one cycle completes but a digital filter needs at least one cycle to provide enough information to identify the frequency.
In order to get proper results, both filters would require at least 1 full cycle, right?


For a drum hit .... should probably sample a drum hit and check on its bandwidth.... just to study it a bit.

rickso234

Thanks for all the info! Made some measurements last night and the response of both the analog and digital filters was anywhere from15ms to 35ms, so needs at least one full cycle. Was hoping to keep that latency to less than 20ms because above that you can notice the delay between the sound and the light lighting.

A peak detector responds pretty quickly, and detecting a higher frequency is quicker too. THere are probably some drum harmonics that can be detected rather than the fundamental which would give faster response. I could live with the 35ms, but the lights just don't have the "pop" as they do with a shorter latency.

THe nice thing about 50Hz is it seems to capture the drum pretty consistently and is not be affected much by bass guitar which is typically playing higher (but not always). I guess beat detection doesn't have to be perfect, just relatively consistent, with some added variety to keep it interesting.

So analog vs digital filters is still up for debate. Digital does require less hardware and is changeable on the fly (you can vary which bins you look at). Analog is less mystical though, and a peak detector is pretty simple to implement too.

Have thought about the idea of beat prediction, an intelligent determination of when the next beat will come, but just haven't wanted to tackle that bear for some reason. Maybe it's time.

Grumpy_Mike

Quote
Digital does require less hardware and is changeable on the fly (you can vary which bins you look at).
Their are no bins to look at with a digital filter, do you mean an FFT?

rickso234

Yes, I incorrectly lumped digital filters (IIR, FIR) with FFT and its derivatives. I'd think though that digital filters could be altered on the fly by changing their coefficients.

pjrc

If you alter a digital IIR filter's coefficients, what do you do about its stored state?

If you leave the old state in place, based on past input history computed with the old coefficients, how much more computation needs to be done with the new coefficients before you're confident the output is fully the new response?

I mean, with FIR the answer is simple.  But IIR?

rickso234

IIR has feedback, so that's where the "old coefficients" come from? How long would it take to flush the old?

I'd read that IIR has a faster response than FIR and its computations are simpler, but IIR can't use integer math like FIR so how is it faster and simpler?

So, for a low-end Arduino, which filter type would be better to perform beat detection... using 2 or 3 frequency bands? Would like to keep the latency low, since a delay of over about 20 msec. between sound and resulting light output is noticeable.

Grumpy_Mike

Quote
I'd read that IIR has a faster response than FIR and its computations are simpler, but IIR can't use integer math like FIR so how is it faster and simpler?
That is just rubbish in every respect.
Any filter can use interger maths in the form of fixed point arithmetic.

The time it takes to flush out old stored values depends on the transient response of the filter design.

By beat detection do you mean musical beats or beats caused by mixing of two very close frequencies?

rickso234

So once again, you can't believe everything you read on the Internet.

Beat detection is for musical beats. Was hoping to detect bass drum hits in the 50-60Hz range, with detection latency from drum beat to light illumination of around 15-20msec, the lowest detectable latency for a human, but that is proving difficult on an Arduino using either analog filters, digital filters, or FFT.

MarkT

Threshold detection on a gently (ie Bessel) low-pass filtered signal ought to work well.

You'd also need to estimate the longer term signal level to set the threshold high enough to
lose spurious triggering.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

pjrc

Any filter can use interger maths in the form of fixed point arithmetic.
I've seen numerous references to well known numerical overflow issues with biquad direct form 2 when implemented using fixed point.  Perhaps the may be some way, using a large number number of bits, to make this work with fixed point math, but the common wisdom seems to be that direct form 2 is not suitable for fixed point.

Of course, direct form 1 avoids these issues and does work very well with fixed point.

Even direct form 1 requires high levels of precision in the coefficients and intermediate calculations when implementing low corner frequencies.  I can tell you from experience with the biquad direct form 1 implementation I wrote for the Teensy audio library, using 32 bits for all coefficients and calculations works well down to only a few hundred HZ corner frequencies, with 44.1 kHz sample rate.

My point is even with direct form 1 which avoids the problematic wide numerical range of intermediate results, to believe any filter can be implemented with fixed point neglects the practical reality that certain filter responses, especially the very low frequency lowpass or bandpass mentioned on this thread, requires much more than 32 bit precision with biquad direct form 2, even if your input and output samples are only 8 or 16 bits.

Of course, other IIR filter types exist.  State variable or Chamberlin in particular has a whole different set of trade-offs.  It actually works very well for lower frequency responses without demanding high precision.  But it has difficult stability issues with coefficients for high frequency and underdamped response.

IIR filters can be quite tricky beasts.  Mosts textbooks and website barely even mention these fixed point numerical precision issues.  The authors and almost everyone in the academic community seems to only use Matlab with at least double precision floats.


Grumpy_Mike

Quote
I've seen numerous references to well known numerical overflow issues with biquad direct form 2 when implemented using fixed point.
Don't confuse the fact you get overflow errors with some implementations of fixed point numbers with the myth that you get it with all implementations of fixed point numbers. You just have to scale your fixed point numbers appropriately.

Go Up