c@225: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@225: c@225: /* c@225: QM DSP Library c@225: c@225: Centre for Digital Music, Queen Mary, University of London. c@309: This file 2005-2006 Christian Landone. c@309: c@309: This program is free software; you can redistribute it and/or c@309: modify it under the terms of the GNU General Public License as c@309: published by the Free Software Foundation; either version 2 of the c@309: License, or (at your option) any later version. See the file c@309: COPYING included with this distribution for more information. c@225: */ c@225: c@225: #include "DFProcess.h" c@241: #include "maths/MathUtilities.h" c@225: c@272: #include c@272: c@225: ////////////////////////////////////////////////////////////////////// c@225: // Construction/Destruction c@225: ////////////////////////////////////////////////////////////////////// c@225: c@225: DFProcess::DFProcess( DFProcConfig Config ) c@225: { c@225: filtSrc = NULL; c@225: filtDst = NULL; c@225: m_filtScratchIn = NULL; c@225: m_filtScratchOut = NULL; c@225: c@225: m_FFOrd = 0; c@225: c@225: initialise( Config ); c@225: } c@225: c@225: DFProcess::~DFProcess() c@225: { c@225: deInitialise(); c@225: } c@225: c@225: void DFProcess::initialise( DFProcConfig Config ) c@225: { c@225: m_length = Config.length; c@225: m_winPre = Config.winPre; c@225: m_winPost = Config.winPost; c@225: m_alphaNormParam = Config.AlphaNormParam; c@225: c@225: m_isMedianPositive = Config.isMedianPositive; c@225: c@225: filtSrc = new double[ m_length ]; c@225: filtDst = new double[ m_length ]; c@225: c@225: c@225: //Low Pass Smoothing Filter Config c@225: m_FilterConfigParams.ord = Config.LPOrd; c@225: m_FilterConfigParams.ACoeffs = Config.LPACoeffs; c@225: m_FilterConfigParams.BCoeffs = Config.LPBCoeffs; c@225: c@225: m_FiltFilt = new FiltFilt( m_FilterConfigParams ); c@225: } c@225: c@225: void DFProcess::deInitialise() c@225: { c@225: delete [] filtSrc; c@225: c@225: delete [] filtDst; c@225: c@225: delete [] m_filtScratchIn; c@225: c@225: delete [] m_filtScratchOut; c@225: c@225: delete m_FiltFilt; c@225: } c@225: c@225: void DFProcess::process(double *src, double* dst) c@225: { c@283: if (m_length == 0) return; c@283: c@225: removeDCNormalize( src, filtSrc ); c@225: c@225: m_FiltFilt->process( filtSrc, filtDst, m_length ); c@225: c@225: medianFilter( filtDst, dst ); c@225: } c@225: c@225: c@225: void DFProcess::medianFilter(double *src, double *dst) c@225: { c@299: int i,k,j,l; c@299: int index = 0; c@225: c@225: double val = 0; c@225: c@225: double* y = new double[ m_winPost + m_winPre + 1]; c@225: memset( y, 0, sizeof( double ) * ( m_winPost + m_winPre + 1) ); c@225: c@225: double* scratch = new double[ m_length ]; c@225: c@305: for( i = 0; i < m_winPre; i++) c@225: { c@299: if (index >= m_length) break; c@299: c@225: k = i + m_winPost + 1; c@225: c@225: for( j = 0; j < k; j++) c@225: { c@225: y[ j ] = src[ j ]; c@225: } c@225: scratch[ index ] = MathUtilities::median( y, k ); c@225: index++; c@225: } c@225: c@299: for( i = 0; i + m_winPost + m_winPre < m_length; i ++) c@225: { c@299: if (index >= m_length) break; c@299: c@225: c@225: l = 0; c@225: for( j = i; j < ( i + m_winPost + m_winPre + 1); j++) c@225: { c@225: y[ l ] = src[ j ]; c@225: l++; c@225: } c@225: c@225: scratch[ index++ ] = MathUtilities::median( y, (m_winPost + m_winPre + 1 )); c@225: } c@225: c@299: for( i = std::max( m_length - m_winPost, 1); i < m_length; i++) c@225: { c@299: if (index >= m_length) break; c@299: c@299: k = std::max( i - m_winPre, 1); c@225: c@225: l = 0; c@225: for( j = k; j < m_length; j++) c@225: { c@225: y[ l ] = src[ j ]; c@225: c@225: l++; c@225: } c@225: c@225: scratch[ index++ ] = MathUtilities::median( y, l); c@225: } c@225: c@225: c@225: for( i = 0; i < m_length; i++ ) c@225: { c@225: val = src[ i ] - scratch[ i ];// - 0.033; c@225: c@225: if( m_isMedianPositive ) c@225: { c@225: if( val > 0 ) c@225: { c@225: dst[ i ] = val; c@225: } c@225: else c@225: { c@225: dst[ i ] = 0; c@225: } c@225: } c@225: else c@225: { c@225: dst[ i ] = val; c@225: } c@225: } c@225: c@225: delete [] y; c@225: delete [] scratch; c@225: } c@225: c@225: c@225: void DFProcess::removeDCNormalize( double *src, double*dst ) c@225: { c@225: double DFmax = 0; c@225: double DFMin = 0; c@225: double DFAlphaNorm = 0; c@225: c@225: MathUtilities::getFrameMinMax( src, m_length, &DFMin, &DFmax ); c@225: c@225: MathUtilities::getAlphaNorm( src, m_length, m_alphaNormParam, &DFAlphaNorm ); c@225: c@225: for( unsigned int i = 0; i< m_length; i++) c@225: { c@225: dst[ i ] = ( src[ i ] - DFMin ) / DFAlphaNorm; c@225: } c@225: }