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