qm-dsp  1.8
PeakPicking.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 "PeakPicking.h"
25 #include "maths/Polyfit.h"
26 
27 #include <iostream>
28 #include <cstring>
29 
30 
32 // Construction/Destruction
34 
36 {
38  initialise( Config );
39 }
40 
42 {
43  deInitialise();
44 }
45 
47 {
48  m_DFLength = Config.length ;
49  Qfilta = Config.QuadThresh.a ;
50  Qfiltb = Config.QuadThresh.b ;
51  Qfiltc = Config.QuadThresh.c ;
52 
61  m_DFProcessingParams.Delta = Config.delta; //add the delta threshold as an adjustable parameter
62 
64 
65  m_workBuffer = new double[ m_DFLength ];
66  memset( m_workBuffer, 0, sizeof(double)*m_DFLength);
67 }
68 
70 {
71  delete [] m_workBuffer;
72  delete m_DFSmoothing;
74 }
75 
76 void PeakPicking::process( double* src, unsigned int len, vector<int> &onsets )
77 {
78  if (len < 4) return;
79 
80  vector <double> m_maxima;
81 
82  // Signal conditioning
84 
85  for( unsigned int u = 0; u < len; u++)
86  {
87  m_maxima.push_back( m_workBuffer[ u ] );
88  }
89 
90  quadEval( m_maxima, onsets );
91 
92  for( int b = 0; b < (int)m_maxima.size(); b++)
93  {
94  src[ b ] = m_maxima[ b ];
95  }
96 }
97 
98 int PeakPicking::quadEval( vector<double> &src, vector<int> &idx )
99 {
100  unsigned int maxLength;
101 
102  vector <int> m_maxIndex;
103  vector <int> m_onsetPosition;
104 
105  vector <double> m_maxFit;
106  vector <double> m_poly;
107  vector <double> m_err;
108 
109  m_poly.push_back(0);
110  m_poly.push_back(0);
111  m_poly.push_back(0);
112 
113  for( int t = -2; t < 3; t++)
114  {
115  m_err.push_back( (double)t );
116  }
117  for( unsigned int i = 2; i < src.size() - 2; i++)
118  {
119  if( (src[i] > src[i-1]) && (src[i] > src[i+1]) && (src[i] > 0) )
120  {
121 // m_maxIndex.push_back( i + 1 );
122  m_maxIndex.push_back(i);
123  }
124  }
125 
126  maxLength = m_maxIndex.size();
127 
128  double selMax = 0;
129 
130  for( unsigned int j = 0; j < maxLength ; j++)
131  {
132  for (int k = -2; k <= 2; ++k)
133  {
134  selMax = src[ m_maxIndex[j] + k ] ;
135  m_maxFit.push_back(selMax);
136  }
137 
138  TPolyFit::PolyFit2(m_err, m_maxFit, m_poly);
139 
140  double f = m_poly[0];
141  double h = m_poly[2];
142 
143  if (h < -Qfilta || f > Qfiltc)
144  {
145  idx.push_back(m_maxIndex[j]);
146  }
147 
148  m_maxFit.clear();
149  }
150 
151  return 1;
152 }
int quadEval(vector< double > &src, vector< int > &idx)
Definition: PeakPicking.cpp:98
double * m_workBuffer
Definition: PeakPicking.h:112
DFProcConfig m_DFProcessingParams
Definition: PeakPicking.h:104
void process(double *src, unsigned int len, vector< int > &onsets)
Definition: PeakPicking.cpp:76
double * LPBCoeffs
Definition: DFProcess.h:34
static double PolyFit2(const vector< double > &x, const vector< double > &y, vector< double > &coef)
Definition: Polyfit.h:102
unsigned int length
Definition: DFProcess.h:31
unsigned int length
Definition: PeakPicking.h:64
unsigned int LPOrd
Definition: PeakPicking.h:68
PeakPicking(PPickParams Config)
Definition: PeakPicking.cpp:35
unsigned int winPre
Definition: DFProcess.h:35
void initialise(PPickParams Config)
Definition: PeakPicking.cpp:46
float delta
Definition: PeakPicking.h:73
float Delta
Definition: DFProcess.h:39
unsigned int post
Definition: PeakPicking.h:39
unsigned int winPost
Definition: DFProcess.h:36
#define NULL
Definition: Filter.h:20
double * LPBCoeffs
Definition: PeakPicking.h:70
double Qfiltc
Definition: PeakPicking.h:109
virtual ~PeakPicking()
Definition: PeakPicking.cpp:41
unsigned int pre
Definition: PeakPicking.h:38
void process(double *src, double *dst)
Definition: DFProcess.cpp:87
unsigned int LPOrd
Definition: DFProcess.h:32
double c
Definition: PeakPicking.h:52
double AlphaNormParam
Definition: DFProcess.h:37
DFProcess * m_DFSmoothing
Definition: PeakPicking.h:114
double b
Definition: PeakPicking.h:51
double Qfiltb
Definition: PeakPicking.h:108
double a
Definition: PeakPicking.h:50
unsigned int m_DFLength
Definition: PeakPicking.h:106
bool isMedianPositive
Definition: DFProcess.h:38
double * LPACoeffs
Definition: PeakPicking.h:69
double * LPACoeffs
Definition: DFProcess.h:33
QFitThresh QuadThresh
Definition: PeakPicking.h:72
unsigned int alpha
Definition: PeakPicking.h:66
double Qfilta
Definition: PeakPicking.h:107
PPWinThresh WinT
Definition: PeakPicking.h:71
void deInitialise()
Definition: PeakPicking.cpp:69