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 "DetectionFunction.h"
|
c@225
|
12
|
c@225
|
13 //////////////////////////////////////////////////////////////////////
|
c@225
|
14 // Construction/Destruction
|
c@225
|
15 //////////////////////////////////////////////////////////////////////
|
c@225
|
16
|
c@225
|
17 DetectionFunction::DetectionFunction( DFConfig Config ) :
|
c@225
|
18 m_window(0)
|
c@225
|
19 {
|
c@227
|
20 m_magHistory = NULL;
|
c@227
|
21 m_phaseHistory = NULL;
|
c@227
|
22 m_phaseHistoryOld = NULL;
|
c@225
|
23
|
c@225
|
24 initialise( Config );
|
c@225
|
25 }
|
c@225
|
26
|
c@225
|
27 DetectionFunction::~DetectionFunction()
|
c@225
|
28 {
|
c@225
|
29 deInitialise();
|
c@225
|
30 }
|
c@225
|
31
|
c@225
|
32
|
c@225
|
33 void DetectionFunction::initialise( DFConfig Config )
|
c@225
|
34 {
|
c@225
|
35 m_dataLength = Config.frameLength;
|
c@225
|
36 m_halfLength = m_dataLength/2;
|
c@225
|
37 m_DFType = Config.DFType;
|
c@225
|
38
|
c@227
|
39 m_magHistory = new double[ m_halfLength ];
|
c@227
|
40 memset(m_magHistory,0, m_halfLength*sizeof(double));
|
c@225
|
41
|
c@227
|
42 m_phaseHistory = new double[ m_halfLength ];
|
c@227
|
43 memset(m_phaseHistory,0, m_halfLength*sizeof(double));
|
c@225
|
44
|
c@227
|
45 m_phaseHistoryOld = new double[ m_halfLength ];
|
c@227
|
46 memset(m_phaseHistoryOld,0, m_halfLength*sizeof(double));
|
c@225
|
47
|
c@225
|
48 m_phaseVoc = new PhaseVocoder;
|
c@225
|
49
|
c@225
|
50 m_DFWindowedFrame = new double[ m_dataLength ];
|
c@225
|
51 m_magnitude = new double[ m_halfLength ];
|
c@225
|
52 m_thetaAngle = new double[ m_halfLength ];
|
c@225
|
53
|
c@225
|
54 m_window = new Window<double>(HanningWindow, m_dataLength);
|
c@225
|
55 }
|
c@225
|
56
|
c@225
|
57 void DetectionFunction::deInitialise()
|
c@225
|
58 {
|
c@227
|
59 delete [] m_magHistory ;
|
c@227
|
60 delete [] m_phaseHistory ;
|
c@227
|
61 delete [] m_phaseHistoryOld ;
|
c@225
|
62
|
c@225
|
63 delete m_phaseVoc;
|
c@225
|
64
|
c@225
|
65 delete [] m_DFWindowedFrame;
|
c@225
|
66 delete [] m_magnitude;
|
c@225
|
67 delete [] m_thetaAngle;
|
c@225
|
68
|
c@225
|
69 delete m_window;
|
c@225
|
70 }
|
c@225
|
71
|
c@225
|
72 double DetectionFunction::process( double *TDomain )
|
c@225
|
73 {
|
c@225
|
74 m_window->cut( TDomain, m_DFWindowedFrame );
|
c@225
|
75
|
c@225
|
76 m_phaseVoc->process( m_dataLength, m_DFWindowedFrame, m_magnitude, m_thetaAngle );
|
c@225
|
77
|
c@227
|
78 return runDF();
|
c@227
|
79 }
|
c@227
|
80
|
c@227
|
81 double DetectionFunction::process( double *magnitudes, double *phases )
|
c@227
|
82 {
|
c@227
|
83 for (size_t i = 0; i < m_halfLength; ++i) {
|
c@227
|
84 m_magnitude[i] = magnitudes[i];
|
c@227
|
85 m_thetaAngle[i] = phases[i];
|
c@227
|
86 }
|
c@227
|
87
|
c@227
|
88 return runDF();
|
c@227
|
89 }
|
c@227
|
90
|
c@227
|
91 double DetectionFunction::runDF()
|
c@227
|
92 {
|
c@227
|
93 double retVal = 0;
|
c@227
|
94
|
c@225
|
95 switch( m_DFType )
|
c@225
|
96 {
|
c@225
|
97 case DF_HFC:
|
c@225
|
98 retVal = HFC( m_halfLength, m_magnitude);
|
c@225
|
99 break;
|
c@225
|
100
|
c@225
|
101 case DF_SPECDIFF:
|
c@225
|
102 retVal = specDiff( m_halfLength, m_magnitude);
|
c@225
|
103 break;
|
c@225
|
104
|
c@225
|
105 case DF_PHASEDEV:
|
c@225
|
106 retVal = phaseDev( m_halfLength, m_magnitude, m_thetaAngle);
|
c@225
|
107 break;
|
c@225
|
108
|
c@225
|
109 case DF_COMPLEXSD:
|
c@225
|
110 retVal = complexSD( m_halfLength, m_magnitude, m_thetaAngle);
|
c@225
|
111 break;
|
c@237
|
112
|
c@237
|
113 case DF_BROADBAND:
|
c@237
|
114 retVal = broadband( m_halfLength, m_magnitude, m_thetaAngle);
|
c@225
|
115 }
|
c@225
|
116
|
c@225
|
117 return retVal;
|
c@225
|
118 }
|
c@225
|
119
|
c@225
|
120 double DetectionFunction::HFC(unsigned int length, double *src)
|
c@225
|
121 {
|
c@225
|
122 unsigned int i;
|
c@225
|
123 double val = 0;
|
c@225
|
124
|
c@225
|
125 for( i = 0; i < length; i++)
|
c@225
|
126 {
|
c@225
|
127 val += src[ i ] * ( i + 1);
|
c@225
|
128 }
|
c@225
|
129 return val;
|
c@225
|
130 }
|
c@225
|
131
|
c@225
|
132 double DetectionFunction::specDiff(unsigned int length, double *src)
|
c@225
|
133 {
|
c@225
|
134 unsigned int i;
|
c@225
|
135 double val = 0.0;
|
c@225
|
136 double temp = 0.0;
|
c@225
|
137 double diff = 0.0;
|
c@225
|
138
|
c@225
|
139 for( i = 0; i < length; i++)
|
c@225
|
140 {
|
c@227
|
141 temp = fabs( (src[ i ] * src[ i ]) - (m_magHistory[ i ] * m_magHistory[ i ]) );
|
c@225
|
142
|
c@225
|
143 diff= sqrt(temp);
|
c@225
|
144
|
c@225
|
145 if( src[ i ] > 0.1)
|
c@225
|
146 {
|
c@225
|
147 val += diff;
|
c@225
|
148 }
|
c@225
|
149
|
c@227
|
150 m_magHistory[ i ] = src[ i ];
|
c@225
|
151 }
|
c@225
|
152
|
c@225
|
153 return val;
|
c@225
|
154 }
|
c@225
|
155
|
c@225
|
156
|
c@225
|
157 double DetectionFunction::phaseDev(unsigned int length, double *srcMagnitude, double *srcPhase)
|
c@225
|
158 {
|
c@225
|
159 unsigned int i;
|
c@225
|
160 double tmpPhase = 0;
|
c@225
|
161 double tmpVal = 0;
|
c@225
|
162 double val = 0;
|
c@225
|
163
|
c@225
|
164 double dev = 0;
|
c@225
|
165
|
c@225
|
166 for( i = 0; i < length; i++)
|
c@225
|
167 {
|
c@227
|
168 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]);
|
c@225
|
169 dev = MathUtilities::princarg( tmpPhase );
|
c@225
|
170
|
c@225
|
171 if( srcMagnitude[ i ] > 0.1)
|
c@225
|
172 {
|
c@225
|
173 tmpVal = fabs( dev);
|
c@225
|
174 val += tmpVal ;
|
c@225
|
175 }
|
c@225
|
176
|
c@227
|
177 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ;
|
c@227
|
178 m_phaseHistory[ i ] = srcPhase[ i ];
|
c@225
|
179 }
|
c@225
|
180
|
c@225
|
181
|
c@225
|
182 return val;
|
c@225
|
183 }
|
c@225
|
184
|
c@225
|
185
|
c@225
|
186 double DetectionFunction::complexSD(unsigned int length, double *srcMagnitude, double *srcPhase)
|
c@225
|
187 {
|
c@225
|
188 unsigned int i;
|
c@225
|
189 double val = 0;
|
c@225
|
190 double tmpPhase = 0;
|
c@225
|
191 double tmpReal = 0;
|
c@225
|
192 double tmpImag = 0;
|
c@225
|
193
|
c@225
|
194 double dev = 0;
|
c@225
|
195 ComplexData meas = ComplexData( 0, 0 );
|
c@227
|
196 ComplexData j = ComplexData( 0, 1 );
|
c@225
|
197
|
c@225
|
198 for( i = 0; i < length; i++)
|
c@225
|
199 {
|
c@227
|
200 tmpPhase = (srcPhase[ i ]- 2*m_phaseHistory[ i ]+m_phaseHistoryOld[ i ]);
|
c@225
|
201 dev= MathUtilities::princarg( tmpPhase );
|
c@225
|
202
|
c@227
|
203 meas = m_magHistory[i] - ( srcMagnitude[ i ] * exp( j * dev) );
|
c@225
|
204
|
c@225
|
205 tmpReal = real( meas );
|
c@225
|
206 tmpImag = imag( meas );
|
c@225
|
207
|
c@225
|
208 val += sqrt( (tmpReal * tmpReal) + (tmpImag * tmpImag) );
|
c@225
|
209
|
c@227
|
210 m_phaseHistoryOld[ i ] = m_phaseHistory[ i ] ;
|
c@227
|
211 m_phaseHistory[ i ] = srcPhase[ i ];
|
c@227
|
212 m_magHistory[ i ] = srcMagnitude[ i ];
|
c@225
|
213 }
|
c@225
|
214
|
c@225
|
215 return val;
|
c@225
|
216 }
|
c@225
|
217
|
c@237
|
218 double DetectionFunction::broadband(unsigned int length, double *srcMagnitude, double *srcPhase)
|
c@237
|
219 {
|
c@237
|
220 double val = 0;
|
c@237
|
221 for (unsigned int i = 0; i < length; ++i) {
|
c@237
|
222 double sqrmag = srcMagnitude[i] * srcMagnitude[i];
|
c@237
|
223 if (m_magHistory[i] > 0.0) {
|
c@237
|
224 double diff = 10.0 * log10(sqrmag / m_magHistory[i]);
|
c@237
|
225 if (diff > m_dbRise) val = val + 1;
|
c@237
|
226 }
|
c@237
|
227 m_magHistory[i] = sqrmag;
|
c@237
|
228 }
|
c@237
|
229 return val;
|
c@237
|
230 }
|
c@237
|
231
|
c@225
|
232 double* DetectionFunction::getSpectrumMagnitude()
|
c@225
|
233 {
|
c@225
|
234 return m_magnitude;
|
c@225
|
235 }
|
c@225
|
236
|