HephAudio v3.0.6
A cross-platform C++ library for recording, playing, and processing audio on Windows, Android, Linux, iOS, and macOS.
Loading...
Searching...
No Matches
Complex.h
Go to the documentation of this file.
1#pragma once
2#include "HephShared.h"
3
6namespace Heph
7{
13 {
18 double real;
19
24 double imag;
25
27 constexpr Complex() : Complex(0, 0) {}
28
35 constexpr Complex(double real, double imag) : real(real), imag(imag) {}
36
37 constexpr Complex operator-() const
38 {
39 return Complex(-this->real, -this->imag);
40 }
41
42 constexpr Complex operator+(double rhs) const
43 {
44 return Complex(this->real + rhs, this->imag);
45 }
46
47 constexpr Complex operator+(const Complex& rhs) const
48 {
49 return Complex(this->real + rhs.real, this->imag + rhs.imag);
50 }
51
52 inline constexpr Complex& operator+=(double rhs)
53 {
54 this->real += rhs;
55 return *this;
56 }
57
58 inline constexpr Complex& operator+=(const Complex& rhs)
59 {
60 this->real += rhs.real;
61 this->imag += rhs.imag;
62 return *this;
63 }
64
65 constexpr Complex operator-(double rhs) const
66 {
67 return Complex(this->real - rhs, this->imag);
68 }
69
70 constexpr Complex operator-(const Complex& rhs) const
71 {
72 return Complex(this->real - rhs.real, this->imag - rhs.imag);
73 }
74
75 inline constexpr Complex& operator-=(double rhs)
76 {
77 this->real -= rhs;
78 return *this;
79 }
80
81 inline constexpr Complex& operator-=(const Complex& rhs)
82 {
83 this->real -= rhs.real;
84 this->imag -= rhs.imag;
85 return *this;
86 }
87
88 constexpr Complex operator*(double rhs) const
89 {
90 return Complex(this->real * rhs, this->imag * rhs);
91 }
92
93 constexpr Complex operator*(const Complex& rhs) const
94 {
95 return Complex(this->real * rhs.real - this->imag * rhs.imag, this->imag * rhs.real + this->real * rhs.imag);
96 }
97
98 inline constexpr Complex& operator*=(double rhs)
99 {
100 this->real *= rhs;
101 this->imag *= rhs;
102 return *this;
103 }
104
105 inline constexpr Complex& operator*=(const Complex& rhs)
106 {
107 const double newReal = this->real * rhs.real - this->imag * rhs.imag;
108 this->imag = this->imag * rhs.real + this->real * rhs.imag;
109 this->real = newReal;
110 return *this;
111 }
112
113 constexpr Complex operator/(double rhs) const
114 {
115 return Complex(this->real / rhs, this->imag / rhs);
116 }
117
118 constexpr Complex operator/(const Complex& rhs) const
119 {
120 const double denomiter = rhs.real * rhs.real + rhs.imag * rhs.imag;
121 return Complex((this->real * rhs.real + this->imag * rhs.imag) / denomiter,
122 (this->imag * rhs.real - this->real * rhs.imag) / denomiter);
123 }
124
125 inline constexpr Complex& operator/=(double rhs)
126 {
127 this->real /= rhs;
128 this->imag /= rhs;
129 return *this;
130 }
131
132 inline constexpr Complex& operator/=(const Complex& rhs)
133 {
134 const double denomiter = rhs.real * rhs.real + rhs.imag * rhs.imag;
135 const double newReal = (this->real * rhs.real + this->imag * rhs.imag) / denomiter;
136 this->imag = (this->imag * rhs.real - this->real * rhs.imag) / denomiter;
137 this->real = newReal;
138 return *this;
139 }
140
141 constexpr bool operator==(const Complex& rhs) const
142 {
143 return this->real == rhs.real && this->imag == rhs.imag;
144 }
145
146 constexpr bool operator!=(const Complex& rhs) const
147 {
148 return this->real != rhs.real || this->imag != rhs.imag;
149 }
150
155 constexpr Complex Conjugate() const
156 {
157 return Complex(this->real, -this->imag);
158 }
159
164 constexpr double MagnitudeSquared() const
165 {
166 return this->real * this->real + this->imag * this->imag;
167 }
168
173 double Magnitude() const;
174
179 double Phase() const;
180 };
181}
182
183inline constexpr Heph::Complex operator+(double lhs, const Heph::Complex& rhs)
184{
185 return rhs + lhs;
186}
187
188inline constexpr Heph::Complex operator-(double lhs, const Heph::Complex& rhs)
189{
190 return Heph::Complex(lhs - rhs.real, -rhs.imag);
191}
192
193inline constexpr Heph::Complex operator*(double lhs, const Heph::Complex& rhs)
194{
195 return rhs * lhs;
196}
197
198inline constexpr Heph::Complex operator/(double lhs, const Heph::Complex& rhs)
199{
200 return Heph::Complex(lhs, 0) / rhs;
201}
#define HEPH_API
Definition HephShared.h:132
struct for representing complex numbers.
Definition Complex.h:13
constexpr Complex Conjugate() const
Definition Complex.h:155
constexpr double MagnitudeSquared() const
Definition Complex.h:164
constexpr Complex(double real, double imag)
Definition Complex.h:35
double Magnitude() const
double imag
Definition Complex.h:24
constexpr Complex()
Definition Complex.h:27
double real
Definition Complex.h:18
double Phase() const