matthiasm@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@9: matthiasm@0: /* Chris@9: pYIN - A fundamental frequency estimator for monophonic audio Chris@9: Centre for Digital Music, Queen Mary, University of London. Chris@9: Chris@9: This program is free software; you can redistribute it and/or Chris@9: modify it under the terms of the GNU General Public License as Chris@9: published by the Free Software Foundation; either version 2 of the Chris@9: License, or (at your option) any later version. See the file Chris@9: COPYING included with this distribution for more information. matthiasm@0: */ matthiasm@0: matthiasm@0: #ifndef _MEAN_FILTER_H_ matthiasm@0: #define _MEAN_FILTER_H_ matthiasm@0: matthiasm@0: class MeanFilter matthiasm@0: { matthiasm@0: public: matthiasm@0: /** matthiasm@0: * Construct a non-causal mean filter with filter length flen, matthiasm@0: * that replaces each sample N with the mean of samples matthiasm@0: * [N-floor(F/2) .. N+floor(F/2)] where F is the filter length. matthiasm@0: * Only odd F are supported. matthiasm@0: */ matthiasm@0: MeanFilter(int flen) : m_flen(flen) { } matthiasm@0: ~MeanFilter() { } matthiasm@0: matthiasm@0: /** matthiasm@0: * Filter the n samples in "in" and place the results in "out" matthiasm@0: */ matthiasm@0: void filter(const double *in, double *out, const int n) { matthiasm@0: filterSubsequence(in, out, n, n, 0); matthiasm@0: } matthiasm@0: matthiasm@0: /** matthiasm@0: * Filter the n samples starting at the given offset in the matthiasm@0: * m-element array "in" and place the results in the n-element matthiasm@0: * array "out" matthiasm@0: */ matthiasm@0: void filterSubsequence(const double *in, double *out, matthiasm@0: const int m, const int n, matthiasm@0: const int offset) { matthiasm@0: int half = m_flen/2; matthiasm@0: for (int i = 0; i < n; ++i) { matthiasm@0: double v = 0; matthiasm@0: int n = 0; matthiasm@0: for (int j = -half; j <= half; ++j) { matthiasm@0: int ix = i + j + offset; matthiasm@0: if (ix >= 0 && ix < m) { matthiasm@0: v += in[ix]; matthiasm@0: ++n; matthiasm@0: } matthiasm@0: } matthiasm@0: out[i] = v / n; matthiasm@0: } matthiasm@0: } matthiasm@0: matthiasm@0: private: matthiasm@0: int m_flen; matthiasm@0: }; matthiasm@0: matthiasm@0: #endif