comparison dsp/onsets/DetectionFunction.cpp @ 0:d7116e3183f8

* Queen Mary C++ DSP library
author cannam
date Wed, 05 Apr 2006 17:35:59 +0000
parents
children c539af5259da
comparison
equal deleted inserted replaced
-1:000000000000 0:d7116e3183f8
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 copyright 2005-2006 Christian Landone.
8 All rights reserved.
9 */
10
11 #include "DetectionFunction.h"
12
13 //////////////////////////////////////////////////////////////////////
14 // Construction/Destruction
15 //////////////////////////////////////////////////////////////////////
16
17 DetectionFunction::DetectionFunction( DFConfig Config ) :
18 m_window(0)
19 {
20 magHistory = NULL;
21 phaseHistory = NULL;
22 phaseHistoryOld = NULL;
23 j = ComplexData( 0, 1 );
24
25 initialise( Config );
26 }
27
28 DetectionFunction::~DetectionFunction()
29 {
30 deInitialise();
31 }
32
33
34 void DetectionFunction::initialise( DFConfig Config )
35 {
36 m_dataLength = Config.frameLength;
37 m_halfLength = m_dataLength/2;
38 m_DFType = Config.DFType;
39
40 magHistory = new double[ m_halfLength ];
41 memset(magHistory,0, m_halfLength*sizeof(double));
42
43 phaseHistory = new double[ m_halfLength ];
44 memset(phaseHistory,0, m_halfLength*sizeof(double));
45
46 phaseHistoryOld = new double[ m_halfLength ];
47 memset(phaseHistoryOld,0, m_halfLength*sizeof(double));
48
49 m_phaseVoc = new PhaseVocoder;
50
51 m_DFWindowedFrame = new double[ m_dataLength ];
52 m_magnitude = new double[ m_halfLength ];
53 m_thetaAngle = new double[ m_halfLength ];
54
55 m_window = new Window<double>(HanningWindow, m_dataLength);
56 }
57
58 void DetectionFunction::deInitialise()
59 {
60 delete [] magHistory ;
61 delete [] phaseHistory ;
62 delete [] phaseHistoryOld ;
63
64 delete m_phaseVoc;
65
66 delete [] m_DFWindowedFrame;
67 delete [] m_magnitude;
68 delete [] m_thetaAngle;
69
70 delete m_window;
71 }
72
73 double DetectionFunction::process( double *TDomain )
74 {
75 double retVal = 0;
76
77 m_window->cut( TDomain, m_DFWindowedFrame );
78
79 m_phaseVoc->process( m_dataLength, m_DFWindowedFrame, m_magnitude, m_thetaAngle );
80
81 switch( m_DFType )
82 {
83 case DF_HFC:
84 retVal = HFC( m_halfLength, m_magnitude);
85 break;
86
87 case DF_SPECDIFF:
88 retVal = specDiff( m_halfLength, m_magnitude);
89 break;
90
91 case DF_PHASEDEV:
92 retVal = phaseDev( m_halfLength, m_magnitude, m_thetaAngle);
93 break;
94
95 case DF_COMPLEXSD:
96 retVal = complexSD( m_halfLength, m_magnitude, m_thetaAngle);
97 break;
98 }
99
100 return retVal;
101 }
102
103 double DetectionFunction::HFC(unsigned int length, double *src)
104 {
105 unsigned int i;
106 double val = 0;
107
108 for( i = 0; i < length; i++)
109 {
110 val += src[ i ] * ( i + 1);
111 }
112 return val;
113 }
114
115 double DetectionFunction::specDiff(unsigned int length, double *src)
116 {
117 unsigned int i;
118 double val = 0.0;
119 double temp = 0.0;
120 double diff = 0.0;
121
122 for( i = 0; i < length; i++)
123 {
124 temp = fabs( (src[ i ] * src[ i ]) - (magHistory[ i ] * magHistory[ i ]) );
125
126 diff= sqrt(temp);
127
128 if( src[ i ] > 0.1)
129 {
130 val += diff;
131 }
132
133 magHistory[ i ] = src[ i ];
134 }
135
136 return val;
137 }
138
139
140 double DetectionFunction::phaseDev(unsigned int length, double *srcMagnitude, double *srcPhase)
141 {
142 unsigned int i;
143 double tmpPhase = 0;
144 double tmpVal = 0;
145 double val = 0;
146
147 double dev = 0;
148
149 for( i = 0; i < length; i++)
150 {
151 tmpPhase = (srcPhase[ i ]- 2*phaseHistory[ i ]+phaseHistoryOld[ i ]);
152 dev = MathUtilities::princarg( tmpPhase );
153
154 if( srcMagnitude[ i ] > 0.1)
155 {
156 tmpVal = fabs( dev);
157 val += tmpVal ;
158 }
159
160 phaseHistoryOld[ i ] = phaseHistory[ i ] ;
161 phaseHistory[ i ] = srcPhase[ i ];
162 }
163
164
165 return val;
166 }
167
168
169 double DetectionFunction::complexSD(unsigned int length, double *srcMagnitude, double *srcPhase)
170 {
171 unsigned int i;
172 double val = 0;
173 double tmpPhase = 0;
174 double tmpReal = 0;
175 double tmpImag = 0;
176
177 double dev = 0;
178 ComplexData meas = ComplexData( 0, 0 );
179
180 for( i = 0; i < length; i++)
181 {
182 tmpPhase = (srcPhase[ i ]- 2*phaseHistory[ i ]+phaseHistoryOld[ i ]);
183 dev= MathUtilities::princarg( tmpPhase );
184
185 meas = magHistory[i] - ( srcMagnitude[ i ] * exp( j * dev) );
186
187 tmpReal = real( meas );
188 tmpImag = imag( meas );
189
190 val += sqrt( (tmpReal * tmpReal) + (tmpImag * tmpImag) );
191
192 phaseHistoryOld[ i ] = phaseHistory[ i ] ;
193 phaseHistory[ i ] = srcPhase[ i ];
194 magHistory[ i ] = srcMagnitude[ i ];
195 }
196
197 return val;
198 }
199
200 double* DetectionFunction::getSpectrumMagnitude()
201 {
202 return m_magnitude;
203 }
204