Edinburgh Speech Tools 2.4-release
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>
45using 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
54cartesian real and imaginary parts, but these can be read as polar
55coordinates using the {\tt mag()} and {\tt ang()} functions. Addition,
56subtraction, multiplication and division are supported. */
57
59 private:
60 double r;
61 double i;
62public:
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
91friend EST_Complex operator + (const EST_Complex& z1, const EST_Complex &z2);
92friend EST_Complex operator + (const EST_Complex &z, float x);
93friend EST_Complex operator + (float x, const EST_Complex &z);
94friend EST_Complex operator - (const EST_Complex &z1, const EST_Complex &z2);
95friend EST_Complex operator - (const EST_Complex &z, float x);
96friend EST_Complex operator - (float x, const EST_Complex &z);
97friend EST_Complex operator * (const EST_Complex &z1, const EST_Complex &z2);
98friend EST_Complex operator * (const EST_Complex &z, float x);
99friend EST_Complex operator * (float x, const EST_Complex &z);
100friend EST_Complex operator / (const EST_Complex &z1, const EST_Complex &z2);
101friend EST_Complex operator / (const EST_Complex &z, float x);
102friend EST_Complex operator / (float x, const EST_Complex &z);
103
104
105friend ostream& operator<< (ostream& s, const EST_Complex& a)
106{ s << a.r << " " << a.i; return s;}
107};
108
109
110
111#endif
EST_Complex()
default constructor, initialises values to 0.0
Definition: EST_Complex.h:66
double ang(int degrees=0) const
Polar angle, read only.
Definition: EST_Complex.h:77
double & imag()
The imaginary part - can be used for reading or writing.
Definition: EST_Complex.h:89
EST_Complex(double real, double imag)
Constructor initialising real and imaginary parts.
Definition: EST_Complex.h:68
double mag() const
Polar magnitude, read only.
Definition: EST_Complex.h:73
double & real()
The real part - can be used for reading or writing.
Definition: EST_Complex.h:87