spandsp: complex.h Source File

spandsp 0.0.6

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