1
0
mirror of https://github.com/TomHarte/CLK.git synced 2026-04-26 03:29:40 +00:00

Take another big swing at indentation, some consts.

This commit is contained in:
Thomas Harte
2024-12-01 21:44:14 -05:00
parent 31c878b654
commit d3ed485e7a
158 changed files with 12552 additions and 12483 deletions
+70 -58
View File
@@ -28,75 +28,87 @@ namespace SignalProcessing {
smaller numbers permit a filter that operates more quickly and with less lag but less effectively.
*/
class FIRFilter {
private:
static constexpr float FixedMultiplier = 32767.0f;
static constexpr int FixedShift = 15;
private:
static constexpr float FixedMultiplier = 32767.0f;
static constexpr int FixedShift = 15;
public:
/*! A suggested default attenuation value. */
constexpr static float DefaultAttenuation = 60.0f;
/*!
Creates an instance of @c FIRFilter.
public:
/*! A suggested default attenuation value. */
constexpr static float DefaultAttenuation = 60.0f;
/*!
Creates an instance of @c FIRFilter.
@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.
@param attenuation The attenuation of the discarded frequencies.
*/
FIRFilter(std::size_t number_of_taps, float input_sample_rate, float low_frequency, float high_frequency, float attenuation = DefaultAttenuation);
FIRFilter(const std::vector<float> &coefficients);
@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.
@param attenuation The attenuation of the discarded frequencies.
*/
FIRFilter(
std::size_t number_of_taps,
float input_sample_rate,
float low_frequency,
float high_frequency,
float attenuation = DefaultAttenuation
);
FIRFilter(const std::vector<float> &coefficients);
/*!
Applies the filter to one batch of input samples, returning the net result.
/*!
Applies the filter to one batch of input samples, returning the net result.
@param src The source buffer to apply the filter to.
@returns The result of applying the filter.
*/
inline short apply(const short *src, size_t stride = 1) const {
#ifdef USE_ACCELERATE
short result;
vDSP_dotpr_s1_15(filter_coefficients_.data(), 1, src, vDSP_Stride(stride), &result, filter_coefficients_.size());
return result;
#else
int outputValue = 0;
for(std::size_t c = 0; c < filter_coefficients_.size(); ++c) {
outputValue += filter_coefficients_[c] * src[c * stride];
}
return short(outputValue >> FixedShift);
#endif
}
@param src The source buffer to apply the filter to.
@returns The result of applying the filter.
*/
inline short apply(const short *src, size_t stride = 1) const {
#ifdef USE_ACCELERATE
short result;
vDSP_dotpr_s1_15(
filter_coefficients_.data(),
1,
src,
vDSP_Stride(stride), &result, filter_coefficients_.size()
);
return result;
#else
int outputValue = 0;
for(std::size_t c = 0; c < filter_coefficients_.size(); ++c) {
outputValue += filter_coefficients_[c] * src[c * stride];
}
return short(outputValue >> FixedShift);
#endif
}
/*! @returns The number of taps used by this filter. */
inline std::size_t get_number_of_taps() const {
return filter_coefficients_.size();
}
/*! @returns The number of taps used by this filter. */
inline std::size_t get_number_of_taps() const {
return filter_coefficients_.size();
}
/*! @returns The weighted coefficients that describe this filter. */
std::vector<float> get_coefficients() const;
/*! @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 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;
/*!
@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;
/*!
@returns A filter that would have the opposite effect of this filter.
*/
FIRFilter operator-() const;
/*!
@returns A filter that would have the opposite effect of this filter.
*/
FIRFilter operator-() const;
private:
std::vector<short> filter_coefficients_;
private:
std::vector<short> filter_coefficients_;
static void coefficients_for_idealised_filter_response(short *filterCoefficients, float *A, float attenuation, std::size_t numberOfTaps);
static float ino(float a);
static void coefficients_for_idealised_filter_response(
short *filterCoefficients, float *A, float attenuation, std::size_t numberOfTaps);
static float ino(float a);
};
}
+57 -57
View File
@@ -23,73 +23,73 @@ namespace SignalProcessing {
that converts from an input clock of 1200 to an output clock of 2 will first fire on cycle 600.
*/
class Stepper {
public:
/*!
Establishes a stepper with a one-to-one conversion rate.
*/
Stepper() : Stepper(1,1) {}
public:
/*!
Establishes a stepper with a one-to-one conversion rate.
*/
Stepper() : Stepper(1,1) {}
/*!
Establishes a stepper that will receive steps at the @c input_rate and dictate the number
of steps that should be taken at the @c output_rate.
*/
Stepper(uint64_t output_rate, uint64_t input_rate) :
accumulated_error_(-(int64_t(input_rate) << 1)),
input_rate_(input_rate),
output_rate_(output_rate),
whole_step_(output_rate / input_rate),
adjustment_up_(int64_t(output_rate % input_rate) << 1),
adjustment_down_(int64_t(input_rate) << 1) {}
/*!
Establishes a stepper that will receive steps at the @c input_rate and dictate the number
of steps that should be taken at the @c output_rate.
*/
Stepper(uint64_t output_rate, uint64_t input_rate) :
accumulated_error_(-(int64_t(input_rate) << 1)),
input_rate_(input_rate),
output_rate_(output_rate),
whole_step_(output_rate / input_rate),
adjustment_up_(int64_t(output_rate % input_rate) << 1),
adjustment_down_(int64_t(input_rate) << 1) {}
/*!
Advances one step at the input rate.
/*!
Advances one step at the input rate.
@returns the number of output steps.
*/
inline uint64_t step() {
uint64_t update = whole_step_;
accumulated_error_ += adjustment_up_;
if(accumulated_error_ > 0) {
update++;
accumulated_error_ -= adjustment_down_;
}
return update;
@returns the number of output steps.
*/
inline uint64_t step() {
uint64_t update = whole_step_;
accumulated_error_ += adjustment_up_;
if(accumulated_error_ > 0) {
update++;
accumulated_error_ -= adjustment_down_;
}
return update;
}
/*!
Advances by @c number_of_steps steps at the input rate.
/*!
Advances by @c number_of_steps steps at the input rate.
@returns the number of output steps.
*/
inline uint64_t step(uint64_t number_of_steps) {
uint64_t update = whole_step_ * number_of_steps;
accumulated_error_ += adjustment_up_ * int64_t(number_of_steps);
if(accumulated_error_ > 0) {
update += 1 + uint64_t(accumulated_error_ / adjustment_down_);
accumulated_error_ = (accumulated_error_ % adjustment_down_) - adjustment_down_;
}
return update;
@returns the number of output steps.
*/
inline uint64_t step(uint64_t number_of_steps) {
uint64_t update = whole_step_ * number_of_steps;
accumulated_error_ += adjustment_up_ * int64_t(number_of_steps);
if(accumulated_error_ > 0) {
update += 1 + uint64_t(accumulated_error_ / adjustment_down_);
accumulated_error_ = (accumulated_error_ % adjustment_down_) - adjustment_down_;
}
return update;
}
/*!
@returns the output rate.
*/
inline uint64_t get_output_rate() {
return output_rate_;
}
/*!
@returns the output rate.
*/
inline uint64_t get_output_rate() const {
return output_rate_;
}
/*!
@returns the input rate.
*/
inline uint64_t get_input_rate() {
return input_rate_;
}
/*!
@returns the input rate.
*/
inline uint64_t get_input_rate() const {
return input_rate_;
}
private:
int64_t accumulated_error_;
uint64_t input_rate_, output_rate_;
uint64_t whole_step_;
int64_t adjustment_up_, adjustment_down_;
private:
int64_t accumulated_error_;
uint64_t input_rate_, output_rate_;
uint64_t whole_step_;
int64_t adjustment_up_, adjustment_down_;
};
}