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@73
|
25 #ifndef MEAN_FILTER_H
|
Chris@73
|
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@73
|
62 double value = in[ix];
|
Chris@73
|
63 if (value == value) { // i.e. not NaN
|
Chris@73
|
64 v += value;
|
Chris@73
|
65 }
|
Chris@73
|
66 ++n;
|
Chris@47
|
67 }
|
Chris@47
|
68 }
|
Chris@73
|
69 if (n > 0) {
|
Chris@73
|
70 out[i] = v / n;
|
Chris@73
|
71 } else {
|
Chris@73
|
72 out[i] = 0.0;
|
Chris@73
|
73 }
|
Chris@47
|
74 }
|
Chris@47
|
75 }
|
Chris@47
|
76
|
Chris@47
|
77 private:
|
Chris@47
|
78 int m_flen;
|
Chris@47
|
79 };
|
Chris@47
|
80
|
Chris@47
|
81 #endif
|