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
Changes from 4 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
12 changes: 12 additions & 0 deletions src/hardware/signal_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub enum Signal {
Cosine,
Square,
Triangle,
WhiteNoise,
}

/// Basic configuration for a generated signal.
Expand Down Expand Up @@ -146,6 +147,7 @@ impl Default for Config {
pub struct SignalGenerator {
phase_accumulator: i32,
config: Config,
noise_state: i32, // state of the xorshift rng
jordens marked this conversation as resolved.
Show resolved Hide resolved
}

impl SignalGenerator {
Expand All @@ -160,6 +162,7 @@ impl SignalGenerator {
Self {
config,
phase_accumulator: 0,
noise_state: 1,
}
}

Expand All @@ -172,6 +175,14 @@ impl SignalGenerator {
pub fn clear_phase_accumulator(&mut self) {
self.phase_accumulator = 0;
}

// Generate a new random number using xorshift.
fn xorshift(&mut self) -> i32 {
jordens marked this conversation as resolved.
Show resolved Hide resolved
self.noise_state ^= self.noise_state << 13;
self.noise_state ^= self.noise_state >> 17;
self.noise_state ^= self.noise_state << 5;
self.noise_state
}
}

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

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