annotate MeanFilter.h @ 48:1343ecde267e

Add FFT test
author Chris Cannam
date Tue, 11 Sep 2012 17:30:07 +0100
parents f72a470fe4b5
children 939cf0e86268
rev   line source
Chris@47 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@47 2 /*
Chris@47 3 This file is Copyright (c) 2012 Chris Cannam
Chris@47 4
Chris@47 5 Permission is hereby granted, free of charge, to any person
Chris@47 6 obtaining a copy of this software and associated documentation
Chris@47 7 files (the "Software"), to deal in the Software without
Chris@47 8 restriction, including without limitation the rights to use, copy,
Chris@47 9 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@47 10 of the Software, and to permit persons to whom the Software is
Chris@47 11 furnished to do so, subject to the following conditions:
Chris@47 12
Chris@47 13 The above copyright notice and this permission notice shall be
Chris@47 14 included in all copies or substantial portions of the Software.
Chris@47 15
Chris@47 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@47 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@47 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@47 19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
Chris@47 20 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@47 21 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@47 22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@47 23 */
Chris@47 24
Chris@47 25 #ifndef _MEAN_FILTER_H_
Chris@47 26 #define _MEAN_FILTER_H_
Chris@47 27
Chris@47 28 class MeanFilter
Chris@47 29 {
Chris@47 30 public:
Chris@47 31 /**
Chris@47 32 * Construct a non-causal mean filter with filter length flen,
Chris@47 33 * that replaces each sample N with the mean of samples
Chris@47 34 * [N-floor(F/2) .. N+floor(F/2)] where F is the filter length.
Chris@47 35 * Only odd F are supported.
Chris@47 36 */
Chris@47 37 MeanFilter(int flen) : m_flen(flen) { }
Chris@47 38 ~MeanFilter() { }
Chris@47 39
Chris@47 40 /**
Chris@47 41 * Filter the n samples in "in" and place the results in "out"
Chris@47 42 */
Chris@47 43 void filter(const double *in, double *out, const int n) {
Chris@47 44 filterSubsequence(in, out, n, n, 0);
Chris@47 45 }
Chris@47 46
Chris@47 47 /**
Chris@47 48 * Filter the n samples starting at the given offset in the
Chris@48 49 * m-element array "in" and place the results in the n-element
Chris@48 50 * array "out"
Chris@47 51 */
Chris@47 52 void filterSubsequence(const double *in, double *out,
Chris@47 53 const int m, const int n,
Chris@47 54 const int offset) {
Chris@47 55 int half = m_flen/2;
Chris@47 56 for (int i = 0; i < n; ++i) {
Chris@47 57 double v = 0;
Chris@47 58 int n = 0;
Chris@47 59 for (int j = -half; j <= half; ++j) {
Chris@47 60 int ix = i + j + offset;
Chris@47 61 if (ix >= 0 && ix < m) {
Chris@47 62 v += in[ix];
Chris@47 63 ++n;
Chris@47 64 }
Chris@47 65 }
Chris@47 66 out[i] = v / n;
Chris@47 67 }
Chris@47 68 }
Chris@47 69
Chris@47 70 private:
Chris@47 71 int m_flen;
Chris@47 72 };
Chris@47 73
Chris@47 74 #endif