diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2020-11-18 12:43:21 -0500 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2020-11-18 12:43:21 -0500 |
commit | 6fda5c31da75bc6d0e38d5a8d25bce1205bc35d8 (patch) | |
tree | e5347b39cac989d12a64b2e621da0f2ec42e6c2c /gui/templates | |
parent | 0fde1b98eee06eda8333ae4099a6731a05a14482 (diff) |
gui: fix buffer size; add differentiator template
Diffstat (limited to 'gui/templates')
-rw-r--r-- | gui/templates/5_iir_differentiator.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/gui/templates/5_iir_differentiator.cpp b/gui/templates/5_iir_differentiator.cpp new file mode 100644 index 0000000..f63bba9 --- /dev/null +++ b/gui/templates/5_iir_differentiator.cpp @@ -0,0 +1,28 @@ +/** + * 5_iir_differentiator.cpp + * Written by Clyne Sullivan. + * + * Does an IIR differentiation on the incoming signal, so that the output is representative of the + * rate of change of the input. + * A scaling factor is applied so that the output's form is more clearly visible. + */ + +adcsample_t *process_data(adcsample_t *samples, unsigned int size) +{ + constexpr int scaling_factor = 4; + static adcsample_t prev = 2048; + + // Compute the first output value using the saved sample. + samples[0] = 2048 + ((samples[0] - prev) * scaling_factor; + // Save the last sample for the next iteration. + prev = samples[size - 1]; + + for (unsigned int i = 1; i < size; i++) { + // Take the rate of change and scale it. + // 2048 is added as the output should be centered in the voltage range. + samples[i] = 2048 + ((samples[i] - samples[i - 1]) * scaling_factor); + } + + return samples; +} + |