annotate dsp/signalconditioning/DFProcess.cpp @ 96:88f3cfcff55f

A threshold (delta) is added in the peak picking parameters structure (PPickParams). It is used as an offset when computing the smoothed detection function. A constructor for the structure PPickParams is also added to set the parameters to 0 when a structure instance is created. Hence programmes using the peak picking parameter structure and which do not set the delta parameter (e.g. QM Vamp note onset detector) won't be affected by the modifications. Functions modified: - dsp/onsets/PeakPicking.cpp - dsp/onsets/PeakPicking.h - dsp/signalconditioning/DFProcess.cpp - dsp/signalconditioning/DFProcess.h
author mathieub <mathieu.barthet@eecs.qmul.ac.uk>
date Mon, 20 Jun 2011 19:01:48 +0100
parents e5907ae6de17
children 2ae4ceb76ac3
rev   line source
cannam@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@0 2
cannam@0 3 /*
cannam@0 4 QM DSP Library
cannam@0 5
cannam@0 6 Centre for Digital Music, Queen Mary, University of London.
Chris@84 7 This file 2005-2006 Christian Landone.
Chris@84 8
mathieu@96 9 Modifications:
mathieu@96 10
mathieu@96 11 - delta threshold
mathieu@96 12 Description: add delta threshold used as offset in the smoothed
mathieu@96 13 detection function
mathieu@96 14 Author: Mathieu Barthet
mathieu@96 15 Date: June 2010
mathieu@96 16
Chris@84 17 This program is free software; you can redistribute it and/or
Chris@84 18 modify it under the terms of the GNU General Public License as
Chris@84 19 published by the Free Software Foundation; either version 2 of the
Chris@84 20 License, or (at your option) any later version. See the file
Chris@84 21 COPYING included with this distribution for more information.
cannam@0 22 */
cannam@0 23
cannam@0 24 #include "DFProcess.h"
cannam@16 25 #include "maths/MathUtilities.h"
cannam@0 26
cannam@47 27 #include <cstring>
cannam@47 28
cannam@0 29 //////////////////////////////////////////////////////////////////////
cannam@0 30 // Construction/Destruction
cannam@0 31 //////////////////////////////////////////////////////////////////////
cannam@0 32
cannam@0 33 DFProcess::DFProcess( DFProcConfig Config )
cannam@0 34 {
cannam@0 35 filtSrc = NULL;
cannam@0 36 filtDst = NULL;
cannam@0 37 m_filtScratchIn = NULL;
cannam@0 38 m_filtScratchOut = NULL;
cannam@0 39
cannam@0 40 m_FFOrd = 0;
cannam@0 41
cannam@0 42 initialise( Config );
cannam@0 43 }
cannam@0 44
cannam@0 45 DFProcess::~DFProcess()
cannam@0 46 {
cannam@0 47 deInitialise();
cannam@0 48 }
cannam@0 49
cannam@0 50 void DFProcess::initialise( DFProcConfig Config )
cannam@0 51 {
cannam@0 52 m_length = Config.length;
cannam@0 53 m_winPre = Config.winPre;
cannam@0 54 m_winPost = Config.winPost;
cannam@0 55 m_alphaNormParam = Config.AlphaNormParam;
cannam@0 56
cannam@0 57 m_isMedianPositive = Config.isMedianPositive;
cannam@0 58
cannam@0 59 filtSrc = new double[ m_length ];
cannam@0 60 filtDst = new double[ m_length ];
cannam@0 61
cannam@0 62
cannam@0 63 //Low Pass Smoothing Filter Config
cannam@0 64 m_FilterConfigParams.ord = Config.LPOrd;
cannam@0 65 m_FilterConfigParams.ACoeffs = Config.LPACoeffs;
cannam@0 66 m_FilterConfigParams.BCoeffs = Config.LPBCoeffs;
cannam@0 67
mathieu@96 68 m_FiltFilt = new FiltFilt( m_FilterConfigParams );
mathieu@96 69
mathieu@96 70 //add delta threshold
mathieu@96 71 m_Delta = Config.Delta;
cannam@0 72 }
cannam@0 73
cannam@0 74 void DFProcess::deInitialise()
cannam@0 75 {
cannam@0 76 delete [] filtSrc;
cannam@0 77
cannam@0 78 delete [] filtDst;
cannam@0 79
cannam@0 80 delete [] m_filtScratchIn;
cannam@0 81
cannam@0 82 delete [] m_filtScratchOut;
cannam@0 83
cannam@0 84 delete m_FiltFilt;
cannam@0 85 }
cannam@0 86
cannam@0 87 void DFProcess::process(double *src, double* dst)
cannam@0 88 {
cannam@58 89 if (m_length == 0) return;
cannam@58 90
cannam@0 91 removeDCNormalize( src, filtSrc );
cannam@0 92
cannam@0 93 m_FiltFilt->process( filtSrc, filtDst, m_length );
cannam@0 94
cannam@0 95 medianFilter( filtDst, dst );
cannam@0 96 }
cannam@0 97
cannam@0 98
cannam@0 99 void DFProcess::medianFilter(double *src, double *dst)
cannam@0 100 {
cannam@74 101 int i,k,j,l;
cannam@74 102 int index = 0;
cannam@0 103
cannam@0 104 double val = 0;
cannam@0 105
cannam@0 106 double* y = new double[ m_winPost + m_winPre + 1];
cannam@0 107 memset( y, 0, sizeof( double ) * ( m_winPost + m_winPre + 1) );
cannam@0 108
cannam@0 109 double* scratch = new double[ m_length ];
cannam@0 110
cannam@80 111 for( i = 0; i < m_winPre; i++)
cannam@0 112 {
cannam@74 113 if (index >= m_length) break;
cannam@74 114
cannam@0 115 k = i + m_winPost + 1;
cannam@0 116
cannam@0 117 for( j = 0; j < k; j++)
cannam@0 118 {
cannam@0 119 y[ j ] = src[ j ];
cannam@0 120 }
cannam@0 121 scratch[ index ] = MathUtilities::median( y, k );
cannam@0 122 index++;
cannam@0 123 }
cannam@0 124
cannam@74 125 for( i = 0; i + m_winPost + m_winPre < m_length; i ++)
cannam@0 126 {
cannam@74 127 if (index >= m_length) break;
cannam@74 128
cannam@0 129
cannam@0 130 l = 0;
cannam@0 131 for( j = i; j < ( i + m_winPost + m_winPre + 1); j++)
cannam@0 132 {
cannam@0 133 y[ l ] = src[ j ];
cannam@0 134 l++;
cannam@0 135 }
cannam@0 136
cannam@0 137 scratch[ index++ ] = MathUtilities::median( y, (m_winPost + m_winPre + 1 ));
cannam@0 138 }
cannam@0 139
cannam@74 140 for( i = std::max( m_length - m_winPost, 1); i < m_length; i++)
cannam@0 141 {
cannam@74 142 if (index >= m_length) break;
cannam@74 143
cannam@74 144 k = std::max( i - m_winPre, 1);
cannam@0 145
cannam@0 146 l = 0;
cannam@0 147 for( j = k; j < m_length; j++)
cannam@0 148 {
cannam@0 149 y[ l ] = src[ j ];
cannam@0 150
cannam@0 151 l++;
cannam@0 152 }
cannam@0 153
cannam@0 154 scratch[ index++ ] = MathUtilities::median( y, l);
cannam@0 155 }
cannam@0 156
cannam@0 157
cannam@0 158 for( i = 0; i < m_length; i++ )
cannam@0 159 {
mathieu@96 160 //add a delta threshold used as an offset when computing the smoothed detection function
mathieu@96 161 //(helps to discard noise when detecting peaks)
mathieu@96 162 val = src[ i ] - scratch[ i ] - m_Delta;
cannam@0 163
cannam@0 164 if( m_isMedianPositive )
cannam@0 165 {
cannam@0 166 if( val > 0 )
cannam@0 167 {
cannam@0 168 dst[ i ] = val;
cannam@0 169 }
cannam@0 170 else
cannam@0 171 {
cannam@0 172 dst[ i ] = 0;
cannam@0 173 }
cannam@0 174 }
cannam@0 175 else
cannam@0 176 {
cannam@0 177 dst[ i ] = val;
cannam@0 178 }
cannam@0 179 }
cannam@0 180
cannam@0 181 delete [] y;
cannam@0 182 delete [] scratch;
cannam@0 183 }
cannam@0 184
cannam@0 185
cannam@0 186 void DFProcess::removeDCNormalize( double *src, double*dst )
cannam@0 187 {
cannam@0 188 double DFmax = 0;
cannam@0 189 double DFMin = 0;
cannam@0 190 double DFAlphaNorm = 0;
cannam@0 191
cannam@0 192 MathUtilities::getFrameMinMax( src, m_length, &DFMin, &DFmax );
cannam@0 193
cannam@0 194 MathUtilities::getAlphaNorm( src, m_length, m_alphaNormParam, &DFAlphaNorm );
cannam@0 195
cannam@0 196 for( unsigned int i = 0; i< m_length; i++)
cannam@0 197 {
cannam@0 198 dst[ i ] = ( src[ i ] - DFMin ) / DFAlphaNorm;
cannam@0 199 }
cannam@0 200 }