1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6 Centre for Digital Music, Queen Mary, University of London.
7 This file 2005-2006 Christian Landone.
12 Description: add delta threshold used as offset in the smoothed
14 Author: Mathieu Barthet
17 This program is free software; you can redistribute it and/or
18 modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation; either version 2 of the
20 License, or (at your option) any later version. See the file
21 COPYING included with this distribution for more information.
24 #include "DFProcess.h"
25 #include "maths/MathUtilities.h"
29 //////////////////////////////////////////////////////////////////////
30 // Construction/Destruction
31 //////////////////////////////////////////////////////////////////////
33 DFProcess::DFProcess( DFProcConfig Config )
37 m_filtScratchIn = NULL;
38 m_filtScratchOut = NULL;
45 DFProcess::~DFProcess()
50 void DFProcess::initialise( DFProcConfig Config )
52 m_length = Config.length;
53 m_winPre = Config.winPre;
54 m_winPost = Config.winPost;
55 m_alphaNormParam = Config.AlphaNormParam;
57 m_isMedianPositive = Config.isMedianPositive;
59 filtSrc = new double[ m_length ];
60 filtDst = new double[ m_length ];
63 //Low Pass Smoothing Filter Config
64 m_FilterConfigParams.ord = Config.LPOrd;
65 m_FilterConfigParams.ACoeffs = Config.LPACoeffs;
66 m_FilterConfigParams.BCoeffs = Config.LPBCoeffs;
68 m_FiltFilt = new FiltFilt( m_FilterConfigParams );
71 m_delta = Config.delta;
74 void DFProcess::deInitialise()
80 delete [] m_filtScratchIn;
82 delete [] m_filtScratchOut;
87 void DFProcess::process(double *src, double* dst)
89 if (m_length == 0) return;
91 removeDCNormalize( src, filtSrc );
93 m_FiltFilt->process( filtSrc, filtDst, m_length );
95 medianFilter( filtDst, dst );
99 void DFProcess::medianFilter(double *src, double *dst)
106 double* y = new double[ m_winPost + m_winPre + 1];
107 memset( y, 0, sizeof( double ) * ( m_winPost + m_winPre + 1) );
109 double* scratch = new double[ m_length ];
111 for( i = 0; i < m_winPre; i++)
113 if (index >= m_length) break;
115 k = i + m_winPost + 1;
117 for( j = 0; j < k; j++)
121 scratch[ index ] = MathUtilities::median( y, k );
125 for( i = 0; i + m_winPost + m_winPre < m_length; i ++)
127 if (index >= m_length) break;
131 for( j = i; j < ( i + m_winPost + m_winPre + 1); j++)
137 scratch[ index++ ] = MathUtilities::median( y, (m_winPost + m_winPre + 1 ));
140 for( i = std::max( m_length - m_winPost, 1); i < m_length; i++)
142 if (index >= m_length) break;
144 k = std::max( i - m_winPre, 1);
147 for( j = k; j < m_length; j++)
154 scratch[ index++ ] = MathUtilities::median( y, l);
158 for( i = 0; i < m_length; i++ )
160 //add a delta threshold used as an offset when computing the smoothed detection function
161 //(helps to discard noise when detecting peaks)
162 val = src[ i ] - scratch[ i ] - m_delta;
164 if( m_isMedianPositive )
186 void DFProcess::removeDCNormalize( double *src, double*dst )
190 double DFAlphaNorm = 0;
192 MathUtilities::getFrameMinMax( src, m_length, &DFMin, &DFmax );
194 MathUtilities::getAlphaNorm( src, m_length, m_alphaNormParam, &DFAlphaNorm );
196 for( unsigned int i = 0; i< m_length; i++)
198 dst[ i ] = ( src[ i ] - DFMin ) / DFAlphaNorm;