TheAlgorithms/Java · error · IllegalArgumentException

aCoeffs must be of size {}, got {}

Error message

aCoeffs must be of size {}, got {}

What it means

Thrown by IIRFilter.setCoeffs when the denominator coefficient array's length does not equal the filter's configured order. The internal coefficient buffers are sized relative to order, so a mismatched array would either leave coefficients unset or cause an out-of-bounds write during the copy loop. Note the array is compared against `order`, not `order + 1` even though the buffers are allocated as `order + 1`.

Source

Thrown at src/main/java/com/thealgorithms/audiofilters/IIRFilter.java:50

        // Sane defaults
        coeffsA[0] = 1.0;
        coeffsB[0] = 1.0;

        historyX = new double[order];
        historyY = new double[order];
    }

    /**
     * Set coefficients
     *
     * @param aCoeffs Denominator coefficients
     * @param bCoeffs Numerator coefficients
     * @throws IllegalArgumentException if {@code aCoeffs} or {@code bCoeffs} is
     * not of size {@code order}, or if {@code aCoeffs[0]} is 0.0
     */
    public void setCoeffs(double[] aCoeffs, double[] bCoeffs) throws IllegalArgumentException {
        if (aCoeffs.length != order) {
            throw new IllegalArgumentException("aCoeffs must be of size " + order + ", got " + aCoeffs.length);
        }

        if (aCoeffs[0] == 0.0) {
            throw new IllegalArgumentException("aCoeffs.get(0) must not be zero");
        }

        if (bCoeffs.length != order) {
            throw new IllegalArgumentException("bCoeffs must be of size " + order + ", got " + bCoeffs.length);
        }

        for (int i = 0; i < order; i++) {
            coeffsA[i] = aCoeffs[i];
            coeffsB[i] = bCoeffs[i];
        }
    }

    /**
     * Process a single sample

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure aCoeffs has exactly `order` elements, matching the length the constructor expects.
  2. If your design tool returns order+1 coefficients (including a0), slice the array to the expected size before passing.
  3. Verify the same order value was used at construction time and at coefficient-generation time.

Example fix

// before
IIRFilter f = new IIRFilter(4);
f.setCoeffs(new double[5], b);   // throws: must be of size 4, got 5

// after
IIRFilter f = new IIRFilter(4);
f.setCoeffs(Arrays.copyOf(a, 4), Arrays.copyOf(b, 4));
Defensive patterns

Strategy: validation

Validate before calling

if (aCoeffs.length != order) throw new IllegalArgumentException("aCoeffs length " + aCoeffs.length + " != order " + order);
filter.setCoeffs(aCoeffs, bCoeffs);

Type guard

public static boolean aCoeffsMatchOrder(double[] a, int order) {
    return a != null && a.length == order;
}

Try / catch

try {
    filter.setCoeffs(a, b);
} catch (IllegalArgumentException e) {
    // resize coefficients to the expected length and retry
    filter.setCoeffs(Arrays.copyOf(a, order), Arrays.copyOf(b, order));
}

Prevention

When it happens

Trigger: Calling `setCoeffs(a, b)` where `a.length != filterOrder`. For example, constructing `new IIRFilter(4)` then calling `setCoeffs(new double[5], ...)` throws with 'aCoeffs must be of size 4, got 5'.

Common situations: Loading coefficients from a DSP design tool (e.g. scipy.signal) that returns arrays of length order+1 including the leading a0; mismatch between the order used to construct the filter and the order assumed when generating coefficients; off-by-one when copying a coefficient list.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/bd5a9faeb15ac6a0. Report an issue: GitHub.