Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/src/spandsp/complex.h @ 4:26cd8f1ef0b1
import spandsp-0.0.6pre17
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 15:50:58 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:c6c5a16ce2f2 | 4:26cd8f1ef0b1 |
---|---|
1 /* | |
2 * SpanDSP - a series of DSP components for telephony | |
3 * | |
4 * complex.h | |
5 * | |
6 * Written by Steve Underwood <steveu@coppice.org> | |
7 * | |
8 * Copyright (C) 2003 Steve Underwood | |
9 * | |
10 * All rights reserved. | |
11 * | |
12 * This program is free software; you can redistribute it and/or modify | |
13 * it under the terms of the GNU Lesser General Public License version 2.1, | |
14 * as published by the Free Software Foundation. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU Lesser General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU Lesser General Public | |
22 * License along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 * | |
25 * $Id: complex.h,v 1.20 2009/02/21 05:39:08 steveu Exp $ | |
26 */ | |
27 | |
28 /*! \file */ | |
29 | |
30 /*! \page complex_page Complex number support | |
31 \section complex_page_sec_1 What does it do? | |
32 Complex number support is part of the C99 standard. However, support for this | |
33 in C compilers is still patchy. A set of complex number feaures is provided as | |
34 a "temporary" measure, until native C language complex number support is | |
35 widespread. | |
36 */ | |
37 | |
38 #if !defined(_SPANDSP_COMPLEX_H_) | |
39 #define _SPANDSP_COMPLEX_H_ | |
40 | |
41 /*! | |
42 Floating complex type. | |
43 */ | |
44 typedef struct | |
45 { | |
46 /*! \brief Real part. */ | |
47 float re; | |
48 /*! \brief Imaginary part. */ | |
49 float im; | |
50 } complexf_t; | |
51 | |
52 /*! | |
53 Floating complex type. | |
54 */ | |
55 typedef struct | |
56 { | |
57 /*! \brief Real part. */ | |
58 double re; | |
59 /*! \brief Imaginary part. */ | |
60 double im; | |
61 } complex_t; | |
62 | |
63 #if defined(HAVE_LONG_DOUBLE) | |
64 /*! | |
65 Long double complex type. | |
66 */ | |
67 typedef struct | |
68 { | |
69 /*! \brief Real part. */ | |
70 long double re; | |
71 /*! \brief Imaginary part. */ | |
72 long double im; | |
73 } complexl_t; | |
74 #endif | |
75 | |
76 /*! | |
77 Complex integer type. | |
78 */ | |
79 typedef struct | |
80 { | |
81 /*! \brief Real part. */ | |
82 int re; | |
83 /*! \brief Imaginary part. */ | |
84 int im; | |
85 } complexi_t; | |
86 | |
87 /*! | |
88 Complex 16 bit integer type. | |
89 */ | |
90 typedef struct | |
91 { | |
92 /*! \brief Real part. */ | |
93 int16_t re; | |
94 /*! \brief Imaginary part. */ | |
95 int16_t im; | |
96 } complexi16_t; | |
97 | |
98 /*! | |
99 Complex 32 bit integer type. | |
100 */ | |
101 typedef struct | |
102 { | |
103 /*! \brief Real part. */ | |
104 int32_t re; | |
105 /*! \brief Imaginary part. */ | |
106 int32_t im; | |
107 } complexi32_t; | |
108 | |
109 #if defined(__cplusplus) | |
110 extern "C" | |
111 { | |
112 #endif | |
113 | |
114 static __inline__ complexf_t complex_setf(float re, float im) | |
115 { | |
116 complexf_t z; | |
117 | |
118 z.re = re; | |
119 z.im = im; | |
120 return z; | |
121 } | |
122 /*- End of function --------------------------------------------------------*/ | |
123 | |
124 static __inline__ complex_t complex_set(double re, double im) | |
125 { | |
126 complex_t z; | |
127 | |
128 z.re = re; | |
129 z.im = im; | |
130 return z; | |
131 } | |
132 /*- End of function --------------------------------------------------------*/ | |
133 | |
134 #if defined(HAVE_LONG_DOUBLE) | |
135 static __inline__ complexl_t complex_setl(long double re, long double im) | |
136 { | |
137 complexl_t z; | |
138 | |
139 z.re = re; | |
140 z.im = im; | |
141 return z; | |
142 } | |
143 /*- End of function --------------------------------------------------------*/ | |
144 #endif | |
145 | |
146 static __inline__ complexi_t complex_seti(int re, int im) | |
147 { | |
148 complexi_t z; | |
149 | |
150 z.re = re; | |
151 z.im = im; | |
152 return z; | |
153 } | |
154 /*- End of function --------------------------------------------------------*/ | |
155 | |
156 static __inline__ complexi16_t complex_seti16(int16_t re, int16_t im) | |
157 { | |
158 complexi16_t z; | |
159 | |
160 z.re = re; | |
161 z.im = im; | |
162 return z; | |
163 } | |
164 /*- End of function --------------------------------------------------------*/ | |
165 | |
166 static __inline__ complexi32_t complex_seti32(int32_t re, int32_t im) | |
167 { | |
168 complexi32_t z; | |
169 | |
170 z.re = re; | |
171 z.im = im; | |
172 return z; | |
173 } | |
174 /*- End of function --------------------------------------------------------*/ | |
175 | |
176 static __inline__ complexf_t complex_addf(const complexf_t *x, const complexf_t *y) | |
177 { | |
178 complexf_t z; | |
179 | |
180 z.re = x->re + y->re; | |
181 z.im = x->im + y->im; | |
182 return z; | |
183 } | |
184 /*- End of function --------------------------------------------------------*/ | |
185 | |
186 static __inline__ complex_t complex_add(const complex_t *x, const complex_t *y) | |
187 { | |
188 complex_t z; | |
189 | |
190 z.re = x->re + y->re; | |
191 z.im = x->im + y->im; | |
192 return z; | |
193 } | |
194 /*- End of function --------------------------------------------------------*/ | |
195 | |
196 #if defined(HAVE_LONG_DOUBLE) | |
197 static __inline__ complexl_t complex_addl(const complexl_t *x, const complexl_t *y) | |
198 { | |
199 complexl_t z; | |
200 | |
201 z.re = x->re + y->re; | |
202 z.im = x->im + y->im; | |
203 return z; | |
204 } | |
205 /*- End of function --------------------------------------------------------*/ | |
206 #endif | |
207 | |
208 static __inline__ complexi_t complex_addi(const complexi_t *x, const complexi_t *y) | |
209 { | |
210 complexi_t z; | |
211 | |
212 z.re = x->re + y->re; | |
213 z.im = x->im + y->im; | |
214 return z; | |
215 } | |
216 /*- End of function --------------------------------------------------------*/ | |
217 | |
218 static __inline__ complexi16_t complex_addi16(const complexi16_t *x, const complexi16_t *y) | |
219 { | |
220 complexi16_t z; | |
221 | |
222 z.re = x->re + y->re; | |
223 z.im = x->im + y->im; | |
224 return z; | |
225 } | |
226 /*- End of function --------------------------------------------------------*/ | |
227 | |
228 static __inline__ complexi32_t complex_addi32(const complexi32_t *x, const complexi32_t *y) | |
229 { | |
230 complexi32_t z; | |
231 | |
232 z.re = x->re + y->re; | |
233 z.im = x->im + y->im; | |
234 return z; | |
235 } | |
236 /*- End of function --------------------------------------------------------*/ | |
237 | |
238 static __inline__ complexf_t complex_subf(const complexf_t *x, const complexf_t *y) | |
239 { | |
240 complexf_t z; | |
241 | |
242 z.re = x->re - y->re; | |
243 z.im = x->im - y->im; | |
244 return z; | |
245 } | |
246 /*- End of function --------------------------------------------------------*/ | |
247 | |
248 static __inline__ complex_t complex_sub(const complex_t *x, const complex_t *y) | |
249 { | |
250 complex_t z; | |
251 | |
252 z.re = x->re - y->re; | |
253 z.im = x->im - y->im; | |
254 return z; | |
255 } | |
256 /*- End of function --------------------------------------------------------*/ | |
257 | |
258 #if defined(HAVE_LONG_DOUBLE) | |
259 static __inline__ complexl_t complex_subl(const complexl_t *x, const complexl_t *y) | |
260 { | |
261 complexl_t z; | |
262 | |
263 z.re = x->re - y->re; | |
264 z.im = x->im - y->im; | |
265 return z; | |
266 } | |
267 /*- End of function --------------------------------------------------------*/ | |
268 #endif | |
269 | |
270 static __inline__ complexi_t complex_subi(const complexi_t *x, const complexi_t *y) | |
271 { | |
272 complexi_t z; | |
273 | |
274 z.re = x->re - y->re; | |
275 z.im = x->im - y->im; | |
276 return z; | |
277 } | |
278 /*- End of function --------------------------------------------------------*/ | |
279 | |
280 static __inline__ complexi16_t complex_subi16(const complexi16_t *x, const complexi16_t *y) | |
281 { | |
282 complexi16_t z; | |
283 | |
284 z.re = x->re - y->re; | |
285 z.im = x->im - y->im; | |
286 return z; | |
287 } | |
288 /*- End of function --------------------------------------------------------*/ | |
289 | |
290 static __inline__ complexi32_t complex_subi32(const complexi32_t *x, const complexi32_t *y) | |
291 { | |
292 complexi32_t z; | |
293 | |
294 z.re = x->re - y->re; | |
295 z.im = x->im - y->im; | |
296 return z; | |
297 } | |
298 /*- End of function --------------------------------------------------------*/ | |
299 | |
300 static __inline__ complexf_t complex_mulf(const complexf_t *x, const complexf_t *y) | |
301 { | |
302 complexf_t z; | |
303 | |
304 z.re = x->re*y->re - x->im*y->im; | |
305 z.im = x->re*y->im + x->im*y->re; | |
306 return z; | |
307 } | |
308 /*- End of function --------------------------------------------------------*/ | |
309 | |
310 static __inline__ complex_t complex_mul(const complex_t *x, const complex_t *y) | |
311 { | |
312 complex_t z; | |
313 | |
314 z.re = x->re*y->re - x->im*y->im; | |
315 z.im = x->re*y->im + x->im*y->re; | |
316 return z; | |
317 } | |
318 /*- End of function --------------------------------------------------------*/ | |
319 | |
320 #if defined(HAVE_LONG_DOUBLE) | |
321 static __inline__ complexl_t complex_mull(const complexl_t *x, const complexl_t *y) | |
322 { | |
323 complexl_t z; | |
324 | |
325 z.re = x->re*y->re - x->im*y->im; | |
326 z.im = x->re*y->im + x->im*y->re; | |
327 return z; | |
328 } | |
329 /*- End of function --------------------------------------------------------*/ | |
330 #endif | |
331 | |
332 static __inline__ complexi_t complex_muli(const complexi_t *x, const complexi_t *y) | |
333 { | |
334 complexi_t z; | |
335 | |
336 z.re = x->re*y->re - x->im*y->im; | |
337 z.im = x->re*y->im + x->im*y->re; | |
338 return z; | |
339 } | |
340 /*- End of function --------------------------------------------------------*/ | |
341 | |
342 static __inline__ complexi16_t complex_muli16(const complexi16_t *x, const complexi16_t *y) | |
343 { | |
344 complexi16_t z; | |
345 | |
346 z.re = (int16_t) ((int32_t) x->re*(int32_t) y->re - (int32_t) x->im*(int32_t) y->im); | |
347 z.im = (int16_t) ((int32_t) x->re*(int32_t) y->im + (int32_t) x->im*(int32_t) y->re); | |
348 return z; | |
349 } | |
350 /*- End of function --------------------------------------------------------*/ | |
351 | |
352 static __inline__ complexi16_t complex_mul_q1_15(const complexi16_t *x, const complexi16_t *y) | |
353 { | |
354 complexi16_t z; | |
355 | |
356 z.re = (int16_t) (((int32_t) x->re*(int32_t) y->re - (int32_t) x->im*(int32_t) y->im) >> 15); | |
357 z.im = (int16_t) (((int32_t) x->re*(int32_t) y->im + (int32_t) x->im*(int32_t) y->re) >> 15); | |
358 return z; | |
359 } | |
360 /*- End of function --------------------------------------------------------*/ | |
361 | |
362 static __inline__ complexi32_t complex_muli32i16(const complexi32_t *x, const complexi16_t *y) | |
363 { | |
364 complexi32_t z; | |
365 | |
366 z.re = x->re*(int32_t) y->re - x->im*(int32_t) y->im; | |
367 z.im = x->re*(int32_t) y->im + x->im*(int32_t) y->re; | |
368 return z; | |
369 } | |
370 /*- End of function --------------------------------------------------------*/ | |
371 | |
372 static __inline__ complexi32_t complex_muli32(const complexi32_t *x, const complexi32_t *y) | |
373 { | |
374 complexi32_t z; | |
375 | |
376 z.re = x->re*y->re - x->im*y->im; | |
377 z.im = x->re*y->im + x->im*y->re; | |
378 return z; | |
379 } | |
380 /*- End of function --------------------------------------------------------*/ | |
381 | |
382 static __inline__ complexf_t complex_divf(const complexf_t *x, const complexf_t *y) | |
383 { | |
384 complexf_t z; | |
385 float f; | |
386 | |
387 f = y->re*y->re + y->im*y->im; | |
388 z.re = ( x->re*y->re + x->im*y->im)/f; | |
389 z.im = (-x->re*y->im + x->im*y->re)/f; | |
390 return z; | |
391 } | |
392 /*- End of function --------------------------------------------------------*/ | |
393 | |
394 static __inline__ complex_t complex_div(const complex_t *x, const complex_t *y) | |
395 { | |
396 complex_t z; | |
397 double f; | |
398 | |
399 f = y->re*y->re + y->im*y->im; | |
400 z.re = ( x->re*y->re + x->im*y->im)/f; | |
401 z.im = (-x->re*y->im + x->im*y->re)/f; | |
402 return z; | |
403 } | |
404 /*- End of function --------------------------------------------------------*/ | |
405 | |
406 #if defined(HAVE_LONG_DOUBLE) | |
407 static __inline__ complexl_t complex_divl(const complexl_t *x, const complexl_t *y) | |
408 { | |
409 complexl_t z; | |
410 long double f; | |
411 | |
412 f = y->re*y->re + y->im*y->im; | |
413 z.re = ( x->re*y->re + x->im*y->im)/f; | |
414 z.im = (-x->re*y->im + x->im*y->re)/f; | |
415 return z; | |
416 } | |
417 /*- End of function --------------------------------------------------------*/ | |
418 #endif | |
419 | |
420 static __inline__ complexf_t complex_conjf(const complexf_t *x) | |
421 { | |
422 complexf_t z; | |
423 | |
424 z.re = x->re; | |
425 z.im = -x->im; | |
426 return z; | |
427 } | |
428 /*- End of function --------------------------------------------------------*/ | |
429 | |
430 static __inline__ complex_t complex_conj(const complex_t *x) | |
431 { | |
432 complex_t z; | |
433 | |
434 z.re = x->re; | |
435 z.im = -x->im; | |
436 return z; | |
437 } | |
438 /*- End of function --------------------------------------------------------*/ | |
439 | |
440 #if defined(HAVE_LONG_DOUBLE) | |
441 static __inline__ complexl_t complex_conjl(const complexl_t *x) | |
442 { | |
443 complexl_t z; | |
444 | |
445 z.re = x->re; | |
446 z.im = -x->im; | |
447 return z; | |
448 } | |
449 /*- End of function --------------------------------------------------------*/ | |
450 #endif | |
451 | |
452 static __inline__ complexi_t complex_conji(const complexi_t *x) | |
453 { | |
454 complexi_t z; | |
455 | |
456 z.re = x->re; | |
457 z.im = -x->im; | |
458 return z; | |
459 } | |
460 /*- End of function --------------------------------------------------------*/ | |
461 | |
462 static __inline__ complexi16_t complex_conji16(const complexi16_t *x) | |
463 { | |
464 complexi16_t z; | |
465 | |
466 z.re = x->re; | |
467 z.im = -x->im; | |
468 return z; | |
469 } | |
470 /*- End of function --------------------------------------------------------*/ | |
471 | |
472 static __inline__ complexi32_t complex_conji32(const complexi32_t *x) | |
473 { | |
474 complexi32_t z; | |
475 | |
476 z.re = x->re; | |
477 z.im = -x->im; | |
478 return z; | |
479 } | |
480 /*- End of function --------------------------------------------------------*/ | |
481 | |
482 static __inline__ float powerf(const complexf_t *x) | |
483 { | |
484 return x->re*x->re + x->im*x->im; | |
485 } | |
486 /*- End of function --------------------------------------------------------*/ | |
487 | |
488 static __inline__ double power(const complex_t *x) | |
489 { | |
490 return x->re*x->re + x->im*x->im; | |
491 } | |
492 /*- End of function --------------------------------------------------------*/ | |
493 | |
494 #if defined(HAVE_LONG_DOUBLE) | |
495 static __inline__ long double powerl(const complexl_t *x) | |
496 { | |
497 return x->re*x->re + x->im*x->im; | |
498 } | |
499 /*- End of function --------------------------------------------------------*/ | |
500 #endif | |
501 | |
502 #if defined(__cplusplus) | |
503 } | |
504 #endif | |
505 | |
506 #endif | |
507 /*- End of file ------------------------------------------------------------*/ |