forked from AMSC-24-25/20-fft-20-fft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time-domain-signal-generator.cpp
29 lines (27 loc) · 1.09 KB
/
time-domain-signal-generator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "signal-generator/time-domain-signal-generator.hpp"
#include <iostream>
std::vector<std::complex<double> > TimeDomainSignalGenerator::generate1DSignal(
const int length, const double frequency, const double phase, const double noise
) {
// init
std::vector<std::complex<double>> signal;
// reserve the space for the signal
try {
signal.reserve(length);
} catch (std::length_error &e) {
std::cerr << "Error: the length of the signal cannot be grater than " << signal.max_size()
<< ". Reason: " << e.what() << std::endl;
}
// there is memory space for the signal, so generate it
// initialize the Gaussian distribution for the noise
std::normal_distribution<> gaussian(0, noise);
const double angular_frequency = 2 * M_PI * frequency;
// generate the signal
for (int time = 0; time < length; ++time) {
signal.emplace_back(
std::cos(angular_frequency * time + phase) + gaussian(_engine),
std::sin(angular_frequency * time + phase) + gaussian(_engine)
);
}
return signal;
}