Code: Select all
private static class HpFilter
{
private double a0, a1, b1;
private double x1, y1;
public HpFilter() { /* */ }
public void set(double fs, double fc)
{
double k = Math.tan(fc * Math.PI / fs);
double b0 = k + 1;
this.a0 = 1.0 / b0;
this.a1 = -this.a0;
this.b1 = (k - 1) / b0;
}
public double getPhase(double wc)
{
return Math.atan(
((this.a1 - this.b1 * this.a0) * Math.sin(wc)) /
((this.a0 + this.b1 * this.a1 + (this.a1 + this.b1 * this.a0) * Math.cos(wc)))
);
}
public double process(double input)
{
this.y1 = this.a0 * input + this.a1 * this.x1 - this.b1 * this.y1 + Synth.DENORM;
this.x1 = input;
return this.y1;
}
}
Code: Select all
private static class Filter
{
private double co, q;
private double a, k, gain;
public double[] buf = new double[4];
private HpFilter hpfilt1 = new HpFilter();
private HpFilter hpfilt2 = new HpFilter();
private HpFilter hpfilt3 = new HpFilter();
private HpFilter hpfilt4 = new HpFilter();
public Filter()
{
this.set(1, 0);
this.hpfilt1.set(Synth.getSampleRate() * 4, 60);
this.hpfilt2.set(Synth.getSampleRate() * 4, 60);
this.hpfilt3.set(Synth.getSampleRate() * 4, 320);
}
private void recalc()
{
double wc = this.co * Math.PI;
double c = Math.cos(wc);
double s = Math.sin(wc);
double wcf = this.hpfilt3.getPhase(wc);
double t = Math.tan((wc + wcf - Math.PI) * 0.25);
double a1 = -t / (c * t - s);
double b0 = 1.0 + a1;
double b02 = b0 * b0;
double g2 = b02 / (1 + a1 * a1 + 2 * a1 * c);
this.a = b0;
this.k = (this.q * 0.95) / (g2 * g2);
this.gain = 1 + this.q;
this.hpfilt4.set(Synth.getSampleRate() * 4, 30 + 180 * this.q);
}
public void set(double c, double res)
{
this.co = c < 0 ? 0 : c > 1 ? 1 : c;
this.q = res < 0 ? 0 : res > 1 ? 1 : res;
this.recalc();
}
public double process(double inp)
{
double fb = -this.hpfilt1.process(this.hpfilt2.process(inp)) - this.k * this.hpfilt3.process(this.buf[3]);
this.buf[0] = this.buf[0] + this.a * (Math.tanh(fb) - this.buf[0]) + DENORM;
this.buf[1] = this.buf[1] + this.a * (this.buf[0] - this.buf[1]) + DENORM;
this.buf[2] = this.buf[2] + this.a * (this.buf[1] - this.buf[2]) + DENORM;
this.buf[3] = this.buf[3] + this.a * (this.buf[2] - this.buf[3]) + DENORM;
return this.hpfilt4.process(this.buf[3]) * this.gain;
}
}
Edit: The above configuration was the one which I developed before Tim's recent posts ... there are similarities to the real filter
Edit2: This is still my 'dirty-evaluation-version'.
