CUGL 1.3
Cornell University Game Library
CUPoleZeroIIR.h
1 //
2 // CUPoleZeroFIR.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class is represents a one-pole, one-zero IIR filter. This is the
6 // standard class for implementing first order highpass filters. For
7 // first-order filters, it is significantly more performant than IIRFilter.
8 //
9 // This class supports vector optimizations for SSE and Neon 64. In timed
10 // simulations, these optimizations provide at least a 3-4x performance
11 // increase (and in isolated cases, much higher). Our implementation is
12 // limited to 128-bit words as 256-bit (e.g. AVX) and higher show no
13 // significant increase in performance.
14 //
15 // For performance reasons, this class does not have a (virtualized) subclass
16 // relationship with other IIR or FIR filters. However, the signature of the
17 // the calculation and coefficient methods has been standardized so that it
18 // can support templated polymorphism.
19 //
20 // This class is NOT THREAD SAFE. This is by design, for performance reasons.
21 // External locking may be required when the filter is shared between multiple
22 // threads (such as between an audio thread and the main thread).
23 //
24 // CUGL MIT License:
25 // This software is provided 'as-is', without any express or implied
26 // warranty. In no event will the authors be held liable for any damages
27 // arising from the use of this software.
28 //
29 // Permission is granted to anyone to use this software for any purpose,
30 // including commercial applications, and to alter it and redistribute it
31 // freely, subject to the following restrictions:
32 //
33 // 1. The origin of this software must not be misrepresented; you must not
34 // claim that you wrote the original software. If you use this software
35 // in a product, an acknowledgment in the product documentation would be
36 // appreciated but is not required.
37 //
38 // 2. Altered source versions must be plainly marked as such, and must not
39 // be misrepresented as being the original software.
40 //
41 // 3. This notice may not be removed or altered from any source distribution.
42 //
43 // Author: Walker White
44 // Version: 6/11/18
45 //
46 #ifndef __CU_POLE_ZERO_FIR_H__
47 #define __CU_POLE_ZERO_FIR_H__
48 
49 #include <cugl/math/dsp/CUIIRFilter.h>
50 #include <cugl/math/CUMathBase.h>
51 #include <cugl/util/CUAligned.h>
52 #include <cstring>
53 #include <vector>
54 
55 namespace cugl {
56  namespace dsp {
57 
94 class PoleZeroFIR {
95 private:
97  unsigned _channels;
99  float _b0, _b1;
101  float _a1, _a2;
102 
104  cugl::Aligned<float> _inns;
106  cugl::Aligned<float> _outs;
107 
108  // Optimization data structures (single channel)
109  float __attribute__((__aligned__(16))) _c1[4];
110  float __attribute__((__aligned__(16))) _d1[16];
111 
112  // Optimization data structures (dual channel)
113  float __attribute__((__aligned__(16))) _c2[8];
114  float __attribute__((__aligned__(16))) _d2[16];
115 
121  void reset();
122 
123 #pragma mark SPECIALIZED FILTERS
124 
153  void stride(float gain, float* input, float* output, size_t size, unsigned channel);
154 
177  void single(float gain, float* input, float* output, size_t size);
178 
202  void dual(float gain, float* input, float* output, size_t size);
203 
230  void trio(float gain, float* input, float* output, size_t size);
231 
255  void quad(float gain, float* input, float* output, size_t size);
256 
280  void quart(float gain, float* input, float* output, size_t size);
281 
282 public:
284  static bool VECTORIZE;
285 
286 #pragma mark Constructors
287 
290  PoleZeroFIR();
291 
297  PoleZeroFIR(unsigned channels);
298 
313  PoleZeroFIR(unsigned channels, float b0, float b1, float a1);
314 
320  PoleZeroFIR(const PoleZeroFIR& copy);
321 
327  PoleZeroFIR(PoleZeroFIR&& filter);
328 
332  ~PoleZeroFIR();
333 
334 #pragma mark IIR Signature
335 
343  unsigned getChannels() const { return _channels; }
344 
353  void setChannels(unsigned channels);
354 
372  void setCoeff(const std::vector<float> &bvals, const std::vector<float> &avals);
373 
385  const std::vector<float> getBCoeff() const;
386 
398  const std::vector<float> getACoeff() const;
399 
400 #pragma mark Specialized Attributes
401 
409  void setBCoeff(float b0, float b1);
410 
418  void setACoeff(float a1);
419 
433  void setHighpass(float frequency);
434 
443  void setAllpass(float coefficient);
444 
454  void setBlockZero(float pole = 0.99f);
455 
456 #pragma mark Filter Methods
457 
473  void step(float gain, float* input, float* output);
474 
494  void calculate(float gain, float* input, float* output, size_t size);
495 
499  void clear();
500 
509  size_t flush(float* output);
510 };
511 
512  }
513 }
514 #endif /* __CU_POLE_ZERO_FIR_H__ */
cugl::dsp::PoleZeroFIR
Definition: CUPoleZeroIIR.h:94
cugl::dsp::PoleZeroFIR::setAllpass
void setAllpass(float coefficient)
cugl::dsp::PoleZeroFIR::getACoeff
const std::vector< float > getACoeff() const
cugl::dsp::PoleZeroFIR::getBCoeff
const std::vector< float > getBCoeff() const
cugl::dsp::PoleZeroFIR::setBlockZero
void setBlockZero(float pole=0.99f)
cugl::dsp::PoleZeroFIR::clear
void clear()
cugl::Aligned< float >
cugl::dsp::PoleZeroFIR::getChannels
unsigned getChannels() const
Definition: CUPoleZeroIIR.h:343
cugl::dsp::PoleZeroFIR::step
void step(float gain, float *input, float *output)
cugl::dsp::PoleZeroFIR::VECTORIZE
static bool VECTORIZE
Definition: CUPoleZeroIIR.h:284
cugl::dsp::PoleZeroFIR::setHighpass
void setHighpass(float frequency)
cugl::dsp::PoleZeroFIR::setChannels
void setChannels(unsigned channels)
cugl::dsp::PoleZeroFIR::~PoleZeroFIR
~PoleZeroFIR()
cugl::dsp::PoleZeroFIR::setBCoeff
void setBCoeff(float b0, float b1)
cugl::dsp::PoleZeroFIR::setCoeff
void setCoeff(const std::vector< float > &bvals, const std::vector< float > &avals)
cugl::dsp::PoleZeroFIR::flush
size_t flush(float *output)
cugl::dsp::PoleZeroFIR::setACoeff
void setACoeff(float a1)
cugl::dsp::PoleZeroFIR::calculate
void calculate(float gain, float *input, float *output, size_t size)
cugl::dsp::PoleZeroFIR::PoleZeroFIR
PoleZeroFIR()