-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
3,145 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include "iir.hpp" | ||
#include <vector> | ||
|
||
/** | ||
High-order filter to be used for anti-aliasing or anti-imaging. | ||
The template parameter N should be 1/2 the desired filter order. | ||
Currently uses an 2*N-th order Butterworth filter. | ||
@TODO: implement Chebyshev, Elliptic filter options. | ||
*/ | ||
template<int N> | ||
class AAFilter | ||
{ | ||
public: | ||
AAFilter() = default; | ||
|
||
/** Calculate Q values for a Butterworth filter of a given order */ | ||
static std::vector<float> calculateButterQs(int order) { | ||
const int lim = int (order / 2); | ||
std::vector<float> Qs; | ||
|
||
for(int k = 1; k <= lim; ++k) { | ||
auto b = -2.0f * std::cos((2.0f * k + order - 1) * 3.14159 / (2.0f * order)); | ||
Qs.push_back(1.0f / b); | ||
} | ||
|
||
std::reverse(Qs.begin(), Qs.end()); | ||
return Qs; | ||
} | ||
|
||
/** | ||
* Resets the filter to process at a new sample rate. | ||
* | ||
* @param sampleRate: The base (i.e. pre-oversampling) sample rate of the audio being processed | ||
* @param osRatio: The oversampling ratio at which the filter is being used | ||
*/ | ||
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 / (osRatio * sampleRate), Qs[i], 1.0f); | ||
} | ||
|
||
inline float process(float x) noexcept { | ||
for(int i = 0; i < N; ++i) | ||
x = filters[i].process(x); | ||
|
||
return x; | ||
} | ||
|
||
private: | ||
BiquadFilter filters[N]; | ||
}; |
Oops, something went wrong.