annotate dsp/signalconditioning/DFProcess.cpp @ 483:fdaa63607c15

Untabify, indent, tidy
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 31 May 2019 11:54:32 +0100
parents 7461bf03194e
children b1f72e469ec8
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@434 28 #include <algorithm>
c@434 29
c@272 30
c@225 31 //////////////////////////////////////////////////////////////////////
c@225 32 // Construction/Destruction
c@225 33 //////////////////////////////////////////////////////////////////////
c@225 34
c@225 35 DFProcess::DFProcess( DFProcConfig Config )
c@225 36 {
c@225 37 filtSrc = NULL;
cannam@483 38 filtDst = NULL;
c@225 39 m_filtScratchIn = NULL;
c@225 40 m_filtScratchOut = NULL;
c@225 41
c@225 42 m_FFOrd = 0;
c@225 43
c@225 44 initialise( Config );
c@225 45 }
c@225 46
c@225 47 DFProcess::~DFProcess()
c@225 48 {
c@225 49 deInitialise();
c@225 50 }
c@225 51
c@225 52 void DFProcess::initialise( DFProcConfig Config )
c@225 53 {
c@225 54 m_length = Config.length;
c@225 55 m_winPre = Config.winPre;
c@225 56 m_winPost = Config.winPost;
c@225 57 m_alphaNormParam = Config.AlphaNormParam;
c@225 58
c@225 59 m_isMedianPositive = Config.isMedianPositive;
c@225 60
c@225 61 filtSrc = new double[ m_length ];
c@225 62 filtDst = new double[ m_length ];
c@225 63
c@417 64 Filter::Parameters params;
cannam@483 65 params.a = std::vector<double>
cannam@483 66 (Config.LPACoeffs, Config.LPACoeffs + Config.LPOrd + 1);
cannam@483 67 params.b = std::vector<double>
cannam@483 68 (Config.LPBCoeffs, Config.LPBCoeffs + Config.LPOrd + 1);
c@417 69
c@417 70 m_FiltFilt = new FiltFilt(params);
cannam@483 71
mathieu@321 72 //add delta threshold
c@410 73 m_delta = Config.delta;
c@225 74 }
c@225 75
c@225 76 void DFProcess::deInitialise()
c@225 77 {
c@225 78 delete [] filtSrc;
c@225 79 delete [] filtDst;
c@225 80 delete [] m_filtScratchIn;
c@225 81 delete [] m_filtScratchOut;
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
cannam@483 109 for( i = 0; i < m_winPre; i++) {
cannam@483 110
cannam@483 111 if (index >= m_length) {
cannam@483 112 break;
cannam@483 113 }
c@299 114
cannam@483 115 k = i + m_winPost + 1;
c@225 116
cannam@483 117 for( j = 0; j < k; j++) {
cannam@483 118 y[ j ] = src[ j ];
cannam@483 119 }
cannam@483 120 scratch[ index ] = MathUtilities::median( y, k );
cannam@483 121 index++;
c@225 122 }
c@225 123
cannam@483 124 for( i = 0; i + m_winPost + m_winPre < m_length; i ++) {
cannam@483 125
cannam@483 126 if (index >= m_length) {
cannam@483 127 break;
cannam@483 128 }
cannam@483 129
cannam@483 130 l = 0;
cannam@483 131 for( j = i; j < ( i + m_winPost + m_winPre + 1); j++) {
cannam@483 132 y[ l ] = src[ j ];
cannam@483 133 l++;
cannam@483 134 }
c@299 135
cannam@483 136 scratch[index] = MathUtilities::median( y, (m_winPost + m_winPre + 1 ));
cannam@483 137 index++;
c@225 138 }
c@225 139
cannam@483 140 for( i = std::max( m_length - m_winPost, 1); i < m_length; i++) {
cannam@483 141
cannam@483 142 if (index >= m_length) {
cannam@483 143 break;
cannam@483 144 }
c@299 145
cannam@483 146 k = std::max( i - m_winPre, 1);
c@225 147
cannam@483 148 l = 0;
cannam@483 149 for( j = k; j < m_length; j++) {
cannam@483 150 y[ l ] = src[ j ];
cannam@483 151 l++;
cannam@483 152 }
cannam@483 153
cannam@483 154 scratch[index] = MathUtilities::median( y, l);
cannam@483 155 index++;
c@225 156 }
c@225 157
cannam@483 158 for( i = 0; i < m_length; i++ ) {
cannam@483 159 //add a delta threshold used as an offset when computing the smoothed detection function
cannam@483 160 //(helps to discard noise when detecting peaks)
cannam@483 161 val = src[ i ] - scratch[ i ] - m_delta;
cannam@483 162
cannam@483 163 if( m_isMedianPositive ) {
cannam@483 164 if( val > 0 ) {
cannam@483 165 dst[ i ] = val;
cannam@483 166 } else {
cannam@483 167 dst[ i ] = 0;
cannam@483 168 }
cannam@483 169 } else {
cannam@483 170 dst[ i ] = val;
cannam@483 171 }
c@225 172 }
cannam@483 173
c@225 174 delete [] y;
c@225 175 delete [] scratch;
c@225 176 }
c@225 177
c@225 178
c@225 179 void DFProcess::removeDCNormalize( double *src, double*dst )
c@225 180 {
c@225 181 double DFmax = 0;
c@225 182 double DFMin = 0;
c@225 183 double DFAlphaNorm = 0;
c@225 184
c@225 185 MathUtilities::getFrameMinMax( src, m_length, &DFMin, &DFmax );
c@225 186
c@225 187 MathUtilities::getAlphaNorm( src, m_length, m_alphaNormParam, &DFAlphaNorm );
c@225 188
cannam@483 189 for (int i = 0; i < m_length; i++) {
cannam@483 190 dst[ i ] = ( src[ i ] - DFMin ) / DFAlphaNorm;
c@225 191 }
c@225 192 }