You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
stmdspgui/templates/5_fir_differentiator.cpp

31 lines
930 B
C++

3 years ago
/**
* 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.
*/
Sample *process_data(Samples samples)
3 years ago
{
constexpr int scaling_factor = 4;
static Sample output[samples.size()];
static Sample prev = 2048;
3 years ago
// Compute the first output value using the saved sample.
output[0] = 2048 + ((samples[0] - prev) * scaling_factor);
for (unsigned int i = 1; i < samples.size(); i++) {
3 years ago
// 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[samples.size() - 1];
3 years ago
return output;
}