annotate dsp/signalconditioning/DFProcess.cpp @ 272:330c2e11f8a9

* Build fixes for gcc 4.3.2 * _Maybe_, but probably not, fix crash in tempo tracker... let's see how we get on
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 10 Nov 2008 14:01:55 +0000
parents a98dd8ec96f8
children d72fcd34d9a7
rev   line source
c@225 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@225 2
c@225 3 /*
c@225 4 QM DSP Library
c@225 5
c@225 6 Centre for Digital Music, Queen Mary, University of London.
c@225 7 This file copyright 2005-2006 Christian Landone.
c@225 8 All rights reserved.
c@225 9 */
c@225 10
c@225 11 #include "DFProcess.h"
c@241 12 #include "maths/MathUtilities.h"
c@225 13
c@272 14 #include <cstring>
c@272 15
c@225 16 //////////////////////////////////////////////////////////////////////
c@225 17 // Construction/Destruction
c@225 18 //////////////////////////////////////////////////////////////////////
c@225 19
c@225 20 DFProcess::DFProcess( DFProcConfig Config )
c@225 21 {
c@225 22 filtSrc = NULL;
c@225 23 filtDst = NULL;
c@225 24 m_filtScratchIn = NULL;
c@225 25 m_filtScratchOut = NULL;
c@225 26
c@225 27 m_FFOrd = 0;
c@225 28
c@225 29 initialise( Config );
c@225 30 }
c@225 31
c@225 32 DFProcess::~DFProcess()
c@225 33 {
c@225 34 deInitialise();
c@225 35 }
c@225 36
c@225 37 void DFProcess::initialise( DFProcConfig Config )
c@225 38 {
c@225 39 m_length = Config.length;
c@225 40 m_winPre = Config.winPre;
c@225 41 m_winPost = Config.winPost;
c@225 42 m_alphaNormParam = Config.AlphaNormParam;
c@225 43
c@225 44 m_isMedianPositive = Config.isMedianPositive;
c@225 45
c@225 46 filtSrc = new double[ m_length ];
c@225 47 filtDst = new double[ m_length ];
c@225 48
c@225 49
c@225 50 //Low Pass Smoothing Filter Config
c@225 51 m_FilterConfigParams.ord = Config.LPOrd;
c@225 52 m_FilterConfigParams.ACoeffs = Config.LPACoeffs;
c@225 53 m_FilterConfigParams.BCoeffs = Config.LPBCoeffs;
c@225 54
c@225 55 m_FiltFilt = new FiltFilt( m_FilterConfigParams );
c@225 56 }
c@225 57
c@225 58 void DFProcess::deInitialise()
c@225 59 {
c@225 60 delete [] filtSrc;
c@225 61
c@225 62 delete [] filtDst;
c@225 63
c@225 64 delete [] m_filtScratchIn;
c@225 65
c@225 66 delete [] m_filtScratchOut;
c@225 67
c@225 68 delete m_FiltFilt;
c@225 69 }
c@225 70
c@225 71 void DFProcess::process(double *src, double* dst)
c@225 72 {
c@225 73 removeDCNormalize( src, filtSrc );
c@225 74
c@225 75 m_FiltFilt->process( filtSrc, filtDst, m_length );
c@225 76
c@225 77 medianFilter( filtDst, dst );
c@225 78 }
c@225 79
c@225 80
c@225 81 void DFProcess::medianFilter(double *src, double *dst)
c@225 82 {
c@225 83 unsigned int i,k,j,l;
c@225 84 unsigned int index = 0;
c@225 85
c@225 86 double val = 0;
c@225 87
c@225 88 double* y = new double[ m_winPost + m_winPre + 1];
c@225 89 memset( y, 0, sizeof( double ) * ( m_winPost + m_winPre + 1) );
c@225 90
c@225 91 double* scratch = new double[ m_length ];
c@225 92
c@225 93 for( i = 0; i < m_winPre; i++)
c@225 94 {
c@225 95 k = i + m_winPost + 1;
c@225 96
c@225 97 for( j = 0; j < k; j++)
c@225 98 {
c@225 99 y[ j ] = src[ j ];
c@225 100 }
c@225 101 scratch[ index ] = MathUtilities::median( y, k );
c@225 102 index++;
c@225 103 }
c@225 104
c@225 105 for( i = 0; i < ( m_length - ( m_winPost + m_winPre ) ); i ++)
c@225 106 {
c@225 107
c@225 108 l = 0;
c@225 109 for( j = i; j < ( i + m_winPost + m_winPre + 1); j++)
c@225 110 {
c@225 111 y[ l ] = src[ j ];
c@225 112 l++;
c@225 113 }
c@225 114
c@225 115 scratch[ index++ ] = MathUtilities::median( y, (m_winPost + m_winPre + 1 ));
c@225 116 }
c@225 117
c@225 118 for( i = std::max( m_length - m_winPost, (unsigned)1); i < m_length; i++)
c@225 119 {
c@225 120 k = std::max( i - m_winPre, (unsigned)1);
c@225 121
c@225 122 l = 0;
c@225 123 for( j = k; j < m_length; j++)
c@225 124 {
c@225 125 y[ l ] = src[ j ];
c@225 126
c@225 127 l++;
c@225 128 }
c@225 129
c@225 130 scratch[ index++ ] = MathUtilities::median( y, l);
c@225 131 }
c@225 132
c@225 133
c@225 134 for( i = 0; i < m_length; i++ )
c@225 135 {
c@225 136 val = src[ i ] - scratch[ i ];// - 0.033;
c@225 137
c@225 138 if( m_isMedianPositive )
c@225 139 {
c@225 140 if( val > 0 )
c@225 141 {
c@225 142 dst[ i ] = val;
c@225 143 }
c@225 144 else
c@225 145 {
c@225 146 dst[ i ] = 0;
c@225 147 }
c@225 148 }
c@225 149 else
c@225 150 {
c@225 151 dst[ i ] = val;
c@225 152 }
c@225 153 }
c@225 154
c@225 155 delete [] y;
c@225 156 delete [] scratch;
c@225 157 }
c@225 158
c@225 159
c@225 160 void DFProcess::removeDCNormalize( double *src, double*dst )
c@225 161 {
c@225 162 double DFmax = 0;
c@225 163 double DFMin = 0;
c@225 164 double DFAlphaNorm = 0;
c@225 165
c@225 166 MathUtilities::getFrameMinMax( src, m_length, &DFMin, &DFmax );
c@225 167
c@225 168 MathUtilities::getAlphaNorm( src, m_length, m_alphaNormParam, &DFAlphaNorm );
c@225 169
c@225 170 for( unsigned int i = 0; i< m_length; i++)
c@225 171 {
c@225 172 dst[ i ] = ( src[ i ] - DFMin ) / DFAlphaNorm;
c@225 173 }
c@225 174 }