annotate dsp/signalconditioning/DFProcess.cpp @ 299:5f2c9119a94a

* A few fixes prompted by vamp-plugin-tester
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 08 Jun 2009 12:30:20 +0000
parents 5e125f030287
children a5eae96a357c
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@283 73 if (m_length == 0) return;
c@283 74
c@225 75 removeDCNormalize( src, filtSrc );
c@225 76
c@225 77 m_FiltFilt->process( filtSrc, filtDst, m_length );
c@225 78
c@225 79 medianFilter( filtDst, dst );
c@225 80 }
c@225 81
c@225 82
c@225 83 void DFProcess::medianFilter(double *src, double *dst)
c@225 84 {
c@299 85 int i,k,j,l;
c@299 86 int index = 0;
c@225 87
c@225 88 double val = 0;
c@225 89
c@225 90 double* y = new double[ m_winPost + m_winPre + 1];
c@225 91 memset( y, 0, sizeof( double ) * ( m_winPost + m_winPre + 1) );
c@225 92
c@225 93 double* scratch = new double[ m_length ];
c@225 94
c@299 95 for( i = 0; i < m_winPre && index; i++)
c@225 96 {
c@299 97 if (index >= m_length) break;
c@299 98
c@225 99 k = i + m_winPost + 1;
c@225 100
c@225 101 for( j = 0; j < k; j++)
c@225 102 {
c@225 103 y[ j ] = src[ j ];
c@225 104 }
c@225 105 scratch[ index ] = MathUtilities::median( y, k );
c@225 106 index++;
c@225 107 }
c@225 108
c@299 109 for( i = 0; i + m_winPost + m_winPre < m_length; i ++)
c@225 110 {
c@299 111 if (index >= m_length) break;
c@299 112
c@225 113
c@225 114 l = 0;
c@225 115 for( j = i; j < ( i + m_winPost + m_winPre + 1); j++)
c@225 116 {
c@225 117 y[ l ] = src[ j ];
c@225 118 l++;
c@225 119 }
c@225 120
c@225 121 scratch[ index++ ] = MathUtilities::median( y, (m_winPost + m_winPre + 1 ));
c@225 122 }
c@225 123
c@299 124 for( i = std::max( m_length - m_winPost, 1); i < m_length; i++)
c@225 125 {
c@299 126 if (index >= m_length) break;
c@299 127
c@299 128 k = std::max( i - m_winPre, 1);
c@225 129
c@225 130 l = 0;
c@225 131 for( j = k; j < m_length; j++)
c@225 132 {
c@225 133 y[ l ] = src[ j ];
c@225 134
c@225 135 l++;
c@225 136 }
c@225 137
c@225 138 scratch[ index++ ] = MathUtilities::median( y, l);
c@225 139 }
c@225 140
c@225 141
c@225 142 for( i = 0; i < m_length; i++ )
c@225 143 {
c@225 144 val = src[ i ] - scratch[ i ];// - 0.033;
c@225 145
c@225 146 if( m_isMedianPositive )
c@225 147 {
c@225 148 if( val > 0 )
c@225 149 {
c@225 150 dst[ i ] = val;
c@225 151 }
c@225 152 else
c@225 153 {
c@225 154 dst[ i ] = 0;
c@225 155 }
c@225 156 }
c@225 157 else
c@225 158 {
c@225 159 dst[ i ] = val;
c@225 160 }
c@225 161 }
c@225 162
c@225 163 delete [] y;
c@225 164 delete [] scratch;
c@225 165 }
c@225 166
c@225 167
c@225 168 void DFProcess::removeDCNormalize( double *src, double*dst )
c@225 169 {
c@225 170 double DFmax = 0;
c@225 171 double DFMin = 0;
c@225 172 double DFAlphaNorm = 0;
c@225 173
c@225 174 MathUtilities::getFrameMinMax( src, m_length, &DFMin, &DFmax );
c@225 175
c@225 176 MathUtilities::getAlphaNorm( src, m_length, m_alphaNormParam, &DFAlphaNorm );
c@225 177
c@225 178 for( unsigned int i = 0; i< m_length; i++)
c@225 179 {
c@225 180 dst[ i ] = ( src[ i ] - DFMin ) / DFAlphaNorm;
c@225 181 }
c@225 182 }