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