From 44d6d9a84fda8f555cdb6a72c97b2bf588d3592d Mon Sep 17 00:00:00 2001 From: Christian Risi <75698846+CnF-Gris@users.noreply.github.com> Date: Sun, 1 Dec 2024 12:10:19 +0000 Subject: [PATCH] Added a Gaussian noise generator --- Package.swift | 21 +++++++++--- .../Classes/Devices/Sensors.swift | 7 +++- .../Utils/GaussianNoiseGenerator.swift | 3 ++ ...eDevice.swift => EdgeDeviceProtocol.swift} | 0 Sources/RandomCpp/NormalGenerator.cpp | 32 +++++++++++++++++++ Sources/RandomCpp/include/GaussianRNG.h | 17 ++++++++++ Sources/RandomCpp/include/RandomCpp.h | 1 + .../IoT_Simulator_CoreTests.swift | 7 ++++ 8 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 Sources/IoT-Simulator-Core/Classes/Utils/GaussianNoiseGenerator.swift rename Sources/IoT-Simulator-Core/Protocols/{EdgeDevice.swift => EdgeDeviceProtocol.swift} (100%) create mode 100644 Sources/RandomCpp/NormalGenerator.cpp create mode 100644 Sources/RandomCpp/include/GaussianRNG.h create mode 100644 Sources/RandomCpp/include/RandomCpp.h diff --git a/Package.swift b/Package.swift index 022c369..5a57155 100644 --- a/Package.swift +++ b/Package.swift @@ -5,22 +5,35 @@ import PackageDescription let package = Package( name: "IoT-Simulator-Core", - defaultLocalization: LanguageTag(stringLiteral: "en-US"), products: [ // Products define the executables and libraries a package produces, making them visible to other packages. .library( name: "IoT-Simulator-Core", - targets: ["IoT-Simulator-Core"]) + targets: ["IoT-Simulator-Core"] + ), + .library(name: "RandomCpp", targets: ["RandomCpp"]) ], dependencies: [], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products from dependencies. .target( - name: "IoT-Simulator-Core"), + name: "RandomCpp" + ), + .target( + name: "IoT-Simulator-Core", + dependencies: [ + "RandomCpp", + ], + swiftSettings: [ + .interoperabilityMode(.Cxx) + ]), .testTarget( name: "IoT-Simulator-CoreTests", - dependencies: ["IoT-Simulator-Core"] + dependencies: ["IoT-Simulator-Core"], + swiftSettings: [ + .interoperabilityMode(.Cxx) + ] ), ] ) diff --git a/Sources/IoT-Simulator-Core/Classes/Devices/Sensors.swift b/Sources/IoT-Simulator-Core/Classes/Devices/Sensors.swift index d76afc1..1e85e2b 100644 --- a/Sources/IoT-Simulator-Core/Classes/Devices/Sensors.swift +++ b/Sources/IoT-Simulator-Core/Classes/Devices/Sensors.swift @@ -59,6 +59,7 @@ public class RealSensor: Sensor { private var _meanNoise: Float private var _stdNoise: Float private let _quantizationBits: Int + //private var gaussianNoise: public init( sensorID: Int, @@ -87,6 +88,10 @@ public class RealSensor: Sensor { super.init(id: sensorID, sensorType: sensorType) } - // TODO: Override and implement read with Vincenzo lib here + override public func read(_ environment: PhysicalEnvironment) -> PhysicalData { + let value: PhysicalData = super.read(environment) + + return value + } } \ No newline at end of file diff --git a/Sources/IoT-Simulator-Core/Classes/Utils/GaussianNoiseGenerator.swift b/Sources/IoT-Simulator-Core/Classes/Utils/GaussianNoiseGenerator.swift new file mode 100644 index 0000000..b28b04f --- /dev/null +++ b/Sources/IoT-Simulator-Core/Classes/Utils/GaussianNoiseGenerator.swift @@ -0,0 +1,3 @@ + + + diff --git a/Sources/IoT-Simulator-Core/Protocols/EdgeDevice.swift b/Sources/IoT-Simulator-Core/Protocols/EdgeDeviceProtocol.swift similarity index 100% rename from Sources/IoT-Simulator-Core/Protocols/EdgeDevice.swift rename to Sources/IoT-Simulator-Core/Protocols/EdgeDeviceProtocol.swift diff --git a/Sources/RandomCpp/NormalGenerator.cpp b/Sources/RandomCpp/NormalGenerator.cpp new file mode 100644 index 0000000..b6949bc --- /dev/null +++ b/Sources/RandomCpp/NormalGenerator.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + + +GaussianRNG:: +GaussianRNG( + float mean, + float std_dev, + unsigned seed +) { + this->seed = seed; + this->generator = std::default_random_engine(this->seed); + this->distribution = std::normal_distribution(mean, std_dev); +} + +GaussianRNG +::GaussianRNG( + float mean, + float std_dev +) { + this->seed = std::chrono::system_clock::now().time_since_epoch().count(); + this->generator = std::default_random_engine(this->seed); + this->distribution = std::normal_distribution(mean, std_dev); +} + +float GaussianRNG +::generate() { + return distribution(generator); +} + + diff --git a/Sources/RandomCpp/include/GaussianRNG.h b/Sources/RandomCpp/include/GaussianRNG.h new file mode 100644 index 0000000..01d12cc --- /dev/null +++ b/Sources/RandomCpp/include/GaussianRNG.h @@ -0,0 +1,17 @@ +#include + +#pragma once + +class GaussianRNG { + +private: + unsigned seed; + std::default_random_engine generator; + std::normal_distribution distribution; + +public: + GaussianRNG(float mean, float std_dev, unsigned seed); + GaussianRNG(float mean, float std_dev); + + float generate(); +}; \ No newline at end of file diff --git a/Sources/RandomCpp/include/RandomCpp.h b/Sources/RandomCpp/include/RandomCpp.h new file mode 100644 index 0000000..7c140ad --- /dev/null +++ b/Sources/RandomCpp/include/RandomCpp.h @@ -0,0 +1 @@ +#include \ No newline at end of file diff --git a/Tests/IoT-Simulator-CoreTests/IoT_Simulator_CoreTests.swift b/Tests/IoT-Simulator-CoreTests/IoT_Simulator_CoreTests.swift index dc70750..06a3d4d 100644 --- a/Tests/IoT-Simulator-CoreTests/IoT_Simulator_CoreTests.swift +++ b/Tests/IoT-Simulator-CoreTests/IoT_Simulator_CoreTests.swift @@ -1,6 +1,13 @@ import Testing +import RandomCpp + @testable import IoT_Simulator_Core @Test func example() async throws { // Write your test here and use APIs like `#expect(...)` to check expected conditions. + var a = GaussianRNG(10, 0.5) + + for _ in 0...10 { + print(a.generate()) + } }