DFProcess.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  QM DSP Library
5 
6  Centre for Digital Music, Queen Mary, University of London.
7  This file 2005-2006 Christian Landone.
8 
9  Modifications:
10 
11  - delta threshold
12  Description: add delta threshold used as offset in the smoothed
13  detection function
14  Author: Mathieu Barthet
15  Date: June 2010
16 
17  This program is free software; you can redistribute it and/or
18  modify it under the terms of the GNU General Public License as
19  published by the Free Software Foundation; either version 2 of the
20  License, or (at your option) any later version. See the file
21  COPYING included with this distribution for more information.
22 */
23 
24 #include "DFProcess.h"
25 #include "maths/MathUtilities.h"
26 
27 #include <cstring>
28 #include <algorithm>
29 
30 
32 // Construction/Destruction
34 
36 {
37  filtSrc = NULL;
38  filtDst = NULL;
39  m_filtScratchIn = NULL;
40  m_filtScratchOut = NULL;
41 
42  m_FFOrd = 0;
43 
44  initialise( config );
45 }
46 
48 {
49  deInitialise();
50 }
51 
53 {
54  m_length = config.length;
55  m_winPre = config.winPre;
56  m_winPost = config.winPost;
58 
60 
61  filtSrc = new double[ m_length ];
62  filtDst = new double[ m_length ];
63 
64  Filter::Parameters params;
65  params.a = std::vector<double>
66  (config.LPACoeffs, config.LPACoeffs + config.LPOrd + 1);
67  params.b = std::vector<double>
68  (config.LPBCoeffs, config.LPBCoeffs + config.LPOrd + 1);
69 
70  m_FiltFilt = new FiltFilt(params);
71 
72  //add delta threshold
73  m_delta = config.delta;
74 }
75 
77 {
78  delete [] filtSrc;
79  delete [] filtDst;
80  delete [] m_filtScratchIn;
81  delete [] m_filtScratchOut;
82  delete m_FiltFilt;
83 }
84 
85 void DFProcess::process(double *src, double* dst)
86 {
87  if (m_length == 0) return;
88 
89  removeDCNormalize( src, filtSrc );
90 
92 
93  medianFilter( filtDst, dst );
94 }
95 
96 
97 void DFProcess::medianFilter(double *src, double *dst)
98 {
99  int i,k,j,l;
100  int index = 0;
101 
102  double val = 0;
103 
104  double* y = new double[ m_winPost + m_winPre + 1];
105  memset( y, 0, sizeof( double ) * ( m_winPost + m_winPre + 1) );
106 
107  double* scratch = new double[ m_length ];
108 
109  for( i = 0; i < m_winPre; i++) {
110 
111  if (index >= m_length) {
112  break;
113  }
114 
115  k = i + m_winPost + 1;
116 
117  for( j = 0; j < k; j++) {
118  y[ j ] = src[ j ];
119  }
120  scratch[ index ] = MathUtilities::median( y, k );
121  index++;
122  }
123 
124  for( i = 0; i + m_winPost + m_winPre < m_length; i ++) {
125 
126  if (index >= m_length) {
127  break;
128  }
129 
130  l = 0;
131  for( j = i; j < ( i + m_winPost + m_winPre + 1); j++) {
132  y[ l ] = src[ j ];
133  l++;
134  }
135 
136  scratch[index] = MathUtilities::median( y, (m_winPost + m_winPre + 1 ));
137  index++;
138  }
139 
140  for( i = std::max( m_length - m_winPost, 1); i < m_length; i++) {
141 
142  if (index >= m_length) {
143  break;
144  }
145 
146  k = std::max( i - m_winPre, 1);
147 
148  l = 0;
149  for( j = k; j < m_length; j++) {
150  y[ l ] = src[ j ];
151  l++;
152  }
153 
154  scratch[index] = MathUtilities::median( y, l);
155  index++;
156  }
157 
158  for( i = 0; i < m_length; i++ ) {
159  //add a delta threshold used as an offset when computing the smoothed detection function
160  //(helps to discard noise when detecting peaks)
161  val = src[ i ] - scratch[ i ] - m_delta;
162 
163  if( m_isMedianPositive ) {
164  if( val > 0 ) {
165  dst[ i ] = val;
166  } else {
167  dst[ i ] = 0;
168  }
169  } else {
170  dst[ i ] = val;
171  }
172  }
173 
174  delete [] y;
175  delete [] scratch;
176 }
177 
178 
179 void DFProcess::removeDCNormalize( double *src, double*dst )
180 {
181  double DFmax = 0;
182  double DFMin = 0;
183  double DFAlphaNorm = 0;
184 
185  MathUtilities::getFrameMinMax( src, m_length, &DFMin, &DFmax );
186 
188 
189  for (int i = 0; i < m_length; i++) {
190  dst[ i ] = ( src[ i ] - DFMin ) / DFAlphaNorm;
191  }
192 }
double * filtSrc
Definition: DFProcess.h:79
double * m_filtScratchOut
Definition: DFProcess.h:83
double * LPBCoeffs
Definition: DFProcess.h:36
Zero-phase digital filter, implemented by processing the data through a filter specified by the given...
Definition: FiltFilt.h:25
double * filtDst
Definition: DFProcess.h:80
void deInitialise()
Definition: DFProcess.cpp:76
float m_delta
Definition: DFProcess.h:88
virtual ~DFProcess()
Definition: DFProcess.cpp:47
void medianFilter(double *src, double *dst)
Definition: DFProcess.cpp:97
std::vector< double > a
Definition: Filter.h:26
static void getFrameMinMax(const double *data, int len, double *min, double *max)
Return through min and max pointers the highest and lowest values in the given array of the given len...
void process(double *src, double *dst)
Definition: DFProcess.cpp:85
void process(const double *const QM_R__ src, double *const QM_R__ dst, const int length)
Definition: FiltFilt.cpp:28
void initialise(DFProcConfig Config)
Definition: DFProcess.cpp:52
bool m_isMedianPositive
Definition: DFProcess.h:87
int m_FFOrd
Definition: DFProcess.h:72
double AlphaNormParam
Definition: DFProcess.h:39
int m_winPre
Definition: DFProcess.h:74
int m_length
Definition: DFProcess.h:71
FiltFilt * m_FiltFilt
Definition: DFProcess.h:85
double * m_filtScratchIn
Definition: DFProcess.h:82
DFProcess(DFProcConfig Config)
Definition: DFProcess.cpp:35
float delta
Definition: DFProcess.h:41
double m_alphaNormParam
Definition: DFProcess.h:77
bool isMedianPositive
Definition: DFProcess.h:40
static void getAlphaNorm(const double *data, int len, int alpha, double *ANorm)
The alpha norm is the alpha&#39;th root of the mean alpha&#39;th power magnitude.
double * LPACoeffs
Definition: DFProcess.h:35
static double median(const double *src, int len)
Return the median of the values in the given array of the given length.
std::vector< double > b
Definition: Filter.h:27
void removeDCNormalize(double *src, double *dst)
Definition: DFProcess.cpp:179
int m_winPost
Definition: DFProcess.h:75