CUGL 1.3
Cornell University Game Library
CUFIRFilter.h
1 //
2 // CUFIRFilter.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class is represents a finite impulse response filter. It is a
6 // general purpose filter that allows an arbitrary number of coefficients.
7 // It is significantly faster than the general purpose IIR filter when no
8 // feedback terms are required. With that said, for less than 3-order FIR
9 // filters, you should use one of the more specific filters for performance
10 // reasons.
11 //
12 // This class supports vector optimizations for SSE and Neon 64. In timed
13 // simulations, these optimizations provide at least a 3-4x performance
14 // increase (and in isolated cases, much higher). Our implementation is
15 // limited to 128-bit words as 256-bit (e.g. AVX) and higher show no
16 // significant increase in performance.
17 //
18 // For performance reasons, this class does not have a (virtualized) subclass
19 // relationship with other IIR or FIR filters. However, the signature of the
20 // the calculation and coefficient methods has been standardized so that it
21 // can support templated polymorphism.
22 //
23 // This class is NOT THREAD SAFE. This is by design, for performance reasons.
24 // External locking may be required when the filter is shared between multiple
25 // threads (such as between an audio thread and the main thread).
26 //
27 // CUGL MIT License:
28 // This software is provided 'as-is', without any express or implied
29 // warranty. In no event will the authors be held liable for any damages
30 // arising from the use of this software.
31 //
32 // Permission is granted to anyone to use this software for any purpose,
33 // including commercial applications, and to alter it and redistribute it
34 // freely, subject to the following restrictions:
35 //
36 // 1. The origin of this software must not be misrepresented; you must not
37 // claim that you wrote the original software. If you use this software
38 // in a product, an acknowledgment in the product documentation would be
39 // appreciated but is not required.
40 //
41 // 2. Altered source versions must be plainly marked as such, and must not
42 // be misrepresented as being the original software.
43 //
44 // 3. This notice may not be removed or altered from any source distribution.
45 //
46 // Author: Walker White
47 // Version: 6/11/18
48 //
49 #ifndef __CU_FIR_FILTER_H__
50 #define __CU_FIR_FILTER_H__
51 
52 #include <cugl/math/dsp/CUIIRFilter.h>
53 #include <cugl/math/CUMathBase.h>
54 #include <cugl/util/CUAligned.h>
55 #include <cstring>
56 #include <vector>
57 
58 namespace cugl {
59  namespace dsp {
60 
92 class FIRFilter {
93 private:
95  unsigned _channels;
97  float _b0;
98 
100  cugl::Aligned<float> _bval;
102  cugl::Aligned<float> _inns;
103 
109  void reset();
110 
111 #pragma mark SPECIALIZED FILTERS
112 
141  void stride(float gain, float* input, float* output, size_t size, unsigned channel);
142 
165  void single(float gain, float* input, float* output, size_t size);
166 
190  void dual(float gain, float* input, float* output, size_t size);
191 
218  void trio(float gain, float* input, float* output, size_t size);
219 
243  void quad(float gain, float* input, float* output, size_t size);
244 
268  void quart(float gain, float* input, float* output, size_t size);
269 
270 public:
272  static bool VECTORIZE;
273 
274 #pragma mark Constructors
275 
278  FIRFilter();
279 
285  FIRFilter(unsigned channels);
286 
299  FIRFilter(unsigned channels, const std::vector<float> &bvals);
300 
306  FIRFilter(const FIRFilter& copy);
307 
313  FIRFilter(FIRFilter&& filter);
314 
318  ~FIRFilter();
319 
320 #pragma mark IIR Signature
321 
329  unsigned getChannels() const { return _channels; }
330 
339  void setChannels(unsigned channels);
340 
355  void setCoeff(const std::vector<float> &bvals, const std::vector<float> &avals);
356 
368  const std::vector<float> getBCoeff() const;
369 
381  const std::vector<float> getACoeff() const;
382 
383 #pragma mark Specialized Attributes
384 
395  void setBCoeff(const std::vector<float> &bvals);
396 
397 #pragma mark Filter Methods
398 
414  void step(float gain, float* input, float* output);
415 
435  void calculate(float gain, float* input, float* output, size_t size);
436 
440  void clear();
441 
452  size_t flush(float* output);
453 };
454  }
455 }
456 #endif /* __CU_FIR_FILTER_H__ */
cugl::dsp::FIRFilter::getACoeff
const std::vector< float > getACoeff() const
cugl::dsp::FIRFilter::clear
void clear()
cugl::dsp::FIRFilter
Definition: CUFIRFilter.h:92
cugl::dsp::FIRFilter::~FIRFilter
~FIRFilter()
cugl::dsp::FIRFilter::setChannels
void setChannels(unsigned channels)
cugl::dsp::FIRFilter::calculate
void calculate(float gain, float *input, float *output, size_t size)
cugl::dsp::FIRFilter::getChannels
unsigned getChannels() const
Definition: CUFIRFilter.h:329
cugl::dsp::FIRFilter::step
void step(float gain, float *input, float *output)
cugl::dsp::FIRFilter::flush
size_t flush(float *output)
cugl::Aligned< float >
cugl::dsp::FIRFilter::getBCoeff
const std::vector< float > getBCoeff() const
cugl::dsp::FIRFilter::FIRFilter
FIRFilter()
cugl::dsp::FIRFilter::setCoeff
void setCoeff(const std::vector< float > &bvals, const std::vector< float > &avals)
cugl::dsp::FIRFilter::VECTORIZE
static bool VECTORIZE
Definition: CUFIRFilter.h:272
cugl::dsp::FIRFilter::setBCoeff
void setBCoeff(const std::vector< float > &bvals)