5
|
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 General Public License version 2, as
|
|
14 * 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 General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * 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.8 2006/10/24 13:45:28 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(_COMPLEX_H_)
|
|
39 #define _COMPLEX_H_
|
|
40
|
|
41 /*!
|
|
42 Floating complex type.
|
|
43 */
|
|
44 typedef struct
|
|
45 {
|
|
46 float re;
|
|
47 float im;
|
|
48 } complexf_t;
|
|
49
|
|
50 /*!
|
|
51 Floating complex type.
|
|
52 */
|
|
53 typedef struct
|
|
54 {
|
|
55 double re;
|
|
56 double im;
|
|
57 } complex_t;
|
|
58
|
|
59 #if defined(HAVE_LONG_DOUBLE)
|
|
60 /*!
|
|
61 Long double complex type.
|
|
62 */
|
|
63 typedef struct
|
|
64 {
|
|
65 long double re;
|
|
66 long double im;
|
|
67 } complexl_t;
|
|
68 #endif
|
|
69
|
|
70 /*!
|
|
71 Complex integer type.
|
|
72 */
|
|
73 typedef struct
|
|
74 {
|
|
75 int re;
|
|
76 int im;
|
|
77 } icomplex_t;
|
|
78
|
|
79 /*!
|
|
80 Complex 16 bit integer type.
|
|
81 */
|
|
82 typedef struct
|
|
83 {
|
|
84 int16_t re;
|
|
85 int16_t im;
|
|
86 } i16complex_t;
|
|
87
|
|
88 /*!
|
|
89 Complex 32 bit integer type.
|
|
90 */
|
|
91 typedef struct
|
|
92 {
|
|
93 int32_t re;
|
|
94 int32_t im;
|
|
95 } i32complex_t;
|
|
96
|
|
97 #ifdef __cplusplus
|
|
98 extern "C" {
|
|
99 #endif
|
|
100
|
|
101 static __inline__ complexf_t complex_setf(float re, float im)
|
|
102 {
|
|
103 complexf_t z;
|
|
104
|
|
105 z.re = re;
|
|
106 z.im = im;
|
|
107 return z;
|
|
108 }
|
|
109 /*- End of function --------------------------------------------------------*/
|
|
110
|
|
111 static __inline__ complex_t complex_set(float re, float im)
|
|
112 {
|
|
113 complex_t z;
|
|
114
|
|
115 z.re = re;
|
|
116 z.im = im;
|
|
117 return z;
|
|
118 }
|
|
119 /*- End of function --------------------------------------------------------*/
|
|
120
|
|
121 #if defined(HAVE_LONG_DOUBLE)
|
|
122 static __inline__ complexl_t complex_setl(long double re, long double im)
|
|
123 {
|
|
124 complexl_t z;
|
|
125
|
|
126 z.re = re;
|
|
127 z.im = im;
|
|
128 return z;
|
|
129 }
|
|
130 /*- End of function --------------------------------------------------------*/
|
|
131 #endif
|
|
132
|
|
133 static __inline__ icomplex_t icomplex_set(int re, int im)
|
|
134 {
|
|
135 icomplex_t z;
|
|
136
|
|
137 z.re = re;
|
|
138 z.im = im;
|
|
139 return z;
|
|
140 }
|
|
141 /*- End of function --------------------------------------------------------*/
|
|
142
|
|
143 static __inline__ complexf_t complex_addf(const complexf_t *x, const complexf_t *y)
|
|
144 {
|
|
145 complexf_t z;
|
|
146
|
|
147 z.re = x->re + y->re;
|
|
148 z.im = x->im + y->im;
|
|
149 return z;
|
|
150 }
|
|
151 /*- End of function --------------------------------------------------------*/
|
|
152
|
|
153 static __inline__ complex_t complex_add(const complex_t *x, const complex_t *y)
|
|
154 {
|
|
155 complex_t z;
|
|
156
|
|
157 z.re = x->re + y->re;
|
|
158 z.im = x->im + y->im;
|
|
159 return z;
|
|
160 }
|
|
161 /*- End of function --------------------------------------------------------*/
|
|
162
|
|
163 #if defined(HAVE_LONG_DOUBLE)
|
|
164 static __inline__ complexl_t complex_addl(const complexl_t *x, const complexl_t *y)
|
|
165 {
|
|
166 complexl_t z;
|
|
167
|
|
168 z.re = x->re + y->re;
|
|
169 z.im = x->im + y->im;
|
|
170 return z;
|
|
171 }
|
|
172 /*- End of function --------------------------------------------------------*/
|
|
173 #endif
|
|
174
|
|
175 static __inline__ icomplex_t icomplex_add(const icomplex_t *x, const icomplex_t *y)
|
|
176 {
|
|
177 icomplex_t z;
|
|
178
|
|
179 z.re = x->re + y->re;
|
|
180 z.im = x->im + y->im;
|
|
181 return z;
|
|
182 }
|
|
183 /*- End of function --------------------------------------------------------*/
|
|
184
|
|
185 static __inline__ complexf_t complex_subf(const complexf_t *x, const complexf_t *y)
|
|
186 {
|
|
187 complexf_t z;
|
|
188
|
|
189 z.re = x->re - y->re;
|
|
190 z.im = x->im - y->im;
|
|
191 return z;
|
|
192 }
|
|
193 /*- End of function --------------------------------------------------------*/
|
|
194
|
|
195 static __inline__ complex_t complex_sub(const complex_t *x, const complex_t *y)
|
|
196 {
|
|
197 complex_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
|
|
205 #if defined(HAVE_LONG_DOUBLE)
|
|
206 static __inline__ complexl_t complex_subl(const complexl_t *x, const complexl_t *y)
|
|
207 {
|
|
208 complexl_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 #endif
|
|
216
|
|
217 static __inline__ icomplex_t icomplex_sub(const icomplex_t *x, const icomplex_t *y)
|
|
218 {
|
|
219 icomplex_t z;
|
|
220
|
|
221 z.re = x->re - y->re;
|
|
222 z.im = x->im - y->im;
|
|
223 return z;
|
|
224 }
|
|
225 /*- End of function --------------------------------------------------------*/
|
|
226
|
|
227 static __inline__ complexf_t complex_mulf(const complexf_t *x, const complexf_t *y)
|
|
228 {
|
|
229 complexf_t z;
|
|
230
|
|
231 z.re = x->re*y->re - x->im*y->im;
|
|
232 z.im = x->re*y->im + x->im*y->re;
|
|
233 return z;
|
|
234 }
|
|
235 /*- End of function --------------------------------------------------------*/
|
|
236
|
|
237 static __inline__ complex_t complex_mul(const complex_t *x, const complex_t *y)
|
|
238 {
|
|
239 complex_t z;
|
|
240
|
|
241 z.re = x->re*y->re - x->im*y->im;
|
|
242 z.im = x->re*y->im + x->im*y->re;
|
|
243 return z;
|
|
244 }
|
|
245 /*- End of function --------------------------------------------------------*/
|
|
246
|
|
247 #if defined(HAVE_LONG_DOUBLE)
|
|
248 static __inline__ complexl_t complex_mull(const complexl_t *x, const complexl_t *y)
|
|
249 {
|
|
250 complexl_t z;
|
|
251
|
|
252 z.re = x->re*y->re - x->im*y->im;
|
|
253 z.im = x->re*y->im + x->im*y->re;
|
|
254 return z;
|
|
255 }
|
|
256 /*- End of function --------------------------------------------------------*/
|
|
257 #endif
|
|
258
|
|
259 static __inline__ complexf_t complex_divf(const complexf_t *x, const complexf_t *y)
|
|
260 {
|
|
261 complexf_t z;
|
|
262 float f;
|
|
263
|
|
264 f = y->re*y->re + y->im*y->im;
|
|
265 z.re = ( x->re*y->re + x->im*y->im)/f;
|
|
266 z.im = (-x->re*y->im + x->im*y->re)/f;
|
|
267 return z;
|
|
268 }
|
|
269 /*- End of function --------------------------------------------------------*/
|
|
270
|
|
271 static __inline__ complex_t complex_div(const complex_t *x, const complex_t *y)
|
|
272 {
|
|
273 complex_t z;
|
|
274 double f;
|
|
275
|
|
276 f = y->re*y->re + y->im*y->im;
|
|
277 z.re = ( x->re*y->re + x->im*y->im)/f;
|
|
278 z.im = (-x->re*y->im + x->im*y->re)/f;
|
|
279 return z;
|
|
280 }
|
|
281 /*- End of function --------------------------------------------------------*/
|
|
282
|
|
283 #if defined(HAVE_LONG_DOUBLE)
|
|
284 static __inline__ complexl_t complex_divl(const complexl_t *x, const complexl_t *y)
|
|
285 {
|
|
286 complexl_t z;
|
|
287 long double f;
|
|
288
|
|
289 f = y->re*y->re + y->im*y->im;
|
|
290 z.re = ( x->re*y->re + x->im*y->im)/f;
|
|
291 z.im = (-x->re*y->im + x->im*y->re)/f;
|
|
292 return z;
|
|
293 }
|
|
294 /*- End of function --------------------------------------------------------*/
|
|
295 #endif
|
|
296
|
|
297 static __inline__ complexf_t complex_conjf(const complexf_t *x)
|
|
298 {
|
|
299 complexf_t z;
|
|
300
|
|
301 z.re = x->re;
|
|
302 z.im = -x->im;
|
|
303 return z;
|
|
304 }
|
|
305 /*- End of function --------------------------------------------------------*/
|
|
306
|
|
307 static __inline__ complex_t complex_conj(const complex_t *x)
|
|
308 {
|
|
309 complex_t z;
|
|
310
|
|
311 z.re = x->re;
|
|
312 z.im = -x->im;
|
|
313 return z;
|
|
314 }
|
|
315 /*- End of function --------------------------------------------------------*/
|
|
316
|
|
317 #if defined(HAVE_LONG_DOUBLE)
|
|
318 static __inline__ complexl_t complex_conjl(const complexl_t *x)
|
|
319 {
|
|
320 complexl_t z;
|
|
321
|
|
322 z.re = x->re;
|
|
323 z.im = -x->im;
|
|
324 return z;
|
|
325 }
|
|
326 /*- End of function --------------------------------------------------------*/
|
|
327 #endif
|
|
328
|
|
329 static __inline__ icomplex_t icomplex_conj(const icomplex_t *x)
|
|
330 {
|
|
331 icomplex_t z;
|
|
332
|
|
333 z.re = x->re;
|
|
334 z.im = -x->im;
|
|
335 return z;
|
|
336 }
|
|
337 /*- End of function --------------------------------------------------------*/
|
|
338
|
|
339 static __inline__ float powerf(const complexf_t *x)
|
|
340 {
|
|
341 return x->re*x->re + x->im*x->im;
|
|
342 }
|
|
343 /*- End of function --------------------------------------------------------*/
|
|
344
|
|
345 static __inline__ double power(const complex_t *x)
|
|
346 {
|
|
347 return x->re*x->re + x->im*x->im;
|
|
348 }
|
|
349 /*- End of function --------------------------------------------------------*/
|
|
350
|
|
351 #if defined(HAVE_LONG_DOUBLE)
|
|
352 static __inline__ long double powerl(const complexl_t *x)
|
|
353 {
|
|
354 return x->re*x->re + x->im*x->im;
|
|
355 }
|
|
356 /*- End of function --------------------------------------------------------*/
|
|
357 #endif
|
|
358
|
|
359 #ifdef __cplusplus
|
|
360 }
|
|
361 #endif
|
|
362
|
|
363 #endif
|
|
364 /*- End of file ------------------------------------------------------------*/
|