FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
timing.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 timing.h
10 * @brief Timing source for simulation objects.
11 *
12 * All objects must adhere to a common timing source, which is modeled and adjusted by the methods in this class.
13 */
14
15#pragma once
16
17#include <cstddef>
18#include <memory>
19#include <random>
20#include <string>
21#include <vector>
22
23#include "core/config.h"
24#include "core/sim_id.h"
26
27namespace timing
28{
29 class PrototypeTiming;
30
31 /**
32 * @class Timing
33 * @brief Represents a timing source for simulation.
34 */
35 class Timing final
36 {
37 public:
38 /**
39 * @brief Constructs a Timing object.
40 *
41 * @param name The name of the timing source.
42 * @param seed The seed for the timing source's internal random number generator.
43 */
44 explicit Timing(std::string name, unsigned seed, const SimId id = 0) noexcept;
45
46 ~Timing() = default;
47
48 Timing(const Timing&) = delete;
49
50 Timing& operator=(const Timing&) = delete;
51
52 Timing(Timing&&) = delete;
53
54 Timing& operator=(Timing&&) = delete;
55
56 /**
57 * @brief Gets the next sample from the timing source.
58 *
59 * @return The next sample value or 0.0 if not enabled.
60 */
61 [[nodiscard]] RealType getNextSample() const noexcept { return _enabled ? _model->getSample() : 0.0; }
62
63 /**
64 * @brief Gets the name of the timing source.
65 *
66 * @return The name of the timing source.
67 */
68 [[nodiscard]] std::string getName() const noexcept { return _name; }
69
70
71 /**
72 * @brief Gets the unique ID of the timing source.
73 *
74 * @return The timing source SimId.
75 */
76 [[nodiscard]] SimId getId() const noexcept { return _id; }
77
78 /**
79 * @brief Gets the initial seed used for the timing source's RNG.
80 *
81 * @return The initial seed value.
82 */
83 [[nodiscard]] unsigned getSeed() const noexcept { return _seed; }
84
85 /**
86 * @brief Checks if the timing source synchronizes on pulse.
87 *
88 * @return True if synchronized on pulse, otherwise false.
89 */
90 [[nodiscard]] bool getSyncOnPulse() const noexcept { return _sync_on_pulse; }
91
92 /**
93 * @brief Gets the frequency of the timing source.
94 *
95 * @return The frequency of the timing source.
96 */
97 [[nodiscard]] RealType getFrequency() const noexcept { return _frequency; }
98
99 /**
100 * @brief Gets the frequency offset of the timing source.
101 * @return The frequency offset.
102 */
103 [[nodiscard]] RealType getFreqOffset() const noexcept { return _freq_offset; }
104
105 /**
106 * @brief Gets the phase offset of the timing source.
107 * @return The phase offset.
108 */
109 [[nodiscard]] RealType getPhaseOffset() const noexcept { return _phase_offset; }
110
111 /**
112 * @brief Checks if the timing source is enabled.
113 *
114 * @return True if enabled, otherwise false.
115 */
116 [[nodiscard]] bool isEnabled() const noexcept { return _enabled && _model && _model->enabled(); }
117
118 /**
119 * @brief Skips a number of samples in the timing model.
120 *
121 * @param samples The number of samples to skip.
122 */
123 void skipSamples(std::size_t samples) noexcept;
124
125 /**
126 * @brief Initializes the timing model.
127 *
128 * @param timing The prototype timing configuration used for initialization.
129 */
130 void initializeModel(const PrototypeTiming* timing) noexcept;
131
132 /**
133 * @brief Resets the timing model.
134 */
135 // NOLINTNEXTLINE(readability-make-member-function-const)
136 void reset() noexcept
137 {
138 if (_model)
139 {
140 _model->reset();
141 }
142 }
143
144 /**
145 * @brief Creates a new Timing instance based on the same prototype.
146 * @return A unique_ptr to the new Timing object.
147 * @throws std::logic_error if the timing object was not initialized from a prototype.
148 */
149 [[nodiscard]] std::unique_ptr<Timing> clone() const;
150
151 private:
152 std::string _name; ///< The name of the timing source.
153 SimId _id; ///< Unique ID for this timing source.
154 bool _enabled{false}; ///< Flag indicating if the timing source is enabled.
155 std::unique_ptr<noise::ClockModelGenerator> _model{nullptr}; ///< Noise generator model for the timing source.
156 std::vector<RealType> _alphas; ///< The alpha values for the noise generator model.
157 std::vector<RealType> _weights; ///< The weights for the noise generator model.
158 RealType _frequency{}; ///< The frequency of the timing source.
159 RealType _freq_offset{}; ///< The frequency offset of the timing source.
160 RealType _phase_offset{}; ///< The phase offset of the timing source.
161 bool _sync_on_pulse{false}; ///< Flag indicating if the timing source synchronizes on pulse.
162 std::mt19937 _rng; ///< Per-object random number generator for statistical independence.
163 const PrototypeTiming* _prototype{nullptr}; ///< Pointer to the prototype used for initialization.
164 unsigned _seed; ///< The initial seed for the RNG.
165 };
166}
Manages timing properties such as frequency, offsets, and synchronization.
Represents a timing source for simulation.
Definition timing.h:36
unsigned getSeed() const noexcept
Gets the initial seed used for the timing source's RNG.
Definition timing.h:83
Timing & operator=(Timing &&)=delete
std::string getName() const noexcept
Gets the name of the timing source.
Definition timing.h:68
void reset() noexcept
Resets the timing model.
Definition timing.h:136
Timing & operator=(const Timing &)=delete
Timing(Timing &&)=delete
~Timing()=default
void initializeModel(const PrototypeTiming *timing) noexcept
Initializes the timing model.
Definition timing.cpp:39
RealType getNextSample() const noexcept
Gets the next sample from the timing source.
Definition timing.h:61
void skipSamples(std::size_t samples) noexcept
Skips a number of samples in the timing model.
Definition timing.cpp:31
RealType getFreqOffset() const noexcept
Gets the frequency offset of the timing source.
Definition timing.h:103
bool getSyncOnPulse() const noexcept
Checks if the timing source synchronizes on pulse.
Definition timing.h:90
bool isEnabled() const noexcept
Checks if the timing source is enabled.
Definition timing.h:116
RealType getFrequency() const noexcept
Gets the frequency of the timing source.
Definition timing.h:97
Timing(const Timing &)=delete
RealType getPhaseOffset() const noexcept
Gets the phase offset of the timing source.
Definition timing.h:109
SimId getId() const noexcept
Gets the unique ID of the timing source.
Definition timing.h:76
std::unique_ptr< Timing > clone() const
Creates a new Timing instance based on the same prototype.
Definition timing.cpp:82
Global configuration file for the project.
double RealType
Type for real numbers.
Definition config.h:27
Header file for noise generator classes.
uint64_t SimId
64-bit Unique Simulation ID.
Definition sim_id.h:18