worked on fir/iir filters
This commit is contained in:
@@ -46,6 +46,14 @@ namespace IIR {
|
||||
|
||||
}
|
||||
|
||||
float getA0() {return 1;}
|
||||
float getA1() {return a1a0;}
|
||||
float getA2() {return a2a0;}
|
||||
|
||||
float getB0() {return b0a0;}
|
||||
float getB1() {return b1a0;}
|
||||
float getB2() {return b2a0;}
|
||||
|
||||
void preFill(const Scalar s) {
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
filter(s);
|
||||
@@ -67,12 +75,14 @@ namespace IIR {
|
||||
}
|
||||
|
||||
/** configure the filter as low-pass. freqFact between ]0;0.5[ */
|
||||
void setLowPass( double freqFact, const float octaves ) {
|
||||
//void setLowPass( double freqFact, const float octaves ) {
|
||||
void setLowPass( double freqFact, const float Q ) {
|
||||
|
||||
sanityCheck(freqFact);
|
||||
|
||||
double w0 = 2.0 * M_PI * freqFact;
|
||||
double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
//double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
double alpha = sin(w0)/(2*Q);
|
||||
|
||||
double b0 = (1.0 - cos(w0))/2.0;
|
||||
double b1 = 1.0 - cos(w0);
|
||||
@@ -94,46 +104,48 @@ namespace IIR {
|
||||
|
||||
|
||||
|
||||
//http://dspwiki.com/index.php?title=Lowpass_Resonant_Biquad_Filter
|
||||
//http://www.opensource.apple.com/source/WebCore/WebCore-7536.26.14/platform/audio/Biquad.cpp
|
||||
/**
|
||||
* configure as low-pass filter with resonance
|
||||
* @param freqFact the frequency factor between ]0;0.5[
|
||||
* @param res
|
||||
*/
|
||||
void setLowPassResonance( double freqFact, float res ) {
|
||||
// //http://dspwiki.com/index.php?title=Lowpass_Resonant_Biquad_Filter
|
||||
// //http://www.opensource.apple.com/source/WebCore/WebCore-7536.26.14/platform/audio/Biquad.cpp
|
||||
// /**
|
||||
// * configure as low-pass filter with resonance
|
||||
// * @param freqFact the frequency factor between ]0;0.5[
|
||||
// * @param res
|
||||
// */
|
||||
// void setLowPassResonance( double freqFact, float res ) {
|
||||
|
||||
sanityCheck(freqFact);
|
||||
// sanityCheck(freqFact);
|
||||
|
||||
res *= 10;
|
||||
// res *= 10;
|
||||
|
||||
double g = pow(10.0, 0.05 * res);
|
||||
double d = sqrt((4 - sqrt(16 - 16 / (g * g))) / 2);
|
||||
// double g = pow(10.0, 0.05 * res);
|
||||
// double d = sqrt((4 - sqrt(16 - 16 / (g * g))) / 2);
|
||||
|
||||
double theta = M_PI * freqFact;
|
||||
double sn = 0.5 * d * sin(theta);
|
||||
double beta = 0.5 * (1 - sn) / (1 + sn);
|
||||
double gamma = (0.5 + beta) * cos(theta);
|
||||
double alpha = 0.25 * (0.5 + beta - gamma);
|
||||
// double theta = M_PI * freqFact;
|
||||
// double sn = 0.5 * d * sin(theta);
|
||||
// double beta = 0.5 * (1 - sn) / (1 + sn);
|
||||
// double gamma = (0.5 + beta) * cos(theta);
|
||||
// double alpha = 0.25 * (0.5 + beta - gamma);
|
||||
|
||||
double a0 = 1.0;
|
||||
double b0 = 2.0 * alpha;
|
||||
double b1 = 2.0 * 2.0 * alpha;
|
||||
double b2 = 2.0 * alpha;
|
||||
double a1 = 2.0 * -gamma;
|
||||
double a2 = 2.0 * beta;
|
||||
// double a0 = 1.0;
|
||||
// double b0 = 2.0 * alpha;
|
||||
// double b1 = 2.0 * 2.0 * alpha;
|
||||
// double b2 = 2.0 * alpha;
|
||||
// double a1 = 2.0 * -gamma;
|
||||
// double a2 = 2.0 * beta;
|
||||
|
||||
setValues(a0, a1, a2, b0, b1, b2);
|
||||
// setValues(a0, a1, a2, b0, b1, b2);
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
/** configure the filter as high-pass. freqFact between ]0;0.5[ */
|
||||
void setHighPass( double freqFact, const float octaves ) {
|
||||
//void setHighPass( double freqFact, const float octaves ) {
|
||||
void setHighPass( double freqFact, const float Q ) {
|
||||
|
||||
sanityCheck(freqFact);
|
||||
|
||||
double w0 = 2.0 * M_PI * freqFact;
|
||||
double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
//double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
double alpha = sin(w0)/(2*Q);
|
||||
|
||||
double b0 = (1.0 + cos(w0))/2.0;
|
||||
double b1 = -(1.0 + cos(w0));
|
||||
@@ -153,17 +165,21 @@ namespace IIR {
|
||||
}
|
||||
|
||||
/** configure the filter as band-pass. freqFact between ]0;0.5[ */
|
||||
void setBandPass( double freqFact, const float octaves ) {
|
||||
//void setBandPass( double freqFact, const float octaves ) {
|
||||
void setBandPass( double freqFact, const float Q ) {
|
||||
|
||||
sanityCheck(freqFact);
|
||||
|
||||
//double w0 = 2 * K_PI * / 2 / freqFact;
|
||||
double w0 = 2.0 * M_PI * freqFact;
|
||||
double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
//double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
double alpha = sin(w0)/(2*Q);
|
||||
|
||||
double b0 = sin(w0)/2.0;
|
||||
|
||||
// constant 0dB peak gain
|
||||
double b0 = alpha;
|
||||
double b1 = 0.0;
|
||||
double b2 = -sin(w0)/2.0;
|
||||
double b2 = -alpha;
|
||||
double a0 = 1.0 + alpha;
|
||||
double a1 = -2.0*cos(w0);
|
||||
double a2 = 1.0 - alpha;
|
||||
@@ -179,108 +195,112 @@ namespace IIR {
|
||||
}
|
||||
|
||||
|
||||
/** configure the filter as all-pass. freqFact between ]0;0.5[ */
|
||||
void setAllPass( double freqFact, const float octaves ) {
|
||||
// /** configure the filter as all-pass. freqFact between ]0;0.5[ */
|
||||
// void setAllPass( double freqFact, const float octaves ) {
|
||||
|
||||
sanityCheck(freqFact);
|
||||
// sanityCheck(freqFact);
|
||||
|
||||
double w0 = 2.0 * M_PI * freqFact;
|
||||
double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
// double w0 = 2.0 * M_PI * freqFact;
|
||||
// double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
|
||||
double b0 = 1 - alpha;
|
||||
double b1 = -2*cos(w0);
|
||||
double b2 = 1 + alpha;
|
||||
double a0 = 1 + alpha;
|
||||
double a1 = -2*cos(w0);
|
||||
double a2 = 1 - alpha;
|
||||
// double b0 = 1 - alpha;
|
||||
// double b1 = -2*cos(w0);
|
||||
// double b2 = 1 + alpha;
|
||||
// double a0 = 1 + alpha;
|
||||
// double a1 = -2*cos(w0);
|
||||
// double a2 = 1 - alpha;
|
||||
|
||||
setValues(a0, a1, a2, b0, b1, b2);
|
||||
// setValues(a0, a1, a2, b0, b1, b2);
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
/** configure the filter as all-pass */
|
||||
void setAllPass( const float freq, const float octaves, const float sRate ) {
|
||||
double freqFact = double(freq) / double(sRate);
|
||||
setAllPass(freqFact, octaves);
|
||||
}
|
||||
|
||||
/** configure as notch filter. freqFact between ]0;0.5[ */
|
||||
void setNotch( double freqFact, const float octaves ) {
|
||||
|
||||
sanityCheck(freqFact);
|
||||
|
||||
double w0 = 2.0 * M_PI * freqFact;
|
||||
double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
|
||||
double b0 = 1.0;
|
||||
double b1 = -2.0*cos(w0);
|
||||
double b2 = 1.0;
|
||||
double a0 = 1.0 + alpha;
|
||||
double a1 = -2.0*cos(w0);
|
||||
double a2 = 1.0 - alpha;
|
||||
|
||||
setValues(a0, a1, a2, b0, b1, b2);
|
||||
|
||||
}
|
||||
|
||||
/** configure as notch filter */
|
||||
void setNotch( const float freq, const float octaves, const float sRate ) {
|
||||
double freqFact = double(freq) / double(sRate);
|
||||
setNotch(freqFact, octaves);
|
||||
}
|
||||
|
||||
/** configure the filter as low-shelf. increase all aplitudes below freq? freqFact between ]0;0.5[ */
|
||||
void setLowShelf( double freqFact, const float octaves, const float gain ) {
|
||||
|
||||
sanityCheck(freqFact);
|
||||
|
||||
double A = sqrt( pow(10, (gain/20.0)) );
|
||||
double w0 = 2.0 * M_PI * freqFact;
|
||||
double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
|
||||
double b0 = A*( (A+1.0) - (A-1.0)*cos(w0) + 2.0*sqrt(A)*alpha );
|
||||
double b1 = 2.0*A*( (A-1.0) - (A+1.0)*cos(w0) );
|
||||
double b2 = A*( (A+1.0) - (A-1.0)*cos(w0) - 2.0*sqrt(A)*alpha );
|
||||
double a0 = (A+1.0) + (A-1.0)*cos(w0) + 2.0*sqrt(A)*alpha;
|
||||
double a1 = -2.0*( (A-1.0) + (A+1.0)*cos(w0) );
|
||||
double a2 = (A+1.0) + (A-1.0)*cos(w0) - 2.0*sqrt(A)*alpha;
|
||||
|
||||
setValues(a0, a1, a2, b0, b1, b2);
|
||||
|
||||
}
|
||||
|
||||
/** configure the filter as low-shelf. increase all aplitudes below freq? */
|
||||
void setLowShelf( const float freq, const float octaves, const float gain, const float sRate ) {
|
||||
double freqFact = double(freq) / double(sRate);
|
||||
setLowShelf(freqFact, octaves, gain);
|
||||
}
|
||||
|
||||
/** configure the filter as high-shelf. increase all amplitues above freq? freqFact between ]0;0.5[ */
|
||||
void setHighShelf( double freqFact, const float octaves, const float gain ) {
|
||||
|
||||
sanityCheck(freqFact);
|
||||
|
||||
double A = sqrt( pow(10, (gain/20.0)) );
|
||||
double w0 = 2.0 * M_PI * freqFact;
|
||||
double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
|
||||
double b0 = A*( (A+1.0) + (A-1.0)*cos(w0) + 2.0*sqrt(A)*alpha );
|
||||
double b1 = -2.0*A*( (A-1.0) + (A+1.0)*cos(w0) );
|
||||
double b2 = A*( (A+1.0) + (A-1.0)*cos(w0) - 2.0*sqrt(A)*alpha );
|
||||
double a0 = (A+1.0) - (A-1.0)*cos(w0) + 2.0*sqrt(A)*alpha;
|
||||
double a1 = 2.0*( (A-1.0) - (A+1.0)*cos(w0) );
|
||||
double a2 = (A+1.0) - (A-1.0)*cos(w0) - 2.0*sqrt(A)*alpha;
|
||||
|
||||
setValues(a0, a1, a2, b0, b1, b2);
|
||||
|
||||
}
|
||||
// /** configure the filter as all-pass */
|
||||
// void setAllPass( const float freq, const float octaves, const float sRate ) {
|
||||
// double freqFact = double(freq) / double(sRate);
|
||||
// setAllPass(freqFact, octaves);
|
||||
// }
|
||||
|
||||
|
||||
/** configure the filter as high-shelf. increase all amplitues above freq? */
|
||||
void setHighShelf( const float freq, const float octaves, const float gain, const float sRate ) {
|
||||
double freqFact = double(freq) / double(sRate);
|
||||
setHighShelf(freqFact, octaves, gain);
|
||||
}
|
||||
|
||||
|
||||
// /** configure as notch filter. freqFact between ]0;0.5[ */
|
||||
// //void setNotch( double freqFact, const float octaves ) {
|
||||
// void setNotch( double freqFact, const float Q ) {
|
||||
|
||||
// sanityCheck(freqFact);
|
||||
|
||||
// double w0 = 2.0 * M_PI * freqFact;
|
||||
// double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
|
||||
// double b0 = 1.0;
|
||||
// double b1 = -2.0*cos(w0);
|
||||
// double b2 = 1.0;
|
||||
// double a0 = 1.0 + alpha;
|
||||
// double a1 = -2.0*cos(w0);
|
||||
// double a2 = 1.0 - alpha;
|
||||
|
||||
// setValues(a0, a1, a2, b0, b1, b2);
|
||||
|
||||
// }
|
||||
|
||||
// /** configure as notch filter */
|
||||
// void setNotch( const float freq, const float octaves, const float sRate ) {
|
||||
// double freqFact = double(freq) / double(sRate);
|
||||
// setNotch(freqFact, octaves);
|
||||
// }
|
||||
|
||||
// /** configure the filter as low-shelf. increase all aplitudes below freq? freqFact between ]0;0.5[ */
|
||||
// void setLowShelf( double freqFact, const float octaves, const float gain ) {
|
||||
|
||||
// sanityCheck(freqFact);
|
||||
|
||||
// double A = sqrt( pow(10, (gain/20.0)) );
|
||||
// double w0 = 2.0 * M_PI * freqFact;
|
||||
// double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
|
||||
// double b0 = A*( (A+1.0) - (A-1.0)*cos(w0) + 2.0*sqrt(A)*alpha );
|
||||
// double b1 = 2.0*A*( (A-1.0) - (A+1.0)*cos(w0) );
|
||||
// double b2 = A*( (A+1.0) - (A-1.0)*cos(w0) - 2.0*sqrt(A)*alpha );
|
||||
// double a0 = (A+1.0) + (A-1.0)*cos(w0) + 2.0*sqrt(A)*alpha;
|
||||
// double a1 = -2.0*( (A-1.0) + (A+1.0)*cos(w0) );
|
||||
// double a2 = (A+1.0) + (A-1.0)*cos(w0) - 2.0*sqrt(A)*alpha;
|
||||
|
||||
// setValues(a0, a1, a2, b0, b1, b2);
|
||||
|
||||
// }
|
||||
|
||||
// /** configure the filter as low-shelf. increase all aplitudes below freq? */
|
||||
// void setLowShelf( const float freq, const float octaves, const float gain, const float sRate ) {
|
||||
// double freqFact = double(freq) / double(sRate);
|
||||
// setLowShelf(freqFact, octaves, gain);
|
||||
// }
|
||||
|
||||
// /** configure the filter as high-shelf. increase all amplitues above freq? freqFact between ]0;0.5[ */
|
||||
// void setHighShelf( double freqFact, const float octaves, const float gain ) {
|
||||
|
||||
// sanityCheck(freqFact);
|
||||
|
||||
// double A = sqrt( pow(10, (gain/20.0)) );
|
||||
// double w0 = 2.0 * M_PI * freqFact;
|
||||
// double alpha = sin(w0)*sinh( log(2)/2 * octaves * w0/sin(w0) );
|
||||
|
||||
// double b0 = A*( (A+1.0) + (A-1.0)*cos(w0) + 2.0*sqrt(A)*alpha );
|
||||
// double b1 = -2.0*A*( (A-1.0) + (A+1.0)*cos(w0) );
|
||||
// double b2 = A*( (A+1.0) + (A-1.0)*cos(w0) - 2.0*sqrt(A)*alpha );
|
||||
// double a0 = (A+1.0) - (A-1.0)*cos(w0) + 2.0*sqrt(A)*alpha;
|
||||
// double a1 = 2.0*( (A-1.0) - (A+1.0)*cos(w0) );
|
||||
// double a2 = (A+1.0) - (A-1.0)*cos(w0) - 2.0*sqrt(A)*alpha;
|
||||
|
||||
// setValues(a0, a1, a2, b0, b1, b2);
|
||||
|
||||
// }
|
||||
|
||||
|
||||
// /** configure the filter as high-shelf. increase all amplitues above freq? */
|
||||
// void setHighShelf( const float freq, const float octaves, const float gain, const float sRate ) {
|
||||
// double freqFact = double(freq) / double(sRate);
|
||||
// setHighShelf(freqFact, octaves, gain);
|
||||
// }
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
Reference in New Issue
Block a user