lasp/cpp_src/dsp/lasp_biquadbank.h

109 lines
3.1 KiB
C++

#pragma once
#include "lasp_filter.h"
#include "lasp_thread.h"
/**
* \addtogroup dsp
* @{
*/
/**
* @brief A set of Biquad filters in series.
*/
class SeriesBiquad : public Filter {
/// The filter coefficients for each of the filters in the Filterbank
/// The *first* axis is the filter no, the second axis contains the
/// filter coefficients, in the order, b_0, b_1, b_2, a_0, a_1, a_2, which
/// corresponds to the transfer function
/// b_0 + b_1 z^-1 + b_2 z^-2
/// H[z] = -------------------------
/// a_0 + a_1 z^-1 + a_2 z^-2
///
dmat sos; /// sos[coef, filter_no]
///
/// Storage for the current state of the output, first axis correspond to
/// the state axis, the second axis to the series filter no. For each filter
/// in series, two state coefficients are remembered.
dmat state;
public:
/**
* @brief Initalize a SeriesBiquad filter.
*
* @param filter_coefs Filter coefficients, should be given in the order as
* [b0, b1, b2, a0==1, a1, a2, b0,...]
*/
SeriesBiquad(const vd &filter_coefs);
virtual void filter(vd &inout) override final;
virtual ~SeriesBiquad() override {}
void reset() override final;
std::unique_ptr<Filter> clone() const override final;
/**
* @brief Create a SeriesBiquad object for a first order high-pass filter
*
* @param fs Sampling frequency [Hz]
* @param cuton_Hz Cuton-frequency [Hz]
*
* @return SeriesBiquad object with a single biquad, corresponding to a first
* order high pass filter.
*/
static SeriesBiquad firstOrderHighPass(const d fs, const d cuton_Hz);
};
/**
* @brief Multiple biquad filters in parallel, each multiplied with a gain
* value, and finally all added together. This class can be used to create
* a graphic equalizer.
*/
class BiquadBank : public Filter {
std::vector<SeriesBiquad> _filters;
vd _gains;
GlobalThreadPool _pool;
mutable std::mutex _mtx;
public:
/**
* @brief Initialize biquadbank.
*
* @param filters Filters for each filter in the bank. First axis isis the
* coefficient index, second axis is the filter index.
* @param gains Gain values. Given as pointer, if not given (nulltpr), gains
* are initialized with unity gain.
*/
BiquadBank(const dmat &filters, const vd *gains = nullptr);
/**
* @brief Construct biquad bank from already given set of series biquad filters and gain values
*
* @param filters The filters to set
* @param gains The gain values for each filter
*/
BiquadBank(std::vector<SeriesBiquad> filters,vd gains):
_filters(std::move(filters)),_gains(std::move(gains)) {}
/**
* @brief Set new gain values for each filter in the BiquadBank
*
* @param gains Vector of gain values. Should be of same length as the number
* of filters installed.
*/
void setGains(const vd& gains);
/**
* @brief Returns the number of Filters
*
* @return The number of filters
*/
us nfilters() const {return _filters.size();}
virtual void filter(vd &inout) override final;
void reset() override final;
std::unique_ptr<Filter> clone() const override final;
};
/** @} */