2

Assuming normalized biquad coefficients:

$$ H(z) = \frac{b_0 + b_1z^{-1} + b_2z^{-2}} {1 + a_1z^{-1} + a_2z^{-2}} $$

And taking the definition of flipping the polarity as multiplying every tap in a signal by -1 i.e.

$$[0.2, 0.1, -0.1, -0.2] \to [-0.2, -0.1, 0.1, 0.2]$$

For a zero delay biquad is it sufficient to check that $b_0 >= 0$ to assume the biquad is not flipping the polarity accidentally (although it will be modifying the waveform) and if it were, a correction $H'(z)$ would be:

$$ H'(z) = \frac{-b_0 - b_1z^{-1} - b_2z^{-2}} {1 + a_1z^{-1} + a_2z^{-2}} $$

This relates to audio.

keith
  • 906
  • 1
  • 7
  • 20

1 Answers1

1

A filter will not flip the polarity for all input signals. It might flip it for certain input frequencies, namely for the ones for which the filter's phase response equals $\pi+2k\pi$. The way to check if your filter flips the polarity of a relevant input signal (i.e., for a certain frequency), you have to evaluate the frequency response at that frequency. For a low pass filter you can check the filter's behavior at DC (i.e., frequency zero):

$$H(1)=\frac{b_0+b_1+b_2}{1+a_1+a_2}\tag{1}$$

You want this value to be positive. For a high pass filter you may want to check the frequency response at Nyquist:

$$H(-1)=\frac{b_0-b_1+b_2}{1-a_1+a_2}\tag{2}$$

And for a band pass filter you should check the frequency response at the center frequency.

In sum, a non-trivial filter will not flip the sign for all input frequencies, it might flip it for specific frequencies. Depending on the filter type, there are certain prominent frequencies (e.g., DC or Nyquist) that you need to check.

EDIT:

It is straightforward to show that checking the sign of $b_0$ is not sufficient. Take the coefficients

b = [0.13111  -0.26221  -0.13111];
a = [1.00000  -0.74779   0.27221];

We have $b_0>0$, but the frequency response at DC is

$$H(1)=-0.5$$

and so the sign of a constant input signal will be flipped (as soon as the transients have died out):

enter image description here

Matt L.
  • 89,963
  • 9
  • 79
  • 179
  • It's the pass band that's of interest i.e. the part of the signal frequency content that's not changed. For instance in stereo audio, if I'm filtering just one channel, whether I'm using H(z) or H'(z) in my example will have huge consequences on the stereo image. – keith Sep 21 '18 at 09:59
  • @keith: That's what I assumed anyway. It's just important to realize that also in the pass band, different frequencies will experience different phase shifts. – Matt L. Sep 21 '18 at 10:01
  • OK thanks, I'll think on it some more. I have a filter designer and I was hoping for a simple mathematical check to determine if the signage is wrong on any of the designed filters. – keith Sep 21 '18 at 10:04
  • @keith: Yes, it's simple, just choose a frequency representative of the pass band and check the phase of the frequency response. It's especially simple for DC and Nyquist (Eqs (1) and (2) of my answer). – Matt L. Sep 21 '18 at 10:06
  • So would it be fair to say, if you know that the phase shift of the biquad is less that or equal to 90 degrees, then it wouldn't matter what frequency to sample the biquad at based on your technique? – keith Sep 21 '18 at 10:15
  • @keith: Yes, if there's no sign reversal (or something close to it) at a certain frequency in the pass band, then the rest of the pass band will also be OK. Normally, filters designed by standard methods won't flip the sign in the pass band anyway. – Matt L. Sep 21 '18 at 10:25
  • If I have a sine wave at 500Hz (whatever frequency doesn't matter), and I low pass it or high pass it with any zero latency butterworth biquad (at any frequency) then at no point would a positive sample become a negative sample unless I multiply the numerator coefficients by -1 (otherwise it implies the sine wave has flipped). Frequency seems to be irrelevant here which means it must be possible to send a single positive or negative sample (say 1) through the difference equation to deduce if it's changing the polarity? – keith Sep 21 '18 at 10:25
  • @keith: It depends on the filter, but for a standard low pass filter, a signal close to Nyquist (and hence in the stop band) would experience a phase shift close to $\pi$. But as I said, normally there's no sign flipping in the pass band. But I can't know how you design your filters, so it's always good to check. – Matt L. Sep 21 '18 at 10:28
  • Using this page http://www.earlevel.com/main/2010/12/20/biquad-calculator/ which I’m assuming uses the RBJ cookbook formula, coefficient a0 (same as my b0) never goes negative for any filter type and parameter value which confirms my theory. I’m therefore not satisfied your answer refutes my theory, but I appreciate your help nonetheless. – keith Sep 21 '18 at 12:44
  • @keith: check my edit for an example with $b_0>0$ flipping the sign at DC. – Matt L. Sep 21 '18 at 15:20
  • Thanks for helping me thrash this out. I like the trick of thinking about what happens at DC that's very useful. – keith Sep 21 '18 at 17:00
  • I had a further thought on this: one option might be to compute the poles/zeros. If the scaling factor $s$ to ensure $s(z^{-1} - z_1)(z^{-1} - z_2) / (z^{-1} - p_1)(z^{-1} - p_2)$ is equal the original filter, then the polarity should not be flipped :-) – keith Oct 18 '18 at 13:21