aboutsummaryrefslogtreecommitdiffstats
path: root/gui/templates/5_fir_differentiator.cpp
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2020-11-19 10:36:52 -0500
committerClyne Sullivan <clyne@bitgloo.com>2020-11-19 10:36:52 -0500
commitf559e9fbef98e16ebf4d72190322ca110f4940be (patch)
treee0db121c0dbd10b367a9bce11697792488f67e67 /gui/templates/5_fir_differentiator.cpp
parent6fda5c31da75bc6d0e38d5a8d25bce1205bc35d8 (diff)
differentiator is fir, also fixed it
Diffstat (limited to 'gui/templates/5_fir_differentiator.cpp')
-rw-r--r--gui/templates/5_fir_differentiator.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/gui/templates/5_fir_differentiator.cpp b/gui/templates/5_fir_differentiator.cpp
new file mode 100644
index 0000000..a3a7d4f
--- /dev/null
+++ b/gui/templates/5_fir_differentiator.cpp
@@ -0,0 +1,30 @@
+/**
+ * 5_fir_differentiator.cpp
+ * Written by Clyne Sullivan.
+ *
+ * Does an FIR 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 output[SIZE];
+ static adcsample_t prev = 2048;
+
+ // Compute the first output value using the saved sample.
+ output[0] = 2048 + ((samples[0] - prev) * scaling_factor);
+
+ 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.
+ output[i] = 2048 + ((samples[i] - samples[i - 1]) * scaling_factor);
+ }
+
+ // Save the last sample for the next iteration.
+ prev = samples[size - 1];
+
+ return output;
+}
+