Toward minimising MovingMedian a bit

This commit is contained in:
Chris Cannam
2022-06-06 21:53:54 +01:00
parent ef2d39b3af
commit 7519ef47cc

View File

@@ -41,15 +41,14 @@ class MovingMedian : public SampleFilter<T>
public: public:
MovingMedian(int size, float percentile = 50.f) : MovingMedian(int size, float percentile = 50.f) :
SampleFilter<T>(size), SampleFilter<T>(size),
m_frame(allocate_and_zero<T>(size)), m_frame(allocate_and_zero<T>(size * 2)),
m_sorted(allocate_and_zero<T>(size)), m_sorted(m_frame + size)
m_sortend(m_sorted + P::m_size - 1) { {
setPercentile(percentile); setPercentile(percentile);
} }
~MovingMedian() { ~MovingMedian() {
deallocate(m_frame); deallocate(m_frame);
deallocate(m_sorted);
} }
void setPercentile(float p) { void setPercentile(float p) {
@@ -63,10 +62,10 @@ public:
std::cerr << "WARNING: MovingMedian: NaN encountered" << std::endl; std::cerr << "WARNING: MovingMedian: NaN encountered" << std::endl;
value = T(); value = T();
} }
drop(m_frame[0]); T toDrop = m_frame[0];
v_move(m_frame, m_frame+1, P::m_size-1); v_move(m_frame, m_frame+1, P::m_size-1);
m_frame[P::m_size-1] = value; m_frame[P::m_size-1] = value;
put(value); dropAndPut(toDrop, value);
} }
T get() const { T get() const {
@@ -102,24 +101,39 @@ public:
private: private:
T *const m_frame; T *const m_frame;
T *const m_sorted; T *const m_sorted;
T *const m_sortend;
int m_index; int m_index;
void put(T value) { void dropAndPut(const T &toDrop, const T &toPut) {
// precondition: m_sorted contains m_size-1 values, packed at start // precondition: m_sorted contains m_size values, one of which is toDrop
// postcondition: m_sorted contains m_size values, one of which is value // postcondition: m_sorted contains m_size values, one of which is toPut
T *index = std::lower_bound(m_sorted, m_sortend, value); // (and one instance of toDrop has been removed)
v_move(index + 1, index, m_sortend - index); int n = P::m_size;
*index = value; int dropIx = std::lower_bound(m_sorted, m_sorted + n, toDrop) - m_sorted;
// if (m_sorted[dropIx] != toDrop) {
// throw std::runtime_error("not found");
// }
int putIx;
if (toPut > toDrop) {
putIx = std::lower_bound(m_sorted + dropIx, m_sorted + n, toPut) - m_sorted;
} else if (toPut < toDrop) {
putIx = std::lower_bound(m_sorted, m_sorted + dropIx, toPut) - m_sorted;
} else {
m_sorted[dropIx] = toPut;
return;
}
if (putIx > dropIx) {
for (int i = dropIx; i+1 < putIx; ++i) {
m_sorted[i] = m_sorted[i+1];
}
m_sorted[putIx-1] = toPut;
} else if (putIx < dropIx) {
for (int i = dropIx; i > putIx; --i) {
m_sorted[i] = m_sorted[i-1];
}
m_sorted[putIx] = toPut;
} else {
m_sorted[putIx] = toPut;
} }
void drop(T value) {
// precondition: m_sorted contains m_size values, one of which is value
// postcondition: m_sorted contains m_size-1 values, packed at start
T *index = std::lower_bound(m_sorted, m_sortend + 1, value);
assert(*index == value);
v_move(index, index + 1, m_sortend - index);
*m_sortend = T(0);
} }
MovingMedian(const MovingMedian &) =delete; MovingMedian(const MovingMedian &) =delete;