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