complex.h

Go to the documentation of this file.
00001 /*
00002  * SpanDSP - a series of DSP components for telephony
00003  *
00004  * complex.h
00005  *
00006  * Written by Steve Underwood <steveu@coppice.org>
00007  *
00008  * Copyright (C) 2003 Steve Underwood
00009  *
00010  * All rights reserved.
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License version 2, as
00014  * published by the Free Software Foundation.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00024  *
00025  * $Id: complex.h,v 1.10 2007/04/05 19:20:49 steveu Exp $
00026  */
00027 
00028 /*! \file */
00029 
00030 /*! \page complex_page Complex number support
00031 \section complex_page_sec_1 What does it do?
00032 Complex number support is part of the C99 standard. However, support for this
00033 in C compilers is still patchy. A set of complex number feaures is provided as
00034 a "temporary" measure, until native C language complex number support is
00035 widespread.
00036 */
00037 
00038 #if !defined(_SPANDSP_COMPLEX_H_)
00039 #define _SPANDSP_COMPLEX_H_
00040 
00041 /*!
00042     Floating complex type.
00043 */
00044 typedef struct
00045 {
00046     float re;
00047     float im;
00048 } complexf_t;
00049 
00050 /*!
00051     Floating complex type.
00052 */
00053 typedef struct
00054 {
00055     double re;
00056     double im;
00057 } complex_t;
00058 
00059 #if defined(HAVE_LONG_DOUBLE)
00060 /*!
00061     Long double complex type.
00062 */
00063 typedef struct
00064 {
00065     long double re;
00066     long double im;
00067 } complexl_t;
00068 #endif
00069 
00070 /*!
00071     Complex integer type.
00072 */
00073 typedef struct
00074 {
00075     int re;
00076     int im;
00077 } icomplex_t;
00078 
00079 /*!
00080     Complex 16 bit integer type.
00081 */
00082 typedef struct
00083 {
00084     int16_t re;
00085     int16_t im;
00086 } i16complex_t;
00087 
00088 /*!
00089     Complex 32 bit integer type.
00090 */
00091 typedef struct
00092 {
00093     int32_t re;
00094     int32_t im;
00095 } i32complex_t;
00096 
00097 #ifdef __cplusplus
00098 extern "C"
00099 {
00100 #endif
00101 
00102 static __inline__ complexf_t complex_setf(float re, float im)
00103 {
00104     complexf_t z;
00105 
00106     z.re = re;
00107     z.im = im;
00108     return z;
00109 }
00110 /*- End of function --------------------------------------------------------*/
00111 
00112 static __inline__ complex_t complex_set(float re, float im)
00113 {
00114     complex_t z;
00115 
00116     z.re = re;
00117     z.im = im;
00118     return z;
00119 }
00120 /*- End of function --------------------------------------------------------*/
00121 
00122 #if defined(HAVE_LONG_DOUBLE)
00123 static __inline__ complexl_t complex_setl(long double re, long double im)
00124 {
00125     complexl_t z;
00126 
00127     z.re = re;
00128     z.im = im;
00129     return z;
00130 }
00131 /*- End of function --------------------------------------------------------*/
00132 #endif
00133 
00134 static __inline__ icomplex_t icomplex_set(int re, int im)
00135 {
00136     icomplex_t z;
00137 
00138     z.re = re;
00139     z.im = im;
00140     return z;
00141 }
00142 /*- End of function --------------------------------------------------------*/
00143 
00144 static __inline__ complexf_t complex_addf(const complexf_t *x, const complexf_t *y)
00145 {
00146     complexf_t z;
00147 
00148     z.re = x->re + y->re;
00149     z.im = x->im + y->im;
00150     return z;
00151 }
00152 /*- End of function --------------------------------------------------------*/
00153 
00154 static __inline__ complex_t complex_add(const complex_t *x, const complex_t *y)
00155 {
00156     complex_t z;
00157 
00158     z.re = x->re + y->re;
00159     z.im = x->im + y->im;
00160     return z;
00161 }
00162 /*- End of function --------------------------------------------------------*/
00163 
00164 #if defined(HAVE_LONG_DOUBLE)
00165 static __inline__ complexl_t complex_addl(const complexl_t *x, const complexl_t *y)
00166 {
00167     complexl_t z;
00168 
00169     z.re = x->re + y->re;
00170     z.im = x->im + y->im;
00171     return z;
00172 }
00173 /*- End of function --------------------------------------------------------*/
00174 #endif
00175 
00176 static __inline__ icomplex_t icomplex_add(const icomplex_t *x, const icomplex_t *y)
00177 {
00178     icomplex_t z;
00179 
00180     z.re = x->re + y->re;
00181     z.im = x->im + y->im;
00182     return z;
00183 }
00184 /*- End of function --------------------------------------------------------*/
00185 
00186 static __inline__ complexf_t complex_subf(const complexf_t *x, const complexf_t *y)
00187 {
00188     complexf_t z;
00189 
00190     z.re = x->re - y->re;
00191     z.im = x->im - y->im;
00192     return z;
00193 }
00194 /*- End of function --------------------------------------------------------*/
00195 
00196 static __inline__ complex_t complex_sub(const complex_t *x, const complex_t *y)
00197 {
00198     complex_t z;
00199 
00200     z.re = x->re - y->re;
00201     z.im = x->im - y->im;
00202     return z;
00203 }
00204 /*- End of function --------------------------------------------------------*/
00205 
00206 #if defined(HAVE_LONG_DOUBLE)
00207 static __inline__ complexl_t complex_subl(const complexl_t *x, const complexl_t *y)
00208 {
00209     complexl_t z;
00210 
00211     z.re = x->re - y->re;
00212     z.im = x->im - y->im;
00213     return z;
00214 }
00215 /*- End of function --------------------------------------------------------*/
00216 #endif
00217 
00218 static __inline__ icomplex_t icomplex_sub(const icomplex_t *x, const icomplex_t *y)
00219 {
00220     icomplex_t z;
00221 
00222     z.re = x->re - y->re;
00223     z.im = x->im - y->im;
00224     return z;
00225 }
00226 /*- End of function --------------------------------------------------------*/
00227 
00228 static __inline__ complexf_t complex_mulf(const complexf_t *x, const complexf_t *y)
00229 {
00230     complexf_t z;
00231 
00232     z.re = x->re*y->re - x->im*y->im;
00233     z.im = x->re*y->im + x->im*y->re;
00234     return z;
00235 }
00236 /*- End of function --------------------------------------------------------*/
00237 
00238 static __inline__ complex_t complex_mul(const complex_t *x, const complex_t *y)
00239 {
00240     complex_t z;
00241 
00242     z.re = x->re*y->re - x->im*y->im;
00243     z.im = x->re*y->im + x->im*y->re;
00244     return z;
00245 }
00246 /*- End of function --------------------------------------------------------*/
00247 
00248 #if defined(HAVE_LONG_DOUBLE)
00249 static __inline__ complexl_t complex_mull(const complexl_t *x, const complexl_t *y)
00250 {
00251     complexl_t z;
00252 
00253     z.re = x->re*y->re - x->im*y->im;
00254     z.im = x->re*y->im + x->im*y->re;
00255     return z;
00256 }
00257 /*- End of function --------------------------------------------------------*/
00258 #endif
00259 
00260 static __inline__ complexf_t complex_divf(const complexf_t *x, const complexf_t *y)
00261 {
00262     complexf_t z;
00263     float f;
00264     
00265     f = y->re*y->re + y->im*y->im;
00266     z.re = ( x->re*y->re + x->im*y->im)/f;
00267     z.im = (-x->re*y->im + x->im*y->re)/f;
00268     return z;
00269 }
00270 /*- End of function --------------------------------------------------------*/
00271 
00272 static __inline__ complex_t complex_div(const complex_t *x, const complex_t *y)
00273 {
00274     complex_t z;
00275     double f;
00276     
00277     f = y->re*y->re + y->im*y->im;
00278     z.re = ( x->re*y->re + x->im*y->im)/f;
00279     z.im = (-x->re*y->im + x->im*y->re)/f;
00280     return z;
00281 }
00282 /*- End of function --------------------------------------------------------*/
00283 
00284 #if defined(HAVE_LONG_DOUBLE)
00285 static __inline__ complexl_t complex_divl(const complexl_t *x, const complexl_t *y)
00286 {
00287     complexl_t z;
00288     long double f;
00289     
00290     f = y->re*y->re + y->im*y->im;
00291     z.re = ( x->re*y->re + x->im*y->im)/f;
00292     z.im = (-x->re*y->im + x->im*y->re)/f;
00293     return z;
00294 }
00295 /*- End of function --------------------------------------------------------*/
00296 #endif
00297 
00298 static __inline__ complexf_t complex_conjf(const complexf_t *x)
00299 {
00300     complexf_t z;
00301 
00302     z.re = x->re;
00303     z.im = -x->im;
00304     return z;
00305 }
00306 /*- End of function --------------------------------------------------------*/
00307 
00308 static __inline__ complex_t complex_conj(const complex_t *x)
00309 {
00310     complex_t z;
00311 
00312     z.re = x->re;
00313     z.im = -x->im;
00314     return z;
00315 }
00316 /*- End of function --------------------------------------------------------*/
00317 
00318 #if defined(HAVE_LONG_DOUBLE)
00319 static __inline__ complexl_t complex_conjl(const complexl_t *x)
00320 {
00321     complexl_t z;
00322 
00323     z.re = x->re;
00324     z.im = -x->im;
00325     return z;
00326 }
00327 /*- End of function --------------------------------------------------------*/
00328 #endif
00329 
00330 static __inline__ icomplex_t icomplex_conj(const icomplex_t *x)
00331 {
00332     icomplex_t z;
00333 
00334     z.re = x->re;
00335     z.im = -x->im;
00336     return z;
00337 }
00338 /*- End of function --------------------------------------------------------*/
00339 
00340 static __inline__ float powerf(const complexf_t *x)
00341 {
00342     return x->re*x->re + x->im*x->im;
00343 }
00344 /*- End of function --------------------------------------------------------*/
00345 
00346 static __inline__ double power(const complex_t *x)
00347 {
00348     return x->re*x->re + x->im*x->im;
00349 }
00350 /*- End of function --------------------------------------------------------*/
00351 
00352 #if defined(HAVE_LONG_DOUBLE)
00353 static __inline__ long double powerl(const complexl_t *x)
00354 {
00355     return x->re*x->re + x->im*x->im;
00356 }
00357 /*- End of function --------------------------------------------------------*/
00358 #endif
00359 
00360 #ifdef __cplusplus
00361 }
00362 #endif
00363 
00364 #endif
00365 /*- End of file ------------------------------------------------------------*/

Generated on Fri Apr 13 13:26:37 2007 for libspandsp by  doxygen 1.4.6