Skip to content

Commit bb45e21

Browse files
committed
Added TNoiseGenerator
feat: Add TNoiseGenerator class for noise generation - Implemented TNoiseGenerator class to generate noisy signals. - Supports white noise generation with customizable amplitude. - Added ability to apply white noise to signal lines.
1 parent 48a9c27 commit bb45e21

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed

src/gen/TNoiseGenerator.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "TNoiseGenerator.h"
2+
#include "TCore.h"
3+
#include "TSignalLine.h"
4+
5+
#include <cstddef>
6+
#include <memory>
7+
#include <random>
8+
#include <string>
9+
#include <utility>
10+
11+
/**
12+
** PUBLIC METHODS
13+
*/
14+
15+
TNoiseGenerator::TNoiseGenerator(const TSignalLine* signalLine,
16+
double noiseAmplitude,
17+
NoiseType noiseType,
18+
std::string graphLabel)
19+
: _sl(std::make_unique<TSignalLine>(signalLine->getParams().pointsCount)),
20+
_params{signalLine, noiseAmplitude, noiseType, std::move(graphLabel)} {}
21+
22+
TNoiseGenerator::TNoiseGenerator(TNoiseGeneratorParams params)
23+
: _sl(std::make_unique<TSignalLine>(
24+
params.signalLine->getParams().pointsCount)),
25+
_params{std::move(params)} {}
26+
27+
const TSignalLine* TNoiseGenerator::getSignalLine() const {
28+
return _sl.get();
29+
}
30+
31+
const TNoiseGeneratorParams& TNoiseGenerator::getParams() const {
32+
return _params;
33+
}
34+
35+
void TNoiseGenerator::execute() {
36+
if (_sl == nullptr) {
37+
throw SignalProcesserException("Signal line is not initialized.");
38+
}
39+
40+
const size_t pointsCount = _sl->getParams().pointsCount;
41+
const double noiseAmplitude = _params.noiseAmplitude;
42+
43+
switch (_params.noiseType) {
44+
case NoiseType::White: {
45+
std::random_device randomDevice;
46+
std::mt19937 gen(randomDevice());
47+
std::uniform_real_distribution<> dist(-noiseAmplitude, noiseAmplitude);
48+
for (size_t i = 0; i < pointsCount; ++i) {
49+
const Point& originalPoint = _params.signalLine->getPoint(i);
50+
const double noise = dist(gen);
51+
_sl->setPoint(i, originalPoint.x, originalPoint.y + noise);
52+
}
53+
break;
54+
}
55+
56+
default:
57+
throw SignalProcesserException("Unknown noise type.");
58+
}
59+
}

src/gen/TNoiseGenerator.h

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#pragma once
2+
3+
#include "TSignalLine.h"
4+
5+
#include <cstdint>
6+
#include <memory>
7+
#include <string>
8+
9+
/**
10+
* @namespace NGEN
11+
* @brief Contains default parameters used in noise generation.
12+
*/
13+
namespace NGEN {
14+
15+
static constexpr double DEFAULT_NOISE_AMPLITUDE = 1.0; ///< Default noise
16+
///< amplitude.
17+
static const std::string DEFAULT_GRAPH_LABEL =
18+
"Noisy Signal"; ///< Default graph label.
19+
20+
} // namespace NGEN
21+
22+
// TODO: Add noise types:
23+
// - `Gaussian`: Generates Gaussian (normal) noise with a specified mean and
24+
// standard deviation.
25+
// - `Pink`: Generates pink noise, which has equal energy per octave (or \(1/f\)
26+
// noise).
27+
// - `Brown`: Generates brown noise (also called red noise), which decreases
28+
// power as \(1/f^2\).
29+
// - `Blue`: Generates blue noise, which increases power as \(f\).
30+
// - `Violet`: Generates violet noise, where power increases as \(f^2\).
31+
// - `Impulse`: Generates impulse noise with random spikes.
32+
/**
33+
* @enum NoiseType
34+
* @brief Specifies the types of noise that can be applied to a signal.
35+
*
36+
* This enumeration defines various types of noise that can be used in signal
37+
* processing:
38+
*
39+
* - `White`: Generates white noise with uniform random values.
40+
*/
41+
enum class NoiseType : std::uint8_t {
42+
White ///< White noise (uniform distribution).
43+
};
44+
45+
/**
46+
* @struct TNoiseGeneratorParams
47+
* @brief Parameters for generating a noisy signal line.
48+
*/
49+
struct TNoiseGeneratorParams {
50+
const TSignalLine* signalLine =
51+
nullptr; ///< Pointer to the signal line to add noise to.
52+
double noiseAmplitude =
53+
NGEN::DEFAULT_NOISE_AMPLITUDE; ///< (optional) Amplitude of the noise.
54+
NoiseType noiseType =
55+
NoiseType::White; ///< (optional) Type of noise to apply.
56+
std::string graphLabel =
57+
NGEN::DEFAULT_GRAPH_LABEL; ///< (optional) Label for the graph.
58+
};
59+
60+
/**
61+
* @class TNoiseGenerator
62+
* @brief Class for generating a noisy signal line.
63+
*
64+
* The TNoiseGenerator class is responsible for adding noise to an existing
65+
* signal line. It provides methods to retrieve the noisy signal and its
66+
* parameters.
67+
*/
68+
class TNoiseGenerator {
69+
public:
70+
/**
71+
* @brief Constructs a TNoiseGenerator with a signal line and noise
72+
* parameters.
73+
*
74+
* @param signalLine Pointer to the signal line to add noise to.
75+
* @param noiseType (optional) The type of noise to apply.
76+
* @param graphLabel (optional) Label for the graph.
77+
*/
78+
TNoiseGenerator(const TSignalLine* signalLine,
79+
double noiseAmplitude = NGEN::DEFAULT_NOISE_AMPLITUDE,
80+
NoiseType noiseType = NoiseType::White,
81+
std::string graphLabel = NGEN::DEFAULT_GRAPH_LABEL);
82+
83+
/**
84+
* @brief Constructs a TNoiseGenerator with noise generation parameters.
85+
*
86+
* @param params Structure containing the parameters for noise generation.
87+
*/
88+
TNoiseGenerator(TNoiseGeneratorParams params);
89+
90+
/**
91+
* @brief Default destructor.
92+
*/
93+
~TNoiseGenerator() = default;
94+
95+
/**
96+
* @brief Default copy constructor.
97+
*/
98+
TNoiseGenerator(const TNoiseGenerator&) = default;
99+
100+
/**
101+
* @brief Default move constructor.
102+
*/
103+
TNoiseGenerator(TNoiseGenerator&&) noexcept = default;
104+
105+
/**
106+
* @brief Default copy assignment operator.
107+
*/
108+
TNoiseGenerator& operator=(const TNoiseGenerator&) = default;
109+
110+
/**
111+
* @brief Default move assignment operator.
112+
*/
113+
TNoiseGenerator& operator=(TNoiseGenerator&&) noexcept = default;
114+
115+
/**
116+
* @brief Retrieves the noisy signal line.
117+
*
118+
* @return const TSignalLine* Pointer to the resulting noisy signal line.
119+
*/
120+
[[nodiscard]] const TSignalLine* getSignalLine() const;
121+
122+
/**
123+
* @brief Retrieves the parameters used for noise generation.
124+
*
125+
* @return const TNoiseGeneratorParams& Reference to the noise generation
126+
* parameters.
127+
*/
128+
[[nodiscard]] const TNoiseGeneratorParams& getParams() const;
129+
130+
/**
131+
* @brief Executes the noise generation process.
132+
*/
133+
void execute();
134+
135+
private:
136+
std::unique_ptr<TSignalLine> _sl =
137+
nullptr; ///< Pointer to the noisy signal line.
138+
TNoiseGeneratorParams _params = {}; ///< Parameters for noise generation.
139+
};

0 commit comments

Comments
 (0)