FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
noise_generators.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright (c) 2006-2008 Marc Brooker and Michael Inggs
4// Copyright (c) 2008-present FERS Contributors (see AUTHORS.md).
5//
6// See the GNU GPLv2 LICENSE file in the FERS project root for more information.
7
8/**
9 * @file noise_generators.h
10 * @brief Header file for noise generator classes.
11 */
12
13#pragma once
14
15#include <cstddef>
16#include <functional>
17#include <memory>
18#include <random>
19#include <vector>
20
21#include "core/config.h"
22#include "falpha_branch.h"
23
24namespace noise
25{
26 /**
27 * @class NoiseGenerator
28 * @brief Abstract base class for noise generators.
29 */
31 {
32 public:
33 NoiseGenerator() = default;
34
35 virtual ~NoiseGenerator() = default;
36
38
40
42
44
45 /**
46 * @brief Pure virtual method to generate a noise sample.
47 *
48 * @return A noise sample of type RealType.
49 */
50 virtual RealType getSample() = 0;
51 };
52
53 /**
54 * @class WgnGenerator
55 * @brief Generates white Gaussian noise.
56 */
57 class WgnGenerator final : public NoiseGenerator
58 {
59 public:
60 /**
61 * @brief Constructor to initialize the WGN generator with a given standard deviation.
62 *
63 * @param rngEngine The random number engine to use for generation.
64 * @param stddev The standard deviation of the generated Gaussian noise. Default is 1.0.
65 */
66 explicit WgnGenerator(std::mt19937& rngEngine, const RealType stddev = 1.0) noexcept :
67 _rng_engine(rngEngine), _dist(0.0, stddev), _stddev(stddev)
68 {
69 }
70
71 /**
72 * @brief Generates a sample of white Gaussian noise.
73 *
74 * @return A noise sample of type RealType.
75 */
76 RealType getSample() noexcept override { return _dist(_rng_engine.get()); }
77
78 private:
79 std::reference_wrapper<std::mt19937> _rng_engine; ///< Reference to the RNG engine.
80 std::normal_distribution<> _dist; ///< Normal distribution for generating Gaussian noise.
81 RealType _stddev; ///< Standard deviation of the generated noise.
82 };
83
84 /**
85 * @class GammaGenerator
86 * @brief Generates Gamma-distributed noise.
87 */
88 class GammaGenerator final : public NoiseGenerator
89 {
90 public:
91 /**
92 * @brief Constructor to initialize the Gamma generator with a shape parameter.
93 *
94 * @param rngEngine The random number engine to use for generation.
95 * @param k The shape parameter of the Gamma distribution.
96 */
97 explicit GammaGenerator(std::mt19937& rngEngine, const RealType k) noexcept :
98 _rng_engine(rngEngine), _dist(k, 1.0)
99 {
100 }
101
102 /**
103 * @brief Generates a sample of Gamma noise.
104 *
105 * @return A noise sample of type RealType.
106 */
107 RealType getSample() noexcept override { return _dist(_rng_engine.get()); }
108
109 private:
110 std::reference_wrapper<std::mt19937> _rng_engine; ///< Reference to the RNG engine.
111 std::gamma_distribution<> _dist; ///< Gamma distribution for generating noise.
112 };
113
114 /**
115 * @class MultirateGenerator
116 * @brief Generates multirate noise using a hierarchical tree structure.
117 */
119 {
120 public:
121 /**
122 * @brief Constructor to initialize the multirate generator.
123 *
124 * @param rngEngine The random number engine to use for generation.
125 * @param alpha The scaling parameter that controls the noise properties.
126 * @param branches The number of branches in the tree structure.
127 */
128 MultirateGenerator(std::mt19937& rngEngine, RealType alpha, unsigned branches);
129
130 /**
131 * @brief Generates a multirate noise sample.
132 *
133 * @return A noise sample of type RealType.
134 */
135 RealType getSample() override { return _topbranch->getSample() * _scale; }
136
137 /**
138 * @brief Skips a number of samples in the noise sequence.
139 *
140 * @param samples The number of samples to skip.
141 */
142 void skipSamples(std::size_t samples) noexcept;
143
144 /**
145 * @brief Resets the noise generator state.
146 */
147 void reset() noexcept;
148
149 private:
150 RealType _scale; ///< Scaling factor for the noise.
151 std::unique_ptr<FAlphaBranch> _topbranch; ///< Pointer to the top branch in the tree structure.
152 std::reference_wrapper<std::mt19937> _rng_engine; ///< Reference to the RNG engine.
153
154 /**
155 * @brief Helper method to create the hierarchical tree structure.
156 *
157 * @param fAlpha Fractional alpha value.
158 * @param fInt Integer part of the scaling factor.
159 * @param branches The number of branches in the tree.
160 */
161 void createTree(RealType fAlpha, int fInt, unsigned branches);
162 };
163
164 /**
165 * @class ClockModelGenerator
166 * @brief Generates noise using a clock model with multiple rates.
167 *
168 * This is useful for simulating clock jitter or other similar phenomena.
169 */
171 {
172 public:
173 /**
174 * @brief Constructor to initialize the clock model generator.
175 *
176 * @param rngEngine The random number engine to use for generation.
177 * @param alpha Vector of scaling parameters for the noise.
178 * @param inWeights Vector of weights for each rate process.
179 * @param frequency The base frequency of the clock model.
180 * @param phaseOffset The phase offset of the generated noise.
181 * @param freqOffset The frequency offset of the generated noise.
182 * @param branches The number of branches in each rate process.
183 */
184 ClockModelGenerator(std::mt19937& rngEngine, const std::vector<RealType>& alpha,
185 const std::vector<RealType>& inWeights, RealType frequency, RealType phaseOffset,
186 RealType freqOffset, int branches) noexcept;
187
188 /**
189 * @brief Generates a clock model noise sample.
190 *
191 * @return A noise sample of type RealType.
192 */
193 RealType getSample() override;
194
195 /**
196 * @brief Skips a number of samples in the noise sequence.
197 *
198 * @param samples The number of samples to skip.
199 */
200 void skipSamples(std::size_t samples);
201
202 /**
203 * @brief Resets the noise generator state.
204 */
205 void reset();
206
207 /**
208 * @brief Checks if the noise generator is enabled.
209 *
210 * @return True if the generator is enabled, false otherwise.
211 */
212 [[nodiscard]] bool enabled() const;
213
214 private:
215 std::reference_wrapper<std::mt19937> _rng_engine; ///< Reference to the RNG engine.
216 std::vector<std::unique_ptr<MultirateGenerator>> _generators; ///< Multirate noise generators.
217 std::vector<RealType> _weights; ///< Weights for each rate process.
218 RealType _phase_offset; ///< Phase offset for the noise.
219 RealType _freq_offset; ///< Frequency offset for the noise.
220 RealType _frequency; ///< Base frequency of the clock model.
221 std::size_t _count = 0; ///< Sample counter.
222 };
223}
Generates noise using a clock model with multiple rates.
Class responsible for generating fractional and integer noise components.
Generates Gamma-distributed noise.
RealType getSample() noexcept override
Generates a sample of Gamma noise.
GammaGenerator(std::mt19937 &rngEngine, const RealType k) noexcept
Constructor to initialize the Gamma generator with a shape parameter.
Generates multirate noise using a hierarchical tree structure.
void reset() noexcept
Resets the noise generator state.
RealType getSample() override
Generates a multirate noise sample.
void skipSamples(std::size_t samples) noexcept
Skips a number of samples in the noise sequence.
Abstract base class for noise generators.
NoiseGenerator(NoiseGenerator &&)=delete
NoiseGenerator & operator=(const NoiseGenerator &)=delete
NoiseGenerator & operator=(NoiseGenerator &&)=delete
NoiseGenerator(const NoiseGenerator &)=delete
virtual ~NoiseGenerator()=default
virtual RealType getSample()=0
Pure virtual method to generate a noise sample.
Generates white Gaussian noise.
WgnGenerator(std::mt19937 &rngEngine, const RealType stddev=1.0) noexcept
Constructor to initialize the WGN generator with a given standard deviation.
RealType getSample() noexcept override
Generates a sample of white Gaussian noise.
Global configuration file for the project.
double RealType
Type for real numbers.
Definition config.h:27
Implementation of the FAlphaBranch class for noise generation.