/* apply filter to an array of values, adding result to output array */ public void Apply( float[] XinVector, int XinVectorOffset, float[] YoutVector, int YoutVectorOffset, int VectorLength, float OutputScaling, SynthParamRec SynthParams) { ParametricEqualizerRec Filter = this; float State1 = Filter.State1; float State0 = Filter.State0; float A = Filter.A; float B = Filter.B; float K = Filter.K; float OneMinusK = 1 - K; float OnePlusK = 1 + K; for (int i = 0; i < VectorLength; i += 1) { float Xin = XinVector[i + XinVectorOffset]; float OrigYOut = YoutVector[i + YoutVectorOffset]; float Allpass = A * (Xin - A * State1) + State1; float Outval = .5f * (OnePlusK * Xin + OneMinusK * Allpass); float Temp = Xin - A * State1 - B * State0; State1 = B * Temp + State0; State0 = Temp; YoutVector[i + YoutVectorOffset] = OrigYOut + Outval * OutputScaling; } Filter.State1 = State1; Filter.State0 = State0; }
public static void SetParametricEqualizerCoefficients( ParametricEqualizerRec Filter, double Cutoff, double Bandwidth, double Gain, double SamplingRate) { double X; double OneOverSamplingRate; if ((Cutoff == Filter.OldCutoff) && (Bandwidth == Filter.OldBandwidth) && (Gain == Filter.OldGain)) { return; } Filter.OldCutoff = Cutoff; Filter.OldBandwidth = Bandwidth; Filter.OldGain = Gain; OneOverSamplingRate = 1.0 / SamplingRate; if (Bandwidth < FILTER_FREQ_EPSILON) { Bandwidth = FILTER_FREQ_EPSILON; } if (Bandwidth > SamplingRate * 0.25 - FILTER_FREQ_EPSILON) { Bandwidth = SamplingRate * 0.25 - FILTER_FREQ_EPSILON; } if (Cutoff < FILTER_FREQ_EPSILON) { Cutoff = FILTER_FREQ_EPSILON; } if (Cutoff > SamplingRate * 0.5 - FILTER_FREQ_EPSILON) { Cutoff = SamplingRate * 0.5 - FILTER_FREQ_EPSILON; } X = Math.Tan(TWOPI * Bandwidth * OneOverSamplingRate); Filter.A = (float)((1 - X) / (1 + X)); Filter.B = (float)(-Math.Cos(TWOPI * Cutoff * OneOverSamplingRate)); Filter.K = (float)Gain; }