annotate dsp/signalconditioning/DFProcess.cpp @ 417:fa851e147e3f

Faster filter implementation with explicit FIR support
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 07 Oct 2015 10:36:09 +0100
parents 7e8d1f26b098
children ccd2019190bf
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@309 7 This file 2005-2006 Christian Landone.
c@309 8
mathieu@321 9 Modifications:
mathieu@321 10
mathieu@321 11 - delta threshold
mathieu@321 12 Description: add delta threshold used as offset in the smoothed
mathieu@321 13 detection function
mathieu@321 14 Author: Mathieu Barthet
mathieu@321 15 Date: June 2010
mathieu@321 16
c@309 17 This program is free software; you can redistribute it and/or
c@309 18 modify it under the terms of the GNU General Public License as
c@309 19 published by the Free Software Foundation; either version 2 of the
c@309 20 License, or (at your option) any later version. See the file
c@309 21 COPYING included with this distribution for more information.
c@225 22 */
c@225 23
c@225 24 #include "DFProcess.h"
c@241 25 #include "maths/MathUtilities.h"
c@225 26
c@272 27 #include <cstring>
c@272 28
c@225 29 //////////////////////////////////////////////////////////////////////
c@225 30 // Construction/Destruction
c@225 31 //////////////////////////////////////////////////////////////////////
c@225 32
c@225 33 DFProcess::DFProcess( DFProcConfig Config )
c@225 34 {
c@225 35 filtSrc = NULL;
c@225 36 filtDst = NULL;
c@225 37 m_filtScratchIn = NULL;
c@225 38 m_filtScratchOut = NULL;
c@225 39
c@225 40 m_FFOrd = 0;
c@225 41
c@225 42 initialise( Config );
c@225 43 }
c@225 44
c@225 45 DFProcess::~DFProcess()
c@225 46 {
c@225 47 deInitialise();
c@225 48 }
c@225 49
c@225 50 void DFProcess::initialise( DFProcConfig Config )
c@225 51 {
c@225 52 m_length = Config.length;
c@225 53 m_winPre = Config.winPre;
c@225 54 m_winPost = Config.winPost;
c@225 55 m_alphaNormParam = Config.AlphaNormParam;
c@225 56
c@225 57 m_isMedianPositive = Config.isMedianPositive;
c@225 58
c@225 59 filtSrc = new double[ m_length ];
c@225 60 filtDst = new double[ m_length ];
c@225 61
c@417 62 Filter::Parameters params;
c@417 63 params.a = std::vector<double>(Config.LPACoeffs, Config.LPACoeffs + Config.LPOrd + 1);
c@417 64 params.b = std::vector<double>(Config.LPBCoeffs, Config.LPBCoeffs + Config.LPOrd + 1);
c@417 65
c@417 66 m_FiltFilt = new FiltFilt(params);
mathieu@321 67
mathieu@321 68 //add delta threshold
c@410 69 m_delta = Config.delta;
c@225 70 }
c@225 71
c@225 72 void DFProcess::deInitialise()
c@225 73 {
c@225 74 delete [] filtSrc;
c@225 75
c@225 76 delete [] filtDst;
c@225 77
c@225 78 delete [] m_filtScratchIn;
c@225 79
c@225 80 delete [] m_filtScratchOut;
c@225 81
c@225 82 delete m_FiltFilt;
c@225 83 }
c@225 84
c@225 85 void DFProcess::process(double *src, double* dst)
c@225 86 {
c@283 87 if (m_length == 0) return;
c@283 88
c@225 89 removeDCNormalize( src, filtSrc );
c@225 90
c@225 91 m_FiltFilt->process( filtSrc, filtDst, m_length );
c@225 92
c@225 93 medianFilter( filtDst, dst );
c@225 94 }
c@225 95
c@225 96
c@225 97 void DFProcess::medianFilter(double *src, double *dst)
c@225 98 {
c@299 99 int i,k,j,l;
c@299 100 int index = 0;
c@225 101
c@225 102 double val = 0;
c@225 103
c@225 104 double* y = new double[ m_winPost + m_winPre + 1];
c@225 105 memset( y, 0, sizeof( double ) * ( m_winPost + m_winPre + 1) );
c@225 106
c@225 107 double* scratch = new double[ m_length ];
c@225 108
c@305 109 for( i = 0; i < m_winPre; i++)
c@225 110 {
c@299 111 if (index >= m_length) break;
c@299 112
c@225 113 k = i + m_winPost + 1;
c@225 114
c@225 115 for( j = 0; j < k; j++)
c@225 116 {
c@225 117 y[ j ] = src[ j ];
c@225 118 }
c@225 119 scratch[ index ] = MathUtilities::median( y, k );
c@225 120 index++;
c@225 121 }
c@225 122
c@299 123 for( i = 0; i + m_winPost + m_winPre < m_length; i ++)
c@225 124 {
c@299 125 if (index >= m_length) break;
c@299 126
c@225 127
c@225 128 l = 0;
c@225 129 for( j = i; j < ( i + m_winPost + m_winPre + 1); j++)
c@225 130 {
c@225 131 y[ l ] = src[ j ];
c@225 132 l++;
c@225 133 }
c@225 134
c@225 135 scratch[ index++ ] = MathUtilities::median( y, (m_winPost + m_winPre + 1 ));
c@225 136 }
c@225 137
c@299 138 for( i = std::max( m_length - m_winPost, 1); i < m_length; i++)
c@225 139 {
c@299 140 if (index >= m_length) break;
c@299 141
c@299 142 k = std::max( i - m_winPre, 1);
c@225 143
c@225 144 l = 0;
c@225 145 for( j = k; j < m_length; j++)
c@225 146 {
c@225 147 y[ l ] = src[ j ];
c@225 148
c@225 149 l++;
c@225 150 }
c@225 151
c@225 152 scratch[ index++ ] = MathUtilities::median( y, l);
c@225 153 }
c@225 154
c@225 155
c@225 156 for( i = 0; i < m_length; i++ )
c@225 157 {
mathieu@321 158 //add a delta threshold used as an offset when computing the smoothed detection function
mathieu@321 159 //(helps to discard noise when detecting peaks)
c@410 160 val = src[ i ] - scratch[ i ] - m_delta;
c@225 161
c@225 162 if( m_isMedianPositive )
c@225 163 {
c@225 164 if( val > 0 )
c@225 165 {
c@225 166 dst[ i ] = val;
c@225 167 }
c@225 168 else
c@225 169 {
c@225 170 dst[ i ] = 0;
c@225 171 }
c@225 172 }
c@225 173 else
c@225 174 {
c@225 175 dst[ i ] = val;
c@225 176 }
c@225 177 }
c@225 178
c@225 179 delete [] y;
c@225 180 delete [] scratch;
c@225 181 }
c@225 182
c@225 183
c@225 184 void DFProcess::removeDCNormalize( double *src, double*dst )
c@225 185 {
c@225 186 double DFmax = 0;
c@225 187 double DFMin = 0;
c@225 188 double DFAlphaNorm = 0;
c@225 189
c@225 190 MathUtilities::getFrameMinMax( src, m_length, &DFMin, &DFmax );
c@225 191
c@225 192 MathUtilities::getAlphaNorm( src, m_length, m_alphaNormParam, &DFAlphaNorm );
c@225 193
c@414 194 for (int i = 0; i < m_length; i++)
c@225 195 {
c@225 196 dst[ i ] = ( src[ i ] - DFMin ) / DFAlphaNorm;
c@225 197 }
c@225 198 }