Mercurial > hg > qm-dsp
comparison maths/MathUtilities.cpp @ 241:a98dd8ec96f8
* Move dsp/maths to maths ; bring PCA and HMM across from Soundbite
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Wed, 09 Jan 2008 10:31:29 +0000 |
parents | |
children | c96785becf96 |
comparison
equal
deleted
inserted
replaced
240:1a406914b3a9 | 241:a98dd8ec96f8 |
---|---|
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 "MathUtilities.h" | |
12 | |
13 #include <iostream> | |
14 #include <cmath> | |
15 | |
16 | |
17 double MathUtilities::mod(double x, double y) | |
18 { | |
19 double a = floor( x / y ); | |
20 | |
21 double b = x - ( y * a ); | |
22 return b; | |
23 } | |
24 | |
25 double MathUtilities::princarg(double ang) | |
26 { | |
27 double ValOut; | |
28 | |
29 ValOut = mod( ang + M_PI, -2 * M_PI ) + M_PI; | |
30 | |
31 return ValOut; | |
32 } | |
33 | |
34 void MathUtilities::getAlphaNorm(const double *data, unsigned int len, unsigned int alpha, double* ANorm) | |
35 { | |
36 unsigned int i; | |
37 double temp = 0.0; | |
38 double a=0.0; | |
39 | |
40 for( i = 0; i < len; i++) | |
41 { | |
42 temp = data[ i ]; | |
43 | |
44 a += ::pow( fabs(temp), alpha ); | |
45 } | |
46 a /= ( double )len; | |
47 a = ::pow( a, ( 1.0 / (double) alpha ) ); | |
48 | |
49 *ANorm = a; | |
50 } | |
51 | |
52 double MathUtilities::getAlphaNorm( const std::vector <double> &data, unsigned int alpha ) | |
53 { | |
54 unsigned int i; | |
55 unsigned int len = data.size(); | |
56 double temp = 0.0; | |
57 double a=0.0; | |
58 | |
59 for( i = 0; i < len; i++) | |
60 { | |
61 temp = data[ i ]; | |
62 | |
63 a += ::pow( fabs(temp), alpha ); | |
64 } | |
65 a /= ( double )len; | |
66 a = ::pow( a, ( 1.0 / (double) alpha ) ); | |
67 | |
68 return a; | |
69 } | |
70 | |
71 double MathUtilities::round(double x) | |
72 { | |
73 double val = (double)floor(x + 0.5); | |
74 | |
75 return val; | |
76 } | |
77 | |
78 double MathUtilities::median(const double *src, unsigned int len) | |
79 { | |
80 unsigned int i, j; | |
81 double tmp = 0.0; | |
82 double tempMedian; | |
83 double medianVal; | |
84 | |
85 double* scratch = new double[ len ];//Vector < double > sortedX = Vector < double > ( size ); | |
86 | |
87 for ( i = 0; i < len; i++ ) | |
88 { | |
89 scratch[i] = src[i]; | |
90 } | |
91 | |
92 for ( i = 0; i < len - 1; i++ ) | |
93 { | |
94 for ( j = 0; j < len - 1 - i; j++ ) | |
95 { | |
96 if ( scratch[j + 1] < scratch[j] ) | |
97 { | |
98 // compare the two neighbors | |
99 tmp = scratch[j]; // swap a[j] and a[j+1] | |
100 scratch[j] = scratch[j + 1]; | |
101 scratch[j + 1] = tmp; | |
102 } | |
103 } | |
104 } | |
105 int middle; | |
106 if ( len % 2 == 0 ) | |
107 { | |
108 middle = len / 2; | |
109 tempMedian = ( scratch[middle] + scratch[middle - 1] ) / 2; | |
110 } | |
111 else | |
112 { | |
113 middle = ( int )floor( len / 2.0 ); | |
114 tempMedian = scratch[middle]; | |
115 } | |
116 | |
117 medianVal = tempMedian; | |
118 | |
119 delete [] scratch; | |
120 return medianVal; | |
121 } | |
122 | |
123 double MathUtilities::sum(const double *src, unsigned int len) | |
124 { | |
125 unsigned int i ; | |
126 double retVal =0.0; | |
127 | |
128 for( i = 0; i < len; i++) | |
129 { | |
130 retVal += src[ i ]; | |
131 } | |
132 | |
133 return retVal; | |
134 } | |
135 | |
136 double MathUtilities::mean(const double *src, unsigned int len) | |
137 { | |
138 double retVal =0.0; | |
139 | |
140 double s = sum( src, len ); | |
141 | |
142 retVal = s / (double)len; | |
143 | |
144 return retVal; | |
145 } | |
146 | |
147 void MathUtilities::getFrameMinMax(const double *data, unsigned int len, double *min, double *max) | |
148 { | |
149 unsigned int i; | |
150 double temp = 0.0; | |
151 double a=0.0; | |
152 | |
153 *min = data[0]; | |
154 *max = data[0]; | |
155 | |
156 for( i = 0; i < len; i++) | |
157 { | |
158 temp = data[ i ]; | |
159 | |
160 if( temp < *min ) | |
161 { | |
162 *min = temp ; | |
163 } | |
164 if( temp > *max ) | |
165 { | |
166 *max = temp ; | |
167 } | |
168 | |
169 } | |
170 } | |
171 | |
172 int MathUtilities::getMax( double* pData, unsigned int Length, double* pMax ) | |
173 { | |
174 unsigned int index = 0; | |
175 unsigned int i; | |
176 double temp = 0.0; | |
177 | |
178 double max = pData[0]; | |
179 | |
180 for( i = 0; i < Length; i++) | |
181 { | |
182 temp = pData[ i ]; | |
183 | |
184 if( temp > max ) | |
185 { | |
186 max = temp ; | |
187 index = i; | |
188 } | |
189 | |
190 } | |
191 | |
192 *pMax = max; | |
193 | |
194 | |
195 return index; | |
196 } | |
197 | |
198 void MathUtilities::circShift( double* pData, int length, int shift) | |
199 { | |
200 shift = shift % length; | |
201 double temp; | |
202 int i,n; | |
203 | |
204 for( i = 0; i < shift; i++) | |
205 { | |
206 temp=*(pData + length - 1); | |
207 | |
208 for( n = length-2; n >= 0; n--) | |
209 { | |
210 *(pData+n+1)=*(pData+n); | |
211 } | |
212 | |
213 *pData = temp; | |
214 } | |
215 } | |
216 | |
217 int MathUtilities::compareInt (const void * a, const void * b) | |
218 { | |
219 return ( *(int*)a - *(int*)b ); | |
220 } | |
221 |