annotate MeanFilter.h @ 164:a7d9c6142f8f tip

Added tag v1.2 for changeset 4a97f7638ffd
author Chris Cannam
date Thu, 06 Feb 2020 15:02:47 +0000
parents 5945b8905d1f
children
rev   line source
matthiasm@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@9 2
matthiasm@0 3 /*
Chris@9 4 pYIN - A fundamental frequency estimator for monophonic audio
Chris@9 5 Centre for Digital Music, Queen Mary, University of London.
Chris@9 6
Chris@9 7 This program is free software; you can redistribute it and/or
Chris@9 8 modify it under the terms of the GNU General Public License as
Chris@9 9 published by the Free Software Foundation; either version 2 of the
Chris@9 10 License, or (at your option) any later version. See the file
Chris@9 11 COPYING included with this distribution for more information.
matthiasm@0 12 */
matthiasm@0 13
matthiasm@0 14 #ifndef _MEAN_FILTER_H_
matthiasm@0 15 #define _MEAN_FILTER_H_
matthiasm@0 16
matthiasm@0 17 class MeanFilter
matthiasm@0 18 {
matthiasm@0 19 public:
matthiasm@0 20 /**
matthiasm@0 21 * Construct a non-causal mean filter with filter length flen,
matthiasm@0 22 * that replaces each sample N with the mean of samples
matthiasm@0 23 * [N-floor(F/2) .. N+floor(F/2)] where F is the filter length.
matthiasm@0 24 * Only odd F are supported.
matthiasm@0 25 */
matthiasm@0 26 MeanFilter(int flen) : m_flen(flen) { }
matthiasm@0 27 ~MeanFilter() { }
matthiasm@0 28
matthiasm@0 29 /**
matthiasm@0 30 * Filter the n samples in "in" and place the results in "out"
matthiasm@0 31 */
matthiasm@0 32 void filter(const double *in, double *out, const int n) {
matthiasm@0 33 filterSubsequence(in, out, n, n, 0);
matthiasm@0 34 }
matthiasm@0 35
matthiasm@0 36 /**
matthiasm@0 37 * Filter the n samples starting at the given offset in the
matthiasm@0 38 * m-element array "in" and place the results in the n-element
matthiasm@0 39 * array "out"
matthiasm@0 40 */
matthiasm@0 41 void filterSubsequence(const double *in, double *out,
matthiasm@0 42 const int m, const int n,
matthiasm@0 43 const int offset) {
matthiasm@0 44 int half = m_flen/2;
matthiasm@0 45 for (int i = 0; i < n; ++i) {
matthiasm@0 46 double v = 0;
matthiasm@0 47 int n = 0;
matthiasm@0 48 for (int j = -half; j <= half; ++j) {
matthiasm@0 49 int ix = i + j + offset;
matthiasm@0 50 if (ix >= 0 && ix < m) {
matthiasm@0 51 v += in[ix];
matthiasm@0 52 ++n;
matthiasm@0 53 }
matthiasm@0 54 }
matthiasm@0 55 out[i] = v / n;
matthiasm@0 56 }
matthiasm@0 57 }
matthiasm@0 58
matthiasm@0 59 private:
matthiasm@0 60 int m_flen;
matthiasm@0 61 };
matthiasm@0 62
matthiasm@0 63 #endif