# No-lag causal bandpass filter kernel formula

Can anyone point me towards a formula for a good (for any values of "good") no-lag causal bandpass filter?

I am doing sound processing and I need to identify starting points of certain sounds (target precision <10 ms.) I've been using a Lanczos filter followed by convolution with a sine wave to do spectral analysis, but that gives me skewed results because amplitudes at any given time are being affected by stuff that's happening at later times (the filter is non-causal).

Simply cutting off the right half of the Lanczos filter resolves that particular problem, but the resulting spectrum is badly messed up (I get heavy sideband spillover). All other simple filters I tried are even worse.

Half an hour of googling turned up several mentions of Butterworth filters, lots of transfer functions (which I could not care less about), but no formulas of actual kernels.

EDIT: Let me try to be more specific. I start with a narrow-band filter with impulse response:

$$h(t) = \begin{cases} C \, e^{i 2 \pi t/T} \operatorname{sinc}^2\left(\tfrac{t}{NT}\right) \qquad & |t| \le NT \\ 0 & |t| > NT \\ \end{cases}$$

where $\operatorname{sinc}(u) \triangleq \frac{\sin(\pi u)}{\pi u}.$

With $T=$2 ms and $N=$10, that gives me 0 dB at 500 Hz and <-40 dB below 440 or above 560 Hz. The support is $2NT$=40 ms. If I use it on an input that goes from zero to a pure 500 Hz sine wave at time $t_0$, response goes from exactly zero to just over zero at $t_0$, to half the maximum level at $t_0+NT$, and stabilizes when the entire area under the filter is a sine wave. The "effective" lag (time to 50% level) is 20 ms.

What I'd like to do is to bring this "effective" lag as close to zero as possible, while maintaining the overall discrimination ability (being able to distinguish between a 440 Hz sine wave and a 500 Hz sine wave.)

• "no-lag" == zero group delay? Because that's mutually exclusive with "causal". – Marcus Müller Jun 9 '18 at 23:27
• Zero would be impossible, of course. Group delay on the order of one period of the bandpass center frequency would be ideal. – Eugene Smith Jun 9 '18 at 23:31
• well, i cleaned it up a little and i don't think i changed the intended meaning. this has support of $2NT$ and it is tuned to center frequency of $\frac{1}{T}$. and it is no lag (the phase delay and group delay are both zero). but it ain't causal. – robert bristow-johnson Jun 10 '18 at 6:24
• So, we can recommend minimal phase filters, but these don't have the property of having the same delay for all frequencies, i.e. you'd detect your 440 Hz at different point in time than your 500 Hz, should they appear simultaneously. Would they still help, @EugeneSmith? – Marcus Müller Jun 10 '18 at 8:31

What you need is not a "no-lag" filter, as such a thing doesn't exist if the filter is still causal (how should it filter, without knowledge of the future and not being aloud to wait).

What you need is a filter with a constant group delay. Any linear phase filter has that. Any time-symmetric filter has linear phase. And by the simple method of shifting a time-symmetric filter by half its length, you make it causal. And have half its length in delay, at any frequency.

So, any filter kernel that is symmetric works. There's far too many bandpass filters that have linear phase, and which one you'd use completely depends on your application. But it's usually easier to find a tool to design a linear-phase filter than to find one that gives a different kind of filter, so you should be fine.

• A symmetric bandpass filter would not work for me. It would, as you say, mean effective delay of half its width. So, say, I have a filter with bandwidth equal to 1/5th the center frequency, that means the length of the filter itself is on the order of 10T (where T is the period), that means the response is going to be effectively delayed by ~5T. – Eugene Smith Jun 10 '18 at 1:13
• @EugeneSmith: There is no relationship between the group delay of a filter and its bandwidth. What matters is the transition band width. Sharper transition bands require higher-order filters, which will have larger delay. There's no free lunch. I would recommend sharing your actual filter specifications so you can get specific suggestions. – Jason R Jun 10 '18 at 1:54
• @EugeneSmith It is likely that a combination of the elements mentioned in these answers will solve your problem but adjusting for the group delay on the index variable of the sound sample is probably the key idea. The other thing you might want to try is this idea – A_A Jun 10 '18 at 5:54

If you want roughly the same frequency response but don't care about phase. then a minimum phase approximation to your linear phase FIR filter might work.

You can use a cepstral approximation. Something like:

y = real(ifft(exp(fft(wn.*real(ifft(log(abs(fft(x)))))))));


Or there may be a matlab command that creates a similar filter.

If a IIR filter does what you want, then try flipping all the zeros to inside the unit circle to convert it to minimum phase.

A filter with a shorter response than a minimum phase filter will distort your desired frequency response. If you can weaken your filter requirements (by widening all the transition bands, allow more ripple, etc.) that generally results in a shorter filter lag (however you measure that) of both a linear phase and a minimum phase filter with those looser requirements.

• I'm afraid that is all Greek to me (including the formula, that does not look like any programming language I am familiar with) – Eugene Smith Jun 10 '18 at 2:33
• The right part, ifft(log(abs(fft(x... results in +-Inf+-NaNi due to the response of the log. I don't know this, but how is it supposed to work? (and what is wn?) – a concerned citizen Jun 10 '18 at 5:57

Prior to remez exchange, linear programming was proposed as a filter design method. There is a section in Rabinier and Gold’s 1975 book. The advantage of LP is that you can place constraints on the impulse response in addition to the frequency specification.

There is a tutorial using Matlab at:

http://eeweb.poly.edu/iselesni/EL713/linprog/linprog.pdf