2016-03-16 01:05:20 +00:00
|
|
|
//
|
|
|
|
// LinearFilter.c
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 01/10/2011.
|
|
|
|
// Copyright 2011 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "FIRFilter.hpp"
|
2021-07-02 03:15:32 +00:00
|
|
|
|
2017-11-11 20:28:40 +00:00
|
|
|
#include <cmath>
|
2016-03-16 01:05:20 +00:00
|
|
|
|
2020-05-27 23:09:56 +00:00
|
|
|
#ifndef M_PI
|
|
|
|
#define M_PI 3.1415926f
|
|
|
|
#endif
|
|
|
|
|
2016-03-16 01:05:20 +00:00
|
|
|
using namespace SignalProcessing;
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
A Kaiser-Bessel filter is a real time window filter. It looks at the last n samples
|
|
|
|
of an incoming data source and computes a filtered value, which is the value you'd
|
|
|
|
get after applying the specified filter, at the centre of the sampling window.
|
|
|
|
|
|
|
|
Hence, if you request a 37 tap filter then filtering introduces a latency of 18
|
|
|
|
samples. Suppose you're receiving input at 44,100Hz and using 4097 taps, then you'll
|
|
|
|
introduce a latency of 2048 samples, which is about 46ms.
|
|
|
|
|
|
|
|
There's a correlation between the number of taps and the quality of the filtering.
|
|
|
|
More samples = better filtering, at the cost of greater latency. Internally, applying
|
|
|
|
the filter involves calculating a weighted sum of previous values, so increasing the
|
|
|
|
number of taps is quite cheap in processing terms.
|
|
|
|
|
|
|
|
Original source for this filter:
|
|
|
|
|
2018-05-13 19:43:03 +00:00
|
|
|
"DIGITAL SIGNAL PROCESSING, II", IEEE Press, pages 123-126.
|
2016-03-16 01:05:20 +00:00
|
|
|
*/
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
/*! Evaluates the 0th order Bessel function at @c a. */
|
|
|
|
float FIRFilter::ino(float a) {
|
2016-03-16 01:05:20 +00:00
|
|
|
float d = 0.0f;
|
|
|
|
float ds = 1.0f;
|
|
|
|
float s = 1.0f;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
do {
|
2016-03-16 01:05:20 +00:00
|
|
|
d += 2.0f;
|
|
|
|
ds *= (a * a) / (d * d);
|
|
|
|
s += ds;
|
2017-03-26 18:34:47 +00:00
|
|
|
} while(ds > s*1e-6f);
|
2016-03-16 01:05:20 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-11-11 20:28:40 +00:00
|
|
|
void FIRFilter::coefficients_for_idealised_filter_response(short *filter_coefficients, float *A, float attenuation, std::size_t number_of_taps) {
|
2016-03-16 01:05:20 +00:00
|
|
|
/* calculate alpha, which is the Kaiser-Bessel window shape factor */
|
|
|
|
float a; // to take the place of alpha in the normal derivation
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(attenuation < 21.0f) {
|
2016-03-16 01:05:20 +00:00
|
|
|
a = 0.0f;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-03-16 01:05:20 +00:00
|
|
|
if(attenuation > 50.0f)
|
|
|
|
a = 0.1102f * (attenuation - 8.7f);
|
|
|
|
else
|
|
|
|
a = 0.5842f * powf(attenuation - 21.0f, 0.4f) + 0.7886f * (attenuation - 21.0f);
|
|
|
|
}
|
|
|
|
|
2017-11-11 17:20:11 +00:00
|
|
|
std::vector<float> filter_coefficients_float(number_of_taps);
|
2016-03-16 01:05:20 +00:00
|
|
|
|
|
|
|
/* work out the right hand side of the filter coefficients */
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t Np = (number_of_taps - 1) / 2;
|
2016-03-22 02:01:25 +00:00
|
|
|
float I0 = ino(a);
|
2020-05-10 03:00:39 +00:00
|
|
|
float Np_squared = float(Np * Np);
|
2017-11-11 17:20:11 +00:00
|
|
|
for(unsigned int i = 0; i <= Np; ++i) {
|
|
|
|
filter_coefficients_float[Np + i] =
|
2017-11-08 03:54:22 +00:00
|
|
|
A[i] *
|
2020-05-10 03:00:39 +00:00
|
|
|
ino(a * sqrtf(1.0f - (float(i * i) / Np_squared) )) /
|
2016-03-16 01:05:20 +00:00
|
|
|
I0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* coefficients are symmetrical, so copy from right hand side to left side */
|
2017-11-11 20:28:40 +00:00
|
|
|
for(std::size_t i = 0; i < Np; ++i) {
|
2017-11-11 17:20:11 +00:00
|
|
|
filter_coefficients_float[i] = filter_coefficients_float[number_of_taps - 1 - i];
|
2016-03-16 01:05:20 +00:00
|
|
|
}
|
2017-11-08 03:54:22 +00:00
|
|
|
|
2016-03-16 01:05:20 +00:00
|
|
|
/* scale back up so that we retain 100% of input volume */
|
|
|
|
float coefficientTotal = 0.0f;
|
2017-11-11 20:28:40 +00:00
|
|
|
for(std::size_t i = 0; i < number_of_taps; ++i) {
|
2017-11-11 17:20:11 +00:00
|
|
|
coefficientTotal += filter_coefficients_float[i];
|
2016-03-16 01:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we'll also need integer versions, potentially */
|
|
|
|
float coefficientMultiplier = 1.0f / coefficientTotal;
|
2017-11-11 20:28:40 +00:00
|
|
|
for(std::size_t i = 0; i < number_of_taps; ++i) {
|
2020-05-10 03:00:39 +00:00
|
|
|
filter_coefficients[i] = short(filter_coefficients_float[i] * FixedMultiplier * coefficientMultiplier);
|
2016-03-16 01:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 17:20:11 +00:00
|
|
|
std::vector<float> FIRFilter::get_coefficients() const {
|
|
|
|
std::vector<float> coefficients;
|
2018-05-01 02:23:57 +00:00
|
|
|
for(const auto short_coefficient: filter_coefficients_) {
|
2020-05-10 03:00:39 +00:00
|
|
|
coefficients.push_back(float(short_coefficient) / FixedMultiplier);
|
2016-03-22 02:01:25 +00:00
|
|
|
}
|
2017-11-11 17:20:11 +00:00
|
|
|
return coefficients;
|
2016-03-22 02:01:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 20:28:40 +00:00
|
|
|
FIRFilter::FIRFilter(std::size_t number_of_taps, float input_sample_rate, float low_frequency, float high_frequency, float attenuation) {
|
2016-03-16 01:05:20 +00:00
|
|
|
// we must be asked to filter based on an odd number of
|
|
|
|
// taps, and at least three
|
|
|
|
if(number_of_taps < 3) number_of_taps = 3;
|
|
|
|
if(attenuation < 21.0f) attenuation = 21.0f;
|
|
|
|
|
|
|
|
// ensure we have an odd number of taps
|
|
|
|
number_of_taps |= 1;
|
|
|
|
|
|
|
|
// store instance variables
|
2017-11-11 17:20:11 +00:00
|
|
|
filter_coefficients_.resize(number_of_taps);
|
2016-03-16 01:05:20 +00:00
|
|
|
|
|
|
|
/* calculate idealised filter response */
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t Np = (number_of_taps - 1) / 2;
|
2017-11-11 17:20:11 +00:00
|
|
|
float two_over_sample_rate = 2.0f / input_sample_rate;
|
2016-03-16 01:05:20 +00:00
|
|
|
|
2018-11-27 03:34:04 +00:00
|
|
|
// Clamp the high cutoff frequency.
|
|
|
|
high_frequency = std::min(high_frequency, input_sample_rate * 0.5f);
|
|
|
|
|
2017-11-11 17:20:11 +00:00
|
|
|
std::vector<float> A(Np+1);
|
2016-04-23 18:16:49 +00:00
|
|
|
A[0] = 2.0f * (high_frequency - low_frequency) / input_sample_rate;
|
2017-11-11 17:20:11 +00:00
|
|
|
for(unsigned int i = 1; i <= Np; ++i) {
|
2020-05-10 03:00:39 +00:00
|
|
|
float i_pi = float(i) * float(M_PI);
|
2017-11-08 03:54:22 +00:00
|
|
|
A[i] =
|
2016-03-16 01:05:20 +00:00
|
|
|
(
|
2017-11-11 17:20:11 +00:00
|
|
|
sinf(two_over_sample_rate * i_pi * high_frequency) -
|
|
|
|
sinf(two_over_sample_rate * i_pi * low_frequency)
|
|
|
|
) / i_pi;
|
2016-03-16 01:05:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 17:20:11 +00:00
|
|
|
FIRFilter::coefficients_for_idealised_filter_response(filter_coefficients_.data(), A.data(), attenuation, number_of_taps);
|
|
|
|
}
|
2016-03-16 01:05:20 +00:00
|
|
|
|
2017-11-11 17:20:11 +00:00
|
|
|
FIRFilter::FIRFilter(const std::vector<float> &coefficients) {
|
2018-05-01 02:23:57 +00:00
|
|
|
for(const auto coefficient: coefficients) {
|
2020-05-10 03:00:39 +00:00
|
|
|
filter_coefficients_.push_back(short(coefficient * FixedMultiplier));
|
2017-11-11 17:20:11 +00:00
|
|
|
}
|
2016-03-16 01:05:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 17:20:11 +00:00
|
|
|
FIRFilter FIRFilter::operator+(const FIRFilter &rhs) const {
|
|
|
|
std::vector<float> coefficients = get_coefficients();
|
|
|
|
std::vector<float> rhs_coefficients = rhs.get_coefficients();
|
|
|
|
|
|
|
|
std::vector<float> sum;
|
2017-11-11 20:28:40 +00:00
|
|
|
for(std::size_t i = 0; i < coefficients.size(); ++i) {
|
2017-11-11 17:20:11 +00:00
|
|
|
sum.push_back((coefficients[i] + rhs_coefficients[i]) / 2.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FIRFilter(sum);
|
|
|
|
}
|
|
|
|
|
2018-11-27 03:34:04 +00:00
|
|
|
FIRFilter FIRFilter::operator-() const {
|
|
|
|
std::vector<float> negative_coefficients;
|
|
|
|
|
|
|
|
for(const auto coefficient: get_coefficients()) {
|
|
|
|
negative_coefficients.push_back(1.0f - coefficient);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FIRFilter(negative_coefficients);
|
|
|
|
}
|
|
|
|
|
2017-11-11 17:20:11 +00:00
|
|
|
FIRFilter FIRFilter::operator*(const FIRFilter &rhs) const {
|
|
|
|
std::vector<float> coefficients = get_coefficients();
|
|
|
|
std::vector<float> rhs_coefficients = rhs.get_coefficients();
|
|
|
|
|
|
|
|
std::vector<float> sum;
|
2017-11-11 20:28:40 +00:00
|
|
|
for(std::size_t i = 0; i < coefficients.size(); ++i) {
|
2017-11-11 17:20:11 +00:00
|
|
|
sum.push_back(coefficients[i] * rhs_coefficients[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FIRFilter(sum);
|
2016-03-16 01:05:20 +00:00
|
|
|
}
|