Commit f791c431 authored by Jonathan Hyde's avatar Jonathan Hyde

Brand new filters.

Fix automation.
Fix meters.
parent 9df90ef2
This diff is collapsed.
/*******************************************************************************
"A Collection of Useful C++ Classes for Digital Signal Processing"
By Vincent Falco
Official project location:
http://code.google.com/p/dspfilterscpp/
See Documentation.cpp for contact information, notes, and bibliography.
--------------------------------------------------------------------------------
License: MIT License (http://www.opensource.org/licenses/mit-license.php)
Copyright (c) 2009 by Vincent Falco
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************************************************************/
#ifndef DSPFILTERS_BIQUAD_H
#define DSPFILTERS_BIQUAD_H
#include "DspFilters/Common.h"
#include "DspFilters/MathSupplement.h"
#include "DspFilters/Types.h"
namespace Dsp {
struct BiquadPoleState;
/*
* Holds coefficients for a second order Infinite Impulse Response
* digital filter. This is the building block for all IIR filters.
*
*/
// Factored interface to prevent outsiders from fiddling
class BiquadBase
{
public:
template <class StateType>
struct State : StateType, private DenormalPrevention
{
template <typename Sample>
inline Sample process (const Sample in, const BiquadBase& b)
{
return static_cast<Sample> (StateType::process1 (in, b, ac()));
}
};
public:
// Calculate filter response at the given normalized frequency.
complex_t response (double normalizedFrequency) const;
std::vector<PoleZeroPair> getPoleZeros () const;
double getA0 () const { return m_a0; }
double getA1 () const { return m_a1*m_a0; }
double getA2 () const { return m_a2*m_a0; }
double getB0 () const { return m_b0*m_a0; }
double getB1 () const { return m_b1*m_a0; }
double getB2 () const { return m_b2*m_a0; }
// Process a block of samples in the given form
template <class StateType, typename Sample>
void process (int numSamples, Sample* dest, StateType& state) const
{
while (--numSamples >= 0)
*dest++ = state.process (*dest, *this);
}
protected:
//
// These are protected so you can't mess with RBJ biquads
//
void setCoefficients (double a0, double a1, double a2,
double b0, double b1, double b2);
void setOnePole (complex_t pole, complex_t zero);
void setTwoPole (complex_t pole1, complex_t zero1,
complex_t pole2, complex_t zero2);
void setPoleZeroPair (const PoleZeroPair& pair)
{
if (pair.isSinglePole ())
setOnePole (pair.poles.first, pair.zeros.first);
else
setTwoPole (pair.poles.first, pair.zeros.first,
pair.poles.second, pair.zeros.second);
}
void setPoleZeroForm (const BiquadPoleState& bps);
void setIdentity ();
void applyScale (double scale);
public:
double m_a0;
double m_a1;
double m_a2;
double m_b1;
double m_b2;
double m_b0;
};
//------------------------------------------------------------------------------
// Expresses a biquad as a pair of pole/zeros, with gain
// values so that the coefficients can be reconstructed precisely.
struct BiquadPoleState : PoleZeroPair
{
BiquadPoleState () { }
explicit BiquadPoleState (const BiquadBase& s);
double gain;
};
// More permissive interface for fooling around
class Biquad : public BiquadBase
{
public:
Biquad ();
explicit Biquad (const BiquadPoleState& bps);
public:
// Process a block of samples, interpolating from the old section's coefficients
// to this section's coefficients, over numSamples. This implements smooth
// parameter changes.
template <class StateType, typename Sample>
void smoothProcess1 (int numSamples,
Sample* dest,
StateType& state,
Biquad sectionPrev) const
{
double t = 1. / numSamples;
double da1 = (m_a1 - sectionPrev.m_a1) * t;
double da2 = (m_a2 - sectionPrev.m_a2) * t;
double db0 = (m_b0 - sectionPrev.m_b0) * t;
double db1 = (m_b1 - sectionPrev.m_b1) * t;
double db2 = (m_b2 - sectionPrev.m_b2) * t;
while (--numSamples >= 0)
{
sectionPrev.m_a1 += da1;
sectionPrev.m_a2 += da2;
sectionPrev.m_b0 += db0;
sectionPrev.m_b1 += db1;
sectionPrev.m_b2 += db2;
*dest++ = state.process (*dest, sectionPrev);
}
}
// Process a block of samples, interpolating from the old section's pole/zeros
// to this section's pole/zeros, over numSamples. The interpolation is done
// in the z-plane using polar coordinates.
template <class StateType, typename Sample>
void smoothProcess2 (int numSamples,
Sample* dest,
StateType& state,
BiquadPoleState zPrev) const
{
BiquadPoleState z (*this);
double t = 1. / numSamples;
complex_t dp0 = (z.poles.first - zPrev.poles.first) * t;
complex_t dp1 = (z.poles.second - zPrev.poles.second) * t;
complex_t dz0 = (z.zeros.first - zPrev.zeros.first) * t;
complex_t dz1 = (z.zeros.second - zPrev.zeros.second) * t;
double dg = (z.gain - zPrev.gain) * t;
while (--numSamples >= 0)
{
zPrev.poles.first += dp0;
zPrev.poles.second += dp1;
zPrev.zeros.first += dz0;
zPrev.zeros.second += dz1;
zPrev.gain += dg;
*dest++ = state.process (*dest, Biquad (zPrev));
}
}
public:
// Export these as public
void setOnePole (complex_t pole, complex_t zero)
{
BiquadBase::setOnePole (pole, zero);
}
void setTwoPole (complex_t pole1, complex_t zero1,
complex_t pole2, complex_t zero2)
{
BiquadBase::setTwoPole (pole1, zero1, pole2, zero2);
}
void setPoleZeroPair (const PoleZeroPair& pair)
{
BiquadBase::setPoleZeroPair (pair);
}
void applyScale (double scale)
{
BiquadBase::applyScale (scale);
}
};
}
#endif
/*******************************************************************************
"A Collection of Useful C++ Classes for Digital Signal Processing"
By Vincent Falco
Official project location:
http://code.google.com/p/dspfilterscpp/
See Documentation.cpp for contact information, notes, and bibliography.
--------------------------------------------------------------------------------
License: MIT License (http://www.opensource.org/licenses/mit-license.php)
Copyright (c) 2009 by Vincent Falco
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************************************************************/
#ifndef DSPFILTERS_BUTTERWORTH_H
#define DSPFILTERS_BUTTERWORTH_H
#include "DspFilters/Common.h"
#include "DspFilters/Cascade.h"
#include "DspFilters/Design.h"
#include "DspFilters/Filter.h"
#include "DspFilters/PoleFilter.h"
namespace Dsp {
/*
* Filters with Butterworth response characteristics
*
*/
namespace Butterworth {
// Half-band analog prototypes (s-plane)
class AnalogLowPass : public LayoutBase
{
public:
AnalogLowPass ();
void design (const int numPoles);
private:
int m_numPoles;
};
//------------------------------------------------------------------------------
class AnalogLowShelf : public LayoutBase
{
public:
AnalogLowShelf ();
void design (int numPoles, double gainDb);
private:
int m_numPoles;
double m_gainDb;
};
//------------------------------------------------------------------------------
// Factored implementations to reduce template instantiations
struct LowPassBase : PoleFilterBase <AnalogLowPass>
{
void setup (int order,
double sampleRate,
double cutoffFrequency);
};
struct HighPassBase : PoleFilterBase <AnalogLowPass>
{
void setup (int order,
double sampleRate,
double cutoffFrequency);
};
struct BandPassBase : PoleFilterBase <AnalogLowPass>
{
void setup (int order,
double sampleRate,
double centerFrequency,
double widthFrequency);
};
struct BandStopBase : PoleFilterBase <AnalogLowPass>
{
void setup (int order,
double sampleRate,
double centerFrequency,
double widthFrequency);
};
struct LowShelfBase : PoleFilterBase <AnalogLowShelf>
{
void setup (int order,
double sampleRate,
double cutoffFrequency,
double gainDb);
};
struct HighShelfBase : PoleFilterBase <AnalogLowShelf>
{
void setup (int order,
double sampleRate,
double cutoffFrequency,
double gainDb);
};
struct BandShelfBase : PoleFilterBase <AnalogLowShelf>
{
void setup (int order,
double sampleRate,
double centerFrequency,
double widthFrequency,
double gainDb);
};
//------------------------------------------------------------------------------
//
// Raw filters
//
template <int MaxOrder>
struct LowPass : PoleFilter <LowPassBase, MaxOrder>
{
};
template <int MaxOrder>
struct HighPass : PoleFilter <HighPassBase, MaxOrder>
{
};
template <int MaxOrder>
struct BandPass : PoleFilter <BandPassBase, MaxOrder, MaxOrder*2>
{
};
template <int MaxOrder>
struct BandStop : PoleFilter <BandStopBase, MaxOrder, MaxOrder*2>
{
};
template <int MaxOrder>
struct LowShelf : PoleFilter <LowShelfBase, MaxOrder>
{
};
template <int MaxOrder>
struct HighShelf : PoleFilter <HighShelfBase, MaxOrder>
{
};
template <int MaxOrder>
struct BandShelf : PoleFilter <BandShelfBase, MaxOrder, MaxOrder*2>
{
};
//------------------------------------------------------------------------------
//
// Gui-friendly Design layer
//
namespace Design {
struct TypeIBase : DesignBase
{
enum
{
NumParams = 3
};
static int getNumParams ()
{
return 3;
}
static const ParamInfo getParamInfo_2 ()
{
return ParamInfo::defaultCutoffFrequencyParam ();
}
};
template <class FilterClass>
struct TypeI : TypeIBase, FilterClass
{
void setParams (const Params& params)
{
FilterClass::setup (int(params[1]), params[0], params[2]);
}
};
struct TypeIIBase : DesignBase
{
enum
{
NumParams = 4
};
static int getNumParams ()
{
return 4;
}
static const ParamInfo getParamInfo_2 ()
{
return ParamInfo::defaultCenterFrequencyParam ();
}
static const ParamInfo getParamInfo_3 ()
{
return ParamInfo::defaultBandwidthHzParam ();
}
};
template <class FilterClass>
struct TypeII : TypeIIBase, FilterClass
{
void setParams (const Params& params)
{
FilterClass::setup (int(params[1]), params[0], params[2], params[3]);
}
};
struct TypeIIIBase : DesignBase
{
enum
{
NumParams = 4
};
static int getNumParams ()
{
return 4;
}
static const ParamInfo getParamInfo_2 ()
{
return ParamInfo::defaultCutoffFrequencyParam ();
}
static const ParamInfo getParamInfo_3 ()
{
return ParamInfo::defaultGainParam ();
}
};
template <class FilterClass>
struct TypeIII : TypeIIIBase, FilterClass
{
void setParams (const Params& params)
{
FilterClass::setup (int(params[1]),
params[0],
params[2],
params[3]);
}
};
struct TypeIVBase : DesignBase
{
enum
{
NumParams = 5
};
static int getNumParams ()
{
return 5;
}
static const ParamInfo getParamInfo_2 ()
{
return ParamInfo::defaultCenterFrequencyParam ();
}
static const ParamInfo getParamInfo_3 ()
{
return ParamInfo::defaultBandwidthHzParam ();
}
static const ParamInfo getParamInfo_4 ()
{
return ParamInfo::defaultGainParam ();
}
};
template <class FilterClass>
struct TypeIV : TypeIVBase, FilterClass
{
void setParams (const Params& params)
{
FilterClass::setup (int(params[1]), params[0], params[2], params[3], params[4]);
}
};
// Factored kind and name
struct LowPassDescription
{
static Kind getKind () { return kindLowPass; }
static const char* getName() { return "Butterworth Low Pass"; }
};
struct HighPassDescription
{
static Kind getKind () { return kindHighPass; }
static const char* getName() { return "Butterworth High Pass"; }
};
struct BandPassDescription
{
static Kind getKind () { return kindHighPass; }
static const char* getName() { return "Butterworth Band Pass"; }
};
struct BandStopDescription
{
static Kind getKind () { return kindHighPass; }
static const char* getName() { return "Butterworth Band Stop"; }
};
struct LowShelfDescription
{
static Kind getKind () { return kindLowShelf; }
static const char* getName() { return "Butterworth Low Shelf"; }
};
struct HighShelfDescription
{
static Kind getKind () { return kindHighShelf; }
static const char* getName() { return "Butterworth High Shelf"; }
};
struct BandShelfDescription
{
static Kind getKind () { return kindBandShelf; }
static const char* getName() { return "Butterworth Band Shelf"; }
};
// This glues on the Order parameter
template <int MaxOrder,
template <class> class TypeClass,
template <int> class FilterClass>
struct OrderBase : TypeClass <FilterClass <MaxOrder> >
{
const ParamInfo getParamInfo_1 () const
{
return ParamInfo (idOrder, "Order", "Order",
1, MaxOrder, 2,
&ParamInfo::Int_toControlValue,
&ParamInfo::Int_toNativeValue,
&ParamInfo::Int_toString);
}
};
//------------------------------------------------------------------------------
//
// Design filters
//
template <int MaxOrder>
struct LowPass : OrderBase <MaxOrder, TypeI, Butterworth::LowPass>,
LowPassDescription
{
};
template <int MaxOrder>
struct HighPass : OrderBase <MaxOrder, TypeI, Butterworth::HighPass>,
HighPassDescription
{
};
template <int MaxOrder>
struct BandPass : OrderBase <MaxOrder, TypeII, Butterworth::BandPass>,
BandPassDescription
{
};
template <int MaxOrder>
struct BandStop : OrderBase <MaxOrder, TypeII, Butterworth::BandStop>,
BandStopDescription
{
};
template <int MaxOrder>
struct LowShelf : OrderBase <MaxOrder, TypeIII, Butterworth::LowShelf>,
LowShelfDescription
{
};
template <int MaxOrder>
struct HighShelf : OrderBase <MaxOrder, TypeIII, Butterworth::HighShelf>,
HighShelfDescription
{
};
template <int MaxOrder>
struct BandShelf : OrderBase <MaxOrder, TypeIV, Butterworth::BandShelf>,
BandShelfDescription
{
};
}
}
}
#endif
/*******************************************************************************
"A Collection of Useful C++ Classes for Digital Signal Processing"
By Vincent Falco
Official project location:
http://code.google.com/p/dspfilterscpp/
See Documentation.cpp for contact information, notes, and bibliography.
--------------------------------------------------------------------------------
License: MIT License (http://www.opensource.org/licenses/mit-license.php)
Copyright (c) 2009 by Vincent Falco
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************************************************************/
#ifndef DSPFILTERS_CASCADE_H
#define DSPFILTERS_CASCADE_H
#include "DspFilters/Common.h"
#include "DspFilters/Biquad.h"
#include "DspFilters/Filter.h"
#include "DspFilters/Layout.h"
#include "DspFilters/MathSupplement.h"
namespace Dsp {
/*
* Holds coefficients for a cascade of second order sections.
*
*/
// Factored implementation to reduce template instantiations
class Cascade
{
public:
template <class StateType>
class StateBase : private DenormalPrevention
{
public:
template <typename Sample>
inline Sample process (const Sample in, const Cascade& c)
{
double out = in;
StateType* state = m_stateArray;
Biquad const* stage = c.m_stageArray;