annotate maths/MedianFilter.h @ 445:d55f5adbaf9b

Add valgrind
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 20 May 2019 16:35:50 +0100
parents f898c901c055
children fa407c1d9923
rev   line source
c@389 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@389 2
c@390 3 /*
c@390 4 QM DSP Library
c@390 5
c@390 6 Centre for Digital Music, Queen Mary, University of London.
c@390 7 This file Copyright 2010 Chris Cannam.
c@390 8
c@390 9 This program is free software; you can redistribute it and/or
c@390 10 modify it under the terms of the GNU General Public License as
c@390 11 published by the Free Software Foundation; either version 2 of the
c@390 12 License, or (at your option) any later version. See the file
c@390 13 COPYING included with this distribution for more information.
c@390 14 */
c@390 15
c@389 16 #ifndef MEDIAN_FILTER_H
c@389 17 #define MEDIAN_FILTER_H
c@389 18
c@389 19 #include <algorithm>
c@389 20 #include <cassert>
c@389 21 #include <cmath>
c@389 22 #include <iostream>
c@392 23 #include <vector>
c@389 24
c@389 25 template <typename T>
c@389 26 class MedianFilter
c@389 27 {
c@389 28 public:
c@389 29 MedianFilter(int size, float percentile = 50.f) :
c@389 30 m_size(size),
c@389 31 m_frame(new T[size]),
c@389 32 m_sorted(new T[size]),
c@389 33 m_sortend(m_sorted + size - 1) {
c@389 34 setPercentile(percentile);
c@389 35 reset();
c@389 36 }
c@389 37
c@389 38 ~MedianFilter() {
c@389 39 delete[] m_frame;
c@389 40 delete[] m_sorted;
c@389 41 }
c@389 42
c@389 43 void setPercentile(float p) {
c@389 44 m_index = int((m_size * p) / 100.f);
c@389 45 if (m_index >= m_size) m_index = m_size-1;
c@389 46 if (m_index < 0) m_index = 0;
c@389 47 }
c@389 48
c@389 49 void push(T value) {
c@394 50 if (value != value) {
c@407 51 std::cerr << "WARNING: MedianFilter::push: attempt to push NaN, pushing zero instead" << std::endl;
c@407 52 // we do need to push something, to maintain the filter length
c@407 53 value = T();
c@394 54 }
c@389 55 drop(m_frame[0]);
c@389 56 const int sz1 = m_size-1;
c@389 57 for (int i = 0; i < sz1; ++i) m_frame[i] = m_frame[i+1];
c@389 58 m_frame[m_size-1] = value;
c@389 59 put(value);
c@389 60 }
c@389 61
c@389 62 T get() const {
c@389 63 return m_sorted[m_index];
c@389 64 }
c@389 65
c@393 66 int getSize() const {
c@393 67 return m_size;
c@393 68 }
c@393 69
c@389 70 T getAt(float percentile) {
c@389 71 int ix = int((m_size * percentile) / 100.f);
c@389 72 if (ix >= m_size) ix = m_size-1;
c@389 73 if (ix < 0) ix = 0;
c@389 74 return m_sorted[ix];
c@389 75 }
c@389 76
c@389 77 void reset() {
c@389 78 for (int i = 0; i < m_size; ++i) m_frame[i] = 0;
c@389 79 for (int i = 0; i < m_size; ++i) m_sorted[i] = 0;
c@389 80 }
c@389 81
c@392 82 static std::vector<T> filter(int size, const std::vector<T> &in) {
c@392 83 std::vector<T> out;
c@392 84 MedianFilter<T> f(size);
c@392 85 for (int i = 0; i < int(in.size()); ++i) {
c@392 86 f.push(in[i]);
c@392 87 T median = f.get();
c@392 88 if (i >= size/2) out.push_back(median);
c@392 89 }
c@392 90 while (out.size() < in.size()) {
c@392 91 f.push(T());
c@392 92 out.push_back(f.get());
c@392 93 }
c@392 94 return out;
c@392 95 }
c@392 96
c@389 97 private:
c@389 98 const int m_size;
c@389 99 T *const m_frame;
c@389 100 T *const m_sorted;
c@389 101 T *const m_sortend;
c@389 102 int m_index;
c@389 103
c@389 104 void put(T value) {
c@389 105 // precondition: m_sorted contains m_size-1 values, packed at start
c@389 106 // postcondition: m_sorted contains m_size values, one of which is value
c@389 107 T *point = std::lower_bound(m_sorted, m_sortend, value);
c@389 108 const int n = m_sortend - point;
c@389 109 for (int i = n; i > 0; --i) point[i] = point[i-1];
c@389 110 *point = value;
c@389 111 }
c@389 112
c@389 113 void drop(T value) {
c@389 114 // precondition: m_sorted contains m_size values, one of which is value
c@389 115 // postcondition: m_sorted contains m_size-1 values, packed at start
c@389 116 T *point = std::lower_bound(m_sorted, m_sortend + 1, value);
c@389 117 if (*point != value) {
c@389 118 std::cerr << "WARNING: MedianFilter::drop: *point is " << *point
c@389 119 << ", expected " << value << std::endl;
c@389 120 }
c@389 121 const int n = m_sortend - point;
c@389 122 for (int i = 0; i < n; ++i) point[i] = point[i+1];
c@389 123 *m_sortend = T(0);
c@389 124 }
c@390 125
c@390 126 MedianFilter(const MedianFilter &); // not provided
c@390 127 MedianFilter &operator=(const MedianFilter &); // not provided
c@389 128 };
c@389 129
c@389 130 #endif
c@389 131