Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nk/noise generator #604

Merged
merged 8 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ spin = { version = "0.9", default-features = false, features = ["spin_mutex"]}
shared-bus = "0.2"
lm75 = "0.2"
enum-iterator = "1.1.3"
rand_xorshift = "0.3.0"
rand_core = "0.6.3"

[dependencies.stm32h7xx-hal]
features = ["stm32h743v", "rt", "ethernet", "xspi"]
Expand Down
8 changes: 7 additions & 1 deletion src/hardware/signal_generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use miniconf::Miniconf;
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
use serde::{Deserialize, Serialize};

/// Types of signals that can be generated.
Expand All @@ -7,6 +9,7 @@ pub enum Signal {
Cosine,
Square,
Triangle,
WhiteNoise,
}

/// Basic configuration for a generated signal.
Expand Down Expand Up @@ -142,10 +145,11 @@ impl Default for Config {
}
}

#[derive(Debug, Default)]
jordens marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug)]
pub struct SignalGenerator {
phase_accumulator: i32,
config: Config,
rng: XorShiftRng,
}

impl SignalGenerator {
Expand All @@ -160,6 +164,7 @@ impl SignalGenerator {
Self {
config,
phase_accumulator: 0,
rng: XorShiftRng::from_seed([0; 16]), // zeros will initialize with XorShiftRng internal seed
}
}

Expand Down Expand Up @@ -197,6 +202,7 @@ impl core::iter::Iterator for SignalGenerator {
}
}
Signal::Triangle => i16::MIN as i32 + (phase >> 15).abs(),
Signal::WhiteNoise => self.rng.next_u32() as i32 >> 16,
};

// Calculate the final output result as an i16.
Expand Down