comparison dsp/onsets/DetectionFunction.cpp @ 227:f06672e8db10

* Make it possible to provide the detection function with frequency domain inputs (i.e. phase vocoder already run)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 15 May 2006 11:21:47 +0000
parents 49844bc8a895
children da277e8b5244
comparison
equal deleted inserted replaced
226:55576b901c06 227:f06672e8db10
15 ////////////////////////////////////////////////////////////////////// 15 //////////////////////////////////////////////////////////////////////
16 16
17 DetectionFunction::DetectionFunction( DFConfig Config ) : 17 DetectionFunction::DetectionFunction( DFConfig Config ) :
18 m_window(0) 18 m_window(0)
19 { 19 {
20 magHistory = NULL; 20 m_magHistory = NULL;
21 phaseHistory = NULL; 21 m_phaseHistory = NULL;
22 phaseHistoryOld = NULL; 22 m_phaseHistoryOld = NULL;
23 j = ComplexData( 0, 1 );
24 23
25 initialise( Config ); 24 initialise( Config );
26 } 25 }
27 26
28 DetectionFunction::~DetectionFunction() 27 DetectionFunction::~DetectionFunction()
35 { 34 {
36 m_dataLength = Config.frameLength; 35 m_dataLength = Config.frameLength;
37 m_halfLength = m_dataLength/2; 36 m_halfLength = m_dataLength/2;
38 m_DFType = Config.DFType; 37 m_DFType = Config.DFType;
39 38
40 magHistory = new double[ m_halfLength ]; 39 m_magHistory = new double[ m_halfLength ];
41 memset(magHistory,0, m_halfLength*sizeof(double)); 40 memset(m_magHistory,0, m_halfLength*sizeof(double));
42 41
43 phaseHistory = new double[ m_halfLength ]; 42 m_phaseHistory = new double[ m_halfLength ];
44 memset(phaseHistory,0, m_halfLength*sizeof(double)); 43 memset(m_phaseHistory,0, m_halfLength*sizeof(double));
45 44
46 phaseHistoryOld = new double[ m_halfLength ]; 45 m_phaseHistoryOld = new double[ m_halfLength ];
47 memset(phaseHistoryOld,0, m_halfLength*sizeof(double)); 46 memset(m_phaseHistoryOld,0, m_halfLength*sizeof(double));
48 47
49 m_phaseVoc = new PhaseVocoder; 48 m_phaseVoc = new PhaseVocoder;
50 49
51 m_DFWindowedFrame = new double[ m_dataLength ]; 50 m_DFWindowedFrame = new double[ m_dataLength ];
52 m_magnitude = new double[ m_halfLength ]; 51 m_magnitude = new double[ m_halfLength ];
55 m_window = new Window<double>(HanningWindow, m_dataLength); 54 m_window = new Window<double>(HanningWindow, m_dataLength);
56 } 55 }
57 56
58 void DetectionFunction::deInitialise() 57 void DetectionFunction::deInitialise()
59 { 58 {
60 delete [] magHistory ; 59 delete [] m_magHistory ;
61 delete [] phaseHistory ; 60 delete [] m_phaseHistory ;
62 delete [] phaseHistoryOld ; 61 delete [] m_phaseHistoryOld ;
63 62
64 delete m_phaseVoc; 63 delete m_phaseVoc;
65 64
66 delete [] m_DFWindowedFrame; 65 delete [] m_DFWindowedFrame;
67 delete [] m_magnitude; 66 delete [] m_magnitude;
70 delete m_window; 69 delete m_window;
71 } 70 }
72 71
73 double DetectionFunction::process( double *TDomain ) 72 double DetectionFunction::process( double *TDomain )
74 { 73 {
74 m_window->cut( TDomain, m_DFWindowedFrame );
75
76 m_phaseVoc->process( m_dataLength, m_DFWindowedFrame, m_magnitude, m_thetaAngle );
77
78 return runDF();
79 }
80
81 double DetectionFunction::process( double *magnitudes, double *phases )
82 {
83 for (size_t i = 0; i < m_halfLength; ++i) {
84 m_magnitude[i] = magnitudes[i];
85 m_thetaAngle[i] = phases[i];
86 }
87
88 return runDF();
89 }
90
91 double DetectionFunction::runDF()
92 {
75 double retVal = 0; 93 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 94
81 switch( m_DFType ) 95 switch( m_DFType )
82 { 96 {
83 case DF_HFC: 97 case DF_HFC:
84 retVal = HFC( m_halfLength, m_magnitude); 98 retVal = HFC( m_halfLength, m_magnitude);
119 double temp = 0.0; 133 double temp = 0.0;
120 double diff = 0.0; 134 double diff = 0.0;
121 135
122 for( i = 0; i < length; i++) 136 for( i = 0; i < length; i++)
123 { 137 {
124 temp = fabs( (src[ i ] * src[ i ]) - (magHistory[ i ] * magHistory[ i ]) ); 138 temp = fabs( (src[ i ] * src[ i ]) - (m_magHistory[ i ] * m_magHistory[ i ]) );
125 139
126 diff= sqrt(temp); 140 diff= sqrt(temp);
127 141
128 if( src[ i ] > 0.1) 142 if( src[ i ] > 0.1)
129 { 143 {
130 val += diff; 144 val += diff;
131 } 145 }
132 146
133 magHistory[ i ] = src[ i ]; 147 m_magHistory[ i ] = src[ i ];
134 } 148 }
135 149
136 return val; 150 return val;
137 } 151 }
138 152
146 160
147 double dev = 0; 161 double dev = 0;
148 162
149 for( i = 0; i < length; i++) 163 for( i = 0; i < length; i++)
150 { 164 {
151 tmpPhase = (srcPhase[ i ]- 2*phaseHistory[ i ]+phaseHistoryOld[ i ]); 165 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]);
152 dev = MathUtilities::princarg( tmpPhase ); 166 dev = MathUtilities::princarg( tmpPhase );
153 167
154 if( srcMagnitude[ i ] > 0.1) 168 if( srcMagnitude[ i ] > 0.1)
155 { 169 {
156 tmpVal = fabs( dev); 170 tmpVal = fabs( dev);
157 val += tmpVal ; 171 val += tmpVal ;
158 } 172 }
159 173
160 phaseHistoryOld[ i ] = phaseHistory[ i ] ; 174 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ;
161 phaseHistory[ i ] = srcPhase[ i ]; 175 m_phaseHistory[ i ] = srcPhase[ i ];
162 } 176 }
163 177
164 178
165 return val; 179 return val;
166 } 180 }
174 double tmpReal = 0; 188 double tmpReal = 0;
175 double tmpImag = 0; 189 double tmpImag = 0;
176 190
177 double dev = 0; 191 double dev = 0;
178 ComplexData meas = ComplexData( 0, 0 ); 192 ComplexData meas = ComplexData( 0, 0 );
179 193 ComplexData j = ComplexData( 0, 1 );
180 for( i = 0; i < length; i++) 194
181 { 195 for( i = 0; i < length; i++)
182 tmpPhase = (srcPhase[ i ]- 2*phaseHistory[ i ]+phaseHistoryOld[ i ]); 196 {
197 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]);
183 dev= MathUtilities::princarg( tmpPhase ); 198 dev= MathUtilities::princarg( tmpPhase );
184 199
185 meas = magHistory[i] - ( srcMagnitude[ i ] * exp( j * dev) ); 200 meas = m_magHistory[i] - ( srcMagnitude[ i ] * exp( j * dev) );
186 201
187 tmpReal = real( meas ); 202 tmpReal = real( meas );
188 tmpImag = imag( meas ); 203 tmpImag = imag( meas );
189 204
190 val += sqrt( (tmpReal * tmpReal) + (tmpImag * tmpImag) ); 205 val += sqrt( (tmpReal * tmpReal) + (tmpImag * tmpImag) );
191 206
192 phaseHistoryOld[ i ] = phaseHistory[ i ] ; 207 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ;
193 phaseHistory[ i ] = srcPhase[ i ]; 208 m_phaseHistory[ i ] = srcPhase[ i ];
194 magHistory[ i ] = srcMagnitude[ i ]; 209 m_magHistory[ i ] = srcMagnitude[ i ];
195 } 210 }
196 211
197 return val; 212 return val;
198 } 213 }
199 214