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 as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00025  *
00026  * $Id: complex.h,v 1.3 2005/11/23 17:09:47 steveu Exp $
00027  */
00028 
00029 /*! \file */
00030 
00031 /*! \page complex_page Complex number support
00032 \section complex_page_sec_1 What does it do?
00033 Complex number support is part of the C99 standard. However, support for this
00034 in C compilers is still patchy. A set of complex number feaures is provided as
00035 a "temporary" measure, until native C language complex number support is
00036 widespread.
00037 */
00038 
00039 #if !defined(_COMPLEX_H_)
00040 #define _COMPLEX_H_
00041 
00042 /*!
00043     Complex type.
00044 */
00045 typedef struct
00046 {
00047     float re;
00048     float im;
00049 } complex_t;
00050 
00051 /*!
00052     Complex integer type.
00053 */
00054 typedef struct
00055 {
00056     int re;
00057     int im;
00058 } icomplex_t;
00059 
00060 /*!
00061     Complex 16 bit integer type.
00062 */
00063 typedef struct
00064 {
00065     int16_t re;
00066     int16_t im;
00067 } i16complex_t;
00068 
00069 /*!
00070     Complex 32 bit integer type.
00071 */
00072 typedef struct
00073 {
00074     int32_t re;
00075     int32_t im;
00076 } i32complex_t;
00077 
00078 #ifdef __cplusplus
00079 extern "C" {
00080 #endif
00081 
00082 static inline complex_t complex_set(float re, float im)
00083 {
00084     complex_t z;
00085 
00086     z.re = re;
00087     z.im = im;
00088     return z;
00089 }
00090 /*- End of function --------------------------------------------------------*/
00091 
00092 static inline icomplex_t icomplex_set(int re, int im)
00093 {
00094     icomplex_t z;
00095 
00096     z.re = re;
00097     z.im = im;
00098     return z;
00099 }
00100 /*- End of function --------------------------------------------------------*/
00101 
00102 static inline complex_t complex_add(const complex_t *x, const complex_t *y)
00103 {
00104     complex_t z;
00105 
00106     z.re = x->re + y->re;
00107     z.im = x->im + y->im;
00108     return z;
00109 }
00110 /*- End of function --------------------------------------------------------*/
00111 
00112 static inline icomplex_t icomplex_add(const icomplex_t *x, const icomplex_t *y)
00113 {
00114     icomplex_t z;
00115 
00116     z.re = x->re + y->re;
00117     z.im = x->im + y->im;
00118     return z;
00119 }
00120 /*- End of function --------------------------------------------------------*/
00121 
00122 static inline complex_t complex_sub(const complex_t *x, const complex_t *y)
00123 {
00124     complex_t z;
00125 
00126     z.re = x->re - y->re;
00127     z.im = x->im - y->im;
00128     return z;
00129 }
00130 /*- End of function --------------------------------------------------------*/
00131 
00132 static inline icomplex_t icomplex_sub(const icomplex_t *x, const icomplex_t *y)
00133 {
00134     icomplex_t z;
00135 
00136     z.re = x->re - y->re;
00137     z.im = x->im - y->im;
00138     return z;
00139 }
00140 /*- End of function --------------------------------------------------------*/
00141 
00142 static inline complex_t complex_mul(const complex_t *x, const complex_t *y)
00143 {
00144     complex_t z;
00145 
00146     z.re = x->re*y->re - x->im*y->im;
00147     z.im = x->re*y->im + x->im*y->re;
00148     return z;
00149 }
00150 /*- End of function --------------------------------------------------------*/
00151 
00152 static inline complex_t complex_div(const complex_t *x, const complex_t *y)
00153 {
00154     complex_t z;
00155     float f;
00156     
00157     f = y->re*y->re + y->im*y->im;
00158     z.re = ( x->re*y->re + x->im*y->im)/f;
00159     z.im = (-x->re*y->im + x->im*y->re)/f;
00160     return z;
00161 }
00162 /*- End of function --------------------------------------------------------*/
00163 
00164 static inline complex_t complex_conj(const complex_t *x)
00165 {
00166     complex_t z;
00167 
00168     z.re = x->re;
00169     z.im = -x->im;
00170     return z;
00171 }
00172 /*- End of function --------------------------------------------------------*/
00173 
00174 static inline icomplex_t icomplex_conj(const icomplex_t *x)
00175 {
00176     icomplex_t z;
00177 
00178     z.re = x->re;
00179     z.im = -x->im;
00180     return z;
00181 }
00182 /*- End of function --------------------------------------------------------*/
00183 
00184 static inline float power(const complex_t *x)
00185 {
00186     return x->re*x->re + x->im*x->im;
00187 }
00188 /*- End of function --------------------------------------------------------*/
00189 
00190 #ifdef __cplusplus
00191 }
00192 #endif
00193 
00194 #endif
00195 /*- End of file ------------------------------------------------------------*/

Generated on Fri Nov 10 09:40:23 2006 for libspandsp by  doxygen 1.5.1