2021-07-27 09:47:15 +02:00
|
|
|
#include <cmath>
|
|
|
|
#include "IIR_1st.h"
|
|
|
|
|
|
|
|
// Seems to be taken from https://github.com/michaelwillis/dragonfly-reverb/blob/master/common/freeverb/efilter.cpp
|
|
|
|
// Or similar sources
|
|
|
|
|
|
|
|
IIR_1st::IIR_1st() {
|
|
|
|
this->b0 = 0.f;
|
|
|
|
this->b1 = 0.f;
|
|
|
|
this->a1 = 0.f;
|
2021-07-27 17:41:38 +02:00
|
|
|
Mute();
|
2021-07-27 09:47:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IIR_1st::Mute() {
|
|
|
|
this->prevSample = 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::SetCoefficients(float b0, float b1, float a1) {
|
|
|
|
this->b0 = b0;
|
|
|
|
this->b1 = b1;
|
|
|
|
this->a1 = a1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define angle(freq, samplerate) (expf(-1*(float)M_PI*(freq)/((float)(samplerate)/2.f)))
|
|
|
|
#define omega() (2.f * (float)M_PI * frequency / (float)samplerate)
|
|
|
|
#define omega_2() ((float)M_PI * frequency / (float)samplerate)
|
|
|
|
|
|
|
|
void IIR_1st::setHPF_A(float frequency, uint32_t samplerate) {
|
|
|
|
this->a1 = angle(frequency, samplerate);
|
2022-08-23 00:26:44 +02:00
|
|
|
float norm = (1 + a1) / 2.f;
|
2021-07-27 09:47:15 +02:00
|
|
|
this->b0 = norm;
|
|
|
|
this->b1 = -norm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setHPF_BW(float frequency, uint32_t samplerate) {
|
|
|
|
float omega_2 = omega_2();
|
|
|
|
float tan_omega_2 = tanf(omega_2);
|
2022-08-23 00:26:44 +02:00
|
|
|
this->b0 = 1 / (1 + tan_omega_2);
|
2021-07-27 09:47:15 +02:00
|
|
|
this->b1 = -this->b0;
|
2022-08-23 00:26:44 +02:00
|
|
|
this->a1 = (1 - tan_omega_2) / (1 + tan_omega_2);
|
2021-07-27 09:47:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setHPF_C(float frequency, uint32_t samplerate) {
|
2022-08-23 00:26:44 +02:00
|
|
|
this->b0 = (float) samplerate / ((float) samplerate + frequency);
|
2021-07-27 09:47:15 +02:00
|
|
|
this->b1 = -1 * this->b0;
|
2022-08-23 00:26:44 +02:00
|
|
|
this->a1 = ((float) samplerate - frequency) / ((float) samplerate + frequency);
|
2021-07-27 09:47:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setHPFwLPS_A(float frequency, uint32_t samplerate) {
|
|
|
|
this->a1 = -0.12f;
|
|
|
|
this->b0 = -1.f;
|
|
|
|
this->b1 = angle(frequency, samplerate);
|
2022-08-23 00:26:44 +02:00
|
|
|
float norm = (1 - this->a1) / fabsf(this->b0 + this->b1);
|
2021-07-27 09:47:15 +02:00
|
|
|
this->b0 *= norm;
|
|
|
|
this->b1 *= norm;;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setHSF_A(float f1, float f2, uint32_t samplerate) {
|
|
|
|
this->a1 = angle(f1, samplerate);
|
|
|
|
this->b0 = -1.f;
|
|
|
|
this->b1 = angle(f2, samplerate);
|
2022-08-23 00:26:44 +02:00
|
|
|
float norm = (1 - this->a1) / (this->b0 + this->b1);
|
2021-07-27 09:47:15 +02:00
|
|
|
this->b0 *= norm;
|
|
|
|
this->b1 *= norm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setLPF_A(float frequency, uint32_t samplerate) {
|
|
|
|
this->a1 = angle(frequency, samplerate);
|
|
|
|
this->b0 = 1.f;
|
|
|
|
this->b1 = 0.12f;
|
2022-08-23 00:26:44 +02:00
|
|
|
float norm = (1 + this->a1) / (this->b0 + this->b1);
|
2021-07-27 09:47:15 +02:00
|
|
|
this->b0 *= norm;
|
|
|
|
this->b1 *= norm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setLPF_BW(float frequency, uint32_t samplerate) {
|
|
|
|
float omega_2 = omega_2();
|
|
|
|
float tan_omega_2 = tanf(omega_2);
|
2022-08-23 00:26:44 +02:00
|
|
|
this->a1 = (1 - tan_omega_2) / (1 + tan_omega_2);
|
|
|
|
this->b0 = tan_omega_2 / (1 + tan_omega_2);
|
2021-07-27 09:47:15 +02:00
|
|
|
this->b1 = this->b0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setLPF_C(float frequency, uint32_t samplerate) {
|
2022-08-23 00:26:44 +02:00
|
|
|
this->b0 = frequency / ((float) samplerate + frequency);
|
2021-07-27 09:47:15 +02:00
|
|
|
this->b1 = this->b0;
|
2022-08-23 00:26:44 +02:00
|
|
|
this->a1 = ((float) samplerate - frequency) / ((float) samplerate + frequency);
|
2021-07-27 09:47:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setLSF_A(float f1, float f2, uint32_t samplerate) {
|
|
|
|
this->a1 = angle(f1, samplerate);
|
|
|
|
this->b0 = -1.f;
|
|
|
|
this->b1 = angle(f2, samplerate);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setPole(float a1) {
|
|
|
|
this->a1 = a1;
|
2022-08-23 00:26:44 +02:00
|
|
|
this->b0 = 1.f - fabsf(a1);
|
2021-07-27 09:47:15 +02:00
|
|
|
this->b1 = 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setPoleHPF(float frequency, uint32_t samplerate) {
|
|
|
|
float omega = omega();
|
|
|
|
float cos_omega = cosf(omega);
|
|
|
|
float tmp = (2.f + cos_omega);
|
2022-08-23 00:26:44 +02:00
|
|
|
float coeff = tmp - sqrtf(tmp * tmp - 1.f);
|
2021-07-27 09:47:15 +02:00
|
|
|
this->a1 = -coeff;
|
|
|
|
this->b0 = 1.f - coeff;
|
|
|
|
this->b1 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setPoleLPF(float frequency, uint32_t samplerate) {
|
|
|
|
float omega = omega();
|
|
|
|
float cos_omega = cosf(omega);
|
|
|
|
float tmp = (2.f - cos_omega);
|
2022-08-23 00:26:44 +02:00
|
|
|
float coeff = tmp - sqrtf(tmp * tmp - 1.f);
|
2021-07-27 09:47:15 +02:00
|
|
|
this->a1 = coeff;
|
|
|
|
this->b0 = 1.f - coeff;
|
|
|
|
this->b1 = 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setZero(float b1) {
|
|
|
|
this->a1 = 0;
|
|
|
|
this->b0 = -1;
|
|
|
|
this->b1 = b1;
|
|
|
|
float norm = fabsf(this->b0) + fabsf(this->b1);
|
|
|
|
this->b0 *= norm;
|
|
|
|
this->b1 *= norm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setZeroHPF(float frequency, uint32_t samplerate) {
|
|
|
|
float omega = omega();
|
|
|
|
float cos_omega = cosf(omega);
|
|
|
|
float tmp = (1.f - 2.f * cos_omega);
|
2022-08-23 00:26:44 +02:00
|
|
|
float coeff = tmp - sqrtf(tmp * tmp - 1.f);
|
2021-07-27 09:47:15 +02:00
|
|
|
this->a1 = 0.f;
|
2022-08-23 00:26:44 +02:00
|
|
|
this->b0 = 1.f / (1.f + coeff);
|
|
|
|
this->b1 = -coeff / (1.f + coeff);
|
2021-07-27 09:47:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IIR_1st::setZeroLPF(float frequency, uint32_t samplerate) {
|
|
|
|
float omega = omega();
|
|
|
|
float cos_omega = cosf(omega);
|
|
|
|
float tmp = (1.f + 2.f * cos_omega);
|
2022-08-23 00:26:44 +02:00
|
|
|
float coeff = tmp - sqrtf(tmp * tmp - 1.f);
|
2021-07-27 09:47:15 +02:00
|
|
|
this->a1 = 0.f;
|
2022-08-23 00:26:44 +02:00
|
|
|
this->b0 = 1.f / (1.f + coeff);
|
|
|
|
this->b1 = coeff / (1.f + coeff);
|
2021-07-27 09:47:15 +02:00
|
|
|
}
|