annotate dsp/onsets/PeakPicking.cpp @ 298:255e431ae3d4

* Key detector: when returning key strengths, use the peak value of the three underlying chromagram correlations (from 36-bin chromagram) corresponding to each key, instead of the mean. Rationale: This is the same method as used when returning the key value, and it's nice to have the same results in both returned value and plot. The peak performed better than the sum with a simple test set of triads, so it seems reasonable to change the plot to match the key output rather than the other way around. * FFT: kiss_fftr returns only the non-conjugate bins, synthesise the rest rather than leaving them (perhaps dangerously) undefined. Fixes an uninitialised data error in chromagram that could cause garbage results from key detector. * Constant Q: remove precalculated values again, I reckon they're not proving such a good tradeoff.
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 05 Jun 2009 15:12:39 +0000
parents 5e125f030287
children e5907ae6de17
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@225 7 This file copyright 2005-2006 Christian Landone.
c@225 8 All rights reserved.
c@225 9 */
c@225 10
c@225 11 #include "PeakPicking.h"
c@241 12 #include "maths/Polyfit.h"
c@225 13
c@237 14 #include <iostream>
c@272 15 #include <cstring>
c@272 16
c@225 17
c@225 18 //////////////////////////////////////////////////////////////////////
c@225 19 // Construction/Destruction
c@225 20 //////////////////////////////////////////////////////////////////////
c@225 21
c@225 22 PeakPicking::PeakPicking( PPickParams Config )
c@225 23 {
c@225 24 m_workBuffer = NULL;
c@225 25 initialise( Config );
c@225 26 }
c@225 27
c@225 28 PeakPicking::~PeakPicking()
c@225 29 {
c@225 30 deInitialise();
c@225 31 }
c@225 32
c@225 33 void PeakPicking::initialise( PPickParams Config )
c@225 34 {
c@225 35 m_DFLength = Config.length ;
c@225 36 Qfilta = Config.QuadThresh.a ;
c@225 37 Qfiltb = Config.QuadThresh.b ;
c@225 38 Qfiltc = Config.QuadThresh.c ;
c@225 39
c@225 40 m_DFProcessingParams.length = m_DFLength;
c@225 41 m_DFProcessingParams.LPOrd = Config.LPOrd;
c@225 42 m_DFProcessingParams.LPACoeffs = Config.LPACoeffs;
c@225 43 m_DFProcessingParams.LPBCoeffs = Config.LPBCoeffs;
c@225 44 m_DFProcessingParams.winPre = Config.WinT.pre;
c@225 45 m_DFProcessingParams.winPost = Config.WinT.post;
c@225 46 m_DFProcessingParams.AlphaNormParam = Config.alpha;
c@225 47 m_DFProcessingParams.isMedianPositive = false;
c@225 48
c@225 49 m_DFSmoothing = new DFProcess( m_DFProcessingParams );
c@225 50
c@225 51 m_workBuffer = new double[ m_DFLength ];
c@225 52 memset( m_workBuffer, 0, sizeof(double)*m_DFLength);
c@225 53 }
c@225 54
c@225 55 void PeakPicking::deInitialise()
c@225 56 {
c@225 57 delete [] m_workBuffer;
c@225 58 delete m_DFSmoothing;
c@225 59 m_workBuffer = NULL;
c@225 60 }
c@225 61
c@225 62 void PeakPicking::process( double* src, unsigned int len, vector<int> &onsets )
c@225 63 {
c@283 64 if (len < 4) return;
c@283 65
c@225 66 vector <double> m_maxima;
c@225 67
c@225 68 // Signal conditioning
c@225 69 m_DFSmoothing->process( src, m_workBuffer );
c@225 70
c@225 71 for( unsigned int u = 0; u < len; u++)
c@225 72 {
c@225 73 m_maxima.push_back( m_workBuffer[ u ] );
c@225 74 }
c@225 75
c@225 76 quadEval( m_maxima, onsets );
c@225 77
c@225 78 for( int b = 0; b < m_maxima.size(); b++)
c@225 79 {
c@225 80 src[ b ] = m_maxima[ b ];
c@225 81 }
c@225 82 }
c@225 83
c@225 84 int PeakPicking::quadEval( vector<double> &src, vector<int> &idx )
c@225 85 {
c@225 86 unsigned int maxLength;
c@225 87
c@225 88 vector <int> m_maxIndex;
c@225 89 vector <int> m_onsetPosition;
c@225 90
c@225 91 vector <double> m_maxFit;
c@225 92 vector <double> m_poly;
c@225 93 vector <double> m_err;
c@225 94
c@225 95 double p;
c@225 96
c@225 97 m_poly.push_back(0);
c@225 98 m_poly.push_back(0);
c@225 99 m_poly.push_back(0);
c@225 100
c@225 101 for( int t = -2; t < 3; t++)
c@225 102 {
c@225 103 m_err.push_back( (double)t );
c@225 104 }
c@225 105 for( unsigned int i = 2; i < src.size() - 2; i++)
c@225 106 {
c@237 107 if( (src[i] > src[i-1]) && (src[i] > src[i+1]) && (src[i] > 0) )
c@225 108 {
c@237 109 // m_maxIndex.push_back( i + 1 );
c@237 110 m_maxIndex.push_back(i);
c@225 111 }
c@225 112 }
c@225 113
c@225 114 maxLength = m_maxIndex.size();
c@225 115
c@225 116 double selMax = 0;
c@225 117
c@225 118 for( unsigned int j = 0; j < maxLength ; j++)
c@225 119 {
c@237 120 for (int k = -2; k <= 2; ++k)
c@225 121 {
c@225 122 selMax = src[ m_maxIndex[j] + k ] ;
c@225 123 m_maxFit.push_back(selMax);
c@225 124 }
c@225 125
c@225 126 p = TPolyFit::PolyFit2( m_err, m_maxFit, m_poly);
c@225 127
c@225 128 double f = m_poly[0];
c@225 129 double g = m_poly[1];
c@225 130 double h = m_poly[2];
c@225 131
c@225 132 int kk = m_poly.size();
c@237 133
c@237 134 if (h < -Qfilta || f > Qfiltc)
c@237 135 {
c@237 136 idx.push_back(m_maxIndex[j]);
c@237 137 }
c@225 138
c@237 139 m_maxFit.clear();
c@225 140 }
c@225 141
c@225 142 return 1;
c@225 143 }