Skip to content

Commit 0f4b388

Browse files
committed
Fix issues found in code review, update gitignore to ignore Cargo.lock
1 parent a7bb231 commit 0f4b388

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.idea/
22
target/
3-
3+
Cargo.lock

utilities/src/lowpass.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ impl LowPassFilter {
3434
/// Create a lowpass filter from cutoff frequency and sample rate
3535
#[inline]
3636
pub fn from_frequency(cutoff_hz: f64, sample_rate: f64) -> Self {
37-
// Avoid division by zero
38-
if sample_rate.abs() < 1e-9 {
39-
return LowPassFilter::from_alpha(0.0);
37+
// Reject invalid or effectively-zero parameters
38+
if sample_rate < 1e-9 || cutoff_hz < 1e-9 {
39+
return Self::from_alpha(0.0);
4040
}
4141

4242
let dt = 1.0 / sample_rate;
@@ -50,7 +50,7 @@ impl LowPassFilter {
5050
}
5151
}
5252

53-
/// Create lowa pass filter from alpha coefficient
53+
/// Create a lowpass filter from alpha coefficient
5454
#[inline]
5555
pub fn from_alpha(filter_alpha: f64) -> Self {
5656
let clamped_alpha = filter_alpha.clamp(0.0, 1.0);
@@ -60,23 +60,23 @@ impl LowPassFilter {
6060
}
6161
}
6262

63-
/// Update lowpass with new sample
63+
/// Update lowpass with a new sample
6464
#[inline]
6565
pub fn update(&mut self, input: f64) -> f64 {
6666
let new_value = self.value * (1.0 - self.alpha) + self.alpha * input;
6767
self.value = new_value;
6868
self.value
6969
}
7070

71-
/// Get current filtered value
71+
/// Get the current filtered value
7272
#[inline]
73-
pub fn get_value(&self) -> f64 {
73+
pub fn value(&self) -> f64 {
7474
self.value
7575
}
7676

7777
/// Get alpha coefficient
7878
#[inline]
79-
pub fn get_alpha(&self) -> f64 {
79+
pub fn alpha(&self) -> f64 {
8080
self.alpha
8181
}
8282
}
@@ -94,7 +94,7 @@ mod tests {
9494
}
9595
// Should converge near 3.0
9696

97-
assert!((filter.get_value() - 3.0).abs() < KINDA_SMALL_NUMBER);
97+
assert!((filter.value() - 3.0).abs() < KINDA_SMALL_NUMBER);
9898
}
9999

100100
#[test]
@@ -116,7 +116,7 @@ mod tests {
116116
let rc = 1.0 / (2.0 * std::f64::consts::PI * cutoff_hz);
117117
let expected_alpha = dt / (rc + dt);
118118

119-
assert!((filter.get_alpha() - expected_alpha).abs() < KINDA_SMALL_NUMBER);
119+
assert!((filter.alpha() - expected_alpha).abs() < KINDA_SMALL_NUMBER);
120120
}
121121

122122
#[test]
@@ -125,7 +125,7 @@ mod tests {
125125

126126
for (cutoff_hz, sample_rate) in cases {
127127
let filter = LowPassFilter::from_frequency(cutoff_hz, sample_rate);
128-
let alpha = filter.get_alpha();
128+
let alpha = filter.alpha();
129129
assert!(
130130
(0.0..=1.0).contains(&alpha),
131131
"alpha out of range for cutoff {cutoff_hz}, sample_rate {sample_rate}"
@@ -138,13 +138,13 @@ mod tests {
138138
let sample_rate = 100.0;
139139
let low = LowPassFilter::from_frequency(1.0, sample_rate);
140140
let high = LowPassFilter::from_frequency(10.0, sample_rate);
141-
assert!(high.get_alpha() > low.get_alpha());
141+
assert!(high.alpha() > low.alpha());
142142
}
143143

144144
#[test]
145145
fn step_response_moves_toward_input_without_overshoot() {
146146
let mut filter = LowPassFilter::from_alpha(0.25);
147-
let mut last = filter.get_value();
147+
let mut last = filter.value();
148148
for _ in 0..50 {
149149
let value = filter.update(1.0);
150150
assert!(value >= last, "response should be non-decreasing");
@@ -159,7 +159,7 @@ mod tests {
159159
let near_zero = LowPassFilter::from_frequency(1e-9, sample_rate);
160160
let near_one = LowPassFilter::from_frequency(1e9, sample_rate);
161161

162-
assert!(near_zero.get_alpha() < KINDA_SMALL_NUMBER);
163-
assert!((1.0 - near_one.get_alpha()) < KINDA_SMALL_NUMBER);
162+
assert!(near_zero.alpha() < KINDA_SMALL_NUMBER);
163+
assert!((1.0 - near_one.alpha()) < KINDA_SMALL_NUMBER);
164164
}
165165
}

0 commit comments

Comments
 (0)