Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 /** \class EST_Complex
54  \brief A class for complex numbers.
55 
56 The class stores the values as
57 cartesian real and imaginary parts, but these can be read as polar
58 coordinates using the EST_Complex::mag() and EST_Complex::ang() methods. Addition,
59 subtraction, multiplication and division are supported. */
60 class EST_Complex {
61  private:
62  double r;
63  double i;
64 public:
65  /**@name Constructor functions */
66  //@{
67  /// default constructor, initialises values to 0.0
68  EST_Complex() {r = 0.0; i = 0.0;}
69  /// Constructor initialising real and imaginary parts
70  EST_Complex(double real, double imag)
71  { r = real; i = imag;}
72  //@}
73 
74  /// Polar magnitude, read only
75  double mag() const
76  { return(sqrt(r*r+i*i)); }
77 
78  /// Polar angle, read only
79  double ang(int degrees=0) const {
80  double a;
81  if ( r == 0. && i == 0. ) a = 0.0;
82  else if ( r >= 0. ) a = atan(i/r);
83  else if ( i >= 0. ) a = atan(i/r) + PI;
84  else a = atan(i/r) - PI;
85  return (degrees == 1) ? (a * 180 / PI) : a;
86  }
87 
88  /// The real part - can be used for reading or writing
89  double &real() {return r;}
90  /// The imaginary part - can be used for reading or writing
91  double &imag() {return i;}
92 
93 friend EST_Complex operator + (const EST_Complex& z1, const EST_Complex &z2);
94 friend EST_Complex operator + (const EST_Complex &z, float x);
95 friend EST_Complex operator + (float x, const EST_Complex &z);
96 friend EST_Complex operator - (const EST_Complex &z1, const EST_Complex &z2);
97 friend EST_Complex operator - (const EST_Complex &z, float x);
98 friend EST_Complex operator - (float x, const EST_Complex &z);
99 friend EST_Complex operator * (const EST_Complex &z1, const EST_Complex &z2);
100 friend EST_Complex operator * (const EST_Complex &z, float x);
101 friend EST_Complex operator * (float x, const EST_Complex &z);
102 friend EST_Complex operator / (const EST_Complex &z1, const EST_Complex &z2);
103 friend EST_Complex operator / (const EST_Complex &z, float x);
104 friend EST_Complex operator / (float x, const EST_Complex &z);
105 
106 
107 friend ostream& operator<< (ostream& s, const EST_Complex& a)
108 { s << a.r << " " << a.i; return s;}
109 };
110 
111 
112 
113 #endif
A class for complex numbers.
Definition: EST_Complex.h:60
double & real()
The real part - can be used for reading or writing.
Definition: EST_Complex.h:89
double ang(int degrees=0) const
Polar angle, read only.
Definition: EST_Complex.h:79
EST_Complex(double real, double imag)
Constructor initialising real and imaginary parts.
Definition: EST_Complex.h:70
double & imag()
The imaginary part - can be used for reading or writing.
Definition: EST_Complex.h:91
double mag() const
Polar magnitude, read only.
Definition: EST_Complex.h:75
EST_Complex()
default constructor, initialises values to 0.0
Definition: EST_Complex.h:68