Skip to content

Commit

Permalink
Fix bugs in IIR and oversampling classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 committed Sep 10, 2020
1 parent b67a810 commit 21701fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
32 changes: 16 additions & 16 deletions src/shared/iir.hpp
Expand Up @@ -216,22 +216,22 @@ struct TBiquadFilter : IIRFilter<3, T> {
} break;

case PEAK: {
if (V >= 1.f) {
float norm = 1.f / (1.f + K / Q + K * K);
this->b[0] = (1.f + K / Q * V + K * K) * norm;
this->b[1] = 2.f * (K * K - 1.f) * norm;
this->b[2] = (1.f - K / Q * V + K * K) * norm;
this->a[1] = this->b[1];
this->a[2] = (1.f - K / Q + K * K) * norm;
}
else {
float norm = 1.f / (1.f + K / Q / V + K * K);
this->b[0] = (1.f + K / Q + K * K) * norm;
this->b[1] = 2.f * (K * K - 1.f) * norm;
this->b[2] = (1.f - K / Q + K * K) * norm;
this->a[1] = this->b[1];
this->a[2] = (1.f - K / Q / V + K * K) * norm;
}
float c = 1.0f / K;
float phi = c * c;
float Knum = c / Q;
float Kdenom = Knum;

if(V > 1.0f)
Knum *= V;
else
Kdenom /= V;

float norm = phi + Kdenom + 1.0;
this->b[0] = (phi + Knum + 1.0f) / norm;
this->b[1] = 2.0f * (1.0f - phi) / norm;
this->b[2] = (phi - Knum + 1.0f) / norm;
this->a[1] = 2.0f * (1.0f - phi) / norm;
this->a[2] = (phi - Kdenom + 1.0f) / norm;
} break;

case NOTCH: {
Expand Down
8 changes: 4 additions & 4 deletions src/shared/oversampling.hpp
Expand Up @@ -29,12 +29,12 @@ class AAFilter
return Qs;
}

void reset(float sampleRate) {
void reset(float sampleRate, int osRatio) {
float fc = 0.98f * (sampleRate / 2.0f);
auto Qs = calculateButterQs(2*N);

for(int i = 0; i < N; ++i)
filters[i].setParameters(BiquadFilter::Type::LOWPASS, fc / sampleRate, Qs[i], 1.0f);
filters[i].setParameters(BiquadFilter::Type::LOWPASS, fc / (osRatio * sampleRate), Qs[i], 1.0f);
}

inline float process(float x) noexcept {
Expand All @@ -58,8 +58,8 @@ class OversampledProcess
OversampledProcess() = default;

void reset(float baseSampleRate) {
aaFilter.reset(baseSampleRate);
aiFilter.reset(baseSampleRate);
aaFilter.reset(baseSampleRate, ratio);
aiFilter.reset(baseSampleRate, ratio);
}

using OSProcess = std::function<float(float)>;
Expand Down

0 comments on commit 21701fb

Please sign in to comment.