2016-03-16 01:05:20 +00:00
|
|
|
//
|
|
|
|
// LinearFilter.h
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 01/10/2011.
|
|
|
|
// Copyright 2011 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef FIRFilter_hpp
|
|
|
|
#define FIRFilter_hpp
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <Accelerate/Accelerate.h>
|
2020-02-16 22:53:26 +00:00
|
|
|
#define USE_ACCELERATE
|
2016-03-16 01:05:20 +00:00
|
|
|
#endif
|
|
|
|
|
2017-11-11 17:20:11 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-03-16 01:05:20 +00:00
|
|
|
namespace SignalProcessing {
|
|
|
|
|
2017-11-11 17:20:11 +00:00
|
|
|
/*!
|
|
|
|
The FIR filter takes a 1d PCM signal with a given sample rate and applies a band-pass filter to it.
|
|
|
|
|
|
|
|
The number of taps (ie, samples considered simultaneously to make an output sample) is configurable;
|
|
|
|
smaller numbers permit a filter that operates more quickly and with less lag but less effectively.
|
|
|
|
*/
|
2016-03-16 01:05:20 +00:00
|
|
|
class FIRFilter {
|
2017-11-09 03:48:44 +00:00
|
|
|
private:
|
2017-11-11 17:20:11 +00:00
|
|
|
static constexpr float FixedMultiplier = 32767.0f;
|
|
|
|
static constexpr int FixedShift = 15;
|
2017-11-09 03:48:44 +00:00
|
|
|
|
2016-03-16 01:05:20 +00:00
|
|
|
public:
|
2018-11-24 03:33:01 +00:00
|
|
|
/*! A suggested default attenuation value. */
|
|
|
|
constexpr static float DefaultAttenuation = 60.0f;
|
2016-03-16 01:05:20 +00:00
|
|
|
/*!
|
|
|
|
Creates an instance of @c FIRFilter.
|
2017-11-08 03:51:06 +00:00
|
|
|
|
2016-03-16 01:05:20 +00:00
|
|
|
@param number_of_taps The size of window for input data.
|
|
|
|
@param input_sample_rate The sampling rate of the input signal.
|
|
|
|
@param low_frequency The lowest frequency of signal to retain in the output.
|
|
|
|
@param high_frequency The highest frequency of signal to retain in the output.
|
2016-03-22 02:01:25 +00:00
|
|
|
@param attenuation The attenuation of the discarded frequencies.
|
2016-03-16 01:05:20 +00:00
|
|
|
*/
|
2018-11-24 03:33:01 +00:00
|
|
|
FIRFilter(std::size_t number_of_taps, float input_sample_rate, float low_frequency, float high_frequency, float attenuation = DefaultAttenuation);
|
2017-11-11 17:20:11 +00:00
|
|
|
FIRFilter(const std::vector<float> &coefficients);
|
2016-03-16 01:05:20 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Applies the filter to one batch of input samples, returning the net result.
|
2017-11-08 03:51:06 +00:00
|
|
|
|
2016-03-16 01:05:20 +00:00
|
|
|
@param src The source buffer to apply the filter to.
|
|
|
|
@returns The result of applying the filter.
|
|
|
|
*/
|
2020-02-15 23:55:19 +00:00
|
|
|
inline short apply(const short *src, size_t stride = 1) const {
|
2020-02-16 22:53:26 +00:00
|
|
|
#ifdef USE_ACCELERATE
|
2016-03-16 01:05:20 +00:00
|
|
|
short result;
|
2020-02-16 19:05:23 +00:00
|
|
|
vDSP_dotpr_s1_15(filter_coefficients_.data(), 1, src, vDSP_Stride(stride), &result, filter_coefficients_.size());
|
2016-03-16 01:05:20 +00:00
|
|
|
return result;
|
|
|
|
#else
|
|
|
|
int outputValue = 0;
|
2017-11-11 20:28:40 +00:00
|
|
|
for(std::size_t c = 0; c < filter_coefficients_.size(); ++c) {
|
2020-02-15 23:55:19 +00:00
|
|
|
outputValue += filter_coefficients_[c] * src[c * stride];
|
2016-03-16 01:05:20 +00:00
|
|
|
}
|
2017-11-11 17:41:49 +00:00
|
|
|
return static_cast<short>(outputValue >> FixedShift);
|
2016-03-16 01:05:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-11-11 17:20:11 +00:00
|
|
|
/*! @returns The number of taps used by this filter. */
|
2017-11-11 20:28:40 +00:00
|
|
|
inline std::size_t get_number_of_taps() const {
|
2017-11-11 17:20:11 +00:00
|
|
|
return filter_coefficients_.size();
|
2016-03-22 02:01:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 17:20:11 +00:00
|
|
|
/*! @returns The weighted coefficients that describe this filter. */
|
|
|
|
std::vector<float> get_coefficients() const;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns A filter that would have the effect of adding (and scaling) the outputs of the two filters.
|
|
|
|
Defined only if both have the same number of taps.
|
|
|
|
*/
|
|
|
|
FIRFilter operator+(const FIRFilter &) const;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns A filter that would have the effect of applying the two filters in succession.
|
|
|
|
Defined only if both have the same number of taps.
|
|
|
|
*/
|
|
|
|
FIRFilter operator*(const FIRFilter &) const;
|
2016-03-22 02:01:25 +00:00
|
|
|
|
2018-11-27 03:34:04 +00:00
|
|
|
/*!
|
|
|
|
@returns A filter that would have the opposite effect of this filter.
|
|
|
|
*/
|
|
|
|
FIRFilter operator-() const;
|
|
|
|
|
2016-03-16 01:05:20 +00:00
|
|
|
private:
|
2017-11-11 17:20:11 +00:00
|
|
|
std::vector<short> filter_coefficients_;
|
2016-03-22 02:01:25 +00:00
|
|
|
|
2017-11-11 20:28:40 +00:00
|
|
|
static void coefficients_for_idealised_filter_response(short *filterCoefficients, float *A, float attenuation, std::size_t numberOfTaps);
|
2016-03-22 02:01:25 +00:00
|
|
|
static float ino(float a);
|
2016-03-16 01:05:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|