hello i would like to ask how to generate the complex signal f(t)=exp^(j*omega*t), j=sqrt(-1), in C++. thanks very much
Printable View
hello i would like to ask how to generate the complex signal f(t)=exp^(j*omega*t), j=sqrt(-1), in C++. thanks very much
C++ has a standard library to handle complex numbers,
https://en.cppreference.com/w/cpp/numeric/complex
Say you have,
f(t) = exp^(j*omega*t)
Note that the imaginary unit j plays a role in formula manipulations only. It is never calculated as sqrt(-1). This is because that number does not exist for real. It is imaginary. So in numeric calculations, a complex number is represented by a tuple of two real numbers, meaning a + j*b is represented by (a,b). Using (a.b) notation the f(t) formula becomes,
f(t) = exp^((0, omega*t))
Now, according to Euler's formula,
https://en.wikipedia.org/wiki/Euler%27s_formula
the result of the f(t) formula should equal the complex number (cos(omega*t), sin(omega*t)). That is, this equality is supposed to hold,
(cos(omega*t), sin(omega*t)) == exp^((0, omega*t))
The omega*t number will be interpreted as an angle by the sin and cos functions. Let us set it to 135 degrees (3*PI/4 radians) and check it out in a C++ program,
The result becomes (-0.707107, 0707107) in both cases as the Euler formula predicts. Both ways will give you f(t).Code:#include <numbers>
#include <complex>
#include <iostream>
void test() {
using Complex = std::complex<double>;
const double PI = std::numbers::pi; // since C++ 20
const double angle_135 = 3.0*PI/4.0;
Complex f = std::exp(Complex(0.0, angle_135));
std::cout << f << std::endl;
std::cout << std::cos(angle_135) << "," << std::sin(angle_135) << std::endl;
}