Added a Gaussian noise generator

This commit is contained in:
Christian Risi 2024-12-01 12:10:19 +00:00
parent 0c82f284e8
commit 44d6d9a84f
8 changed files with 83 additions and 5 deletions

View File

@ -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)
]
),
]
)

View File

@ -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
}
}

View File

@ -0,0 +1,32 @@
#include <random>
#include <chrono>
#include <GaussianRNG.h>
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<float>(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<float>(mean, std_dev);
}
float GaussianRNG
::generate() {
return distribution(generator);
}

View File

@ -0,0 +1,17 @@
#include <random>
#pragma once
class GaussianRNG {
private:
unsigned seed;
std::default_random_engine generator;
std::normal_distribution<float> distribution;
public:
GaussianRNG(float mean, float std_dev, unsigned seed);
GaussianRNG(float mean, float std_dev);
float generate();
};

View File

@ -0,0 +1 @@
#include <GaussianRNG.h>

View File

@ -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())
}
}