Class for implementation of FIR filters
This commit is contained in:
parent
0ebcbeed5d
commit
f7bec2358b
|
|
@ -32,7 +32,10 @@ typedef int16_t audio_sample_t;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -67,17 +70,69 @@ void dsp_invertPhase(audio_sample_t *buffer, uint16_t length);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies a generic FIR filter on the audio buffer passed as parameter.
|
* Class for FIR filter with configurable coefficients.
|
||||||
* The buffer will be processed in place to save memory.
|
* Adapted from the original implementation by Rob Riggs, Mobilinkd LLC.
|
||||||
*
|
|
||||||
* @param buffer: the buffer to be used as both source and destination.
|
|
||||||
* @param length: the length of the input buffer.
|
|
||||||
* @param taps: an array of coefficients which defines the transfer function.
|
|
||||||
*/
|
*/
|
||||||
template<size_t order>
|
template < size_t N >
|
||||||
void dsp_applyFIR(audio_sample_t *buffer,
|
class Fir
|
||||||
uint16_t length,
|
{
|
||||||
std::array<float, order> taps);
|
public:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param taps: reference to a std::array of floating poing values representing
|
||||||
|
* the FIR filter coefficients.
|
||||||
|
*/
|
||||||
|
Fir(const std::array< float, N >& taps) : taps(taps), pos(0)
|
||||||
|
{
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destructor.
|
||||||
|
*/
|
||||||
|
~Fir() { }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform one step of the FIR filter, computing a new output value given
|
||||||
|
* the input value and the history of previous input values.
|
||||||
|
*
|
||||||
|
* @param input: FIR input value for the current time step.
|
||||||
|
* @return FIR output as a function of the current and past input values.
|
||||||
|
*/
|
||||||
|
float operator()(const float& input)
|
||||||
|
{
|
||||||
|
hist[pos++] = input;
|
||||||
|
if(pos >= N) pos = 0;
|
||||||
|
|
||||||
|
float result = 0.0;
|
||||||
|
size_t index = pos;
|
||||||
|
|
||||||
|
for(size_t i = 0; i < N; i++)
|
||||||
|
{
|
||||||
|
index = (index != 0 ? index - 1 : N - 1);
|
||||||
|
result += hist[index] * taps[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset FIR history, clearing the memory of past values.
|
||||||
|
*/
|
||||||
|
void reset()
|
||||||
|
{
|
||||||
|
hist.fill(0);
|
||||||
|
pos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
const std::array< float, N >& taps; ///< FIR filter coefficients.
|
||||||
|
std::array< float, N > hist; ///< History of past inputs.
|
||||||
|
size_t pos; ///< Current position in history.
|
||||||
|
};
|
||||||
|
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue