2021-07-28 23:50:34 +02:00
|
|
|
#include "HighShelf.h"
|
2022-09-18 05:12:26 +02:00
|
|
|
#include <cmath>
|
2021-07-28 23:50:34 +02:00
|
|
|
|
2022-09-18 05:12:26 +02:00
|
|
|
double HighShelf::Process(double sample) {
|
|
|
|
double out = (((this->x_1 * this->b1 + sample * this->b0 + this->b2 * this->x_2) - this->y_1 * this->a1) - this->a2 * this->y_2) * this->a0;
|
2021-07-28 23:50:34 +02:00
|
|
|
this->y_2 = this->y_1;
|
|
|
|
this->y_1 = out;
|
|
|
|
this->x_2 = this->x_1;
|
|
|
|
this->x_1 = sample;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2022-09-19 04:30:07 +02:00
|
|
|
void HighShelf::SetFrequency(float freq) {
|
2021-07-28 23:50:34 +02:00
|
|
|
this->frequency = freq;
|
|
|
|
}
|
|
|
|
|
2022-09-19 04:30:07 +02:00
|
|
|
void HighShelf::SetGain(float gain) {
|
|
|
|
this->gain = 20.0 * log10((double) gain);
|
2021-07-28 23:50:34 +02:00
|
|
|
}
|
|
|
|
|
2022-09-19 04:30:07 +02:00
|
|
|
void HighShelf::SetSamplingRate(uint32_t samplingRate) {
|
|
|
|
double x = (2 * M_PI * this->frequency) / (double) samplingRate;
|
2022-09-18 05:12:26 +02:00
|
|
|
double sinX = sin(x);
|
|
|
|
double cosX = cos(x);
|
|
|
|
double y = exp((this->gain * log(10.0)) / 40.0);
|
|
|
|
|
|
|
|
this->x_1 = 0.0;
|
|
|
|
this->x_2 = 0.0;
|
|
|
|
this->y_1 = 0.0;
|
|
|
|
this->y_2 = 0.0;
|
|
|
|
|
|
|
|
double z = sqrt(y * 2.0) * sinX;
|
|
|
|
double a = (y - 1.0) * cosX;
|
|
|
|
double b = (y + 1.0) - a;
|
|
|
|
double c = z + b;
|
|
|
|
double d = (y + 1.0) * cosX;
|
|
|
|
double e = (y + 1.0) + a;
|
|
|
|
double f = (y - 1.0) - d;
|
2021-07-28 23:50:34 +02:00
|
|
|
|
2022-09-18 05:12:26 +02:00
|
|
|
this->a0 = 1.0 / c;
|
2022-09-19 04:30:07 +02:00
|
|
|
this->a1 = f * 2.0;
|
2022-09-18 05:12:26 +02:00
|
|
|
this->a2 = b - z;
|
|
|
|
this->b0 = (e + z) * y;
|
|
|
|
this->b1 = -y * 2.0 * ((y - 1.0) + d);
|
|
|
|
this->b2 = (e - z) * y;
|
2021-07-28 23:50:34 +02:00
|
|
|
}
|