sipxmedialib  Version 3.3
MpDspUtilsSumVect.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2007-2012 SIPez LLC. All rights reserved.
3 //
4 // Copyright (C) 2007 SIPfoundry Inc.
5 // Licensed by SIPfoundry under the LGPL license.
6 //
7 // $$
9 
10 // Author: Alexander Chemeris <Alexander DOT Chemeris AT SIPez DOT com>
11 
12 #ifndef _MpDspUtilsSumVect_h_
13 #define _MpDspUtilsSumVect_h_
14 
22 /* ============================ INLINE METHODS ============================ */
23 
24 #ifdef MP_FIXED_POINT // [
25 
26 OsStatus MpDspUtils::add_I(const int16_t *pSrc1, int32_t *pSrc2Dst, int dataLength)
27 {
28  for (int i=0; i<dataLength; i++)
29  {
30  add_I(pSrc2Dst[i], (int32_t)pSrc1[i]);
31  }
32  return OS_SUCCESS;
33 }
34 
35 OsStatus MpDspUtils::add_IGain(const int16_t *pSrc1, int32_t *pSrc2Dst, int dataLength, unsigned src1ScaleFactor)
36 {
37  for (int i=0; i<dataLength; i++)
38  {
39  add_I(pSrc2Dst[i], ((int32_t)pSrc1[i])<<src1ScaleFactor);
40  }
41 
42  return OS_SUCCESS;
43 }
44 
45 OsStatus MpDspUtils::add_IAtt(const int16_t *pSrc1, int32_t *pSrc2Dst, int dataLength, unsigned src1ScaleFactor)
46 {
47  for (int i=0; i<dataLength; i++)
48  {
49  add_I(pSrc2Dst[i], (int32_t)pSrc1[i]>>src1ScaleFactor);
50  }
51 
52  return OS_SUCCESS;
53 }
54 
55 OsStatus MpDspUtils::add(const int32_t *pSrc1, const int32_t *pSrc2, int32_t *pDst, int dataLength)
56 {
57  for (int i=0; i<dataLength; i++)
58  {
59  pDst[i] = add(pSrc1[i], pSrc2[i]);
60  }
61  return OS_SUCCESS;
62 }
63 
64 OsStatus MpDspUtils::addMul_I(const int16_t *pSrc1, int16_t val, int32_t *pSrc2Dst, int dataLength)
65 {
66  for (int i=0; i<dataLength; i++)
67  {
68  addMul_I(pSrc2Dst[i], pSrc1[i], val);
69  }
70 
71  return OS_SUCCESS;
72 }
73 
74 OsStatus MpDspUtils::addMulLinear_I(const int16_t *pSrc1, int16_t valStart, int16_t valEnd,
75  int32_t *pSrc2Dst, int dataLength)
76 {
77  // TODO:: This works fine only when (dataLength << (valStart - valEnd)).
78  // In other case we need smarter step value calculation, e.g.
79  // http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
80  // Or at least we need to distinguish the case when
81  // (valStart - valEnd) < dataLength.
82  int16_t step = (valEnd - valStart) / dataLength;
83  int16_t val = valStart;
84 
85  for (int i=0; i<dataLength; i++, val += step)
86  {
87  addMul_I(pSrc2Dst[i], pSrc1[i], val);
88  }
89  return OS_SUCCESS;
90 }
91 
92 OsStatus MpDspUtils::mul(const int16_t *pSrc, const int16_t val, int32_t *pDst, int dataLength)
93 {
94  for (int i=0; i<dataLength; i++)
95  {
96  pDst[i] = pSrc[i]*val;
97  }
98 
99  return OS_SUCCESS;
100 }
101 
102 OsStatus MpDspUtils::mul_I(int16_t *pSrcDst, const int16_t val, int dataLength)
103 {
104  const int16_t thresold = INT16_MAX/val;
105  for (int i=0; i<dataLength; i++)
106  {
107  if (pSrcDst[i] > thresold)
108  {
109  pSrcDst[i] = INT16_MAX;
110  }
111  else if (pSrcDst[i] < -thresold)
112  {
113  pSrcDst[i] = -INT16_MAX;
114  }
115  else
116  {
117  pSrcDst[i] *= val;
118  }
119  }
120 
121  return OS_SUCCESS;
122 }
123 
124 OsStatus MpDspUtils::mulLinear(const int16_t *pSrc, int16_t valStart, int16_t valEnd,
125  int32_t *pDst, int dataLength)
126 {
127  // TODO:: This works fine only when (dataLength << (valStart - valEnd)).
128  // In other case we need smarter step value calculation, e.g.
129  // http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
130  // Or at least we need to distinguish the case when
131  // (valStart - valEnd) < dataLength.
132  int16_t step = (valEnd - valStart) / dataLength;
133  int16_t val = valStart;
134 
135  for (int i=0; i<dataLength; i++, val += step)
136  {
137  pDst[i] = pSrc[i] * val;
138  }
139  return OS_SUCCESS;
140 }
141 
142 #else // MP_FIXED_POINT ][
143 
144 OsStatus MpDspUtils::add_I(const int16_t *pSrc1, float *pSrc2Dst, int dataLength)
145 {
146  for (int i=0; i<dataLength; i++)
147  {
148  add_I(pSrc2Dst[i], pSrc1[i]);
149  }
150 
151  return OS_SUCCESS;
152 }
153 
154 OsStatus MpDspUtils::add(const float *pSrc1, const float *pSrc2, float *pDst, int dataLength)
155 {
156  for (int i=0; i<dataLength; i++)
157  {
158  pDst[i] = add(pSrc1[i], pSrc2[i]);
159  }
160 
161  return OS_SUCCESS;
162 }
163 
164 OsStatus MpDspUtils::addMul_I(const int16_t *pSrc1, float val, float *pSrc2Dst, int dataLength)
165 {
166  for (int i=0; i<dataLength; i++)
167  {
168  add_I(pSrc2Dst[i], pSrc1[i]*val);
169  }
170 
171  return OS_SUCCESS;
172 }
173 
174 OsStatus MpDspUtils::addMulLinear_I(const int16_t *pSrc1, float valStart, float valEnd,
175  float *pSrc2Dst, int dataLength)
176 {
177  float step = (valEnd - valStart) / dataLength;
178  float val = valStart;
179 
180  for (int i=0; i<dataLength; i++, val += step)
181  {
182  addMul_I(pSrc2Dst[i], pSrc1[i], val);
183  }
184  return OS_SUCCESS;
185 }
186 
187 OsStatus MpDspUtils::mul(const int16_t *pSrc, const float val, float *pDst, int dataLength)
188 {
189  for (int i=0; i<dataLength; i++)
190  {
191  pDst[i] = pSrc[i]*val;
192  }
193 
194  return OS_SUCCESS;
195 }
196 
197 OsStatus MpDspUtils::mulLinear(const int16_t *pSrc, float valStart, float valEnd,
198  float *pDst, int dataLength)
199 {
200  // TODO:: This works fine only when (dataLength << (valStart - valEnd)).
201  // In other case we need smarter step value calculation, e.g.
202  // http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
203  // Or at least we need to distinguish the case when
204  // (valStart - valEnd) < dataLength.
205  float step = (valEnd - valStart) / dataLength;
206  float val = valStart;
207 
208  for (int i=0; i<dataLength; i++, val += step)
209  {
210  pDst[i] = pSrc[i] * val;
211  }
212  return OS_SUCCESS;
213 }
214 
215 #endif // MP_FIXED_POINT ]
216 
217 int MpDspUtils::maxAbs(const int16_t *pSrc, int dataLength)
218 {
219  int16_t startValue = pSrc[0];
220  if (startValue < 0)
221  startValue = -startValue;
222 
223  for (int i = 1; i < dataLength; i++)
224  {
225  startValue = maximum(startValue,
226  (int16_t)( (pSrc[i] > 0) ? pSrc[i] : -(pSrc[i]) ));
227  }
228  return startValue;
229 }
230 
231 int16_t MpDspUtils::maximum(const int16_t *pSrc, int dataLength)
232 {
233  int16_t val = pSrc[0];
234  for (int i = 0; i < dataLength; i++)
235  if (pSrc[i] > val)
236  val = pSrc[i];
237 
238  return val;
239 }
240 
241 int32_t MpDspUtils::maximum(const int32_t *pSrc, int dataLength)
242 {
243  int32_t val = pSrc[0];
244  for (int i = 0; i < dataLength; i++)
245  if (pSrc[i] > val)
246  val = pSrc[i];
247 
248  return val;
249 }
250 
251 int16_t MpDspUtils::minimum(const int16_t *pSrc, int dataLength)
252 {
253  int16_t val = pSrc[0];
254  for (int16_t i = 0; i < dataLength; i++)
255  if (pSrc[i] < val)
256  val = pSrc[i];
257 
258  return val;
259 }
260 
261 int32_t MpDspUtils::minimum(const int32_t *pSrc, int dataLength)
262 {
263  int32_t val = pSrc[0];
264  for (int32_t i = 0; i < dataLength; i++)
265  if (pSrc[i] < val)
266  val = pSrc[i];
267 
268  return val;
269 }
270 
271 int32_t MpDspUtils::countClippedValues(const int16_t *pSrc, int dataLength)
272 {
273  int32_t clippedCount = 0;
274  for (int32_t i = 0; i < dataLength; i++)
275  {
276  if(pSrc[i] >= INT16_MAX || pSrc[i] <= -INT16_MAX)
277  {
278  clippedCount++;
279  }
280  }
281 
282  return(clippedCount);
283 }
284 
285 #endif // _MpDspUtilsSumVect_h_
static int maxAbs(const int16_t *pSrc, int dataLength)
Calculate absolute maximum value of array.
Definition: MpDspUtilsSumVect.h:217
static MP_DSP_VECTOR_API OsStatus mul_I(int16_t *pSrcDst, const int16_t val, int dataLength)
Multiply vector by constant with saturation.
static MP_DSP_VECTOR_API OsStatus add_IAtt(const int16_t *pSrc1, int32_t *pSrc2Dst, int dataLength, unsigned src1ScaleFactor)
Attenuate source vector by 2^src1ScaleFactor factor and add it to accumulator.
static int32_t countClippedValues(const int16_t *pSrc, int dataLength)
Find the number of values in vector that are at maximum or minimum 16 bit value.
Definition: MpDspUtilsSumVect.h:271
static void addMul_I(int32_t &a, int16_t b, int16_t c)
Perform (a+=b*c) saturated (32-bit accumulator, 16-bit operands).
Definition: MpDspUtilsSum.h:32
static void add_I(int16_t &a, int16_t b)
Perform (a+=b) saturated (16-bit).
Definition: MpDspUtilsSum.h:27
static int16_t minimum(int16_t a, int16_t b)
Perform minimum value calculation.
Definition: MpDspUtilsSum.h:103
static MP_DSP_VECTOR_API OsStatus addMulLinear_I(const int16_t *pSrc1, int16_t valStart, int16_t valEnd, int32_t *pSrc2Dst, int dataLength)
Multiply source vector by values linearly changing from valStart to valEnd and add it to accumulator ...
Definition: MpDspUtilsSumVect.h:174
static int16_t maximum(int16_t a, int16_t b)
Perform maximum value calculation.
Definition: MpDspUtilsSum.h:108
static MP_DSP_VECTOR_API OsStatus mulLinear(const int16_t *pSrc, int16_t valStart, int16_t valEnd, int32_t *pDst, int dataLength)
Multiply source vector by values linearly changing from valStart to valEnd and add it to accumulator ...
Definition: MpDspUtilsSumVect.h:197
static int16_t add(int16_t a, int16_t b)
Return (a+b) saturated (16-bit).
Definition: MpDspUtilsSum.h:22
static MP_DSP_VECTOR_API OsStatus add_IGain(const int16_t *pSrc1, int32_t *pSrc2Dst, int dataLength, unsigned src1ScaleFactor)
Gain source vector by 2^src1ScaleFactor factor and add it to accumulator.
static MP_DSP_VECTOR_API OsStatus mul(const int16_t *pSrc, const int16_t val, int32_t *pDst, int dataLength)
Multiply vector by constant.
Definition: MpDspUtilsSumVect.h:187