CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member Join Date
Jan 2022
Posts
1

## complex signal

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  Reply With Quote

2. Member +   Join Date
Feb 2017
Posts
655

## Re: complex signal Originally Posted by sddfds 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,

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;
}```
The result becomes (-0.707107, 0707107) in both cases as the Euler formula predicts. Both ways will give you f(t).
Last edited by wolle; January 9th, 2022 at 02:37 AM.  Reply With Quote

c++ #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•