IIR Filter - Raw coefficients weird values?

Hi,

I’m trying to read the coefficients of a IIR peak filter, this way:

filterIIR.coefficients = *juce::dsp::IIR::Coefficients::makePeakFilter(getSampleRate(), 700, 1.0f, 1.0);
float c1Value = filterIIR.coefficients.get()->getRawCoefficients()[0]; // i.e. it should be C1

But it returns 1.0 on c1Value. Not sure why with those input values; if I look at JUCE source code: https://github.com/juce-framework/JUCE/blob/master/modules/juce_dsp/processors/juce_IIRFilter.cpp#L270

C1 (i.e. first value in the returned object) can be 1 only if alphaTimesA is 0 (since its 1 + alphaTimesA). But alphaTimesA can’t be 0 with gain 1 and freq 700.

Am I missing somethings?

The short answer is because the coefficients are normalised.

The function you linked is called internally here and the result is passed into the Coefficients constructor, which calls asignImpl() on itself which divides all the coefficients by the a0 coefficient.

For a peak filter, a0 and b0 are the same, so the first coefficient you read back was 1.0, instead of its original, non-normalised value.

1 Like

Oh I see now, miss that ctor logic inside, thanks.

Not sure about this:

for (size_t i = 0; i < Num; ++i)
    if (i != a0Index)
        coefficients.add (values[i] * a0Inv);

if a0 = b0, here it just “skip” the b0 valorization, but in fact it is set (i.e. I got a value, 1.04978 in this specific case). but clearQuick should set to 0, so where this value come from? at filterIIR.coefficients.get()->getRawCoefficients()[3] ?

Also, what’s the difference between a0 != NumericType() and a0 != 0.0?

Oops sorry, I misread the makePeakFilter()!

1 + alphaTimesA != 1 + alphaOverA :person_facepalming:

Practically there’s no difference, but since NumericType could be a float or double, comparing against 0.0 could throw a warning when using float. Although, NumericType() assumes that the compiler will initialise to 0.0 which, IIRC, isn’t guarenteed. Personally I’d have done:

a0 != static_cast<NumericType>(0) ?
1 Like

All clear!! Thanks dude, I’ll work it out and let you know if there are some more misunderstaing. Have a nice day.

1 Like