-
-
Save JC3/9f4fcb38b24a7cb5cfc71d44a09a3138 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ThresholdingDemo::threshold (const Params ¶ms, | |
Output &output, | |
const QVector<float> &input) | |
{ | |
// adapted from https://stackoverflow.com/a/46956908/616460 | |
static const auto mean = [](const QVector<float> &vec, int from, int to) { | |
float sum = 0; | |
for (int k = from; k <= to; ++ k) sum += vec[k]; | |
return sum / (to - from + 1); | |
}; | |
static const auto stdDev = [](const QVector<float> &vec, float mean, int from, int to) { | |
float sum = 0; | |
for (int k = from; k <= to; ++ k) sum += std::pow(vec[k] - mean, 2); | |
return std::sqrt(sum / (to - from + 1)); | |
}; | |
int lag = params.lag; | |
float threshold = params.threshold; | |
float influence = params.influence; | |
output.clear(); | |
if (input.size() <= lag + 2) | |
return; | |
//Initialise variables | |
QVector<int> signls(input.size(), 0.0); | |
QVector<float> avgFilter(input.size(), 0.0); | |
QVector<float> stdFilter(input.size(), 0.0); | |
QVector<float> filteredY = input; | |
avgFilter[lag] = mean(filteredY, 0, lag); | |
stdFilter[lag] = stdDev(filteredY, avgFilter[lag], 0, lag); | |
for (auto i = lag + 1; i < input.size(); i++) | |
{ | |
if (std::abs(input[i] - avgFilter[i-1]) > threshold * stdFilter[i-1]) { | |
signls[i] = (input[i] > avgFilter[i-1]) ? 1 : -1; | |
filteredY[i] = influence* input[i] + (1 - influence) * filteredY[i-1]; | |
} else { | |
signls[i] = 0; //# No signal | |
filteredY[i] = input[i]; | |
} | |
avgFilter[i] = mean(filteredY, i-lag, i); | |
stdFilter[i] = stdDev(filteredY, avgFilter[i], i-lag, i); | |
} | |
output.input = input; | |
output.params = params; | |
output.mean = avgFilter; | |
output.stddev = stdFilter; | |
output.signal = signls; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Params { | |
int lag; | |
float threshold; | |
float influence; | |
Params () : lag(30), threshold(5), influence(0) { } | |
}; | |
struct Output { | |
QVector<float> input; | |
Params params; | |
QVector<float> mean; | |
QVector<float> stddev; | |
QVector<int> signal; | |
void clear () { input.clear(); mean.clear(); stddev.clear(); signal.clear(); } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment