From e3162a2162677cc86fa2ee7c18873e584a7d5dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20W=C3=A4chtler?= Date: Sun, 22 Jan 2023 19:19:57 +0100 Subject: [PATCH 1/9] Add new field prev_rand_value to TenChannelSampleGenerator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jens Wächtler --- Apps/generator/src/sample_generator.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Apps/generator/src/sample_generator.rs b/Apps/generator/src/sample_generator.rs index 3a89f021..54891227 100644 --- a/Apps/generator/src/sample_generator.rs +++ b/Apps/generator/src/sample_generator.rs @@ -1,5 +1,6 @@ use dasp_signal::{self as signal, Signal}; use rand::Rng; +use rand::thread_rng; pub struct TenChannelSampleGenerator { pub sine_signal: signal::Sine, @@ -8,6 +9,7 @@ pub struct TenChannelSampleGenerator { pub rng: rand::rngs::ThreadRng, pub amplitude: f64, pub add_noise: bool, + pub prev_rand_value: f64, } impl TenChannelSampleGenerator { @@ -21,6 +23,7 @@ impl TenChannelSampleGenerator { let saw_signal = signal::rate(sampling_rate).const_hz(frequency).saw(); let square_signal = signal::rate(sampling_rate).const_hz(frequency).square(); let rng = rand::thread_rng(); + let prev_rand_value = 0.0; TenChannelSampleGenerator { sine_signal, @@ -29,6 +32,7 @@ impl TenChannelSampleGenerator { rng, amplitude, add_noise, + prev_rand_value, } } From 40b8dcdcf4ce2dee0ccab310d6c7bcc554f935b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20W=C3=A4chtler?= Date: Sun, 22 Jan 2023 19:21:43 +0100 Subject: [PATCH 2/9] Add function for generating sample data between -1 and 1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jens Wächtler --- Apps/generator/src/sample_generator.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Apps/generator/src/sample_generator.rs b/Apps/generator/src/sample_generator.rs index 54891227..f192e06e 100644 --- a/Apps/generator/src/sample_generator.rs +++ b/Apps/generator/src/sample_generator.rs @@ -60,6 +60,18 @@ impl TenChannelSampleGenerator { samples.push(0.0); samples } + + fn random_bounded_values(self: &mut TenChannelSampleGenerator) -> f64 { + let mut rng = thread_rng(); + let prev_value = self.prev_rand_value; + loop { + let value = rng.gen_range(-1.0..1.0); + if (prev_value - value).abs() <= 0.2 { + self.prev_rand_value = value; + return value; + } + } + } } #[cfg(test)] From c47981eb7dd7d6e16a19cc03e0c6d61a83ebd523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20W=C3=A4chtler?= Date: Sun, 22 Jan 2023 19:23:34 +0100 Subject: [PATCH 3/9] Change forth signal to output of random_bounded_values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jens Wächtler --- Apps/generator/src/sample_generator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Apps/generator/src/sample_generator.rs b/Apps/generator/src/sample_generator.rs index f192e06e..393e4331 100644 --- a/Apps/generator/src/sample_generator.rs +++ b/Apps/generator/src/sample_generator.rs @@ -49,6 +49,7 @@ impl TenChannelSampleGenerator { samples.push(self.amplitude * self.square_signal.next() + noise); samples.push(self.amplitude * self.sine_signal.next() + noise); samples.push(self.amplitude * self.saw_signal.next() + noise); + samples.push(self.amplitude * self.random_bounded_values()); // remaining channels are set to a constant 0-Signal samples.push(0.0); @@ -57,7 +58,6 @@ impl TenChannelSampleGenerator { samples.push(0.0); samples.push(0.0); samples.push(0.0); - samples.push(0.0); samples } From 2866b559c02e4c20bba73b23dfacb9a9069cb863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20W=C3=A4chtler?= Date: Sun, 22 Jan 2023 19:24:16 +0100 Subject: [PATCH 4/9] Add tests for random sample data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jens Wächtler --- Apps/generator/src/sample_generator.rs | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Apps/generator/src/sample_generator.rs b/Apps/generator/src/sample_generator.rs index 393e4331..e8476b4d 100644 --- a/Apps/generator/src/sample_generator.rs +++ b/Apps/generator/src/sample_generator.rs @@ -128,4 +128,45 @@ mod tests { assert_eq!(first, second); assert_eq!(second, third); } + + #[test] + fn test_random_bounded_values() { + const SAMPLING_RATE: f64 = 50.0; + const FREQUENCY: f64 = 1.0; + const AMPLITUDE: f64 = 10.0; + let mut generator = TenChannelSampleGenerator::new(SAMPLING_RATE, FREQUENCY, AMPLITUDE, false); + let first_value = generator.random_bounded_values(); + + let second_value = generator.random_bounded_values(); + assert!((first_value - second_value).abs() <= 0.2); + + let third_value = generator.random_bounded_values(); + assert!((first_value - third_value).abs() <= 0.4); + assert!((second_value - third_value).abs() <= 0.2); + + let fourth_value = generator.random_bounded_values(); + assert!((first_value - fourth_value).abs() <= 0.6); + assert!((second_value - fourth_value).abs() <= 0.4); + assert!((third_value - fourth_value).abs() <= 0.2); + } + + #[test] + fn test_random_bounded_values_min_max() { + let mut min:f64 = 10000.0; + let mut max: f64 = -100000.0; + + const SAMPLING_RATE: f64 = 50.0; + const FREQUENCY: f64 = 1.0; + const AMPLITUDE: f64 = 10.0; + let mut generator = TenChannelSampleGenerator::new(SAMPLING_RATE, FREQUENCY, AMPLITUDE, false); + + for _ in 0..1000 { + let value = generator.random_bounded_values(); + min = min.min(value); + max = max.max(value); + } + + assert!(min > -1.0); + assert!(max < 1.0); + } } From fd18a9d9c4b31bf8635cf0a8e6f04885d306b7d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20W=C3=A4chtler?= Date: Sun, 22 Jan 2023 19:42:02 +0100 Subject: [PATCH 5/9] Introduce const for random difference MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jens Wächtler --- Apps/generator/src/sample_generator.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Apps/generator/src/sample_generator.rs b/Apps/generator/src/sample_generator.rs index e8476b4d..49a68ed9 100644 --- a/Apps/generator/src/sample_generator.rs +++ b/Apps/generator/src/sample_generator.rs @@ -1,6 +1,7 @@ use dasp_signal::{self as signal, Signal}; use rand::Rng; use rand::thread_rng; +const RANDOM_DIFF: f64 = 0.01; pub struct TenChannelSampleGenerator { pub sine_signal: signal::Sine, @@ -50,6 +51,7 @@ impl TenChannelSampleGenerator { samples.push(self.amplitude * self.sine_signal.next() + noise); samples.push(self.amplitude * self.saw_signal.next() + noise); samples.push(self.amplitude * self.random_bounded_values()); + samples.push(self.rng.gen_range(-1.0..1.0)); // remaining channels are set to a constant 0-Signal samples.push(0.0); @@ -57,7 +59,6 @@ impl TenChannelSampleGenerator { samples.push(0.0); samples.push(0.0); samples.push(0.0); - samples.push(0.0); samples } @@ -66,7 +67,7 @@ impl TenChannelSampleGenerator { let prev_value = self.prev_rand_value; loop { let value = rng.gen_range(-1.0..1.0); - if (prev_value - value).abs() <= 0.2 { + if (prev_value - value).abs() <= RANDOM_DIFF { self.prev_rand_value = value; return value; } @@ -138,16 +139,16 @@ mod tests { let first_value = generator.random_bounded_values(); let second_value = generator.random_bounded_values(); - assert!((first_value - second_value).abs() <= 0.2); + assert!((first_value - second_value).abs() <= RANDOM_DIFF); let third_value = generator.random_bounded_values(); - assert!((first_value - third_value).abs() <= 0.4); - assert!((second_value - third_value).abs() <= 0.2); + assert!((first_value - third_value).abs() <= RANDOM_DIFF * 2.0); + assert!((second_value - third_value).abs() <= RANDOM_DIFF); let fourth_value = generator.random_bounded_values(); - assert!((first_value - fourth_value).abs() <= 0.6); - assert!((second_value - fourth_value).abs() <= 0.4); - assert!((third_value - fourth_value).abs() <= 0.2); + assert!((first_value - fourth_value).abs() <= 3.0 * RANDOM_DIFF); + assert!((second_value - fourth_value).abs() <= 2.0 * RANDOM_DIFF); + assert!((third_value - fourth_value).abs() <= RANDOM_DIFF); } #[test] From 61549ae0fa9c38f80fbc70496d03198dca8a0ddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20W=C3=A4chtler?= Date: Sun, 22 Jan 2023 21:40:51 +0100 Subject: [PATCH 6/9] Introduce sinus curve with changing amplitude MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jens Wächtler --- Apps/generator/src/sample_generator.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Apps/generator/src/sample_generator.rs b/Apps/generator/src/sample_generator.rs index 49a68ed9..3d18471a 100644 --- a/Apps/generator/src/sample_generator.rs +++ b/Apps/generator/src/sample_generator.rs @@ -11,6 +11,8 @@ pub struct TenChannelSampleGenerator { pub amplitude: f64, pub add_noise: bool, pub prev_rand_value: f64, + pub sine_amplitude_iterator: Box>, + pub sine_amplitude_signal: signal::Sine, } impl TenChannelSampleGenerator { @@ -20,11 +22,16 @@ impl TenChannelSampleGenerator { amplitude: f64, add_noise: bool, ) -> TenChannelSampleGenerator { + fn oscillate(rate: f64) -> impl Iterator { + (0..).map(move |x| (x as f64 * std::f64::consts::PI / rate).sin() + 1.0) + } let sine_signal = signal::rate(sampling_rate).const_hz(frequency).sine(); let saw_signal = signal::rate(sampling_rate).const_hz(frequency).saw(); let square_signal = signal::rate(sampling_rate).const_hz(frequency).square(); - let rng = rand::thread_rng(); + let rng = thread_rng(); let prev_rand_value = 0.0; + let sine_amplitude_iterator = Box::new(oscillate(sampling_rate * 10.0)); + let sine_amplitude_signal = signal::rate(sampling_rate).const_hz(frequency).sine(); TenChannelSampleGenerator { sine_signal, @@ -34,6 +41,8 @@ impl TenChannelSampleGenerator { amplitude, add_noise, prev_rand_value, + sine_amplitude_iterator, + sine_amplitude_signal, } } @@ -52,13 +61,13 @@ impl TenChannelSampleGenerator { samples.push(self.amplitude * self.saw_signal.next() + noise); samples.push(self.amplitude * self.random_bounded_values()); samples.push(self.rng.gen_range(-1.0..1.0)); + samples.push(self.amplitude * self.sine_amplitude_signal.next() * self.sine_amplitude_iterator.next().unwrap()); // remaining channels are set to a constant 0-Signal samples.push(0.0); samples.push(0.0); samples.push(0.0); samples.push(0.0); - samples.push(0.0); samples } @@ -153,7 +162,7 @@ mod tests { #[test] fn test_random_bounded_values_min_max() { - let mut min:f64 = 10000.0; + let mut min: f64 = 10000.0; let mut max: f64 = -100000.0; const SAMPLING_RATE: f64 = 50.0; @@ -169,5 +178,5 @@ mod tests { assert!(min > -1.0); assert!(max < 1.0); - } + } } From bd77522f1254a0363c0e19279dddb3fb26a0b2e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20W=C3=A4chtler?= Date: Sun, 22 Jan 2023 21:53:13 +0100 Subject: [PATCH 7/9] Add no noise signals MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jens Wächtler --- Apps/generator/src/sample_generator.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Apps/generator/src/sample_generator.rs b/Apps/generator/src/sample_generator.rs index 3d18471a..9d0b4a6f 100644 --- a/Apps/generator/src/sample_generator.rs +++ b/Apps/generator/src/sample_generator.rs @@ -13,6 +13,9 @@ pub struct TenChannelSampleGenerator { pub prev_rand_value: f64, pub sine_amplitude_iterator: Box>, pub sine_amplitude_signal: signal::Sine, + pub sine_signal_no_noise: signal::Sine, + pub saw_signal_no_noise: signal::Saw, + pub square_signal_no_noise: signal::Square, } impl TenChannelSampleGenerator { @@ -32,6 +35,9 @@ impl TenChannelSampleGenerator { let prev_rand_value = 0.0; let sine_amplitude_iterator = Box::new(oscillate(sampling_rate * 10.0)); let sine_amplitude_signal = signal::rate(sampling_rate).const_hz(frequency).sine(); + let sine_signal_no_noise = signal::rate(sampling_rate).const_hz(frequency).sine(); + let saw_signal_no_noise = signal::rate(sampling_rate).const_hz(frequency).saw(); + let square_signal_no_noise = signal::rate(sampling_rate).const_hz(frequency).square(); TenChannelSampleGenerator { sine_signal, @@ -43,6 +49,9 @@ impl TenChannelSampleGenerator { prev_rand_value, sine_amplitude_iterator, sine_amplitude_signal, + sine_signal_no_noise, + saw_signal_no_noise, + square_signal_no_noise, } } @@ -62,12 +71,12 @@ impl TenChannelSampleGenerator { samples.push(self.amplitude * self.random_bounded_values()); samples.push(self.rng.gen_range(-1.0..1.0)); samples.push(self.amplitude * self.sine_amplitude_signal.next() * self.sine_amplitude_iterator.next().unwrap()); + samples.push(self.amplitude * self.sine_signal_no_noise.next()); + samples.push(self.amplitude * self.saw_signal_no_noise.next()); + samples.push(self.amplitude * self.square_signal_no_noise.next()); // remaining channels are set to a constant 0-Signal samples.push(0.0); - samples.push(0.0); - samples.push(0.0); - samples.push(0.0); samples } From e31e0f683069f077c5d9475767fe4a9efab3a21a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20W=C3=A4chtler?= Date: Sun, 22 Jan 2023 22:12:31 +0100 Subject: [PATCH 8/9] Change Wave to be in -1 1 range MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jens Wächtler --- Apps/generator/src/sample_generator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Apps/generator/src/sample_generator.rs b/Apps/generator/src/sample_generator.rs index 9d0b4a6f..dd3733bc 100644 --- a/Apps/generator/src/sample_generator.rs +++ b/Apps/generator/src/sample_generator.rs @@ -26,7 +26,7 @@ impl TenChannelSampleGenerator { add_noise: bool, ) -> TenChannelSampleGenerator { fn oscillate(rate: f64) -> impl Iterator { - (0..).map(move |x| (x as f64 * std::f64::consts::PI / rate).sin() + 1.0) + (0..).map(move |x| ((x as f64 * std::f64::consts::PI / rate).sin() + 1.0) / 2.0) } let sine_signal = signal::rate(sampling_rate).const_hz(frequency).sine(); let saw_signal = signal::rate(sampling_rate).const_hz(frequency).saw(); From 7f3ba21e3b4f1ecd2198ad01f0541ade79dd2e5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20W=C3=A4chtler?= Date: Mon, 23 Jan 2023 08:57:54 +0100 Subject: [PATCH 9/9] Fix test to respect not periodic functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jens Wächtler --- Apps/generator/src/sample_generator.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Apps/generator/src/sample_generator.rs b/Apps/generator/src/sample_generator.rs index dd3733bc..e87dab38 100644 --- a/Apps/generator/src/sample_generator.rs +++ b/Apps/generator/src/sample_generator.rs @@ -144,8 +144,8 @@ mod tests { let second: Vec = generator.next(); let third: Vec = generator.next(); - assert_eq!(first, second); - assert_eq!(second, third); + assert_eq!(first[0..3], second[0..3]); + assert_eq!(second[0..3], third[0..3]); } #[test]