Skip to content

[AUD-008][High] Correct band IIR section capacity and gain normalization #304

Description

@gabrielsantosphilips

Audit finding

  • ID: AUD-008
  • Status: Verified defect
  • Severity: High
  • Confidence: High
  • Audited revision: 2f479320d805a1f9f35ebe4afaaeeded48913a94

Problem

Band transforms create 2 * order poles, but storage remains ceil(MaxOrder/2) sections. BuildSections() silently stops at capacity. Band-pass and band-stop also hardcode scalar gain 1.

Source:

static constexpr std::size_t kMaxSections{ (MaxOrder + 1) / 2 };
using ComplexT = math::Complex<T>;
,
while (ai < numA && count < kMaxSections)
{
T a1{};
T a2{};
T b0{};
T b1{};
T b2{};
PairPole(az, numA, aUsed, ai, a1, a2);
PairZero(bz, numB, bUsed, bi, b0, b1, b2);
const T sectionGain{ (count == 0) ? gain : T{ 1 } };
if (math::Abs(a2) < static_cast<T>(1e-10))
out[count] = { sectionGain * b0, sectionGain * b1, T{ 0 }, a1, T{ 0 } };
else
out[count] = { sectionGain * b0, sectionGain * b1, sectionGain * b2, a1, a2 };
++count;
while (ai < numA && aUsed[ai])
++ai;
}
}
template<typename T, std::size_t MaxOrder>
OPTIMIZE_FOR_SPEED std::size_t IirFilterDesign<T, MaxOrder>::DesignLowPass(std::size_t order, T wc, T fs,
const std::array<ComplexT, 16>& protoPoles) noexcept
{
std::array<ComplexT, 32> az{};
std::array<ComplexT, 32> bz{};
const std::size_t numA{ order };
const std::size_t numB{ order };
, and
const T gain{ T{ 1 } };
BuildSections(bz, numB, az, numA, gain, sections, sectionCount);
return sectionCount;
}
template<typename T, std::size_t MaxOrder>
OPTIMIZE_FOR_SPEED std::size_t IirFilterDesign<T, MaxOrder>::DesignBandStop(std::size_t order, T wc, T fs,
const std::array<ComplexT, 16>& protoPoles) noexcept
{
const T bw{ wc };
const T wc0{ wc };
std::array<ComplexT, 32> az{};
std::array<ComplexT, 32> bz{};
std::size_t numA{ 0 };
std::size_t numB{ 0 };
for (std::size_t k{ 0 }; k < order; ++k)
{
const T mag2{ protoPoles[k].Real() * protoPoles[k].Real() + protoPoles[k].Imaginary() * protoPoles[k].Imaginary() };
const ComplexT invLp{ protoPoles[k].Real() / mag2, -protoPoles[k].Imaginary() / mag2 };
const ComplexT bsLp{ invLp.Real() * bw / T{ 2 }, invLp.Imaginary() * bw / T{ 2 } };
const T discRe{ bsLp.Real() * bsLp.Real() - bsLp.Imaginary() * bsLp.Imaginary() - wc0 * wc0 };
const T discIm{ T{ 2 } * bsLp.Real() * bsLp.Imaginary() };
const T discMag{ math::Sqrt(math::Sqrt(discRe * discRe + discIm * discIm)) };
const T discAngle{ math::Atan2(discIm, discRe) / T{ 2 } };
const ComplexT sqrtDisc{ discMag * math::Cos(discAngle), discMag * math::Sin(discAngle) };
az[numA++] = BilinearS2Z(bsLp + sqrtDisc, fs);
az[numA++] = BilinearS2Z(bsLp - sqrtDisc, fs);
bz[numB++] = BilinearS2Z({ T{ 0 }, wc0 }, fs);
bz[numB++] = BilinearS2Z({ T{ 0 }, -wc0 }, fs);
}
const T gain{ T{ 1 } };

Reproduction

  • Prototype order 8 band-pass returns four sections instead of eight.
  • First-order Butterworth band-pass center gain is approximately 4.4026, not unity.

Acceptance criteria

  • Define whether order is prototype or final digital order.
  • Allocate sufficient bounded section storage or reject unsupported transformed order without truncation.
  • Normalize band-pass/band-stop gain at documented reference frequencies.
  • Add section-count and absolute response tests for low and maximum orders.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions