Skip to content

Commit

Permalink
sns: special cases for median-average filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Feb 8, 2024
1 parent 9792140 commit a33c71b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
19 changes: 12 additions & 7 deletions code/espurna/filters/MedianFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MedianFilter : public BaseFilter {
}

double value() const override {
double sum { 0.0 };
double out { 0.0 };

if (_values.size() > 2) {
auto median = [](double previous, double current, double next) {
Expand All @@ -44,16 +44,21 @@ class MedianFilter : public BaseFilter {
return current;
};

for (auto prev = _values.begin(); prev != (_values.end() - 2); ++prev) {
sum += median(*prev, *std::next(prev, 1), *std::next(prev, 2));
double counter = 0.0;
for (auto it = _values.begin(); it != (_values.end() - 2); ++it) {
out += median(*it, *std::next(it, 1), *std::next(it, 2));
counter += 1.0;
}

sum /= (_values.size() - 2);
} else if (_values.size() > 0) {
sum = _values.front();
out /= counter;
} else if (_values.size() == 2) {
out = _values[0] + _values[1];
out /= 2.0;
} else if (_values.size() == 1) {
out = _values.front();
}

return sum;
return out;
}

size_t capacity() const override {
Expand Down
30 changes: 26 additions & 4 deletions code/test/unit/src/filters/filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,39 @@ void test_median() {
auto filter = MedianFilter();
TEST_ASSERT_EQUAL(0, filter.capacity());

const double samples[] {1., 2., 2., 3., 4., 7., 9.};
filter.resize(std::size(samples));
const double one[] {4., 3., 5., 6., 2., 2., 3., 4., 7., 9.};
filter.resize(std::size(one));

TEST_ASSERT_EQUAL(std::size(samples), filter.capacity());
TEST_ASSERT_EQUAL_DOUBLE(0.0, filter.value());

for (const auto& sample : samples) {
for (const auto& sample : one) {
filter.update(sample);
}

TEST_ASSERT_EQUAL_DOUBLE(4.0, filter.value());

const double two[] {6., 6.1, 6.2, 6.3, 6.4, 6.5, 2.5, 4.5, 2.6, 2.5, 2.4};
filter.resize(std::size(two));

TEST_ASSERT_EQUAL_DOUBLE(9, filter.value());

for (const auto& sample : two) {
filter.update(sample);
}

TEST_ASSERT_EQUAL_DOUBLE(4.97, filter.value());

const double three[] {2.4, 2.4};
filter.resize(std::size(three));

TEST_ASSERT_EQUAL_DOUBLE(2.4, filter.value());

for (const auto& sample : three) {
filter.update(sample);
}

TEST_ASSERT_EQUAL_DOUBLE(3.6, filter.value());
TEST_ASSERT_EQUAL_DOUBLE(2.4, filter.value());
}

void test_moving_average() {
Expand Down

0 comments on commit a33c71b

Please sign in to comment.