audit fix and code stability improvement
This commit is contained in:
@@ -38,7 +38,7 @@ impl OptimizerEngine {
|
||||
Self { window_size }
|
||||
}
|
||||
|
||||
/// Applies a simple moving average (SMA) filter to a stream of values.
|
||||
/// Applies a simple moving average (SMA) filter with outlier rejection.
|
||||
pub fn smooth(&self, data: &[f32]) -> Vec<f32> {
|
||||
if data.is_empty() { return vec![]; }
|
||||
let mut smoothed = Vec::with_capacity(data.len());
|
||||
@@ -46,8 +46,19 @@ impl OptimizerEngine {
|
||||
for i in 0..data.len() {
|
||||
let start = if i < self.window_size { 0 } else { i - self.window_size + 1 };
|
||||
let end = i + 1;
|
||||
let sum: f32 = data[start..end].iter().sum();
|
||||
smoothed.push(sum / (end - start) as f32);
|
||||
|
||||
// Outlier rejection: only average values within a reasonable range
|
||||
let window = &data[start..end];
|
||||
let avg: f32 = window.iter().sum::<f32>() / window.len() as f32;
|
||||
let filtered: Vec<f32> = window.iter()
|
||||
.filter(|&&v| (v - avg).abs() < 20.0) // Reject spikes > 20 units
|
||||
.cloned().collect();
|
||||
|
||||
if filtered.is_empty() {
|
||||
smoothed.push(avg);
|
||||
} else {
|
||||
smoothed.push(filtered.iter().sum::<f32>() / filtered.len() as f32);
|
||||
}
|
||||
}
|
||||
smoothed
|
||||
}
|
||||
@@ -55,11 +66,9 @@ impl OptimizerEngine {
|
||||
/// Calculates Thermal Resistance: R_theta = (T_core - T_ambient) / P_package
|
||||
pub fn calculate_thermal_resistance(&self, profile: &ThermalProfile) -> f32 {
|
||||
profile.points.iter()
|
||||
.filter(|p| p.power_w > 1.0 && p.temp_c > 30.0) // Filter invalid data
|
||||
.max_by(|a, b| a.power_w.partial_cmp(&b.power_w).unwrap_or(std::cmp::Ordering::Equal))
|
||||
.map(|p| {
|
||||
if p.power_w < 1.0 { 0.0 }
|
||||
else { (p.temp_c - profile.ambient_temp) / p.power_w }
|
||||
})
|
||||
.map(|p| (p.temp_c - profile.ambient_temp) / p.power_w)
|
||||
.unwrap_or(0.0)
|
||||
}
|
||||
|
||||
@@ -73,11 +82,16 @@ impl OptimizerEngine {
|
||||
/// Finds the "Silicon Knee" - the point where performance per watt (efficiency)
|
||||
/// starts to diminish significantly and thermal density spikes.
|
||||
pub fn find_silicon_knee(&self, profile: &ThermalProfile) -> f32 {
|
||||
if profile.points.len() < 3 {
|
||||
let valid_points: Vec<_> = profile.points.iter()
|
||||
.filter(|p| p.power_w > 5.0 && p.temp_c > 40.0) // Filter idle/noise
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
if valid_points.len() < 3 {
|
||||
return profile.points.last().map(|p| p.power_w).unwrap_or(15.0);
|
||||
}
|
||||
|
||||
let mut points = profile.points.clone();
|
||||
let mut points = valid_points;
|
||||
points.sort_by(|a, b| a.power_w.partial_cmp(&b.power_w).unwrap_or(std::cmp::Ordering::Equal));
|
||||
|
||||
let mut best_pl = points[0].power_w;
|
||||
|
||||
Reference in New Issue
Block a user