Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_Complex.h
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1997 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Paul Taylor (pault@cstr.ed.ac.uk) */
34 /* Date : December 1997 */
35 /*-----------------------------------------------------------------------*/
36 /* */
37 /* */
38 /*************************************************************************/
39 
40 #ifndef __EST_COMPLEX_H__
41 #define __EST_COMPLEX_H__
42 
43 #include <iostream>
44 #include <cmath>
45 using namespace std;
46 
47 
48 #ifndef PI
49 #define PI 3.14159265358979323846
50 #endif
51 
52 
53 /** A class for complex numbers. The class stores the values as
54 cartesian real and imaginary parts, but these can be read as polar
55 coordinates using the {\tt mag()} and {\tt ang()} functions. Addition,
56 subtraction, multiplication and division are supported. */
57 
58 class EST_Complex {
59  private:
60  double r;
61  double i;
62 public:
63  /**@name Constructor functions */
64  //@{
65  /// default constructor, initialises values to 0.0
66  EST_Complex() {r = 0.0; i = 0.0;}
67  /// Constructor initialising real and imaginary parts
68  EST_Complex(double real, double imag)
69  { r = real; i = imag;}
70  //@}
71 
72  /// Polar magnitude, read only
73  double mag() const
74  { return(sqrt(r*r+i*i)); }
75 
76  /// Polar angle, read only
77  double ang(int degrees=0) const {
78  double a;
79  if ( r == 0. && i == 0. ) a = 0.0;
80  else if ( r >= 0. ) a = atan(i/r);
81  else if ( i >= 0. ) a = atan(i/r) + PI;
82  else a = atan(i/r) - PI;
83  return (degrees == 1) ? (a * 180 / PI) : a;
84  }
85 
86  /// The real part - can be used for reading or writing
87  double &real() {return r;}
88  /// The imaginary part - can be used for reading or writing
89  double &imag() {return i;}
90 
91 friend EST_Complex operator + (const EST_Complex& z1, const EST_Complex &z2);
92 friend EST_Complex operator + (const EST_Complex &z, float x);
93 friend EST_Complex operator + (float x, const EST_Complex &z);
94 friend EST_Complex operator - (const EST_Complex &z1, const EST_Complex &z2);
95 friend EST_Complex operator - (const EST_Complex &z, float x);
96 friend EST_Complex operator - (float x, const EST_Complex &z);
97 friend EST_Complex operator * (const EST_Complex &z1, const EST_Complex &z2);
98 friend EST_Complex operator * (const EST_Complex &z, float x);
99 friend EST_Complex operator * (float x, const EST_Complex &z);
100 friend EST_Complex operator / (const EST_Complex &z1, const EST_Complex &z2);
101 friend EST_Complex operator / (const EST_Complex &z, float x);
102 friend EST_Complex operator / (float x, const EST_Complex &z);
103 
104 
105 friend ostream& operator<< (ostream& s, const EST_Complex& a)
106 { s << a.r << " " << a.i; return s;}
107 };
108 
109 
110 
111 #endif