Skip to content

Commit

Permalink
fix(math): correct and simplify some math functions
Browse files Browse the repository at this point in the history
Signed-off-by: Riccardo Gallo <[email protected]>
  • Loading branch information
rgallor committed Nov 7, 2024
1 parent 67bf216 commit ba6c64d
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ pub enum MathFunction {
Saw,
/// Rect
Rect,
/// Sinc
/// Normalized Sinc
///
/// sinc(x) = sin(PI*x) / (PI*x)
Sinc,
/// Random value
Random,
Expand Down Expand Up @@ -122,14 +124,15 @@ fn sin(value: f64) -> f64 {
}

fn noise_sin(value: f64) -> f64 {
value.sin() + rand::random::<f64>() / f64::MAX * 0.2
value.sin() + random()

Check warning on line 127 in src/math.rs

View check run for this annotation

Codecov / codecov/patch

src/math.rs#L126-L127

Added lines #L126 - L127 were not covered by tests
}

fn random_spikes_sin(value: f64) -> f64 {
if value.sin() + rand::random::<f64>() / f64::MAX * 0.1 + rand::random::<f64>() / f64::MAX * 0.1
> 0.999
{
100.0
let v = noise_sin(value);

Check warning on line 131 in src/math.rs

View check run for this annotation

Codecov / codecov/patch

src/math.rs#L130-L131

Added lines #L130 - L131 were not covered by tests

// spike(x) = 1, if 0 ≤ x < 1, 0 elsewhere
if (0.0..0.999).contains(&v) {
1.0

Check warning on line 135 in src/math.rs

View check run for this annotation

Codecov / codecov/patch

src/math.rs#L134-L135

Added lines #L134 - L135 were not covered by tests
} else {
0.0

Check warning on line 137 in src/math.rs

View check run for this annotation

Codecov / codecov/patch

src/math.rs#L137

Added line #L137 was not covered by tests
}
Expand All @@ -156,7 +159,6 @@ fn rect(value: f64) -> f64 {
}
}

/// Normalized sinc function: sin(PI*x) / (PI*x)
fn sinc(value: f64) -> f64 {
if value == 0.0 {
1.0

Check warning on line 164 in src/math.rs

View check run for this annotation

Codecov / codecov/patch

src/math.rs#L162-L164

Added lines #L162 - L164 were not covered by tests
Expand All @@ -167,11 +169,11 @@ fn sinc(value: f64) -> f64 {
}

fn random() -> f64 {
rand::random::<f64>() / f64::MAX
rand::random::<f64>()

Check warning on line 172 in src/math.rs

View check run for this annotation

Codecov / codecov/patch

src/math.rs#L171-L172

Added lines #L171 - L172 were not covered by tests
}

fn random_interval() -> f64 {
(rand::random::<f64>() % 600.0) * 1000.0 + (rand::random::<f64>() % 1000.0)
(random() * 1000.0) % 600.0 + random()

Check warning on line 176 in src/math.rs

View check run for this annotation

Codecov / codecov/patch

src/math.rs#L175-L176

Added lines #L175 - L176 were not covered by tests
}

fn default(value: f64) -> f64 {
Expand Down

0 comments on commit ba6c64d

Please sign in to comment.