complex signal

Jan 5, 2022 at 6:59pm
Something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Example program
#include <iostream>
#include <complex>

const double Pi = 3.14159265358979323846;

std::complex<double> f(double t)
{
    using namespace std::complex_literals;
    const double omega = 440.0 * 2.0 * Pi;
    
    return std::exp(1i*omega*t);
}

int main()
{
    for (int i = 0; i <= 10; i++)
    {
        double t = static_cast<double>(i) / 100;
        std::cout << f(t) << '\n';
    }
}


Also, in C++20 you can use stuff like std::numbers::pi
Last edited on Jan 5, 2022 at 7:12pm
Jan 5, 2022 at 7:14pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
#include <complex>
using namespace std;

const double PI = 4.0 * atan( 1.0 );

int main ()
{
   double omega = 1.0;
   int N = 16;
   double dt = 2 * PI / N;
   cout << fixed << setprecision(4);
   
   for ( int i = 0; i <= N; i++ )
   {
      double t = i * dt;
      cout << t << '\t' << polar( 1.0, omega * t ) << '\n';
   }
}


0.0000	(1.0000,0.0000)
0.3927	(0.9239,0.3827)
0.7854	(0.7071,0.7071)
1.1781	(0.3827,0.9239)
1.5708	(0.0000,1.0000)
1.9635	(-0.3827,0.9239)
2.3562	(-0.7071,0.7071)
2.7489	(-0.9239,0.3827)
3.1416	(-1.0000,0.0000)
3.5343	(-0.9239,-0.3827)
3.9270	(-0.7071,-0.7071)
4.3197	(-0.3827,-0.9239)
4.7124	(-0.0000,-1.0000)
5.1051	(0.3827,-0.9239)
5.4978	(0.7071,-0.7071)
5.8905	(0.9239,-0.3827)
6.2832	(1.0000,-0.0000)
Jan 5, 2022 at 9:07pm
Heh, the SO link is 404ed, same as here. I guess someone didn't like the spam.
Jan 5, 2022 at 9:11pm
Tricked again! >:(
Jan 5, 2022 at 9:19pm
Better to have tried to help and found out it was likely to be a spam bot than ignore someone who really is needing help.

Though just dumping what looks like a school assignment and expecting us to do the work gratis certainly doesn't encourage me to "bump a hump" writing even simple code. If'n the OP makes SOME effort by showing they tried to write code makes helping easier to justify taking the time.
Jan 5, 2022 at 9:25pm
True. As far as "doing the work"; if they ask a concise question about a very specific issue, then it's easy enough to just show a bare-minimum example. In general, it's a case-by-case basis. For this topic, asking "how do I write exp(j omega t) in C++" is focused and doesn't sound like something like homework (at least, not a whole assignment), so I'm more likely to throw them a bone.
Topic archived. No new replies allowed.