Chris@47: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@47: /* Chris@47: This file is Copyright (c) 2012 Chris Cannam Chris@47: Chris@47: Permission is hereby granted, free of charge, to any person Chris@47: obtaining a copy of this software and associated documentation Chris@47: files (the "Software"), to deal in the Software without Chris@47: restriction, including without limitation the rights to use, copy, Chris@47: modify, merge, publish, distribute, sublicense, and/or sell copies Chris@47: of the Software, and to permit persons to whom the Software is Chris@47: furnished to do so, subject to the following conditions: Chris@47: Chris@47: The above copyright notice and this permission notice shall be Chris@47: included in all copies or substantial portions of the Software. Chris@47: Chris@47: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@47: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@47: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND Chris@47: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR Chris@47: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@47: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@47: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@47: */ Chris@47: Chris@73: #ifndef MEAN_FILTER_H Chris@73: #define MEAN_FILTER_H Chris@47: Chris@47: class MeanFilter Chris@47: { Chris@47: public: Chris@47: /** Chris@47: * Construct a non-causal mean filter with filter length flen, Chris@47: * that replaces each sample N with the mean of samples Chris@47: * [N-floor(F/2) .. N+floor(F/2)] where F is the filter length. Chris@47: * Only odd F are supported. Chris@47: */ Chris@47: MeanFilter(int flen) : m_flen(flen) { } Chris@47: ~MeanFilter() { } Chris@47: Chris@47: /** Chris@47: * Filter the n samples in "in" and place the results in "out" Chris@47: */ Chris@47: void filter(const double *in, double *out, const int n) { Chris@47: filterSubsequence(in, out, n, n, 0); Chris@47: } Chris@47: Chris@47: /** Chris@47: * Filter the n samples starting at the given offset in the Chris@48: * m-element array "in" and place the results in the n-element Chris@48: * array "out" Chris@47: */ Chris@47: void filterSubsequence(const double *in, double *out, Chris@47: const int m, const int n, Chris@47: const int offset) { Chris@47: int half = m_flen/2; Chris@47: for (int i = 0; i < n; ++i) { Chris@47: double v = 0; Chris@47: int n = 0; Chris@47: for (int j = -half; j <= half; ++j) { Chris@47: int ix = i + j + offset; Chris@47: if (ix >= 0 && ix < m) { Chris@73: double value = in[ix]; Chris@73: if (value == value) { // i.e. not NaN Chris@73: v += value; Chris@73: } Chris@73: ++n; Chris@47: } Chris@47: } Chris@73: if (n > 0) { Chris@73: out[i] = v / n; Chris@73: } else { Chris@73: out[i] = 0.0; Chris@73: } Chris@47: } Chris@47: } Chris@47: Chris@47: private: Chris@47: int m_flen; Chris@47: }; Chris@47: Chris@47: #endif