Audio Effects Framework
Vector.h
Go to the documentation of this file.
1 #if !defined(__VectorFloat_hdr__)
2 #define __VectorFloat_hdr__
3 
4 #include <cassert>
5 #include <cstring>
6 #include <limits>
7 #include <cmath>
8 
11 class CVector
12 {
13 public:
19  template<typename T>
20  static void setZero (T *pfSrcDest, int iLength)
21  {
22  assert (iLength >= 0);
23  assert (pfSrcDest);
24 
25  if (iLength > 0)
26  memset (pfSrcDest, 0, sizeof(T)*iLength);
27  }
34  template<typename T>
35  static void setZeroBelowThresh (T *pfSrcDest, int iLength, T Thresh)
36  {
37  assert (iLength >= 0);
38  assert (pfSrcDest);
39 
40  for (int i = 0; i < iLength; i++)
41  if (pfSrcDest[i] < Thresh)
42  pfSrcDest[i] = 0;
43  }
50  template<typename T>
51  static void copy(T *pDest, const T *pSource, int iLength)
52  {
53  assert(iLength >= 0);
54 
55  if (iLength > 0)
56  {
57  assert(pDest);
58  assert(pSource);
59  memcpy(pDest, pSource, sizeof(T)*iLength);
60  }
61  }
67  template<typename T>
68  static void flip_I(T *pfSrcDest, int iLength)
69  {
70  assert(iLength >= 0);
71 
72  if (iLength > 0)
73  {
74  assert(pfSrcDest);
75 
76  int iLoopLength = iLength / 2; // integer division!
77  for (int i = 0; i < iLoopLength; i++)
78  {
79  T Tmp = pfSrcDest[i];
80  pfSrcDest[i] = pfSrcDest[iLength - 1 - i];
81  pfSrcDest[iLength - 1 - i] = Tmp;
82  }
83  }
84  }
92  template<typename T>
93  static void moveInMem (T *pfSrcDest, int iDestIdx, int iSrcIdx, int iLength)
94  {
95  assert (iLength >= 0);
96  assert (pfSrcDest);
97 
98  if (iLength > 0)
99  memmove (&pfSrcDest[iDestIdx], &pfSrcDest[iSrcIdx], sizeof(T)*iLength);
100  }
101 };
102 
106 {
107 public:
108 
114  static inline void setZero (float *pfSrcDest, int iLength)
115  {
116  CVector::setZero(pfSrcDest, iLength);
117  }
118 
125  static inline void setZeroBelowThresh (float *pfSrcDest, int iLength, float fThresh)
126  {
127  CVector::setZeroBelowThresh(pfSrcDest, iLength, fThresh);
128  }
129 
136  static inline void copy(float *pfDest, const float *pfSource, int iLength)
137  {
138  CVector::copy(pfDest, pfSource, iLength);
139  }
140 
146  static inline void flip_I(float *pfSrcDest, int iLength)
147  {
148  CVector::flip_I(pfSrcDest, iLength);
149  }
150 
158  static inline void moveInMem (float *pfSrcDest, int iDestIdx, int iSrcIdx, int iLength)
159  {
160  CVector::moveInMem(pfSrcDest, iDestIdx, iSrcIdx, iLength);
161  }
162 
169  static inline void setValue (float *pfDest, float fValue, int iLength)
170  {
171  assert (iLength >= 0);
172  assert (pfDest);
173 
174  for (int i = 0; i < iLength; i++)
175  pfDest[i] = fValue;
176  }
177 
184  static inline void mulC_I (float *pfSrcDest, float fScale, int iLength)
185  {
186  assert (iLength >= 0);
187  assert (pfSrcDest);
188 
189  for (int i = 0; i < iLength; i++)
190  pfSrcDest[i] *= fScale;
191  }
192 
199  static inline void mul_I (float *pfSrcDest, const float *pfSrc, int iLength)
200  {
201  assert (iLength >= 0);
202  assert (pfSrcDest);
203  assert (pfSrc);
204 
205  for (int i = 0; i < iLength; i++)
206  pfSrcDest[i] *= pfSrc[i];
207  }
208 
215  static inline float mulScalar (const float *pfSrc1, const float *pfSrc2, int iLength)
216  {
217  assert (iLength >= 0);
218  assert (pfSrc1);
219  assert (pfSrc2);
220  float fResult = 0;
221 
222  for (int i = 0; i < iLength; i++)
223  fResult += pfSrc1[i] * pfSrc2[i];
224 
225  return fResult;
226  }
227 
234  static inline void div_I (float *pfSrcDest, const float *pfSrc, int iLength)
235  {
236  assert (iLength >= 0);
237  assert (pfSrcDest);
238  assert (pfSrc);
239 
240  for (int i = 0; i < iLength; i++)
241  {
242  assert(pfSrc[i] != 0);
243  pfSrcDest[i] /= pfSrc[i];
244  }
245  }
246 
253  static inline void add_I (float *pfSrcDest, const float *pfSrc, int iLength)
254  {
255  assert (iLength >= 0);
256  assert (pfSrcDest);
257  assert (pfSrc);
258 
259  for (int i = 0; i < iLength; i++)
260  pfSrcDest[i] += pfSrc[i];
261  }
262 
269  static inline void addC_I (float *pfSrcDest, float fScale, int iLength)
270  {
271  assert (iLength >= 0);
272  assert (pfSrcDest);
273 
274  for (int i = 0; i < iLength; i++)
275  pfSrcDest[i] += fScale;
276  }
277 
284  static inline void sub_I (float *pfSrcDest, const float *pfSrc, int iLength)
285  {
286  assert (iLength >= 0);
287  assert (pfSrcDest);
288  assert (pfSrc);
289 
290  for (int i = 0; i < iLength; i++)
291  pfSrcDest[i] -= pfSrc[i];
292  }
293 
300  static inline float sum (const float *pfSrc, int iLength, bool bAbs = false)
301  {
302  assert (iLength >= 0);
303  assert (pfSrc);
304 
305  float fResult = 0;
306  if (bAbs)
307  {
308  for (int i = 0; i < iLength; i++)
309  fResult += std::abs(pfSrc[i]);
310  }
311  else
312  {
313  for (int i = 0; i < iLength; i++)
314  fResult += pfSrc[i];
315  }
316  return fResult;
317  }
318 
325  static inline bool isEqual (const float *pfSrc1, const float *pfSrc2, int iLength)
326  {
327  assert (iLength >= 0);
328  assert (pfSrc1);
329  assert (pfSrc2);
330 
331  return (memcmp (pfSrc1, pfSrc2, iLength * sizeof(float)) == 0);
332  }
333 
339  static inline float getMean (const float *pfSrc, long long int iLength)
340  {
341  assert (iLength >= 0);
342 
343  float fMean = 0;
344 
345  for (int i=0; i < iLength; i++)
346  {
347  fMean += pfSrc[i];
348  }
349 
350  if (iLength > 0)
351  {
352  fMean /= iLength;
353  }
354 
355  return fMean;
356  }
357 
364  static inline float getStd (const float *pfSrc, long long int iLength, float fMean = std::numeric_limits<float>::max())
365  {
366  assert (iLength >= 0);
367 
368  float fStd = 0;
369 
370  if (fMean == std::numeric_limits<float>::max())
371  {
372  fMean = getMean(pfSrc, iLength);
373  }
374 
375  for (int i=0; i < iLength; i++)
376  {
377  fStd += (pfSrc[i] - fMean) * (pfSrc[i] - fMean);
378  }
379 
380  if (iLength > 1)
381  {
382  //dStd /= (iLength - 1);
383  fStd /= iLength;
384  }
385 
386  return std::sqrt(fStd);
387  }
388 
394  static inline float getRms (const float *pfSrc, long long int iLength)
395  {
396  assert (iLength >= 0);
397 
398  float fRms = 0;
399 
400 
401  for (int i=0; i < iLength; i++)
402  {
403  fRms += pfSrc[i] * pfSrc[i];
404  }
405 
406  if (iLength > 0)
407  {
408  fRms /= iLength;
409  }
410 
411  return std::sqrt(fRms);
412  }
413 
420  static inline float getMax (const float *pfSrc, long long int iLength, bool bAbs = false)
421  {
422  float fMax;
423  long long iMax;
424 
425  findMax(pfSrc, fMax, iMax, iLength, bAbs);
426 
427  return fMax;
428  }
429 
436  static inline float getMin (const float *pfSrc, long long int iLength, bool bAbs = false)
437  {
438  float fMin;
439  long long iMin;
440 
441  findMin(pfSrc, fMin, iMin, iLength, bAbs);
442 
443  return fMin;
444  }
445 
454  static inline void findMax (const float *pfSrc, float &fMax, long long &iMax, long long int iLength, bool bAbs = false)
455  {
456  assert (iLength >= 0);
457  assert (pfSrc);
458 
459  fMax = -std::numeric_limits<float>::max();
460  iMax = -1;
461 
462  for (int i = 0; i < iLength; i++)
463  {
464  float fCurr = (bAbs)? std::abs(pfSrc[i]) : pfSrc[i];
465 
466  if (fCurr > fMax)
467  {
468  fMax = fCurr;
469  iMax = i;
470  }
471  }
472  }
473 
482  static inline void findMin (const float *pfSrc, float &fMin, long long &iMin, long long int iLength, bool bAbs = false)
483  {
484  assert (iLength >= 0);
485  assert (pfSrc);
486 
487  fMin = std::numeric_limits<float>::max();
488  iMin = -1;
489 
490  for (int i = 0; i < iLength; i++)
491  {
492  float fCurr = (bAbs)? std::abs(pfSrc[i]) : pfSrc[i];
493 
494  if (fCurr < fMin)
495  {
496  fMin = fCurr;
497  iMin = i;
498  }
499  }
500  }
501 };
502 #endif // __VectorFloat_hdr__
CVectorFloat::div_I
static void div_I(float *pfSrcDest, const float *pfSrc, int iLength)
Definition: Vector.h:234
CVectorFloat::add_I
static void add_I(float *pfSrcDest, const float *pfSrc, int iLength)
Definition: Vector.h:253
CVectorFloat::setValue
static void setValue(float *pfDest, float fValue, int iLength)
Definition: Vector.h:169
CVectorFloat::getRms
static float getRms(const float *pfSrc, long long int iLength)
Definition: Vector.h:394
CVector::setZero
static void setZero(T *pfSrcDest, int iLength)
Definition: Vector.h:20
CVectorFloat::mulC_I
static void mulC_I(float *pfSrcDest, float fScale, int iLength)
Definition: Vector.h:184
CVectorFloat::moveInMem
static void moveInMem(float *pfSrcDest, int iDestIdx, int iSrcIdx, int iLength)
Definition: Vector.h:158
CVectorFloat::getMax
static float getMax(const float *pfSrc, long long int iLength, bool bAbs=false)
Definition: Vector.h:420
CVectorFloat::setZeroBelowThresh
static void setZeroBelowThresh(float *pfSrcDest, int iLength, float fThresh)
Definition: Vector.h:125
CVectorFloat::setZero
static void setZero(float *pfSrcDest, int iLength)
Definition: Vector.h:114
CVectorFloat::getStd
static float getStd(const float *pfSrc, long long int iLength, float fMean=std::numeric_limits< float >::max())
Definition: Vector.h:364
CVectorFloat::sub_I
static void sub_I(float *pfSrcDest, const float *pfSrc, int iLength)
Definition: Vector.h:284
CVectorFloat::isEqual
static bool isEqual(const float *pfSrc1, const float *pfSrc2, int iLength)
Definition: Vector.h:325
CVector::moveInMem
static void moveInMem(T *pfSrcDest, int iDestIdx, int iSrcIdx, int iLength)
Definition: Vector.h:93
CVectorFloat::copy
static void copy(float *pfDest, const float *pfSource, int iLength)
Definition: Vector.h:136
CVector::flip_I
static void flip_I(T *pfSrcDest, int iLength)
Definition: Vector.h:68
CVectorFloat::getMean
static float getMean(const float *pfSrc, long long int iLength)
Definition: Vector.h:339
CVector::setZeroBelowThresh
static void setZeroBelowThresh(T *pfSrcDest, int iLength, T Thresh)
Definition: Vector.h:35
CVector
class with static functions for buffer operations with type T
Definition: Vector.h:12
CVector::copy
static void copy(T *pDest, const T *pSource, int iLength)
Definition: Vector.h:51
CVectorFloat::findMin
static void findMin(const float *pfSrc, float &fMin, long long &iMin, long long int iLength, bool bAbs=false)
Definition: Vector.h:482
CVectorFloat::flip_I
static void flip_I(float *pfSrcDest, int iLength)
Definition: Vector.h:146
CVectorFloat::getMin
static float getMin(const float *pfSrc, long long int iLength, bool bAbs=false)
Definition: Vector.h:436
CVectorFloat::findMax
static void findMax(const float *pfSrc, float &fMax, long long &iMax, long long int iLength, bool bAbs=false)
Definition: Vector.h:454
CVectorFloat::sum
static float sum(const float *pfSrc, int iLength, bool bAbs=false)
Definition: Vector.h:300
CVectorFloat::mulScalar
static float mulScalar(const float *pfSrc1, const float *pfSrc2, int iLength)
Definition: Vector.h:215
CVectorFloat::mul_I
static void mul_I(float *pfSrcDest, const float *pfSrc, int iLength)
Definition: Vector.h:199
CVectorFloat
class with static functions for buffer operations with type float
Definition: Vector.h:106
CVectorFloat::addC_I
static void addC_I(float *pfSrcDest, float fScale, int iLength)
Definition: Vector.h:269