FilterModelConfig.java

package builder.resid.residfp.circuit;

import libsidplay.config.IFilterSection;

/** Small-signal cutoff estimate; a curve does not describe the complete nonlinear response. */
public final class FilterModelConfig {
    private static final double VDDT = 12 * 1.015 - 1.31;
    private static final class CutoffDac {
        static final double[] VALUES = CircuitModelConfig.dac(11);
    }
    private FilterModelConfig() { }

    public static double estimateFrequency(IFilterSection settings, int fc) {
        if (settings.isCircuitFilter8580()) {
            double gate = 4.75 * 1.01 * (1.8 - settings.getCircuitCurve() * 3.0 / 5) - 0.8;
            return 0.00615 * (fc == 0 ? 0.5 : fc) * 100e-6 * (gate - 4.8125) / (2 * Math.PI * 22e-9);
        }
        if (!settings.isCircuitFilter6581()) throw new IllegalArgumentException("A circuit chip model is required");
        double cutoff = 7.15 + 2 * settings.getCircuitCurve() - 1 + 2.63 * CutoffDac.VALUES[fc];
        double ucox = (1 + 39 * settings.getCircuitRange()) * 1e-6;
        double step = 0.01;
        double current = current(cutoff, 4.54 + step, 4.54, ucox)
                - current(cutoff, 4.54 - step, 4.54, ucox);
        return Math.abs(current / (2 * step)) / (2 * Math.PI * settings.getCircuitCapacitance() * 1e-12);
    }

    private static double current(double cutoff, double vi, double vx, double ucox) {
        double a = VDDT - vx, b = VDDT - vi;
        double snake = ucox / (2 * 115) * (a * a - b * b);
        double gate = VDDT - Math.sqrt((Math.pow(VDDT - cutoff, 2) + b * b) / 2);
        double ut = 1.380649e-23 * (27 + 273.15) / 1.602176634e-19;
        double forward = Math.log1p(Math.exp((gate - 1.31 - vx) / (2 * ut)));
        double reverse = Math.log1p(Math.exp((gate - 1.31 - vi) / (2 * ut)));
        return snake + 2 * ucox * ut * ut * 9 * (forward * forward - reverse * reverse);
    }
}