Skip to main content

math_audio_dsp/
analysis.rs

1//! FFT-based frequency analysis for recorded signals
2//!
3//! This module provides functions to analyze recorded audio signals and extract:
4//! - Frequency spectrum (magnitude in dBFS)
5//! - Phase spectrum (compensated for latency)
6//! - Latency estimation via cross-correlation
7//! - Microphone compensation for calibrated measurements
8//! - Standalone WAV buffer analysis (wav2csv functionality)
9
10use hound::WavReader;
11use math_audio_iir_fir::{Biquad, BiquadFilterType};
12use rustfft::FftPlanner;
13use rustfft::num_complex::Complex;
14use std::cell::RefCell;
15use std::f32::consts::PI;
16use std::io::Write;
17use std::path::Path;
18use std::sync::Arc;
19
20/// Spectrum result: (frequencies, magnitudes_db, phases_deg)
21type SpectrumResult = Result<(Vec<f32>, Vec<f32>, Vec<f32>), String>;
22
23thread_local! {
24    static FFT_PLANNER: RefCell<FftPlanner<f32>> = RefCell::new(FftPlanner::new());
25}
26
27/// Get a cached forward FFT plan for the given size (f32).
28///
29/// Uses a thread-local planner so repeated calls with the same size
30/// return the same plan without recomputing twiddle factors.
31pub fn plan_fft_forward(size: usize) -> Arc<dyn rustfft::Fft<f32>> {
32    FFT_PLANNER.with(|p| p.borrow_mut().plan_fft_forward(size))
33}
34
35/// Get a cached inverse FFT plan for the given size (f32).
36pub fn plan_fft_inverse(size: usize) -> Arc<dyn rustfft::Fft<f32>> {
37    FFT_PLANNER.with(|p| p.borrow_mut().plan_fft_inverse(size))
38}
39
40/// Microphone compensation data (frequency response correction)
41#[derive(Debug, Clone)]
42pub struct MicrophoneCompensation {
43    /// Frequency points in Hz
44    pub frequencies: Vec<f32>,
45    /// SPL deviation in dB (positive = mic is louder, negative = mic is quieter)
46    pub spl_db: Vec<f32>,
47}
48
49impl MicrophoneCompensation {
50    /// Apply pre-compensation to a sweep signal
51    ///
52    /// For log sweeps, this modulates the amplitude based on the instantaneous frequency
53    /// to pre-compensate for the microphone's response.
54    ///
55    /// # Arguments
56    /// * `signal` - The sweep signal to compensate
57    /// * `start_freq` - Start frequency of the sweep in Hz
58    /// * `end_freq` - End frequency of the sweep in Hz
59    /// * `sample_rate` - Sample rate in Hz
60    /// * `inverse` - If true, applies inverse compensation (boost where mic is weak)
61    ///
62    /// # Returns
63    /// Pre-compensated signal
64    pub fn apply_to_sweep(
65        &self,
66        signal: &[f32],
67        start_freq: f32,
68        end_freq: f32,
69        sample_rate: u32,
70        inverse: bool,
71    ) -> Vec<f32> {
72        let duration = signal.len() as f32 / sample_rate as f32;
73        let mut compensated = Vec::with_capacity(signal.len());
74
75        // Debug: print some sample points
76        let debug_points = [0, signal.len() / 4, signal.len() / 2, 3 * signal.len() / 4];
77
78        for (i, &sample) in signal.iter().enumerate() {
79            let t = i as f32 / sample_rate as f32;
80
81            // Compute instantaneous frequency for log sweep
82            // f(t) = f0 * exp(t * ln(f1/f0) / T)
83            let freq = start_freq * ((t * (end_freq / start_freq).ln()) / duration).exp();
84
85            // Get compensation at this frequency (in dB)
86            let comp_db = self.interpolate_at(freq);
87
88            // Apply inverse or direct compensation
89            let gain_db = if inverse { -comp_db } else { comp_db };
90
91            // Convert dB to linear gain
92            let gain = 10_f32.powf(gain_db / 20.0);
93
94            // Debug output for sample points
95            if debug_points.contains(&i) {
96                log::debug!(
97                    "[apply_to_sweep] t={:.3}s, freq={:.1}Hz, comp_db={:.2}dB, gain_db={:.2}dB, gain={:.3}x",
98                    t,
99                    freq,
100                    comp_db,
101                    gain_db,
102                    gain
103                );
104            }
105
106            compensated.push(sample * gain);
107        }
108
109        log::debug!(
110            "[apply_to_sweep] Processed {} samples, duration={:.2}s",
111            signal.len(),
112            duration
113        );
114        compensated
115    }
116
117    /// Load microphone compensation from a CSV or TXT file
118    ///
119    /// File format:
120    /// - CSV: frequency_hz,spl_db (with or without header, comma-separated)
121    /// - TXT: freq spl (space/tab-separated, no header assumed)
122    pub fn from_file(path: &Path) -> Result<Self, String> {
123        use std::fs::File;
124        use std::io::{BufRead, BufReader};
125
126        log::debug!("[MicrophoneCompensation] Loading from {:?}", path);
127
128        let file = File::open(path)
129            .map_err(|e| format!("Failed to open compensation file {:?}: {}", path, e))?;
130        let reader = BufReader::new(file);
131
132        // Determine if this is a .txt file (no header expected)
133        let is_txt_file = path
134            .extension()
135            .and_then(|e| e.to_str())
136            .map(|e| e.to_lowercase() == "txt")
137            .unwrap_or(false);
138
139        if is_txt_file {
140            log::info!(
141                "[MicrophoneCompensation] Detected .txt file - assuming space/tab-separated without header"
142            );
143        }
144
145        let mut frequencies = Vec::new();
146        let mut spl_db = Vec::new();
147
148        for (line_num, line) in reader.lines().enumerate() {
149            let line = line.map_err(|e| format!("Failed to read line {}: {}", line_num + 1, e))?;
150            let line = line.trim();
151
152            // Skip empty lines and comments
153            if line.is_empty() || line.starts_with('#') {
154                continue;
155            }
156
157            // For CSV files, skip header line
158            if !is_txt_file && line.starts_with("frequency") {
159                continue;
160            }
161
162            // For TXT files, skip lines that don't start with a number
163            if is_txt_file {
164                let first_char = line.chars().next().unwrap_or(' ');
165                if !first_char.is_ascii_digit() && first_char != '-' && first_char != '+' {
166                    log::info!(
167                        "[MicrophoneCompensation] Skipping non-numeric line {}: '{}'",
168                        line_num + 1,
169                        line
170                    );
171                    continue;
172                }
173            }
174
175            // Parse based on file type with auto-detection for TXT
176            let parts: Vec<&str> = if is_txt_file {
177                // TXT: Try to auto-detect separator
178                // First, try comma (in case it's mislabeled CSV)
179                let comma_parts: Vec<&str> = line.split(',').map(|s| s.trim()).collect();
180                if comma_parts.len() >= 2
181                    && comma_parts[0].parse::<f32>().is_ok()
182                    && comma_parts[1].parse::<f32>().is_ok()
183                {
184                    comma_parts
185                } else {
186                    // Try tab
187                    let tab_parts: Vec<&str> = line.split('\t').map(|s| s.trim()).collect();
188                    if tab_parts.len() >= 2
189                        && tab_parts[0].parse::<f32>().is_ok()
190                        && tab_parts[1].parse::<f32>().is_ok()
191                    {
192                        tab_parts
193                    } else {
194                        // Fall back to whitespace
195                        line.split_whitespace().collect()
196                    }
197                }
198            } else {
199                // CSV: comma separated
200                line.split(',').collect()
201            };
202
203            if parts.len() < 2 {
204                let separator = if is_txt_file {
205                    "separator (comma/tab/space)"
206                } else {
207                    "comma"
208                };
209                return Err(format!(
210                    "Invalid format at line {}: expected {} with 2+ values but got '{}'",
211                    line_num + 1,
212                    separator,
213                    line
214                ));
215            }
216
217            let freq: f32 = parts[0]
218                .trim()
219                .parse()
220                .map_err(|e| format!("Invalid frequency at line {}: {}", line_num + 1, e))?;
221            let spl: f32 = parts[1]
222                .trim()
223                .parse()
224                .map_err(|e| format!("Invalid SPL at line {}: {}", line_num + 1, e))?;
225
226            frequencies.push(freq);
227            spl_db.push(spl);
228        }
229
230        if frequencies.is_empty() {
231            return Err(format!("No compensation data found in {:?}", path));
232        }
233
234        // Validate that frequencies are sorted
235        for i in 1..frequencies.len() {
236            if frequencies[i] <= frequencies[i - 1] {
237                return Err(format!(
238                    "Frequencies must be strictly increasing: found {} after {} at line {}",
239                    frequencies[i],
240                    frequencies[i - 1],
241                    i + 1
242                ));
243            }
244        }
245
246        log::info!(
247            "[MicrophoneCompensation] Loaded {} calibration points: {:.1} Hz - {:.1} Hz",
248            frequencies.len(),
249            frequencies[0],
250            frequencies[frequencies.len() - 1]
251        );
252        log::info!(
253            "[MicrophoneCompensation] SPL range: {:.2} dB to {:.2} dB",
254            spl_db.iter().fold(f32::INFINITY, |a, &b| a.min(b)),
255            spl_db.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b))
256        );
257
258        Ok(Self {
259            frequencies,
260            spl_db,
261        })
262    }
263
264    /// Interpolate compensation value at a given frequency
265    ///
266    /// Uses linear interpolation in dB domain.
267    /// Returns 0.0 for frequencies outside the calibration range.
268    pub fn interpolate_at(&self, freq: f32) -> f32 {
269        if freq < self.frequencies[0] || freq > self.frequencies[self.frequencies.len() - 1] {
270            // Outside calibration range - no compensation
271            return 0.0;
272        }
273
274        // Find the two nearest points
275        let idx = match self
276            .frequencies
277            .binary_search_by(|f| f.partial_cmp(&freq).unwrap_or(std::cmp::Ordering::Equal))
278        {
279            Ok(i) => return self.spl_db[i], // Exact match
280            Err(i) => i,
281        };
282
283        if idx == 0 {
284            return self.spl_db[0];
285        }
286        if idx >= self.frequencies.len() {
287            return self.spl_db[self.frequencies.len() - 1];
288        }
289
290        // Linear interpolation
291        let f0 = self.frequencies[idx - 1];
292        let f1 = self.frequencies[idx];
293        let s0 = self.spl_db[idx - 1];
294        let s1 = self.spl_db[idx];
295
296        let t = (freq - f0) / (f1 - f0);
297        s0 + t * (s1 - s0)
298    }
299}
300
301// ============================================================================
302// WAV Buffer Analysis (wav2csv functionality)
303// ============================================================================
304
305/// Configuration for standalone WAV buffer analysis
306#[derive(Debug, Clone)]
307pub struct WavAnalysisConfig {
308    /// Number of output frequency points (default: 2000)
309    pub num_points: usize,
310    /// Minimum frequency in Hz (default: 20)
311    pub min_freq: f32,
312    /// Maximum frequency in Hz (default: 20000)
313    pub max_freq: f32,
314    /// FFT size (if None, auto-computed based on signal length and mode)
315    pub fft_size: Option<usize>,
316    /// Window overlap ratio for Welch's method (0.0-1.0, default: 0.5)
317    pub overlap: f32,
318    /// Use single FFT instead of Welch's method (better for sweeps and impulse responses)
319    pub single_fft: bool,
320    /// Apply pink compensation (-3dB/octave) for log sweeps
321    pub pink_compensation: bool,
322    /// Use rectangular window instead of Hann
323    pub no_window: bool,
324}
325
326impl Default for WavAnalysisConfig {
327    fn default() -> Self {
328        Self {
329            num_points: 2000,
330            min_freq: 20.0,
331            max_freq: 20000.0,
332            fft_size: None,
333            overlap: 0.5,
334            single_fft: false,
335            pink_compensation: false,
336            no_window: false,
337        }
338    }
339}
340
341impl WavAnalysisConfig {
342    /// Create config optimized for log sweep analysis
343    pub fn for_log_sweep() -> Self {
344        Self {
345            single_fft: true,
346            pink_compensation: true,
347            no_window: true,
348            ..Default::default()
349        }
350    }
351
352    /// Create config optimized for impulse response analysis
353    pub fn for_impulse_response() -> Self {
354        Self {
355            single_fft: true,
356            ..Default::default()
357        }
358    }
359
360    /// Create config for stationary signals (music, noise)
361    pub fn for_stationary() -> Self {
362        Self::default()
363    }
364}
365
366/// Result of standalone WAV buffer analysis
367#[derive(Debug, Clone)]
368pub struct WavAnalysisOutput {
369    /// Frequency points in Hz (log-spaced)
370    pub frequencies: Vec<f32>,
371    /// Magnitude in dB
372    pub magnitude_db: Vec<f32>,
373    /// Phase in degrees
374    pub phase_deg: Vec<f32>,
375}
376
377/// Analyze a buffer of audio samples and return frequency response
378///
379/// # Arguments
380/// * `samples` - Mono audio samples (f32, -1.0 to 1.0)
381/// * `sample_rate` - Sample rate in Hz
382/// * `config` - Analysis configuration
383///
384/// # Returns
385/// Analysis result with frequency, magnitude, and phase data
386pub fn analyze_wav_buffer(
387    samples: &[f32],
388    sample_rate: u32,
389    config: &WavAnalysisConfig,
390) -> Result<WavAnalysisOutput, String> {
391    if samples.is_empty() {
392        return Err("Signal is empty".to_string());
393    }
394
395    // Determine FFT size
396    let fft_size = if config.single_fft {
397        config
398            .fft_size
399            .unwrap_or_else(|| wav_next_power_of_two(samples.len()))
400    } else {
401        config.fft_size.unwrap_or(16384)
402    };
403
404    // Compute spectrum
405    let (freqs, magnitudes_db, phases_deg) = if config.single_fft {
406        compute_single_fft_spectrum_internal(samples, sample_rate, fft_size, config.no_window)?
407    } else {
408        compute_welch_spectrum_internal(samples, sample_rate, fft_size, config.overlap)?
409    };
410
411    // Generate logarithmically spaced frequency points
412    let log_freqs = generate_log_frequencies(config.num_points, config.min_freq, config.max_freq);
413
414    // Interpolate magnitude and phase at log frequencies
415    let mut interp_mag = interpolate_log(&freqs, &magnitudes_db, &log_freqs);
416    let interp_phase = interpolate_log_phase(&freqs, &phases_deg, &log_freqs);
417
418    // Apply pink compensation if requested (for log sweeps)
419    if config.pink_compensation {
420        let ref_freq = 1000.0;
421        for (i, freq) in log_freqs.iter().enumerate() {
422            if *freq > 0.0 {
423                let correction = 10.0 * (freq / ref_freq).log10();
424                interp_mag[i] += correction;
425            }
426        }
427    }
428
429    Ok(WavAnalysisOutput {
430        frequencies: log_freqs,
431        magnitude_db: interp_mag,
432        phase_deg: interp_phase,
433    })
434}
435
436/// Analyze a WAV file and return frequency response
437///
438/// # Arguments
439/// * `path` - Path to WAV file
440/// * `config` - Analysis configuration
441///
442/// # Returns
443/// Analysis result with frequency, magnitude, and phase data
444pub fn analyze_wav_file(
445    path: &Path,
446    config: &WavAnalysisConfig,
447) -> Result<WavAnalysisOutput, String> {
448    let (samples, sample_rate) = load_wav_mono_with_rate(path)?;
449    analyze_wav_buffer(&samples, sample_rate, config)
450}
451
452/// Load WAV file as mono and return samples with sample rate
453fn load_wav_mono_with_rate(path: &Path) -> Result<(Vec<f32>, u32), String> {
454    let mut reader =
455        WavReader::open(path).map_err(|e| format!("Failed to open WAV file: {}", e))?;
456
457    let spec = reader.spec();
458    let sample_rate = spec.sample_rate;
459    let channels = spec.channels as usize;
460
461    let samples: Result<Vec<f32>, _> = match spec.sample_format {
462        hound::SampleFormat::Float => reader.samples::<f32>().collect(),
463        hound::SampleFormat::Int => {
464            let max_val = (1_i64 << (spec.bits_per_sample - 1)) as f32;
465            reader
466                .samples::<i32>()
467                .map(|s| s.map(|v| v as f32 / max_val))
468                .collect()
469        }
470    };
471
472    let samples = samples.map_err(|e| format!("Failed to read samples: {}", e))?;
473
474    // Convert to mono by averaging channels
475    let mono = if channels == 1 {
476        samples
477    } else {
478        samples
479            .chunks(channels)
480            .map(|chunk| chunk.iter().sum::<f32>() / channels as f32)
481            .collect()
482    };
483
484    Ok((mono, sample_rate))
485}
486
487/// Write WAV analysis result to CSV file
488///
489/// # Arguments
490/// * `result` - Analysis output
491/// * `path` - Path to output CSV file
492pub fn write_wav_analysis_csv(result: &WavAnalysisOutput, path: &Path) -> Result<(), String> {
493    let mut file =
494        std::fs::File::create(path).map_err(|e| format!("Failed to create CSV: {}", e))?;
495
496    writeln!(file, "frequency_hz,spl_db,phase_deg")
497        .map_err(|e| format!("Failed to write CSV header: {}", e))?;
498
499    for i in 0..result.frequencies.len() {
500        writeln!(
501            file,
502            "{:.2},{:.2},{:.2}",
503            result.frequencies[i], result.magnitude_db[i], result.phase_deg[i]
504        )
505        .map_err(|e| format!("Failed to write CSV row: {}", e))?;
506    }
507
508    Ok(())
509}
510
511/// Compute spectrum using Welch's method (averaged periodograms) - internal version
512fn compute_welch_spectrum_internal(
513    signal: &[f32],
514    sample_rate: u32,
515    fft_size: usize,
516    overlap: f32,
517) -> SpectrumResult {
518    if signal.is_empty() {
519        return Err("Signal is empty".to_string());
520    }
521
522    let overlap_samples = (fft_size as f32 * overlap.clamp(0.0, 0.95)) as usize;
523    let hop_size = fft_size - overlap_samples;
524
525    let num_windows = if signal.len() >= fft_size {
526        1 + (signal.len() - fft_size) / hop_size
527    } else {
528        1
529    };
530
531    let num_bins = fft_size / 2;
532    let mut magnitude_sum = vec![0.0_f32; num_bins];
533    let mut phase_real_sum = vec![0.0_f32; num_bins];
534    let mut phase_imag_sum = vec![0.0_f32; num_bins];
535
536    // Precompute symmetric Hann window (N-1 divisor for spectral analysis)
537    let hann_window = crate::stft::generate_hann_window_symmetric(fft_size);
538
539    let window_power: f32 = hann_window.iter().map(|&w| w * w).sum();
540    let scale_factor = 2.0 / window_power;
541
542    let fft = plan_fft_forward(fft_size);
543
544    let mut windowed = vec![0.0_f32; fft_size];
545    let mut buffer = vec![Complex::new(0.0, 0.0); fft_size];
546
547    for window_idx in 0..num_windows {
548        let start = window_idx * hop_size;
549        let end = (start + fft_size).min(signal.len());
550        let window_len = end - start;
551
552        // Apply window
553        for i in 0..window_len {
554            windowed[i] = signal[start + i] * hann_window[i];
555        }
556        // Zero-pad the rest if necessary
557        windowed[window_len..fft_size].fill(0.0);
558
559        // Convert to complex
560        for (i, &val) in windowed.iter().enumerate() {
561            buffer[i] = Complex::new(val, 0.0);
562        }
563
564        fft.process(&mut buffer);
565
566        for i in 0..num_bins {
567            let mag = buffer[i].norm() * scale_factor.sqrt();
568            magnitude_sum[i] += mag * mag;
569            phase_real_sum[i] += buffer[i].re;
570            phase_imag_sum[i] += buffer[i].im;
571        }
572    }
573
574    let magnitudes_db: Vec<f32> = magnitude_sum
575        .iter()
576        .map(|&mag_sq| {
577            let mag = (mag_sq / num_windows as f32).sqrt();
578            if mag > 1e-10 {
579                20.0 * mag.log10()
580            } else {
581                -200.0
582            }
583        })
584        .collect();
585
586    let phases_deg: Vec<f32> = phase_real_sum
587        .iter()
588        .zip(phase_imag_sum.iter())
589        .map(|(&re, &im)| (im / num_windows as f32).atan2(re / num_windows as f32) * 180.0 / PI)
590        .collect();
591
592    let freqs: Vec<f32> = (0..num_bins)
593        .map(|i| i as f32 * sample_rate as f32 / fft_size as f32)
594        .collect();
595
596    Ok((freqs, magnitudes_db, phases_deg))
597}
598
599/// Compute spectrum using a single FFT - internal version
600fn compute_single_fft_spectrum_internal(
601    signal: &[f32],
602    sample_rate: u32,
603    fft_size: usize,
604    no_window: bool,
605) -> SpectrumResult {
606    if signal.is_empty() {
607        return Err("Signal is empty".to_string());
608    }
609
610    let mut windowed = vec![0.0_f32; fft_size];
611    let copy_len = signal.len().min(fft_size);
612    windowed[..copy_len].copy_from_slice(&signal[..copy_len]);
613
614    let window_scale_factor = if no_window {
615        1.0
616    } else {
617        let hann_window = crate::stft::generate_hann_window_symmetric(fft_size);
618
619        for (i, sample) in windowed.iter_mut().enumerate() {
620            *sample *= hann_window[i];
621        }
622
623        hann_window.iter().map(|&w| w * w).sum::<f32>()
624    };
625
626    let mut buffer: Vec<Complex<f32>> = windowed.iter().map(|&x| Complex::new(x, 0.0)).collect();
627
628    let fft = plan_fft_forward(fft_size);
629    fft.process(&mut buffer);
630
631    let scale_factor = if no_window {
632        (2.0 / fft_size as f32).sqrt()
633    } else {
634        (2.0 / window_scale_factor).sqrt()
635    };
636
637    let num_bins = fft_size / 2;
638    let magnitudes_db: Vec<f32> = buffer[..num_bins]
639        .iter()
640        .map(|c| {
641            let mag = c.norm() * scale_factor;
642            if mag > 1e-10 {
643                20.0 * mag.log10()
644            } else {
645                -200.0
646            }
647        })
648        .collect();
649
650    let phases_deg: Vec<f32> = buffer[..num_bins]
651        .iter()
652        .map(|c| c.arg() * 180.0 / PI)
653        .collect();
654
655    let freqs: Vec<f32> = (0..num_bins)
656        .map(|i| i as f32 * sample_rate as f32 / fft_size as f32)
657        .collect();
658
659    Ok((freqs, magnitudes_db, phases_deg))
660}
661
662/// Next power of two for wav analysis (capped at 1M)
663fn wav_next_power_of_two(n: usize) -> usize {
664    let mut p = 1;
665    while p < n {
666        p *= 2;
667    }
668    p.min(1048576)
669}
670
671/// Generate logarithmically spaced frequencies
672fn generate_log_frequencies(num_points: usize, min_freq: f32, max_freq: f32) -> Vec<f32> {
673    let log_min = min_freq.ln();
674    let log_max = max_freq.ln();
675    let step = (log_max - log_min) / (num_points - 1) as f32;
676
677    (0..num_points)
678        .map(|i| (log_min + i as f32 * step).exp())
679        .collect()
680}
681
682/// Logarithmic interpolation
683fn interpolate_log(x: &[f32], y: &[f32], x_new: &[f32]) -> Vec<f32> {
684    x_new
685        .iter()
686        .map(|&freq| {
687            let idx = x.partition_point(|&f| f < freq).min(x.len() - 1);
688
689            if idx == 0 {
690                return y[0];
691            }
692
693            let x0 = x[idx - 1];
694            let x1 = x[idx];
695            let y0 = y[idx - 1];
696            let y1 = y[idx];
697
698            if x1 <= x0 {
699                return y0;
700            }
701
702            let t = (freq - x0) / (x1 - x0);
703            y0 + t * (y1 - y0)
704        })
705        .collect()
706}
707
708/// Logarithmic interpolation for phase data (degrees).
709/// Uses circular interpolation to correctly handle ±180° wrap boundaries.
710fn interpolate_log_phase(x: &[f32], phase_deg: &[f32], x_new: &[f32]) -> Vec<f32> {
711    x_new
712        .iter()
713        .map(|&freq| {
714            let idx = x.partition_point(|&f| f < freq).min(x.len() - 1);
715
716            if idx == 0 {
717                return phase_deg[0];
718            }
719
720            let x0 = x[idx - 1];
721            let x1 = x[idx];
722
723            if x1 <= x0 {
724                return phase_deg[idx - 1];
725            }
726
727            let t = (freq - x0) / (x1 - x0);
728
729            // Circular interpolation: find shortest arc between the two angles
730            let p0 = phase_deg[idx - 1];
731            let p1 = phase_deg[idx];
732            let mut diff = p1 - p0;
733            // Wrap diff to [-180, 180]
734            diff -= 360.0 * (diff / 360.0).round();
735            p0 + t * diff
736        })
737        .collect()
738}
739
740// ============================================================================
741// Recording Analysis (reference vs recorded comparison)
742// ============================================================================
743
744/// Result of FFT analysis
745#[derive(Debug, Clone)]
746pub struct AnalysisResult {
747    /// Frequency bins in Hz
748    pub frequencies: Vec<f32>,
749    /// Magnitude in dBFS
750    pub spl_db: Vec<f32>,
751    /// Phase in degrees (compensated for latency)
752    pub phase_deg: Vec<f32>,
753    /// Estimated latency in samples
754    pub estimated_lag_samples: isize,
755    /// Impulse response (time domain)
756    pub impulse_response: Vec<f32>,
757    /// Time vector for impulse response in ms
758    pub impulse_time_ms: Vec<f32>,
759    /// Excess group delay in ms
760    pub excess_group_delay_ms: Vec<f32>,
761    /// Total Harmonic Distortion + Noise (%)
762    pub thd_percent: Vec<f32>,
763    /// Harmonic distortion curves (2nd, 3rd, etc) in dB
764    pub harmonic_distortion_db: Vec<Vec<f32>>,
765    /// RT60 decay time in ms
766    pub rt60_ms: Vec<f32>,
767    /// Clarity C50 in dB
768    pub clarity_c50_db: Vec<f32>,
769    /// Clarity C80 in dB
770    pub clarity_c80_db: Vec<f32>,
771    /// Spectrogram (Time x Freq magnitude in dB)
772    pub spectrogram_db: Vec<Vec<f32>>,
773}
774
775/// Analyze a recorded WAV file against a reference signal
776///
777/// # Arguments
778/// * `recorded_path` - Path to the recorded WAV file
779/// * `reference_signal` - Reference signal (should match the signal used for playback)
780/// * `sample_rate` - Sample rate in Hz
781/// * `sweep_range` - Optional (start_freq, end_freq) if the signal is a log sweep
782///
783/// # Returns
784/// Analysis result with frequency, SPL, and phase data
785pub fn analyze_recording(
786    recorded_path: &Path,
787    reference_signal: &[f32],
788    sample_rate: u32,
789    sweep_range: Option<(f32, f32)>,
790) -> Result<AnalysisResult, String> {
791    // Load recorded WAV
792    log::debug!("[FFT Analysis] Loading recorded file: {:?}", recorded_path);
793    let recorded = load_wav_mono(recorded_path)?;
794    log::debug!(
795        "[FFT Analysis] Loaded {} samples from recording",
796        recorded.len()
797    );
798    log::debug!(
799        "[FFT Analysis] Reference has {} samples",
800        reference_signal.len()
801    );
802
803    if recorded.is_empty() {
804        return Err("Recorded signal is empty!".to_string());
805    }
806    if reference_signal.is_empty() {
807        return Err("Reference signal is empty!".to_string());
808    }
809
810    // Don't truncate yet - we need full signals for lag estimation
811    let recorded = &recorded[..];
812    let reference = reference_signal;
813
814    // Debug: Check signal statistics (guarded to skip O(n) computation when disabled)
815    if log::log_enabled!(log::Level::Debug) {
816        let ref_max = reference
817            .iter()
818            .map(|&x| x.abs())
819            .fold(0.0_f32, |a, b| a.max(b));
820        let rec_max = recorded
821            .iter()
822            .map(|&x| x.abs())
823            .fold(0.0_f32, |a, b| a.max(b));
824        let ref_rms =
825            (reference.iter().map(|&x| x * x).sum::<f32>() / reference.len() as f32).sqrt();
826        let rec_rms = (recorded.iter().map(|&x| x * x).sum::<f32>() / recorded.len() as f32).sqrt();
827
828        log::debug!(
829            "[FFT Analysis] Reference: max={:.4}, RMS={:.4}",
830            ref_max,
831            ref_rms
832        );
833        log::debug!(
834            "[FFT Analysis] Recorded:  max={:.4}, RMS={:.4}",
835            rec_max,
836            rec_rms
837        );
838        log::debug!(
839            "[FFT Analysis] First 5 reference samples: {:?}",
840            &reference[..5.min(reference.len())]
841        );
842        log::debug!(
843            "[FFT Analysis] First 5 recorded samples:  {:?}",
844            &recorded[..5.min(recorded.len())]
845        );
846
847        let check_len = reference.len().min(recorded.len());
848        let mut identical_count = 0;
849        for (r, c) in reference[..check_len]
850            .iter()
851            .zip(recorded[..check_len].iter())
852        {
853            if (r - c).abs() < 1e-6 {
854                identical_count += 1;
855            }
856        }
857        log::debug!(
858            "[FFT Analysis] Identical samples: {}/{} ({:.1}%)",
859            identical_count,
860            check_len,
861            identical_count as f32 * 100.0 / check_len as f32
862        );
863    }
864
865    // Estimate lag using cross-correlation
866    let lag = estimate_lag(reference, recorded)?;
867
868    log::debug!(
869        "[FFT Analysis] Estimated lag: {} samples ({:.2} ms)",
870        lag,
871        lag as f32 * 1000.0 / sample_rate as f32
872    );
873
874    // Time-align the signals before FFT
875    // If recorded is delayed (positive lag), skip the lag samples in recorded
876    let (aligned_ref, aligned_rec) = if lag >= 0 {
877        let lag_usize = lag as usize;
878        if lag_usize >= recorded.len() {
879            return Err("Lag is larger than recorded signal length".to_string());
880        }
881        // Capture full tail
882        (reference, &recorded[lag_usize..])
883    } else {
884        // Recorded leads reference - rare
885        let lag_usize = (-lag) as usize;
886        if lag_usize >= reference.len() {
887            return Err("Negative lag is larger than reference signal length".to_string());
888        }
889        // Pad reference start? No, just slice reference
890        (&reference[lag_usize..], recorded)
891    };
892
893    log::debug!(
894        "[FFT Analysis] Aligned lengths: ref={}, rec={} (tail included)",
895        aligned_ref.len(),
896        aligned_rec.len()
897    );
898
899    // Compute FFT size to include the longer of the two (usually rec with tail)
900    let fft_size = next_power_of_two(aligned_ref.len().max(aligned_rec.len()));
901
902    let ref_spectrum = compute_fft(aligned_ref, fft_size, WindowType::Tukey(0.1))?;
903    let rec_spectrum = compute_fft(aligned_rec, fft_size, WindowType::Tukey(0.1))?;
904
905    // Generate 2000 log-spaced frequency points between 20 Hz and 20 kHz
906    let num_output_points = 2000;
907    let log_start = 20.0_f32.ln();
908    let log_end = 20000.0_f32.ln();
909
910    let mut frequencies = Vec::with_capacity(num_output_points);
911    let mut spl_db = Vec::with_capacity(num_output_points);
912    let mut phase_deg = Vec::with_capacity(num_output_points);
913
914    let freq_resolution = sample_rate as f32 / fft_size as f32;
915    let num_bins = fft_size / 2; // Single-sided spectrum
916
917    // Compute regularization threshold relative to the peak reference energy.
918    // Bins where the reference has very little energy (e.g., disconnected speaker
919    // with a misaligned sweep) produce unreliable transfer functions — division by
920    // near-zero gives spurious high-dB peaks. We skip bins where the reference
921    // energy is more than 60 dB below the peak.
922    let ref_peak_mag_sq = ref_spectrum[1..num_bins.min(ref_spectrum.len())]
923        .iter()
924        .map(|c| c.norm_sqr())
925        .fold(0.0_f32, |a, b| a.max(b));
926    // 60 dB below peak = 10^(-6) in power
927    let ref_regularization_threshold = ref_peak_mag_sq * 1e-6;
928
929    // Apply 1/24 octave smoothing for each target frequency
930    let mut skipped_count = 0;
931    for i in 0..num_output_points {
932        // Log-spaced target frequency
933        let target_freq =
934            (log_start + (log_end - log_start) * i as f32 / (num_output_points - 1) as f32).exp();
935
936        // 1/24 octave bandwidth: +/- 1/48 octave around target frequency
937        // Lower and upper frequency bounds: f * 2^(+/- 1/48)
938        let octave_fraction = 1.0 / 48.0;
939        let freq_lower = target_freq * 2.0_f32.powf(-octave_fraction);
940        let freq_upper = target_freq * 2.0_f32.powf(octave_fraction);
941
942        // Find FFT bins within this frequency range
943        let bin_lower = ((freq_lower / freq_resolution).floor() as usize).max(1);
944        let bin_upper = ((freq_upper / freq_resolution).ceil() as usize).min(num_bins);
945
946        if bin_lower > bin_upper || bin_upper >= ref_spectrum.len() {
947            if skipped_count < 5 {
948                log::debug!(
949                    "[FFT Analysis] Skipping freq {:.1} Hz: bin_lower={}, bin_upper={}, ref_spectrum.len()={}",
950                    target_freq,
951                    bin_lower,
952                    bin_upper,
953                    ref_spectrum.len()
954                );
955            }
956            skipped_count += 1;
957            // Output noise-floor placeholder so all channels produce the same
958            // number of frequency points (prevents ndarray shape mismatches).
959            frequencies.push(target_freq);
960            spl_db.push(-200.0);
961            phase_deg.push(0.0);
962            continue;
963        }
964
965        // Average transfer function magnitude and phase across bins in the smoothing range
966        let mut sum_magnitude = 0.0;
967        let mut sum_sin = 0.0; // For circular averaging of phase
968        let mut sum_cos = 0.0;
969        let mut bin_count = 0;
970
971        for k in bin_lower..=bin_upper {
972            if k >= ref_spectrum.len() {
973                break;
974            }
975
976            // Compute transfer function: H(f) = recorded / reference
977            // This gives the system response (for loopback, should be ~1.0 or 0 dB)
978            // Skip bins where the reference energy is too low (>60 dB below peak):
979            // dividing by near-zero produces unreliable, spuriously high values
980            // (e.g., disconnected speaker where the recording is just noise).
981            let ref_mag_sq = ref_spectrum[k].norm_sqr();
982            if ref_mag_sq <= ref_regularization_threshold {
983                continue;
984            }
985            let transfer_function = rec_spectrum[k] / ref_spectrum[k];
986            let magnitude = transfer_function.norm();
987
988            // Phase from cross-spectrum (signals are already time-aligned)
989            let cross_spectrum = ref_spectrum[k].conj() * rec_spectrum[k];
990            let phase_rad = cross_spectrum.arg();
991
992            // Accumulate for averaging
993            sum_magnitude += magnitude;
994            sum_sin += phase_rad.sin();
995            sum_cos += phase_rad.cos();
996            bin_count += 1;
997        }
998
999        // When no valid bins contribute (reference energy too low at this frequency,
1000        // e.g., LFE sweep above 500 Hz), output a noise-floor value instead of skipping.
1001        // Skipping would produce fewer output points than other channels, causing
1002        // ndarray shape mismatches when curves are combined downstream.
1003        let (avg_magnitude, db) = if bin_count == 0 {
1004            (0.0, -200.0)
1005        } else {
1006            let avg = sum_magnitude / bin_count as f32;
1007            (avg, 20.0 * avg.max(1e-10).log10())
1008        };
1009
1010        if frequencies.len() < 5 {
1011            log::debug!(
1012                "[FFT Analysis] freq={:.1} Hz: avg_magnitude={:.6}, dB={:.2}",
1013                target_freq,
1014                avg_magnitude,
1015                db
1016            );
1017        }
1018
1019        // Average phase using circular mean
1020        let avg_phase_rad = sum_sin.atan2(sum_cos);
1021        let phase = avg_phase_rad * 180.0 / PI;
1022
1023        frequencies.push(target_freq);
1024        spl_db.push(db);
1025        phase_deg.push(phase);
1026    }
1027
1028    log::debug!(
1029        "[FFT Analysis] Generated {} frequency points for CSV output",
1030        frequencies.len()
1031    );
1032    log::debug!(
1033        "[FFT Analysis] Skipped {} frequency points (out of {})",
1034        skipped_count,
1035        num_output_points
1036    );
1037
1038    if log::log_enabled!(log::Level::Debug) && !spl_db.is_empty() {
1039        let min_spl = spl_db.iter().fold(f32::INFINITY, |a, &b| a.min(b));
1040        let max_spl = spl_db.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
1041        log::debug!(
1042            "[FFT Analysis] SPL range: {:.2} dB to {:.2} dB",
1043            min_spl,
1044            max_spl
1045        );
1046    }
1047
1048    // --- Compute Impulse Response ---
1049    // H(f) = Recorded(f) / Reference(f)
1050    let mut transfer_function = vec![Complex::new(0.0, 0.0); fft_size];
1051    for k in 0..fft_size {
1052        // Handle DC and Nyquist specially if needed, but for complex FFT it's just bins
1053        // Avoid division by zero
1054        let ref_mag_sq = ref_spectrum[k].norm_sqr();
1055        if ref_mag_sq > 1e-20 {
1056            transfer_function[k] = rec_spectrum[k] / ref_spectrum[k];
1057        }
1058    }
1059
1060    // IFFT to get Impulse Response
1061    let ifft = plan_fft_inverse(fft_size);
1062    ifft.process(&mut transfer_function);
1063
1064    // Normalize and take real part (input was real, so output should be real-ish)
1065    // Scale by 1.0/N is done by IFFT? rustfft typically does NOT scale.
1066    // Standard IFFT definition: sum(X[k] * exp(...)) / N?
1067    // RustFFT inverse is unnormalized sum. So we divide by N.
1068    let norm = 1.0 / fft_size as f32;
1069    let mut impulse_response: Vec<f32> = transfer_function.iter().map(|c| c.re * norm).collect();
1070
1071    // Find the peak and shift the IR so the peak is near the beginning
1072    // This is necessary because the IFFT result has the peak at an arbitrary position
1073    // due to the phase of the transfer function (system latency)
1074    let peak_idx = impulse_response
1075        .iter()
1076        .enumerate()
1077        .max_by(|(_, a), (_, b)| a.abs().partial_cmp(&b.abs()).unwrap())
1078        .map(|(i, _)| i)
1079        .unwrap_or(0);
1080
1081    // Shift the IR so peak is at a small offset (e.g., 5ms for pre-ringing visibility)
1082    let pre_ring_samples = (0.005 * sample_rate as f32) as usize; // 5ms pre-ring buffer
1083    let shift_amount = peak_idx.saturating_sub(pre_ring_samples);
1084
1085    if shift_amount > 0 {
1086        impulse_response.rotate_left(shift_amount);
1087        log::info!(
1088            "[FFT Analysis] IR peak was at index {}, shifted by {} samples to put peak near beginning",
1089            peak_idx,
1090            shift_amount
1091        );
1092    }
1093
1094    // Generate time vector for IR (0 to duration)
1095    let _ir_duration_sec = fft_size as f32 / sample_rate as f32;
1096    let impulse_time_ms: Vec<f32> = (0..fft_size)
1097        .map(|i| i as f32 / sample_rate as f32 * 1000.0)
1098        .collect();
1099
1100    // --- Compute THD if sweep range is provided ---
1101    let (thd_percent, harmonic_distortion_db) = if let Some((start, end)) = sweep_range {
1102        // Assume sweep duration is same as impulse length (circular convolution)
1103        // or derived from reference signal length
1104        let duration = reference_signal.len() as f32 / sample_rate as f32;
1105        compute_thd_from_ir(
1106            &impulse_response,
1107            sample_rate as f32,
1108            &frequencies,
1109            &spl_db,
1110            start,
1111            end,
1112            duration,
1113        )
1114    } else {
1115        (vec![0.0; frequencies.len()], Vec::new())
1116    };
1117
1118    // --- Compute Excess Group Delay ---
1119    // (Placeholder)
1120    let excess_group_delay_ms = vec![0.0; frequencies.len()];
1121
1122    // --- Compute Acoustic Metrics ---
1123    // Debug: Log impulse response stats
1124    let ir_max = impulse_response.iter().fold(0.0f32, |a, &b| a.max(b.abs()));
1125    let ir_len = impulse_response.len();
1126    log::info!(
1127        "[Analysis] Impulse response: len={}, max_abs={:.6}, sample_rate={}",
1128        ir_len,
1129        ir_max,
1130        sample_rate
1131    );
1132
1133    let rt60_ms = compute_rt60_spectrum(&impulse_response, sample_rate as f32, &frequencies);
1134    let (clarity_c50_db, clarity_c80_db) =
1135        compute_clarity_spectrum(&impulse_response, sample_rate as f32, &frequencies);
1136
1137    // Debug: Log computed metrics
1138    if !rt60_ms.is_empty() {
1139        let rt60_min = rt60_ms.iter().fold(f32::INFINITY, |a, &b| a.min(b));
1140        let rt60_max = rt60_ms.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
1141        log::info!(
1142            "[Analysis] RT60 range: {:.1} - {:.1} ms",
1143            rt60_min,
1144            rt60_max
1145        );
1146    }
1147    if !clarity_c50_db.is_empty() {
1148        let c50_min = clarity_c50_db.iter().fold(f32::INFINITY, |a, &b| a.min(b));
1149        let c50_max = clarity_c50_db
1150            .iter()
1151            .fold(f32::NEG_INFINITY, |a, &b| a.max(b));
1152        log::info!(
1153            "[Analysis] Clarity C50 range: {:.1} - {:.1} dB",
1154            c50_min,
1155            c50_max
1156        );
1157    }
1158
1159    // Compute Spectrogram
1160    let (spectrogram_db, _, _) =
1161        compute_spectrogram(&impulse_response, sample_rate as f32, 512, 128);
1162
1163    Ok(AnalysisResult {
1164        frequencies,
1165        spl_db,
1166        phase_deg,
1167        estimated_lag_samples: lag,
1168        impulse_response,
1169        impulse_time_ms,
1170        excess_group_delay_ms,
1171        thd_percent,
1172        harmonic_distortion_db,
1173        rt60_ms,
1174        clarity_c50_db,
1175        clarity_c80_db,
1176        spectrogram_db,
1177    })
1178}
1179
1180/// Compute Total Harmonic Distortion (THD) from Impulse Response
1181///
1182/// Uses Farina's method to extract harmonics from the impulse response of a log sweep.
1183fn compute_thd_from_ir(
1184    impulse: &[f32],
1185    sample_rate: f32,
1186    frequencies: &[f32],
1187    fundamental_db: &[f32],
1188    start_freq: f32,
1189    end_freq: f32,
1190    duration: f32,
1191) -> (Vec<f32>, Vec<Vec<f32>>) {
1192    if frequencies.is_empty() {
1193        return (Vec::new(), Vec::new());
1194    }
1195
1196    let n = impulse.len();
1197    if n == 0 {
1198        return (vec![0.0; frequencies.len()], Vec::new());
1199    }
1200
1201    let num_harmonics = 4; // Compute 2nd, 3rd, 4th, 5th
1202    // Initialize to -120 dB (very low but not absurdly so)
1203    let mut harmonics_db = vec![vec![-120.0; frequencies.len()]; num_harmonics];
1204
1205    // Find main peak index (t=0)
1206    let peak_idx = impulse
1207        .iter()
1208        .enumerate()
1209        .max_by(|(_, a), (_, b)| a.abs().partial_cmp(&b.abs()).unwrap())
1210        .map(|(i, _)| i)
1211        .unwrap_or(0);
1212
1213    let sweep_ratio = end_freq / start_freq;
1214    log::debug!(
1215        "[THD] Impulse len={}, peak_idx={}, duration={:.3}s, sweep {:.0}-{:.0} Hz (ratio {:.1})",
1216        n,
1217        peak_idx,
1218        duration,
1219        start_freq,
1220        end_freq,
1221        sweep_ratio
1222    );
1223
1224    // Compute harmonics
1225    for (k_idx, harmonic_db) in harmonics_db.iter_mut().enumerate().take(num_harmonics) {
1226        let harmonic_order = k_idx + 2; // 2nd harmonic is k=2
1227
1228        // Calculate delay for this harmonic
1229        // dt = T * ln(k) / ln(f2/f1)
1230        let dt = duration * (harmonic_order as f32).ln() / sweep_ratio.ln();
1231        let dn = (dt * sample_rate).round() as isize;
1232
1233        // Center of harmonic impulse (negative time wraps to end of array)
1234        let center = peak_idx as isize - dn;
1235        let center_wrapped = center.rem_euclid(n as isize) as usize;
1236
1237        // Window size logic: distance to next harmonic * 0.8 to avoid overlap
1238        let dt_next_rel = duration
1239            * ((harmonic_order as f32 + 1.0).ln() - (harmonic_order as f32).ln())
1240            / sweep_ratio.ln();
1241        let win_len = ((dt_next_rel * sample_rate * 0.8).max(256.0) as usize).min(n / 2);
1242
1243        // Extract windowed harmonic IR
1244        let mut harmonic_ir = vec![0.0f32; win_len];
1245        let mut max_harmonic_sample = 0.0f32;
1246        for (i, harmonic_ir_val) in harmonic_ir.iter_mut().enumerate() {
1247            let src_idx =
1248                (center - (win_len as isize / 2) + i as isize).rem_euclid(n as isize) as usize;
1249            // Apply Hann window
1250            let w = 0.5 * (1.0 - (2.0 * PI * i as f32 / (win_len as f32 - 1.0)).cos());
1251            *harmonic_ir_val = impulse[src_idx] * w;
1252            max_harmonic_sample = max_harmonic_sample.max(harmonic_ir_val.abs());
1253        }
1254
1255        if k_idx == 0 {
1256            log::debug!(
1257                "[THD] H{}: dt={:.3}s, dn={}, center_wrapped={}, win_len={}, max_sample={:.2e}",
1258                harmonic_order,
1259                dt,
1260                dn,
1261                center_wrapped,
1262                win_len,
1263                max_harmonic_sample
1264            );
1265        }
1266
1267        // Compute spectrum
1268        let fft_size = next_power_of_two(win_len);
1269        let nyquist_bin = fft_size / 2; // Only use positive frequency bins
1270        if let Ok(spectrum) = compute_fft_padded(&harmonic_ir, fft_size) {
1271            let freq_resolution = sample_rate / fft_size as f32;
1272
1273            for (i, &f) in frequencies.iter().enumerate() {
1274                let bin = (f / freq_resolution).round() as usize;
1275                // Only access positive frequency bins (0 to nyquist)
1276                if bin < nyquist_bin && bin < spectrum.len() {
1277                    // compute_fft_padded already applies 1/N normalization, matching
1278                    // the scale of fundamental_db (derived from transfer function ratios)
1279                    let mag = spectrum[bin].norm();
1280                    // Convert to dB (threshold at -120 dB to avoid log of tiny values)
1281                    if mag > 1e-6 {
1282                        harmonic_db[i] = 20.0 * mag.log10();
1283                    }
1284                }
1285            }
1286        }
1287    }
1288
1289    // Log a summary of detected harmonic levels
1290    if !frequencies.is_empty() {
1291        let mid_idx = frequencies.len() / 2;
1292        log::debug!(
1293            "[THD] Harmonic levels at {:.0} Hz: H2={:.1}dB, H3={:.1}dB, H4={:.1}dB, H5={:.1}dB, fundamental={:.1}dB",
1294            frequencies[mid_idx],
1295            harmonics_db[0][mid_idx],
1296            harmonics_db[1][mid_idx],
1297            harmonics_db[2][mid_idx],
1298            harmonics_db[3][mid_idx],
1299            fundamental_db[mid_idx]
1300        );
1301    }
1302
1303    // Compute THD %
1304    let mut thd_percent = Vec::with_capacity(frequencies.len());
1305    for i in 0..frequencies.len() {
1306        let fundamental = 10.0f32.powf(fundamental_db[i] / 20.0);
1307        let mut harmonic_sum_sq = 0.0;
1308
1309        for harmonic_db in harmonics_db.iter().take(num_harmonics) {
1310            let h_mag = 10.0f32.powf(harmonic_db[i] / 20.0);
1311            harmonic_sum_sq += h_mag * h_mag;
1312        }
1313
1314        // THD = sqrt(sum(harmonics^2)) / fundamental
1315        let thd = if fundamental > 1e-9 {
1316            (harmonic_sum_sq.sqrt() / fundamental) * 100.0
1317        } else {
1318            0.0
1319        };
1320        thd_percent.push(thd);
1321    }
1322
1323    // Log THD summary
1324    if !thd_percent.is_empty() {
1325        let max_thd = thd_percent.iter().fold(0.0f32, |a, &b| a.max(b));
1326        let min_thd = thd_percent.iter().fold(f32::INFINITY, |a, &b| a.min(b));
1327        log::debug!("[THD] THD range: {:.4}% to {:.4}%", min_thd, max_thd);
1328    }
1329
1330    (thd_percent, harmonics_db)
1331}
1332
1333/// Write analysis results to CSV file with optional microphone compensation
1334///
1335/// # Arguments
1336/// * `result` - Analysis result
1337/// * `output_path` - Path to output CSV file
1338/// * `compensation` - Optional microphone compensation to apply (inverse)
1339///
1340/// When compensation is provided, the inverse is applied: the microphone's
1341/// SPL deviation is subtracted from the measured SPL to get the true SPL.
1342///
1343/// CSV format includes all analysis metrics:
1344/// frequency_hz, spl_db, phase_deg, thd_percent, rt60_ms, c50_db, c80_db, group_delay_ms
1345pub fn write_analysis_csv(
1346    result: &AnalysisResult,
1347    output_path: &Path,
1348    compensation: Option<&MicrophoneCompensation>,
1349) -> Result<(), String> {
1350    use std::fs::File;
1351    use std::io::Write;
1352
1353    log::info!(
1354        "[write_analysis_csv] Writing {} frequency points to {:?}",
1355        result.frequencies.len(),
1356        output_path
1357    );
1358
1359    if let Some(comp) = compensation {
1360        log::info!(
1361            "[write_analysis_csv] Applying inverse microphone compensation ({} calibration points)",
1362            comp.frequencies.len()
1363        );
1364    }
1365
1366    if result.frequencies.is_empty() {
1367        return Err("Cannot write CSV: Analysis result has no frequency points!".to_string());
1368    }
1369
1370    let mut file =
1371        File::create(output_path).map_err(|e| format!("Failed to create CSV file: {}", e))?;
1372
1373    // Write header with all metrics
1374    writeln!(
1375        file,
1376        "frequency_hz,spl_db,phase_deg,thd_percent,rt60_ms,c50_db,c80_db,group_delay_ms"
1377    )
1378    .map_err(|e| format!("Failed to write header: {}", e))?;
1379
1380    // Write data with compensation applied
1381    for i in 0..result.frequencies.len() {
1382        let freq = result.frequencies[i];
1383        let mut spl = result.spl_db[i];
1384
1385        // Apply inverse compensation: subtract microphone deviation
1386        // If mic reads +2dB at this frequency, the true level is 2dB lower
1387        if let Some(comp) = compensation {
1388            let mic_deviation = comp.interpolate_at(freq);
1389            spl -= mic_deviation;
1390        }
1391
1392        let phase = result.phase_deg[i];
1393        let thd = result.thd_percent.get(i).copied().unwrap_or(0.0);
1394        let rt60 = result.rt60_ms.get(i).copied().unwrap_or(0.0);
1395        let c50 = result.clarity_c50_db.get(i).copied().unwrap_or(0.0);
1396        let c80 = result.clarity_c80_db.get(i).copied().unwrap_or(0.0);
1397        let gd = result.excess_group_delay_ms.get(i).copied().unwrap_or(0.0);
1398
1399        writeln!(
1400            file,
1401            "{:.6},{:.3},{:.6},{:.6},{:.3},{:.3},{:.3},{:.6}",
1402            freq, spl, phase, thd, rt60, c50, c80, gd
1403        )
1404        .map_err(|e| format!("Failed to write data: {}", e))?;
1405    }
1406
1407    log::info!(
1408        "[write_analysis_csv] Successfully wrote {} data rows to CSV",
1409        result.frequencies.len()
1410    );
1411
1412    Ok(())
1413}
1414
1415/// Read analysis results from CSV file
1416///
1417/// Parses CSV with columns: frequency_hz, spl_db, phase_deg, thd_percent, rt60_ms, c50_db, c80_db, group_delay_ms
1418/// Also supports legacy format with just: frequency_hz, spl_db, phase_deg
1419pub fn read_analysis_csv(csv_path: &Path) -> Result<AnalysisResult, String> {
1420    use std::fs::File;
1421    use std::io::{BufRead, BufReader};
1422
1423    let file = File::open(csv_path).map_err(|e| format!("Failed to open CSV: {}", e))?;
1424    let reader = BufReader::new(file);
1425    let mut lines = reader.lines();
1426
1427    // Read header
1428    let header = lines
1429        .next()
1430        .ok_or("Empty CSV file")?
1431        .map_err(|e| format!("Failed to read header: {}", e))?;
1432
1433    let columns: Vec<&str> = header.split(',').map(|s| s.trim()).collect();
1434    let has_extended_format = columns.len() >= 8;
1435
1436    let mut frequencies = Vec::new();
1437    let mut spl_db = Vec::new();
1438    let mut phase_deg = Vec::new();
1439    let mut thd_percent = Vec::new();
1440    let mut rt60_ms = Vec::new();
1441    let mut clarity_c50_db = Vec::new();
1442    let mut clarity_c80_db = Vec::new();
1443    let mut excess_group_delay_ms = Vec::new();
1444
1445    for line in lines {
1446        let line = line.map_err(|e| format!("Failed to read line: {}", e))?;
1447        let parts: Vec<&str> = line.split(',').map(|s| s.trim()).collect();
1448
1449        if parts.len() < 3 {
1450            continue;
1451        }
1452
1453        let freq: f32 = parts[0].parse().unwrap_or(0.0);
1454        let spl: f32 = parts[1].parse().unwrap_or(0.0);
1455        let phase: f32 = parts[2].parse().unwrap_or(0.0);
1456
1457        frequencies.push(freq);
1458        spl_db.push(spl);
1459        phase_deg.push(phase);
1460
1461        if has_extended_format && parts.len() >= 8 {
1462            thd_percent.push(parts[3].parse().unwrap_or(0.0));
1463            rt60_ms.push(parts[4].parse().unwrap_or(0.0));
1464            clarity_c50_db.push(parts[5].parse().unwrap_or(0.0));
1465            clarity_c80_db.push(parts[6].parse().unwrap_or(0.0));
1466            excess_group_delay_ms.push(parts[7].parse().unwrap_or(0.0));
1467        }
1468    }
1469
1470    // If legacy format, fill with zeros
1471    let n = frequencies.len();
1472    if thd_percent.is_empty() {
1473        thd_percent = vec![0.0; n];
1474        rt60_ms = vec![0.0; n];
1475        clarity_c50_db = vec![0.0; n];
1476        clarity_c80_db = vec![0.0; n];
1477        excess_group_delay_ms = vec![0.0; n];
1478    }
1479
1480    Ok(AnalysisResult {
1481        frequencies,
1482        spl_db,
1483        phase_deg,
1484        estimated_lag_samples: 0,
1485        impulse_response: Vec::new(),
1486        impulse_time_ms: Vec::new(),
1487        thd_percent,
1488        harmonic_distortion_db: Vec::new(),
1489        rt60_ms,
1490        clarity_c50_db,
1491        clarity_c80_db,
1492        excess_group_delay_ms,
1493        spectrogram_db: Vec::new(),
1494    })
1495}
1496
1497/// Window function type for FFT
1498#[derive(Debug, Clone, Copy)]
1499enum WindowType {
1500    Hann,
1501    Tukey(f32), // alpha parameter (0.0-1.0)
1502}
1503
1504/// Estimate lag between reference and recorded signals using cross-correlation
1505///
1506/// Uses FFT-based cross-correlation for efficiency
1507///
1508/// # Arguments
1509/// * `reference` - Reference signal
1510/// * `recorded` - Recorded signal
1511///
1512/// # Returns
1513/// Estimated lag in samples (negative means recorded leads)
1514fn estimate_lag(reference: &[f32], recorded: &[f32]) -> Result<isize, String> {
1515    let len = reference.len().min(recorded.len());
1516
1517    // Zero-pad to avoid circular correlation artifacts
1518    let fft_size = next_power_of_two(len * 2);
1519
1520    // Use Hann window for correlation to suppress edge effects
1521    let ref_fft = compute_fft(reference, fft_size, WindowType::Hann)?;
1522    let rec_fft = compute_fft(recorded, fft_size, WindowType::Hann)?;
1523
1524    // Cross-correlation in frequency domain: conj(X) * Y
1525    let mut cross_corr_fft: Vec<Complex<f32>> = ref_fft
1526        .iter()
1527        .zip(rec_fft.iter())
1528        .map(|(x, y)| x.conj() * y)
1529        .collect();
1530
1531    // IFFT to get cross-correlation in time domain
1532    let ifft = plan_fft_inverse(fft_size);
1533    ifft.process(&mut cross_corr_fft);
1534
1535    // Find peak
1536    let mut max_val = 0.0;
1537    let mut max_idx = 0;
1538
1539    for (i, &val) in cross_corr_fft.iter().enumerate() {
1540        let magnitude = val.norm();
1541        if magnitude > max_val {
1542            max_val = magnitude;
1543            max_idx = i;
1544        }
1545    }
1546
1547    // Convert index to lag (handle wrap-around)
1548    Ok(if max_idx <= fft_size / 2 {
1549        max_idx as isize
1550    } else {
1551        max_idx as isize - fft_size as isize
1552    })
1553}
1554
1555/// Result of cross-correlation with analytic envelope detection.
1556///
1557/// The envelope peak corresponds to the probe's arrival time, detected
1558/// via Hilbert transform of the cross-correlation.
1559#[derive(Debug, Clone)]
1560pub struct CrossCorrelationEnvelopeResult {
1561    /// Analytic envelope of the cross-correlation
1562    pub envelope: Vec<f32>,
1563    /// Sample index of the peak (integer arrival time)
1564    pub peak_sample: usize,
1565    /// Sub-sample refined peak position via parabolic interpolation
1566    pub peak_sample_refined: f64,
1567    /// Peak envelope value (proportional to channel gain)
1568    pub peak_value: f32,
1569    /// Arrival time in milliseconds (sub-sample precision)
1570    pub arrival_ms: f64,
1571}
1572
1573/// Cross-correlate a probe with a recording and compute the analytic envelope.
1574///
1575/// Uses FFT-based cross-correlation followed by the Hilbert transform
1576/// (via `analytic_signal`) to extract a smooth envelope whose peak
1577/// indicates the arrival time with sub-sample precision.
1578///
1579/// This is the matched-filter approach recommended by Johnston (AES):
1580/// narrowband probes give excellent noise rejection, and the analytic
1581/// envelope provides a clean, unambiguous peak even in reverberant rooms.
1582///
1583/// # Arguments
1584/// * `probe` - The known probe signal that was played
1585/// * `recorded` - The recorded signal from the microphone
1586/// * `sample_rate` - Sample rate in Hz
1587pub fn cross_correlate_envelope(
1588    probe: &[f32],
1589    recorded: &[f32],
1590    sample_rate: u32,
1591) -> Result<CrossCorrelationEnvelopeResult, String> {
1592    if probe.is_empty() || recorded.is_empty() {
1593        return Err("Probe and recorded signals must be non-empty".to_string());
1594    }
1595
1596    // Zero-pad to avoid circular correlation artifacts
1597    let fft_size = next_power_of_two(probe.len() + recorded.len());
1598
1599    // Raw FFT (no normalization) — we handle normalization once after IFFT.
1600    // Using unnormalized FFT avoids the scale-dependent gain errors that
1601    // occur when compute_fft_padded's 1/N normalization interacts with IFFT.
1602    let fft_forward = plan_fft_forward(fft_size);
1603
1604    let mut probe_buf: Vec<Complex<f32>> = vec![Complex::new(0.0, 0.0); fft_size];
1605    for (dst, &src) in probe_buf.iter_mut().zip(probe.iter()) {
1606        dst.re = src;
1607    }
1608    fft_forward.process(&mut probe_buf);
1609
1610    let mut rec_buf: Vec<Complex<f32>> = vec![Complex::new(0.0, 0.0); fft_size];
1611    for (dst, &src) in rec_buf.iter_mut().zip(recorded.iter()) {
1612        dst.re = src;
1613    }
1614    fft_forward.process(&mut rec_buf);
1615
1616    // Cross-correlation: conj(Probe) * Recorded
1617    let mut cross_fft: Vec<Complex<f32>> = probe_buf
1618        .iter()
1619        .zip(rec_buf.iter())
1620        .map(|(p, r)| p.conj() * r)
1621        .collect();
1622
1623    // IFFT to get cross-correlation in time domain
1624    let ifft = plan_fft_inverse(fft_size);
1625    ifft.process(&mut cross_fft);
1626
1627    // Single 1/N normalization (standard for round-trip FFT→IFFT)
1628    let norm = 1.0 / fft_size as f32;
1629    let xcorr: Vec<f32> = cross_fft.iter().map(|c| c.re * norm).collect();
1630
1631    // Compute analytic envelope via Hilbert transform
1632    let analytic = crate::instantaneous_frequency::analytic_signal(&xcorr);
1633    let envelope: Vec<f32> = analytic.iter().map(|c| c.norm()).collect();
1634
1635    // Find peak in the causal part (first half — positive lags only)
1636    let search_len = fft_size / 2;
1637    let mut peak_sample = 0_usize;
1638    let mut peak_value = 0.0_f32;
1639    for (i, &val) in envelope.iter().enumerate().take(search_len) {
1640        if val > peak_value {
1641            peak_value = val;
1642            peak_sample = i;
1643        }
1644    }
1645
1646    // Parabolic interpolation for sub-sample precision
1647    let peak_refined = if peak_sample > 0 && peak_sample < search_len - 1 {
1648        let y_prev = envelope[peak_sample - 1] as f64;
1649        let y_peak = envelope[peak_sample] as f64;
1650        let y_next = envelope[peak_sample + 1] as f64;
1651        let denom = 2.0 * (2.0 * y_peak - y_prev - y_next);
1652        if denom.abs() > 1e-12 {
1653            peak_sample as f64 + (y_prev - y_next) / denom
1654        } else {
1655            peak_sample as f64
1656        }
1657    } else {
1658        peak_sample as f64
1659    };
1660
1661    let arrival_ms = peak_refined / sample_rate as f64 * 1000.0;
1662
1663    Ok(CrossCorrelationEnvelopeResult {
1664        envelope,
1665        peak_sample,
1666        peak_sample_refined: peak_refined,
1667        peak_value,
1668        arrival_ms,
1669    })
1670}
1671
1672/// Frequency responses computed from different time windows of an impulse response.
1673///
1674/// Direct sound, early reflections, and late reverb each have different
1675/// perceptual roles (Toole, Johnston) and should be corrected differently.
1676#[derive(Debug, Clone)]
1677pub struct WindowedFrequencyResponse {
1678    /// Direct sound frequency response (frequencies in Hz, SPL in dB)
1679    pub direct_sound_freq: Vec<f32>,
1680    pub direct_sound_spl: Vec<f32>,
1681    /// Early reflections frequency response
1682    pub early_reflections_freq: Vec<f32>,
1683    pub early_reflections_spl: Vec<f32>,
1684    /// Late/reverberant field frequency response
1685    pub late_reverb_freq: Vec<f32>,
1686    pub late_reverb_spl: Vec<f32>,
1687    /// Time boundaries used (in ms)
1688    pub direct_end_ms: f64,
1689    pub early_end_ms: f64,
1690}
1691
1692/// Compute frequency responses for different time windows of the impulse response.
1693///
1694/// Uses SSIR segmentation boundaries to separate:
1695/// - Direct sound: \[0, first_reflection_onset)
1696/// - Early reflections: \[first_reflection_onset, mixing_time)
1697/// - Late reverb: \[mixing_time, end)
1698///
1699/// Each window gets a half-Hann fade at edges to avoid spectral leakage,
1700/// then FFT -> magnitude -> 1/24 octave smoothing.
1701pub fn compute_windowed_fr(
1702    impulse_response: &[f32],
1703    direct_end_sample: usize,
1704    early_end_sample: usize,
1705    sample_rate: u32,
1706    num_output_points: usize,
1707) -> Result<WindowedFrequencyResponse, String> {
1708    if impulse_response.is_empty() {
1709        return Err("Impulse response must be non-empty".to_string());
1710    }
1711    if num_output_points == 0 {
1712        return Err("num_output_points must be > 0".to_string());
1713    }
1714
1715    let ir_len = impulse_response.len();
1716    let direct_end = direct_end_sample.min(ir_len);
1717    let early_end = early_end_sample.max(direct_end).min(ir_len);
1718
1719    let direct_end_ms = direct_end as f64 / sample_rate as f64 * 1000.0;
1720    let early_end_ms = early_end as f64 / sample_rate as f64 * 1000.0;
1721
1722    // Fade length: 1ms or half the window, whichever is smaller
1723    let fade_1ms = (sample_rate as usize) / 1000;
1724
1725    let window_to_fr = |start: usize, end: usize| -> (Vec<f32>, Vec<f32>) {
1726        let win_len = end.saturating_sub(start);
1727        if win_len == 0 {
1728            // Return silence at the output frequencies
1729            let log_start = 20.0_f32.ln();
1730            let log_end = 20000.0_f32.ln();
1731            let freqs: Vec<f32> = (0..num_output_points)
1732                .map(|i| {
1733                    (log_start
1734                        + (log_end - log_start) * i as f32 / (num_output_points.max(2) - 1) as f32)
1735                        .exp()
1736                })
1737                .collect();
1738            let spl = vec![-200.0_f32; num_output_points];
1739            return (freqs, spl);
1740        }
1741
1742        // Extract and fade the window edges to reduce spectral leakage.
1743        // Skip fade-in at the physical start of the IR (start==0) to avoid
1744        // attenuating the direct sound impulse.
1745        let mut window: Vec<f32> = impulse_response[start..end].to_vec();
1746        let fade_len = fade_1ms.min(win_len / 2).max(1);
1747        if start > 0 {
1748            crate::signals::apply_fade_in(&mut window, fade_len);
1749        }
1750        crate::signals::apply_fade_out(&mut window, fade_len);
1751
1752        // Zero-pad to next power of 2
1753        let fft_size = next_power_of_two(win_len);
1754        let fft_forward = plan_fft_forward(fft_size);
1755
1756        let mut buf: Vec<Complex<f32>> = vec![Complex::new(0.0, 0.0); fft_size];
1757        for (dst, &src) in buf.iter_mut().zip(window.iter()) {
1758            dst.re = src;
1759        }
1760        fft_forward.process(&mut buf);
1761
1762        // Normalize by FFT size
1763        let norm = 1.0 / fft_size as f32;
1764
1765        // Generate log-spaced output frequencies and compute magnitude in dB
1766        let log_start = 20.0_f32.ln();
1767        let log_end = 20000.0_f32.ln();
1768        let freq_resolution = sample_rate as f32 / fft_size as f32;
1769        let num_bins = fft_size / 2;
1770
1771        let mut freqs = Vec::with_capacity(num_output_points);
1772        let mut raw_db = Vec::with_capacity(num_output_points);
1773
1774        for i in 0..num_output_points {
1775            let target_freq = (log_start
1776                + (log_end - log_start) * i as f32 / (num_output_points.max(2) - 1) as f32)
1777                .exp();
1778            freqs.push(target_freq);
1779
1780            // Map to nearest FFT bin
1781            let bin = ((target_freq / freq_resolution).round() as usize).clamp(1, num_bins - 1);
1782            let mag = buf[bin].norm() * norm;
1783            let db = if mag > 1e-20 {
1784                20.0 * mag.log10()
1785            } else {
1786                -200.0
1787            };
1788            raw_db.push(db);
1789        }
1790
1791        // Apply 1/24 octave smoothing
1792        let smoothed = smooth_response_f32(&freqs, &raw_db, 1.0 / 24.0);
1793        (freqs, smoothed)
1794    };
1795
1796    let (direct_sound_freq, direct_sound_spl) = window_to_fr(0, direct_end);
1797    let (early_reflections_freq, early_reflections_spl) = window_to_fr(direct_end, early_end);
1798    let (late_reverb_freq, late_reverb_spl) = window_to_fr(early_end, ir_len);
1799
1800    Ok(WindowedFrequencyResponse {
1801        direct_sound_freq,
1802        direct_sound_spl,
1803        early_reflections_freq,
1804        early_reflections_spl,
1805        late_reverb_freq,
1806        late_reverb_spl,
1807        direct_end_ms,
1808        early_end_ms,
1809    })
1810}
1811
1812/// Compute FFT of a signal with specified windowing
1813///
1814/// # Arguments
1815/// * `signal` - Input signal
1816/// * `fft_size` - FFT size (should be power of 2)
1817/// * `window_type` - Type of window to apply
1818///
1819/// # Returns
1820/// Complex FFT spectrum
1821fn compute_fft(
1822    signal: &[f32],
1823    fft_size: usize,
1824    window_type: WindowType,
1825) -> Result<Vec<Complex<f32>>, String> {
1826    // Apply window
1827    let windowed = match window_type {
1828        WindowType::Hann => apply_hann_window(signal),
1829        WindowType::Tukey(alpha) => apply_tukey_window(signal, alpha),
1830    };
1831
1832    compute_fft_padded(&windowed, fft_size)
1833}
1834
1835/// Compute FFT with zero-padding
1836fn compute_fft_padded(signal: &[f32], fft_size: usize) -> Result<Vec<Complex<f32>>, String> {
1837    // Single allocation at final size; trailing elements are already zero-padded
1838    let mut buffer = vec![Complex::new(0.0, 0.0); fft_size];
1839    for (dst, &src) in buffer.iter_mut().zip(signal.iter()) {
1840        dst.re = src;
1841    }
1842
1843    // Compute FFT
1844    let fft = plan_fft_forward(fft_size);
1845    fft.process(&mut buffer);
1846
1847    // Normalize by FFT size (standard FFT normalization)
1848    let norm_factor = 1.0 / fft_size as f32;
1849    for val in buffer.iter_mut() {
1850        *val *= norm_factor;
1851    }
1852
1853    Ok(buffer)
1854}
1855
1856/// Apply Hann window to a signal
1857fn apply_hann_window(signal: &[f32]) -> Vec<f32> {
1858    let len = signal.len();
1859    if len < 2 {
1860        return signal.to_vec();
1861    }
1862    signal
1863        .iter()
1864        .enumerate()
1865        .map(|(i, &x)| {
1866            let window = 0.5 * (1.0 - (2.0 * PI * i as f32 / (len - 1) as f32).cos());
1867            x * window
1868        })
1869        .collect()
1870}
1871
1872/// Apply Tukey window to a signal
1873///
1874/// Tukey window is a "tapered cosine" window.
1875/// alpha=0.0 is rectangular, alpha=1.0 is Hann.
1876fn apply_tukey_window(signal: &[f32], alpha: f32) -> Vec<f32> {
1877    let len = signal.len();
1878    if len < 2 {
1879        return signal.to_vec();
1880    }
1881
1882    let alpha = alpha.clamp(0.0, 1.0);
1883    let limit = (alpha * (len as f32 - 1.0) / 2.0).round() as usize;
1884
1885    if limit == 0 {
1886        return signal.to_vec();
1887    }
1888
1889    signal
1890        .iter()
1891        .enumerate()
1892        .map(|(i, &x)| {
1893            let w = if i < limit {
1894                // Fade in (Half-Hann)
1895                0.5 * (1.0 - (PI * i as f32 / limit as f32).cos())
1896            } else if i >= len - limit {
1897                // Fade out (Half-Hann)
1898                let n = len - 1 - i;
1899                0.5 * (1.0 - (PI * n as f32 / limit as f32).cos())
1900            } else {
1901                // Flat top
1902                1.0
1903            };
1904            x * w
1905        })
1906        .collect()
1907}
1908
1909/// Find the next power of two greater than or equal to n
1910fn next_power_of_two(n: usize) -> usize {
1911    if n == 0 {
1912        return 1;
1913    }
1914    n.next_power_of_two()
1915}
1916
1917/// Load a mono WAV file and convert to f32 samples
1918/// Load a WAV file and extract a specific channel or convert to mono
1919///
1920/// # Arguments
1921/// * `path` - Path to WAV file
1922/// * `channel_index` - Optional channel index to extract (0-based). If None, will average all channels for mono
1923fn load_wav_mono_channel(path: &Path, channel_index: Option<usize>) -> Result<Vec<f32>, String> {
1924    let mut reader =
1925        WavReader::open(path).map_err(|e| format!("Failed to open WAV file: {}", e))?;
1926
1927    let spec = reader.spec();
1928    let channels = spec.channels as usize;
1929
1930    log::info!(
1931        "[load_wav_mono_channel] WAV file: {} channels, {} Hz, {:?} format",
1932        channels,
1933        spec.sample_rate,
1934        spec.sample_format
1935    );
1936
1937    // Read all samples and convert to f32
1938    let samples: Result<Vec<f32>, _> = match spec.sample_format {
1939        hound::SampleFormat::Float => reader.samples::<f32>().collect(),
1940        hound::SampleFormat::Int => reader
1941            .samples::<i32>()
1942            .map(|s| s.map(|v| v as f32 / i32::MAX as f32))
1943            .collect(),
1944    };
1945
1946    let samples = samples.map_err(|e| format!("Failed to read samples: {}", e))?;
1947    log::info!(
1948        "[load_wav_mono_channel] Read {} total samples",
1949        samples.len()
1950    );
1951
1952    // Handle mono file - return as-is
1953    if channels == 1 {
1954        log::info!(
1955            "[load_wav_mono_channel] File is already mono, returning {} samples",
1956            samples.len()
1957        );
1958        return Ok(samples);
1959    }
1960
1961    // Handle multi-channel file
1962    if let Some(ch_idx) = channel_index {
1963        // Extract specific channel
1964        if ch_idx >= channels {
1965            return Err(format!(
1966                "Channel index {} out of range (file has {} channels)",
1967                ch_idx, channels
1968            ));
1969        }
1970        log::info!(
1971            "[load_wav_mono_channel] Extracting channel {} from {} channels",
1972            ch_idx,
1973            channels
1974        );
1975        Ok(samples
1976            .chunks(channels)
1977            .map(|chunk| chunk[ch_idx])
1978            .collect())
1979    } else {
1980        // Average all channels to mono
1981        log::info!(
1982            "[load_wav_mono_channel] Averaging {} channels to mono",
1983            channels
1984        );
1985        Ok(samples
1986            .chunks(channels)
1987            .map(|chunk| chunk.iter().sum::<f32>() / channels as f32)
1988            .collect())
1989    }
1990}
1991
1992/// Load a WAV file as mono (averages channels if multi-channel)
1993fn load_wav_mono(path: &Path) -> Result<Vec<f32>, String> {
1994    load_wav_mono_channel(path, None)
1995}
1996
1997// ============================================================================
1998// DSP Utilities (Moved from frontend dsp.rs)
1999// ============================================================================
2000
2001/// Apply octave smoothing to frequency response data (f64 version)
2002///
2003/// Frequencies must be sorted in ascending order (as from FFT or log-spaced grids).
2004/// Uses a prefix sum with two-pointer sliding window for O(n) complexity.
2005pub fn smooth_response_f64(frequencies: &[f64], values: &[f64], octaves: f64) -> Vec<f64> {
2006    if octaves <= 0.0 || frequencies.is_empty() || values.is_empty() {
2007        return values.to_vec();
2008    }
2009
2010    let n = values.len();
2011
2012    // Prefix sum for O(1) range averages
2013    let mut prefix = Vec::with_capacity(n + 1);
2014    prefix.push(0.0);
2015    for &v in values {
2016        prefix.push(prefix.last().unwrap() + v);
2017    }
2018
2019    let ratio = 2.0_f64.powf(octaves / 2.0);
2020    let mut smoothed = Vec::with_capacity(n);
2021    let mut lo = 0usize;
2022    let mut hi = 0usize;
2023
2024    for (i, &center_freq) in frequencies.iter().enumerate() {
2025        if center_freq <= 0.0 {
2026            smoothed.push(values[i]);
2027            continue;
2028        }
2029
2030        let low_freq = center_freq / ratio;
2031        let high_freq = center_freq * ratio;
2032
2033        // Advance lo past frequencies below the window
2034        while lo < n && frequencies[lo] < low_freq {
2035            lo += 1;
2036        }
2037        // Advance hi to include frequencies within the window
2038        while hi < n && frequencies[hi] <= high_freq {
2039            hi += 1;
2040        }
2041
2042        let count = hi - lo;
2043        if count > 0 {
2044            smoothed.push((prefix[hi] - prefix[lo]) / count as f64);
2045        } else {
2046            smoothed.push(values[i]);
2047        }
2048    }
2049
2050    smoothed
2051}
2052
2053/// Apply octave smoothing to frequency response data (f32 version)
2054///
2055/// Frequencies must be sorted in ascending order (as from FFT or log-spaced grids).
2056/// Uses a prefix sum with two-pointer sliding window for O(n) complexity.
2057pub fn smooth_response_f32(frequencies: &[f32], values: &[f32], octaves: f32) -> Vec<f32> {
2058    if octaves <= 0.0 || frequencies.is_empty() || values.is_empty() {
2059        return values.to_vec();
2060    }
2061
2062    let n = values.len();
2063
2064    // Prefix sum for O(1) range averages (accumulate in f64 to avoid precision loss)
2065    let mut prefix = Vec::with_capacity(n + 1);
2066    prefix.push(0.0_f64);
2067    for &v in values {
2068        prefix.push(prefix.last().unwrap() + v as f64);
2069    }
2070
2071    let ratio = 2.0_f32.powf(octaves / 2.0);
2072    let mut smoothed = Vec::with_capacity(n);
2073    let mut lo = 0usize;
2074    let mut hi = 0usize;
2075
2076    for (i, &center_freq) in frequencies.iter().enumerate() {
2077        if center_freq <= 0.0 {
2078            smoothed.push(values[i]);
2079            continue;
2080        }
2081
2082        let low_freq = center_freq / ratio;
2083        let high_freq = center_freq * ratio;
2084
2085        // Advance lo past frequencies below the window
2086        while lo < n && frequencies[lo] < low_freq {
2087            lo += 1;
2088        }
2089        // Advance hi to include frequencies within the window
2090        while hi < n && frequencies[hi] <= high_freq {
2091            hi += 1;
2092        }
2093
2094        let count = hi - lo;
2095        if count > 0 {
2096            smoothed.push(((prefix[hi] - prefix[lo]) / count as f64) as f32);
2097        } else {
2098            smoothed.push(values[i]);
2099        }
2100    }
2101
2102    smoothed
2103}
2104
2105/// Compute group delay from phase data
2106/// Group delay = -d(phase)/d(frequency) / (2*pi)
2107///
2108/// Phase is unwrapped before differentiation to avoid spurious spikes
2109/// at ±180° wrap boundaries.
2110pub fn compute_group_delay(frequencies: &[f32], phase_deg: &[f32]) -> Vec<f32> {
2111    if frequencies.len() < 2 {
2112        return vec![0.0; frequencies.len()];
2113    }
2114
2115    // Unwrap phase to remove ±180° discontinuities before differentiation
2116    let unwrapped = unwrap_phase_deg(phase_deg);
2117
2118    let mut group_delay_ms = Vec::with_capacity(frequencies.len());
2119
2120    for i in 0..frequencies.len() {
2121        let delay = if i == 0 {
2122            // Forward difference at start
2123            let df = frequencies[1] - frequencies[0];
2124            let dp = unwrapped[1] - unwrapped[0];
2125            if df.abs() > 1e-6 {
2126                -dp / df / 360.0 * 1000.0 // Convert to ms
2127            } else {
2128                0.0
2129            }
2130        } else if i == frequencies.len() - 1 {
2131            // Backward difference at end
2132            let df = frequencies[i] - frequencies[i - 1];
2133            let dp = unwrapped[i] - unwrapped[i - 1];
2134            if df.abs() > 1e-6 {
2135                -dp / df / 360.0 * 1000.0
2136            } else {
2137                0.0
2138            }
2139        } else {
2140            // Central difference
2141            let df = frequencies[i + 1] - frequencies[i - 1];
2142            let dp = unwrapped[i + 1] - unwrapped[i - 1];
2143            if df.abs() > 1e-6 {
2144                -dp / df / 360.0 * 1000.0
2145            } else {
2146                0.0
2147            }
2148        };
2149        group_delay_ms.push(delay);
2150    }
2151
2152    group_delay_ms
2153}
2154
2155/// Unwrap phase in degrees to produce a continuous phase curve.
2156/// Wraps each inter-sample difference to [-180, 180] and accumulates,
2157/// handling arbitrarily large jumps (not just single ±360° wraps).
2158fn unwrap_phase_deg(phase_deg: &[f32]) -> Vec<f32> {
2159    if phase_deg.is_empty() {
2160        return Vec::new();
2161    }
2162
2163    let mut unwrapped = Vec::with_capacity(phase_deg.len());
2164    unwrapped.push(phase_deg[0]);
2165
2166    for i in 1..phase_deg.len() {
2167        let diff = phase_deg[i] - phase_deg[i - 1];
2168        let wrapped_diff = diff - 360.0 * (diff / 360.0).round();
2169        unwrapped.push(unwrapped[i - 1] + wrapped_diff);
2170    }
2171
2172    unwrapped
2173}
2174
2175/// Compute impulse response from frequency response via inverse FFT.
2176///
2177/// The input frequency/magnitude/phase data (possibly irregularly spaced) is
2178/// interpolated onto a uniform FFT frequency grid, assembled into a complex
2179/// spectrum with Hermitian symmetry, and transformed with an inverse FFT.
2180///
2181/// Returns (times_ms, impulse) where impulse is peak-normalized to [-1, 1].
2182pub fn compute_impulse_response_from_fr(
2183    frequencies: &[f32],
2184    magnitude_db: &[f32],
2185    phase_deg: &[f32],
2186    sample_rate: f32,
2187) -> (Vec<f32>, Vec<f32>) {
2188    let fft_size = 1024;
2189    let half = fft_size / 2; // Number of positive-frequency bins (excluding DC)
2190    let freq_bin = sample_rate / fft_size as f32;
2191
2192    // Unwrap phase before interpolation to avoid discontinuities
2193    let unwrapped_phase = unwrap_phase_deg(phase_deg);
2194
2195    // Build complex spectrum on uniform FFT grid via linear interpolation
2196    let mut spectrum = vec![Complex::new(0.0_f32, 0.0); fft_size];
2197
2198    for (k, spectrum_bin) in spectrum.iter_mut().enumerate().take(half + 1) {
2199        let f = k as f32 * freq_bin;
2200
2201        // Interpolate magnitude (dB) and phase (deg) at this bin frequency
2202        let (mag_db, phase_d) = interpolate_fr(frequencies, magnitude_db, &unwrapped_phase, f);
2203
2204        let mag_linear = 10.0_f32.powf(mag_db / 20.0);
2205        let phase_rad = phase_d * PI / 180.0;
2206
2207        *spectrum_bin = Complex::new(mag_linear * phase_rad.cos(), mag_linear * phase_rad.sin());
2208    }
2209
2210    // Enforce Hermitian symmetry: X[N-k] = conj(X[k])
2211    for k in 1..half {
2212        spectrum[fft_size - k] = spectrum[k].conj();
2213    }
2214
2215    // Inverse FFT (uses thread-local cached planner)
2216    let ifft = plan_fft_inverse(fft_size);
2217    ifft.process(&mut spectrum);
2218
2219    // Extract real part and scale by 1/N (rustfft doesn't normalize)
2220    let scale = 1.0 / fft_size as f32;
2221    let mut impulse: Vec<f32> = spectrum.iter().map(|c| c.re * scale).collect();
2222
2223    // Normalize to [-1, 1]
2224    let max_val = impulse.iter().map(|v| v.abs()).fold(0.0_f32, f32::max);
2225    if max_val > 0.0 {
2226        for v in &mut impulse {
2227            *v /= max_val;
2228        }
2229    }
2230
2231    let time_step = 1.0 / sample_rate;
2232    let times: Vec<f32> = (0..fft_size)
2233        .map(|i| i as f32 * time_step * 1000.0)
2234        .collect();
2235
2236    (times, impulse)
2237}
2238
2239/// Linearly interpolate magnitude and phase at a target frequency.
2240/// Clamps to the nearest endpoint if `target_freq` is outside the data range.
2241///
2242/// Phase must be pre-unwrapped (continuous) for correct interpolation.
2243fn interpolate_fr(
2244    frequencies: &[f32],
2245    magnitude_db: &[f32],
2246    unwrapped_phase_deg: &[f32],
2247    target_freq: f32,
2248) -> (f32, f32) {
2249    if frequencies.is_empty() {
2250        return (0.0, 0.0);
2251    }
2252    if target_freq <= frequencies[0] {
2253        return (magnitude_db[0], unwrapped_phase_deg[0]);
2254    }
2255    let last = frequencies.len() - 1;
2256    if target_freq >= frequencies[last] {
2257        return (magnitude_db[last], unwrapped_phase_deg[last]);
2258    }
2259
2260    // Binary search for the interval containing target_freq
2261    let idx = match frequencies.binary_search_by(|f| f.partial_cmp(&target_freq).unwrap()) {
2262        Ok(i) => return (magnitude_db[i], unwrapped_phase_deg[i]),
2263        Err(i) => i, // target_freq is between frequencies[i-1] and frequencies[i]
2264    };
2265
2266    let f0 = frequencies[idx - 1];
2267    let f1 = frequencies[idx];
2268    let t = (target_freq - f0) / (f1 - f0);
2269
2270    let mag = magnitude_db[idx - 1] + t * (magnitude_db[idx] - magnitude_db[idx - 1]);
2271    let phase = unwrapped_phase_deg[idx - 1]
2272        + t * (unwrapped_phase_deg[idx] - unwrapped_phase_deg[idx - 1]);
2273    (mag, phase)
2274}
2275
2276/// Compute Schroeder energy decay curve
2277fn compute_schroeder_decay(impulse: &[f32]) -> Vec<f32> {
2278    let mut energy = 0.0;
2279    let mut decay = vec![0.0; impulse.len()];
2280
2281    // Backward integration
2282    for i in (0..impulse.len()).rev() {
2283        energy += impulse[i] * impulse[i];
2284        decay[i] = energy;
2285    }
2286
2287    // Normalize to 0dB max (1.0 linear)
2288    let max_energy = decay.first().copied().unwrap_or(1.0);
2289    if max_energy > 0.0 {
2290        for v in &mut decay {
2291            *v /= max_energy;
2292        }
2293    }
2294
2295    decay
2296}
2297
2298/// Compute RT60 from Impulse Response (Broadband)
2299/// Uses T20 (-5dB to -25dB) extrapolation
2300pub fn compute_rt60_broadband(impulse: &[f32], sample_rate: f32) -> f32 {
2301    let decay = compute_schroeder_decay(impulse);
2302    let decay_db: Vec<f32> = decay.iter().map(|&v| 10.0 * v.max(1e-9).log10()).collect();
2303
2304    // Find -5dB and -25dB points
2305    let t_minus_5 = decay_db.iter().position(|&v| v < -5.0);
2306    let t_minus_25 = decay_db.iter().position(|&v| v < -25.0);
2307
2308    match (t_minus_5, t_minus_25) {
2309        (Some(start), Some(end)) if end > start => {
2310            let dt = (end - start) as f32 / sample_rate; // Time for 20dB decay
2311            dt * 3.0 // Extrapolate to 60dB (T20 * 3)
2312        }
2313        _ => 0.0,
2314    }
2315}
2316
2317/// Compute Clarity (C50, C80) from Impulse Response (Broadband)
2318/// Returns (C50_dB, C80_dB)
2319pub fn compute_clarity_broadband(impulse: &[f32], sample_rate: f32) -> (f32, f32) {
2320    let mut energy_0_50 = 0.0;
2321    let mut energy_50_inf = 0.0;
2322    let mut energy_0_80 = 0.0;
2323    let mut energy_80_inf = 0.0;
2324
2325    let samp_50ms = (0.050 * sample_rate) as usize;
2326    let samp_80ms = (0.080 * sample_rate) as usize;
2327
2328    for (i, &samp) in impulse.iter().enumerate() {
2329        let sq = samp * samp;
2330
2331        if i < samp_50ms {
2332            energy_0_50 += sq;
2333        } else {
2334            energy_50_inf += sq;
2335        }
2336
2337        if i < samp_80ms {
2338            energy_0_80 += sq;
2339        } else {
2340            energy_80_inf += sq;
2341        }
2342    }
2343
2344    // When late energy is negligible, clarity is very high (capped at 60 dB)
2345    // When early energy is negligible, clarity is very low (capped at -60 dB)
2346    const MAX_CLARITY_DB: f32 = 60.0;
2347
2348    let c50 = if energy_50_inf > 1e-12 && energy_0_50 > 1e-12 {
2349        let ratio = energy_0_50 / energy_50_inf;
2350        (10.0 * ratio.log10()).clamp(-MAX_CLARITY_DB, MAX_CLARITY_DB)
2351    } else if energy_0_50 > energy_50_inf {
2352        MAX_CLARITY_DB // Early energy dominates - excellent clarity
2353    } else {
2354        -MAX_CLARITY_DB // Late energy dominates - poor clarity
2355    };
2356
2357    let c80 = if energy_80_inf > 1e-12 && energy_0_80 > 1e-12 {
2358        let ratio = energy_0_80 / energy_80_inf;
2359        (10.0 * ratio.log10()).clamp(-MAX_CLARITY_DB, MAX_CLARITY_DB)
2360    } else if energy_80_inf > energy_0_80 {
2361        MAX_CLARITY_DB // Early energy dominates - excellent clarity
2362    } else {
2363        -MAX_CLARITY_DB // Late energy dominates - poor clarity
2364    };
2365
2366    (c50, c80)
2367}
2368
2369/// Compute RT60 spectrum using octave band filtering
2370pub fn compute_rt60_spectrum(impulse: &[f32], sample_rate: f32, frequencies: &[f32]) -> Vec<f32> {
2371    if impulse.is_empty() {
2372        return vec![0.0; frequencies.len()];
2373    }
2374
2375    // Octave band center frequencies
2376    let centers = [
2377        63.0f32, 125.0, 250.0, 500.0, 1000.0, 2000.0, 4000.0, 8000.0, 16000.0,
2378    ];
2379    let mut band_rt60s = Vec::with_capacity(centers.len());
2380    let mut valid_centers = Vec::with_capacity(centers.len());
2381
2382    // Compute RT60 for each band
2383    for &freq in &centers {
2384        // Skip if frequency is too high for sample rate
2385        if freq >= sample_rate / 2.0 {
2386            continue;
2387        }
2388
2389        // Apply bandpass filter
2390        // Q=1.414 (sqrt(2)) gives approx 1 octave bandwidth
2391        let mut biquad = Biquad::new(
2392            BiquadFilterType::Bandpass,
2393            freq as f64,
2394            sample_rate as f64,
2395            1.414,
2396            0.0,
2397        );
2398
2399        // Process in f64
2400        let mut filtered: Vec<f64> = impulse.iter().map(|&x| x as f64).collect();
2401        biquad.process_block(&mut filtered);
2402        let filtered_f32: Vec<f32> = filtered.iter().map(|&x| x as f32).collect();
2403
2404        // Compute RT60 for this band
2405        let rt60 = compute_rt60_broadband(&filtered_f32, sample_rate);
2406
2407        band_rt60s.push(rt60);
2408        valid_centers.push(freq);
2409    }
2410
2411    // Log per-band values
2412    log::info!(
2413        "[RT60] Per-band values: {:?}",
2414        valid_centers
2415            .iter()
2416            .zip(band_rt60s.iter())
2417            .map(|(f, v)| format!("{:.0}Hz:{:.1}ms", f, v))
2418            .collect::<Vec<_>>()
2419    );
2420
2421    if valid_centers.is_empty() {
2422        return vec![0.0; frequencies.len()];
2423    }
2424
2425    // Interpolate to output frequencies
2426    interpolate_log(&valid_centers, &band_rt60s, frequencies)
2427}
2428
2429/// Compute Clarity spectrum (C50, C80) using octave band filtering
2430/// Returns (C50_vec, C80_vec)
2431pub fn compute_clarity_spectrum(
2432    impulse: &[f32],
2433    sample_rate: f32,
2434    frequencies: &[f32],
2435) -> (Vec<f32>, Vec<f32>) {
2436    if impulse.is_empty() || frequencies.is_empty() {
2437        return (vec![0.0; frequencies.len()], vec![0.0; frequencies.len()]);
2438    }
2439
2440    // Octave band center frequencies
2441    let centers = [
2442        63.0f32, 125.0, 250.0, 500.0, 1000.0, 2000.0, 4000.0, 8000.0, 16000.0,
2443    ];
2444    let mut band_c50s = Vec::with_capacity(centers.len());
2445    let mut band_c80s = Vec::with_capacity(centers.len());
2446    let mut valid_centers = Vec::with_capacity(centers.len());
2447
2448    // Time boundaries for clarity calculation
2449    let samp_50ms = (0.050 * sample_rate) as usize;
2450    let samp_80ms = (0.080 * sample_rate) as usize;
2451
2452    // Compute Clarity for each band using cascaded bandpass for better selectivity
2453    for &freq in &centers {
2454        if freq >= sample_rate / 2.0 {
2455            continue;
2456        }
2457
2458        // Use cascaded biquads for sharper filter response (reduces filter ringing effects)
2459        let mut biquad1 = Biquad::new(
2460            BiquadFilterType::Bandpass,
2461            freq as f64,
2462            sample_rate as f64,
2463            0.707, // Lower Q per stage, cascaded gives Q ~ 1.0
2464            0.0,
2465        );
2466        let mut biquad2 = Biquad::new(
2467            BiquadFilterType::Bandpass,
2468            freq as f64,
2469            sample_rate as f64,
2470            0.707,
2471            0.0,
2472        );
2473
2474        let mut filtered: Vec<f64> = impulse.iter().map(|&x| x as f64).collect();
2475        biquad1.process_block(&mut filtered);
2476        biquad2.process_block(&mut filtered);
2477
2478        // Compute energy in early and late windows directly
2479        let mut energy_0_50 = 0.0f64;
2480        let mut energy_50_inf = 0.0f64;
2481        let mut energy_0_80 = 0.0f64;
2482        let mut energy_80_inf = 0.0f64;
2483
2484        for (i, &samp) in filtered.iter().enumerate() {
2485            let sq = samp * samp;
2486
2487            if i < samp_50ms {
2488                energy_0_50 += sq;
2489            } else {
2490                energy_50_inf += sq;
2491            }
2492
2493            if i < samp_80ms {
2494                energy_0_80 += sq;
2495            } else {
2496                energy_80_inf += sq;
2497            }
2498        }
2499
2500        // Compute C50 and C80 with proper handling
2501        // When late energy is very small, clarity is high (capped at 40 dB for display)
2502        const MAX_CLARITY_DB: f32 = 40.0;
2503        const MIN_ENERGY: f64 = 1e-20;
2504
2505        let c50 = if energy_50_inf > MIN_ENERGY && energy_0_50 > MIN_ENERGY {
2506            let ratio = energy_0_50 / energy_50_inf;
2507            (10.0 * ratio.log10() as f32).clamp(-MAX_CLARITY_DB, MAX_CLARITY_DB)
2508        } else if energy_0_50 > energy_50_inf {
2509            MAX_CLARITY_DB
2510        } else {
2511            -MAX_CLARITY_DB
2512        };
2513
2514        let c80 = if energy_80_inf > MIN_ENERGY && energy_0_80 > MIN_ENERGY {
2515            let ratio = energy_0_80 / energy_80_inf;
2516            (10.0 * ratio.log10() as f32).clamp(-MAX_CLARITY_DB, MAX_CLARITY_DB)
2517        } else if energy_0_80 > energy_80_inf {
2518            MAX_CLARITY_DB
2519        } else {
2520            -MAX_CLARITY_DB
2521        };
2522
2523        band_c50s.push(c50);
2524        band_c80s.push(c80);
2525        valid_centers.push(freq);
2526    }
2527
2528    // Log per-band values
2529    log::info!(
2530        "[Clarity] Per-band C50: {:?}",
2531        valid_centers
2532            .iter()
2533            .zip(band_c50s.iter())
2534            .map(|(f, v)| format!("{:.0}Hz:{:.1}dB", f, v))
2535            .collect::<Vec<_>>()
2536    );
2537
2538    if valid_centers.is_empty() {
2539        return (vec![0.0; frequencies.len()], vec![0.0; frequencies.len()]);
2540    }
2541
2542    // Interpolate to output frequency grid
2543    let c50_interp = interpolate_log(&valid_centers, &band_c50s, frequencies);
2544    let c80_interp = interpolate_log(&valid_centers, &band_c80s, frequencies);
2545
2546    (c50_interp, c80_interp)
2547}
2548
2549/// Compute Spectrogram from Impulse Response
2550/// Returns (spectrogram_matrix_db, frequency_bins, time_bins)
2551/// `window_size` samples (e.g. 512), `hop_size` samples (e.g. 128).
2552pub fn compute_spectrogram(
2553    impulse: &[f32],
2554    sample_rate: f32,
2555    window_size: usize,
2556    hop_size: usize,
2557) -> (Vec<Vec<f32>>, Vec<f32>, Vec<f32>) {
2558    use rustfft::num_complex::Complex;
2559
2560    if impulse.len() < window_size {
2561        return (Vec::new(), Vec::new(), Vec::new());
2562    }
2563
2564    let num_frames = (impulse.len() - window_size) / hop_size;
2565    let mut spectrogram = Vec::with_capacity(num_frames);
2566    let mut times = Vec::with_capacity(num_frames);
2567
2568    // Precompute Hann window
2569    let window: Vec<f32> = (0..window_size)
2570        .map(|i| 0.5 * (1.0 - (2.0 * PI * i as f32 / (window_size as f32 - 1.0)).cos()))
2571        .collect();
2572
2573    // Setup FFT
2574    let fft = plan_fft_forward(window_size);
2575
2576    for i in 0..num_frames {
2577        let start = i * hop_size;
2578        let time_ms = (start as f32 / sample_rate) * 1000.0;
2579        times.push(time_ms);
2580
2581        let mut buffer: Vec<Complex<f32>> = (0..window_size)
2582            .map(|j| {
2583                let sample = impulse.get(start + j).copied().unwrap_or(0.0);
2584                Complex::new(sample * window[j], 0.0)
2585            })
2586            .collect();
2587
2588        fft.process(&mut buffer);
2589
2590        // Take magnitude of first half (up to Nyquist)
2591        // Store as dB
2592        let magnitude_db: Vec<f32> = buffer[..window_size / 2]
2593            .iter()
2594            .map(|c| {
2595                let mag = c.norm();
2596                if mag > 1e-9 {
2597                    20.0 * mag.log10()
2598                } else {
2599                    -180.0
2600                }
2601            })
2602            .collect();
2603
2604        spectrogram.push(magnitude_db);
2605    }
2606
2607    // Generate frequency bins
2608    let num_bins = window_size / 2;
2609    let freq_step = sample_rate / window_size as f32;
2610    let freqs: Vec<f32> = (0..num_bins).map(|i| i as f32 * freq_step).collect();
2611
2612    (spectrogram, freqs, times)
2613}
2614
2615/// Find a frequency point where the magnitude reaches a specific dB level
2616///
2617/// # Arguments
2618/// * `frequencies` - Frequency points in Hz
2619/// * `magnitude_db` - Magnitude in dB
2620/// * `target_db` - The target level to find (e.g., -3.0)
2621/// * `from_start` - If true, search from the beginning of the curve. If false, search from the end.
2622///
2623/// # Returns
2624/// The interpolated frequency where the target dB is reached, or None if not found.
2625pub fn find_db_point(
2626    frequencies: &[f32],
2627    magnitude_db: &[f32],
2628    target_db: f32,
2629    from_start: bool,
2630) -> Option<f32> {
2631    if frequencies.len() < 2 || frequencies.len() != magnitude_db.len() {
2632        return None;
2633    }
2634
2635    if from_start {
2636        for i in 0..magnitude_db.len() - 1 {
2637            let m0 = magnitude_db[i];
2638            let m1 = magnitude_db[i + 1];
2639
2640            // Check if target_db is between m0 and m1
2641            if (m0 <= target_db && target_db <= m1) || (m1 <= target_db && target_db <= m0) {
2642                // Linear interpolation: m0 + t * (m1 - m0) = target_db
2643                let denominator = m1 - m0;
2644                if denominator.abs() < 1e-9 {
2645                    return Some(frequencies[i]);
2646                }
2647                let t = (target_db - m0) / denominator;
2648                return Some(frequencies[i] + t * (frequencies[i + 1] - frequencies[i]));
2649            }
2650        }
2651    } else {
2652        for i in (1..magnitude_db.len()).rev() {
2653            let m0 = magnitude_db[i];
2654            let m1 = magnitude_db[i - 1];
2655
2656            // Check if target_db is between m0 and m1
2657            if (m0 <= target_db && target_db <= m1) || (m1 <= target_db && target_db <= m0) {
2658                let denominator = m1 - m0;
2659                if denominator.abs() < 1e-9 {
2660                    return Some(frequencies[i]);
2661                }
2662                let t = (target_db - m0) / denominator;
2663                return Some(frequencies[i] + t * (frequencies[i - 1] - frequencies[i]));
2664            }
2665        }
2666    }
2667
2668    None
2669}
2670
2671/// Compute a log-frequency weighted reference response level in dB.
2672///
2673/// # Arguments
2674/// * `frequencies` - Frequency points in Hz
2675/// * `magnitude_db` - Magnitude in dB
2676/// * `freq_range` - Optional (start_freq, end_freq) to limit the averaging range.
2677///   If None, averages over the full bandwidth.
2678///
2679/// # Returns
2680/// The log-frequency weighted average in the dB domain.
2681///
2682/// This is intended as a stable acoustic reference level for comparison and
2683/// normalization. It is not a pressure- or energy-domain average.
2684pub fn compute_average_response(
2685    frequencies: &[f32],
2686    magnitude_db: &[f32],
2687    freq_range: Option<(f32, f32)>,
2688) -> f32 {
2689    if frequencies.len() < 2 || frequencies.len() != magnitude_db.len() {
2690        return magnitude_db.first().copied().unwrap_or(0.0);
2691    }
2692
2693    let (start_freq, end_freq) =
2694        freq_range.unwrap_or((frequencies[0], frequencies[frequencies.len() - 1]));
2695
2696    let mut sum_weighted_db = 0.0;
2697    let mut sum_weights = 0.0;
2698
2699    for i in 0..frequencies.len() - 1 {
2700        let f0 = frequencies[i];
2701        let f1 = frequencies[i + 1];
2702
2703        // Check if this segment overlaps with the target range
2704        if f1 < start_freq || f0 > end_freq {
2705            continue;
2706        }
2707
2708        // Clamp segment to target range
2709        let fa = f0.max(start_freq);
2710        let fb = f1.min(end_freq);
2711
2712        if fb <= fa {
2713            continue;
2714        }
2715
2716        // For acoustic data, we weight by log frequency (octaves)
2717        // weight = log2(fb/fa)
2718        let weight = (fb / fa).log2();
2719
2720        // Average magnitude in this segment
2721        // We'll use the midpoint value of the segment (or average of endpoints)
2722        // If the segment is partially outside start_freq/end_freq, we should interpolate
2723        // but for many points simple average of endpoints in the segment is fine.
2724        let m0 = magnitude_db[i];
2725        let m1 = magnitude_db[i + 1];
2726        let avg_m = (m0 + m1) / 2.0;
2727
2728        sum_weighted_db += avg_m * weight;
2729        sum_weights += weight;
2730    }
2731
2732    if sum_weights > 0.0 {
2733        sum_weighted_db / sum_weights
2734    } else {
2735        magnitude_db.first().copied().unwrap_or(0.0)
2736    }
2737}
2738
2739// ---------------------------------------------------------------------------
2740// GD-Opt v2 Phase GD-1c — multi-sweep coherence + noise-floor primitives
2741//
2742// These three pure functions let callers (sotf-engine's
2743// `record_multi_sweep`) turn a batch of N recorded sweeps + a
2744// pre-silence noise-floor window into the extended `Curve` columns
2745// `coherence` and `noise_floor_db` documented in §2.3 / §2.4 of
2746// `docs/gd_opt_v2_plan.md`.
2747// ---------------------------------------------------------------------------
2748
2749/// Magnitude-squared coherence γ²(f) across N complex spectra that
2750/// should be measurements of the same deterministic transfer
2751/// function. Per `docs/gd_opt_v2_plan.md` §2.2:
2752///
2753/// ```text
2754///    γ²(f) = |H̄(f)|² / ⟨|H(f)|²⟩
2755///    H̄(f) = (1/N) Σ_i H_i(f)              (complex average)
2756///    ⟨|H(f)|²⟩ = (1/N) Σ_i |H_i(f)|²       (power average)
2757/// ```
2758///
2759/// Returns a vector of per-bin γ² in `[0, 1]`. For N = 1 γ² ≡ 1 by
2760/// construction — a single measurement is trivially "consistent with
2761/// itself" so callers must enforce the "at least 4 sweeps" rule at
2762/// a higher level (the `"insufficient_bass_duration"` advisory in
2763/// GD-1g).
2764///
2765/// Returns `Err` iff `realizations` is empty or any realization has
2766/// a different length than the first.
2767pub fn compute_coherence_from_realizations(
2768    realizations: &[Vec<Complex<f32>>],
2769) -> Result<Vec<f32>, String> {
2770    let n = realizations.len();
2771    if n == 0 {
2772        return Err("compute_coherence: empty realizations".to_string());
2773    }
2774    let bins = realizations[0].len();
2775    if bins == 0 {
2776        return Ok(Vec::new());
2777    }
2778    for (i, r) in realizations.iter().enumerate() {
2779        if r.len() != bins {
2780            return Err(format!(
2781                "compute_coherence: realization {i} has {} bins, expected {bins}",
2782                r.len()
2783            ));
2784        }
2785    }
2786
2787    let mut coherence = Vec::with_capacity(bins);
2788    for k in 0..bins {
2789        let mut sum = Complex::new(0.0_f64, 0.0_f64);
2790        let mut sum_sq = 0.0_f64;
2791        for r in realizations {
2792            let h = Complex::new(r[k].re as f64, r[k].im as f64);
2793            sum += h;
2794            sum_sq += h.re * h.re + h.im * h.im;
2795        }
2796        let mean = sum / (n as f64);
2797        let mean_sq = sum_sq / (n as f64);
2798        if mean_sq <= f64::EPSILON {
2799            // Dead bin — report as zero-confidence rather than NaN so
2800            // downstream thresholds just drop it.
2801            coherence.push(0.0);
2802        } else {
2803            let coh = (mean.norm_sqr() / mean_sq).clamp(0.0, 1.0);
2804            coherence.push(coh as f32);
2805        }
2806    }
2807
2808    Ok(coherence)
2809}
2810
2811/// Deconvolve a single recorded log sweep by dividing the recording's
2812/// spectrum by the emitted sweep's spectrum, producing a complex
2813/// frequency response on the FFT grid `[0, Nyquist]`.
2814///
2815/// The inverse-filter approach is the standard log-sweep
2816/// deconvolution:
2817///
2818/// ```text
2819///    H(f) = Y(f) / X(f)
2820/// ```
2821///
2822/// where Y is the recording and X is the emitted sweep. A small
2823/// regularisation term ε is added to the denominator to keep out-
2824/// of-band bins from blowing up — 60 dB below the sweep's peak is a
2825/// safe default.
2826///
2827/// The returned spectrum has `recording.len().next_power_of_two() / 2 + 1`
2828/// complex bins, indexed so bin k corresponds to frequency
2829/// `k * sample_rate / fft_size`.
2830///
2831/// Callers that want multiple realisations pass each captured sweep
2832/// through this function in turn and feed the collected `Vec<Vec<_>>`
2833/// to [`compute_coherence_from_realizations`].
2834pub fn deconvolve_sweep(
2835    recording: &[f32],
2836    reference: &[f32],
2837    sample_rate: u32,
2838) -> Result<Vec<Complex<f32>>, String> {
2839    if recording.len() != reference.len() {
2840        return Err(format!(
2841            "deconvolve_sweep: recording len {} != reference len {}",
2842            recording.len(),
2843            reference.len()
2844        ));
2845    }
2846    if recording.is_empty() {
2847        return Err("deconvolve_sweep: empty input".to_string());
2848    }
2849    if sample_rate == 0 {
2850        return Err("deconvolve_sweep: zero sample_rate".to_string());
2851    }
2852
2853    let n = recording.len();
2854    let fft_size = n.next_power_of_two();
2855
2856    let mut y: Vec<Complex<f32>> = recording.iter().map(|&s| Complex::new(s, 0.0)).collect();
2857    y.resize(fft_size, Complex::new(0.0, 0.0));
2858    let mut x: Vec<Complex<f32>> = reference.iter().map(|&s| Complex::new(s, 0.0)).collect();
2859    x.resize(fft_size, Complex::new(0.0, 0.0));
2860
2861    let fft = plan_fft_forward(fft_size);
2862    fft.process(&mut y);
2863    fft.process(&mut x);
2864
2865    // Regularisation: 60 dB below the sweep's peak bin magnitude.
2866    let x_peak = x
2867        .iter()
2868        .map(|c| c.norm())
2869        .fold(0.0_f32, f32::max)
2870        .max(1e-20);
2871    let epsilon = x_peak * 1e-3; // 60 dB below peak
2872    let eps_sq = epsilon * epsilon;
2873
2874    let spectrum_size = fft_size / 2 + 1;
2875    let mut h = Vec::with_capacity(spectrum_size);
2876    for k in 0..spectrum_size {
2877        // H = Y / X with Tikhonov-style regularisation:
2878        //   H = (Y · conj(X)) / (|X|² + ε²)
2879        let yk = y[k];
2880        let xk = x[k];
2881        let num = yk * xk.conj();
2882        let den = xk.norm_sqr() + eps_sq;
2883        h.push(num / den);
2884    }
2885    Ok(h)
2886}
2887
2888/// Estimate per-bin noise floor in dB from a silence window.
2889///
2890/// Takes the pre-silence samples captured before the sweep starts,
2891/// windows the FFT the same way the sweep analysis does, and returns
2892/// one dB value per positive-frequency bin (including DC and Nyquist,
2893/// i.e. `fft_size / 2 + 1` values). Result is reference-to-full-scale
2894/// (i.e., a silence bin at 0.001 linear amplitude maps to -60 dB).
2895///
2896/// The FFT size is taken as `silence.len().next_power_of_two()` so
2897/// the bin grid matches [`deconvolve_sweep`] when the silence window
2898/// is the same length as the sweep.
2899///
2900/// A Hann window is applied before the FFT to reduce spectral
2901/// leakage that would otherwise push DC noise into every other bin
2902/// and make bass SNR look healthier than it is.
2903pub fn estimate_noise_floor_db_from_silence(silence: &[f32], _sample_rate: u32) -> Vec<f32> {
2904    if silence.is_empty() {
2905        return Vec::new();
2906    }
2907    let n = silence.len();
2908    let fft_size = n.next_power_of_two();
2909    let spectrum_size = fft_size / 2 + 1;
2910
2911    // Hann-window the silence before FFT.
2912    let mut buf: Vec<Complex<f32>> = silence
2913        .iter()
2914        .enumerate()
2915        .map(|(k, &s)| {
2916            let w = 0.5 * (1.0 - (2.0 * std::f32::consts::PI * k as f32 / (n as f32 - 1.0)).cos());
2917            Complex::new(s * w, 0.0)
2918        })
2919        .collect();
2920    buf.resize(fft_size, Complex::new(0.0, 0.0));
2921
2922    let fft = plan_fft_forward(fft_size);
2923    fft.process(&mut buf);
2924
2925    // Windowed amplitude normalisation for a real sinusoid on a bin
2926    // centre. The FFT of `sin(2π·m·k/N)` has magnitude `N/2` at bin
2927    // `m`, and Hann windowing multiplies that by its coherent gain
2928    // of `0.5` — so the windowed peak is `N/4`. Multiply by `4/N` to
2929    // recover the underlying sinusoid amplitude (and let
2930    // `20·log10(mag)` match the tone's dBFS).
2931    let norm = 4.0 / n as f32;
2932
2933    buf.into_iter()
2934        .take(spectrum_size)
2935        .map(|c| {
2936            let mag = c.norm() * norm;
2937            if mag > 1e-20 {
2938                20.0 * mag.log10()
2939            } else {
2940                -400.0 // effectively "nothing"; avoids -inf leaking downstream
2941            }
2942        })
2943        .collect()
2944}
2945
2946#[cfg(test)]
2947mod gd_1c_tests {
2948    use super::*;
2949    use std::f32::consts::PI;
2950
2951    #[test]
2952    fn coherence_single_realization_is_unity() {
2953        let h = vec![
2954            Complex::new(1.0, 0.0),
2955            Complex::new(0.5, 0.5),
2956            Complex::new(0.0, 1.0),
2957        ];
2958        let coh = compute_coherence_from_realizations(&[h]).unwrap();
2959        assert_eq!(coh.len(), 3);
2960        for c in coh {
2961            // A single realization is trivially "coherent with itself"
2962            // — γ² = 1 by construction. Callers must enforce N ≥ 4
2963            // at a higher level.
2964            assert!(
2965                (c - 1.0).abs() < 1e-6,
2966                "single-realization γ² should be 1, got {c}"
2967            );
2968        }
2969    }
2970
2971    #[test]
2972    fn coherence_identical_realizations_is_unity() {
2973        let r = vec![
2974            Complex::new(0.8, 0.2),
2975            Complex::new(0.0, 1.0),
2976            Complex::new(-0.5, 0.5),
2977        ];
2978        let realizations = vec![r.clone(), r.clone(), r.clone(), r];
2979        let coh = compute_coherence_from_realizations(&realizations).unwrap();
2980        for c in coh {
2981            assert!(
2982                (c - 1.0).abs() < 1e-6,
2983                "identical realizations → γ² = 1, got {c}"
2984            );
2985        }
2986    }
2987
2988    #[test]
2989    fn coherence_random_realizations_is_zero() {
2990        // Four realizations whose phases cancel out on average:
2991        // ±1 and ±i. The complex mean is 0, so γ² = 0.
2992        let bins = 3;
2993        let r0: Vec<Complex<f32>> = (0..bins).map(|_| Complex::new(1.0, 0.0)).collect();
2994        let r1: Vec<Complex<f32>> = (0..bins).map(|_| Complex::new(-1.0, 0.0)).collect();
2995        let r2: Vec<Complex<f32>> = (0..bins).map(|_| Complex::new(0.0, 1.0)).collect();
2996        let r3: Vec<Complex<f32>> = (0..bins).map(|_| Complex::new(0.0, -1.0)).collect();
2997        let coh = compute_coherence_from_realizations(&[r0, r1, r2, r3]).unwrap();
2998        for c in coh {
2999            assert!(c < 1e-6, "canceling-phase realizations → γ² ≈ 0, got {c}");
3000        }
3001    }
3002
3003    #[test]
3004    fn coherence_rejects_mismatched_lengths() {
3005        let r0 = vec![Complex::new(1.0_f32, 0.0); 3];
3006        let r1 = vec![Complex::new(1.0_f32, 0.0); 4];
3007        let err = compute_coherence_from_realizations(&[r0, r1]).unwrap_err();
3008        assert!(err.contains("has 4 bins, expected 3"), "got: {err}");
3009    }
3010
3011    #[test]
3012    fn coherence_empty_input_errors() {
3013        let err = compute_coherence_from_realizations(&[]).unwrap_err();
3014        assert!(err.contains("empty"), "got: {err}");
3015    }
3016
3017    #[test]
3018    fn deconvolve_matches_unity_system() {
3019        // If the recorded signal IS the emitted sweep, H should be
3020        // approximately 1 across the passband.
3021        let n: usize = 1024;
3022        let sr = 48_000_u32;
3023        let sweep: Vec<f32> = (0..n)
3024            .map(|k| {
3025                let t = k as f32 / sr as f32;
3026                let f = 100.0 * (10.0_f32).powf(3.0 * t / (n as f32 / sr as f32));
3027                (2.0 * PI * f * t).sin() * 0.5
3028            })
3029            .collect();
3030        let recording = sweep.clone();
3031        let h = deconvolve_sweep(&recording, &sweep, sr).unwrap();
3032        assert_eq!(h.len(), n.next_power_of_two() / 2 + 1);
3033        // Mid-band bins should be ≈ 1 (within the regularisation
3034        // floor). Check bins 10..50 — avoids DC where the sweep has
3035        // no energy and the Nyquist edge where the log sweep dies out.
3036        let mid_slice = &h[10..50];
3037        for (i, c) in mid_slice.iter().enumerate() {
3038            let mag = c.norm();
3039            assert!(
3040                mag > 0.1 && mag < 10.0,
3041                "bin {} magnitude {mag} out of expected range",
3042                i + 10
3043            );
3044        }
3045    }
3046
3047    #[test]
3048    fn deconvolve_rejects_length_mismatch() {
3049        let a = vec![0.0_f32; 10];
3050        let b = vec![0.0_f32; 11];
3051        let err = deconvolve_sweep(&a, &b, 48_000).unwrap_err();
3052        assert!(err.contains("!="), "got: {err}");
3053    }
3054
3055    #[test]
3056    fn noise_floor_pure_silence_is_very_low() {
3057        let silence = vec![0.0_f32; 4096];
3058        let nf = estimate_noise_floor_db_from_silence(&silence, 48_000);
3059        assert_eq!(nf.len(), 4096 / 2 + 1);
3060        for (i, v) in nf.iter().enumerate() {
3061            assert!(
3062                *v < -200.0,
3063                "pure silence bin {i} should report extremely low dB, got {v}",
3064            );
3065        }
3066    }
3067
3068    #[test]
3069    fn noise_floor_tone_peaks_at_exact_bin() {
3070        // Pick a frequency that lands exactly on an FFT bin centre
3071        // so there's no inter-bin leakage. Hann windowing still
3072        // splits ~half the peak energy into the two adjacent bins by
3073        // design; at the exact centre the main-lobe peak returns
3074        // within ~1 dB of the target.
3075        let sr = 48_000_u32;
3076        let n: usize = 4096;
3077        let target_bin = 100_usize;
3078        let freq = (target_bin as f32 * sr as f32) / n as f32; // 1171.875 Hz
3079        let amp_db = -40.0_f32;
3080        let amp = 10.0_f32.powf(amp_db / 20.0);
3081        let tone: Vec<f32> = (0..n)
3082            .map(|k| amp * (2.0 * PI * freq * k as f32 / sr as f32).sin())
3083            .collect();
3084        let nf = estimate_noise_floor_db_from_silence(&tone, sr);
3085        // Find the peak bin in a small bracket around the target.
3086        let mut peak_db = f32::NEG_INFINITY;
3087        let mut peak_bin = 0;
3088        for (k, v) in nf
3089            .iter()
3090            .enumerate()
3091            .take(target_bin + 3)
3092            .skip(target_bin - 2)
3093        {
3094            if *v > peak_db {
3095                peak_db = *v;
3096                peak_bin = k;
3097            }
3098        }
3099        assert_eq!(
3100            peak_bin, target_bin,
3101            "peak bin should be at the tone frequency"
3102        );
3103        assert!(
3104            (peak_db - amp_db).abs() < 1.5,
3105            "peak dB {peak_db} should be within ±1.5 dB of target {amp_db}"
3106        );
3107    }
3108}
3109
3110#[cfg(test)]
3111mod tests {
3112    use super::*;
3113
3114    #[test]
3115    fn test_next_power_of_two() {
3116        assert_eq!(next_power_of_two(1), 1);
3117        assert_eq!(next_power_of_two(2), 2);
3118        assert_eq!(next_power_of_two(3), 4);
3119        assert_eq!(next_power_of_two(1000), 1024);
3120        assert_eq!(next_power_of_two(1024), 1024);
3121        assert_eq!(next_power_of_two(1025), 2048);
3122    }
3123
3124    #[test]
3125    fn test_hann_window() {
3126        let signal = vec![1.0; 100];
3127        let windowed = apply_hann_window(&signal);
3128
3129        // First and last samples should be near zero
3130        assert!(windowed[0].abs() < 0.01);
3131        assert!(windowed[99].abs() < 0.01);
3132
3133        // Middle sample should be near 1.0
3134        assert!((windowed[50] - 1.0).abs() < 0.01);
3135    }
3136
3137    #[test]
3138    fn test_estimate_lag_zero() {
3139        // Identical signals should have zero lag
3140        let signal = vec![1.0, 2.0, 3.0, 4.0, 5.0];
3141        let lag = estimate_lag(&signal, &signal).unwrap();
3142        assert_eq!(lag, 0);
3143    }
3144
3145    #[test]
3146    fn test_estimate_lag_positive() {
3147        // Reference leads recorded (recorded is delayed)
3148        // Use longer signals for reliable FFT-based cross-correlation
3149        let mut reference = vec![0.0; 100];
3150        let mut recorded = vec![0.0; 100];
3151
3152        // Create a pulse pattern that will correlate well
3153        for (j, val) in reference[10..20].iter_mut().enumerate() {
3154            *val = j as f32 / 10.0;
3155        }
3156        // Same pattern but delayed by 5 samples
3157        for (j, val) in recorded[15..25].iter_mut().enumerate() {
3158            *val = j as f32 / 10.0;
3159        }
3160
3161        let lag = estimate_lag(&reference, &recorded).unwrap();
3162        assert_eq!(lag, 5, "Recorded signal is delayed by 5 samples");
3163    }
3164
3165    #[test]
3166    fn test_identical_signals_have_zero_lag() {
3167        // When signals are truly identical (like in the bug case),
3168        // lag should be exactly zero
3169        let signal = vec![1.0, 2.0, 3.0, 4.0, 5.0];
3170        let lag = estimate_lag(&signal, &signal).unwrap();
3171        assert_eq!(lag, 0, "Identical signals should have zero lag");
3172    }
3173
3174    /// Write a mono f32 WAV file for testing
3175    fn write_test_wav(path: &std::path::Path, samples: &[f32], sample_rate: u32) {
3176        let spec = hound::WavSpec {
3177            channels: 1,
3178            sample_rate,
3179            bits_per_sample: 32,
3180            sample_format: hound::SampleFormat::Float,
3181        };
3182        let mut writer = hound::WavWriter::create(path, spec).unwrap();
3183        for &s in samples {
3184            writer.write_sample(s).unwrap();
3185        }
3186        writer.finalize().unwrap();
3187    }
3188
3189    /// Generate a log sweep signal (same as the recording system uses)
3190    fn generate_test_sweep(
3191        start_freq: f32,
3192        end_freq: f32,
3193        duration_secs: f32,
3194        sample_rate: u32,
3195        amplitude: f32,
3196    ) -> Vec<f32> {
3197        let num_samples = (duration_secs * sample_rate as f32) as usize;
3198        let mut signal = Vec::with_capacity(num_samples);
3199        let ln_ratio = (end_freq / start_freq).ln();
3200        for i in 0..num_samples {
3201            let t = i as f32 / sample_rate as f32;
3202            let phase = 2.0 * PI * start_freq * duration_secs / ln_ratio
3203                * ((t / duration_secs * ln_ratio).exp() - 1.0);
3204            signal.push(amplitude * phase.sin());
3205        }
3206        signal
3207    }
3208
3209    #[test]
3210    fn test_analyze_recording_normal_channel() {
3211        // Simulate a normal speaker: reference sweep played back and recorded
3212        // with some attenuation and small delay
3213        let sample_rate = 48000;
3214        let duration = 1.0;
3215        let reference = generate_test_sweep(20.0, 20000.0, duration, sample_rate, 0.5);
3216
3217        // Simulate recording: attenuate by ~-6dB (factor 0.5) and delay by 100 samples
3218        let delay = 100;
3219        let attenuation = 0.5;
3220        let mut recorded = vec![0.0_f32; reference.len() + delay];
3221        for (i, &s) in reference.iter().enumerate() {
3222            recorded[i + delay] = s * attenuation;
3223        }
3224
3225        let dir = std::env::temp_dir().join(format!("sotf_test_normal_{}", std::process::id()));
3226        std::fs::create_dir_all(&dir).unwrap();
3227        let wav_path = dir.join("test_normal.wav");
3228        write_test_wav(&wav_path, &recorded, sample_rate);
3229
3230        let result = analyze_recording(&wav_path, &reference, sample_rate, None).unwrap();
3231        std::fs::remove_dir_all(&dir).ok();
3232
3233        // Compute average SPL in the passband (200 Hz - 10 kHz)
3234        let mut sum = 0.0_f32;
3235        let mut count = 0;
3236        for (&freq, &db) in result.frequencies.iter().zip(result.spl_db.iter()) {
3237            if (200.0..=10000.0).contains(&freq) {
3238                sum += db;
3239                count += 1;
3240            }
3241        }
3242        let avg_db = sum / count as f32;
3243
3244        // Expected: ~-6 dB (attenuation factor 0.5)
3245        // Allow generous tolerance for windowing/FFT artifacts
3246        assert!(
3247            avg_db > -12.0 && avg_db < 0.0,
3248            "Normal channel avg SPL should be near -6 dB, got {:.1} dB",
3249            avg_db
3250        );
3251
3252        // No bin should exceed +6 dB (physically implausible for passive attenuation)
3253        let max_db = result
3254            .spl_db
3255            .iter()
3256            .zip(result.frequencies.iter())
3257            .filter(|&(_, &f)| (200.0..=10000.0).contains(&f))
3258            .map(|(&db, _)| db)
3259            .fold(f32::NEG_INFINITY, f32::max);
3260        assert!(
3261            max_db < 6.0,
3262            "Normal channel should not have bins above +6 dB, got {:.1} dB",
3263            max_db
3264        );
3265    }
3266
3267    #[test]
3268    fn test_analyze_recording_silent_channel() {
3269        // Simulate a disconnected speaker: reference sweep played but recording
3270        // is just low-level noise (no speaker output)
3271        let sample_rate = 48000;
3272        let duration = 1.0;
3273        let reference = generate_test_sweep(20.0, 20000.0, duration, sample_rate, 0.5);
3274
3275        // Recording is pure noise at -60 dBFS (amplitude 0.001)
3276        let noise_amplitude = 0.001;
3277        let num_samples = reference.len();
3278        let mut recorded = Vec::with_capacity(num_samples);
3279        // Use deterministic "noise" (alternating small values)
3280        for i in 0..num_samples {
3281            let pseudo_noise =
3282                noise_amplitude * (((i as f32 * 0.1).sin() + (i as f32 * 0.37).cos()) * 0.5);
3283            recorded.push(pseudo_noise);
3284        }
3285
3286        let dir = std::env::temp_dir().join(format!("sotf_test_silent_{}", std::process::id()));
3287        std::fs::create_dir_all(&dir).unwrap();
3288        let wav_path = dir.join("test_silent.wav");
3289        write_test_wav(&wav_path, &recorded, sample_rate);
3290
3291        let result = analyze_recording(&wav_path, &reference, sample_rate, None).unwrap();
3292        std::fs::remove_dir_all(&dir).ok();
3293
3294        // For a disconnected channel, the transfer function should be very low
3295        // (noise / sweep ≈ noise floor). It must NOT show spurious high-dB peaks.
3296        let max_db = result
3297            .spl_db
3298            .iter()
3299            .zip(result.frequencies.iter())
3300            .filter(|&(_, &f)| (100.0..=10000.0).contains(&f))
3301            .map(|(&db, _)| db)
3302            .fold(f32::NEG_INFINITY, f32::max);
3303
3304        assert!(
3305            max_db < 0.0,
3306            "Silent/disconnected channel should not have positive dB values, got max {:.1} dB",
3307            max_db
3308        );
3309    }
3310
3311    #[test]
3312    fn test_analyze_recording_lfe_narrow_sweep_same_point_count() {
3313        // Simulate a 5.1 scenario: LFE uses a narrow sweep (20-500 Hz) while
3314        // main channels use the full range (20-20000 Hz). Both must produce
3315        // the same number of output frequency points to avoid ndarray shape
3316        // mismatches when curves are combined in the optimizer.
3317        let sample_rate = 48000;
3318        let duration = 1.0;
3319
3320        // Full-range reference (main channel)
3321        let ref_full = generate_test_sweep(20.0, 20000.0, duration, sample_rate, 0.5);
3322        // Narrow reference (LFE)
3323        let ref_lfe = generate_test_sweep(20.0, 500.0, duration, sample_rate, 0.5);
3324
3325        // Simulate recordings: attenuated copies with delay
3326        let delay = 50;
3327        let atten = 0.3;
3328
3329        let mut rec_full = vec![0.0_f32; ref_full.len() + delay];
3330        for (i, &s) in ref_full.iter().enumerate() {
3331            rec_full[i + delay] = s * atten;
3332        }
3333
3334        let mut rec_lfe = vec![0.0_f32; ref_lfe.len() + delay];
3335        for (i, &s) in ref_lfe.iter().enumerate() {
3336            rec_lfe[i + delay] = s * atten;
3337        }
3338
3339        let dir = std::env::temp_dir().join(format!("sotf_test_lfe_points_{}", std::process::id()));
3340        std::fs::create_dir_all(&dir).unwrap();
3341
3342        let wav_full = dir.join("main.wav");
3343        let wav_lfe = dir.join("lfe.wav");
3344        write_test_wav(&wav_full, &rec_full, sample_rate);
3345        write_test_wav(&wav_lfe, &rec_lfe, sample_rate);
3346
3347        let result_full = analyze_recording(&wav_full, &ref_full, sample_rate, None).unwrap();
3348        let result_lfe = analyze_recording(&wav_lfe, &ref_lfe, sample_rate, None).unwrap();
3349        std::fs::remove_dir_all(&dir).ok();
3350
3351        // Both must produce the same number of frequency points
3352        assert_eq!(
3353            result_full.frequencies.len(),
3354            result_lfe.frequencies.len(),
3355            "Main ({}) and LFE ({}) must have the same number of frequency points",
3356            result_full.frequencies.len(),
3357            result_lfe.frequencies.len()
3358        );
3359        assert_eq!(
3360            result_full.spl_db.len(),
3361            result_lfe.spl_db.len(),
3362            "SPL arrays must match in length"
3363        );
3364
3365        // LFE should have valid data below ~500 Hz and noise floor above
3366        let lfe_valid_count = result_lfe
3367            .spl_db
3368            .iter()
3369            .zip(result_lfe.frequencies.iter())
3370            .filter(|&(&db, &f)| f <= 500.0 && db > -100.0)
3371            .count();
3372        assert!(
3373            lfe_valid_count > 100,
3374            "LFE should have valid data below 500 Hz, got {} points",
3375            lfe_valid_count
3376        );
3377
3378        let lfe_above_500_max = result_lfe
3379            .spl_db
3380            .iter()
3381            .zip(result_lfe.frequencies.iter())
3382            .filter(|&(_, &f)| f > 1000.0)
3383            .map(|(&db, _)| db)
3384            .fold(f32::NEG_INFINITY, f32::max);
3385        assert!(
3386            lfe_above_500_max <= -100.0,
3387            "LFE above 1 kHz should be at noise floor, got {:.1} dB",
3388            lfe_above_500_max
3389        );
3390    }
3391
3392    #[test]
3393    fn test_cross_correlate_envelope_known_delay() {
3394        // Generate a narrowband probe, delay it, and verify detection
3395        let n = 4096;
3396        let sr = 48000_u32;
3397        let probe = crate::signals::gen_narrowband_probe(n, sr, 0.5, 42, 800.0, 2000.0);
3398
3399        // Simulate recording: delay by 240 samples (~5ms) + attenuation
3400        let delay = 240_usize;
3401        let attenuation = 0.3;
3402        let mut recorded = vec![0.0_f32; n + delay + 1000];
3403        for (i, &s) in probe.iter().enumerate() {
3404            recorded[i + delay] += s * attenuation;
3405        }
3406
3407        let result = cross_correlate_envelope(&probe, &recorded, sr).unwrap();
3408
3409        // Peak should be near the known delay
3410        let detected_samples = result.peak_sample;
3411        assert!(
3412            (detected_samples as isize - delay as isize).unsigned_abs() <= 2,
3413            "Expected delay ~{} samples, got {}",
3414            delay,
3415            detected_samples
3416        );
3417
3418        // Arrival time should be ~5ms
3419        assert!(
3420            (result.arrival_ms - 5.0).abs() < 0.1,
3421            "Expected ~5.0 ms, got {:.3} ms",
3422            result.arrival_ms
3423        );
3424    }
3425
3426    #[test]
3427    fn test_cross_correlate_envelope_with_noise() {
3428        // Probe detection should work even with additive noise
3429        let n = 4096;
3430        let sr = 48000_u32;
3431        let probe = crate::signals::gen_narrowband_probe(n, sr, 0.5, 42, 800.0, 2000.0);
3432
3433        let delay = 480_usize; // 10ms
3434        let mut recorded = vec![0.0_f32; n + delay + 1000];
3435        for (i, &s) in probe.iter().enumerate() {
3436            recorded[i + delay] += s * 0.5;
3437        }
3438        // Add noise
3439        let noise = crate::signals::gen_white_noise(0.1, sr, recorded.len() as f32 / sr as f32);
3440        for (r, &n_s) in recorded.iter_mut().zip(noise.iter()) {
3441            *r += n_s;
3442        }
3443
3444        let result = cross_correlate_envelope(&probe, &recorded, sr).unwrap();
3445
3446        assert!(
3447            (result.peak_sample as isize - delay as isize).unsigned_abs() <= 2,
3448            "Expected delay ~{}, got {} (with noise)",
3449            delay,
3450            result.peak_sample
3451        );
3452    }
3453
3454    #[test]
3455    fn test_windowed_fr_synthetic() {
3456        // Create synthetic IR: impulse at sample 0 + delayed impulse at sample 240 (5ms)
3457        // Direct window [0, 240) should show flat response
3458        // Early window [240, 1920) should show the reflection's response
3459        let sr = 48000;
3460        let mut ir = vec![0.0f32; 4096];
3461        ir[0] = 1.0; // direct sound
3462        ir[240] = 0.5; // reflection at 5ms, -6dB
3463
3464        let result = compute_windowed_fr(&ir, 240, 1920, sr, 200).unwrap();
3465
3466        // Direct window should have content
3467        assert!(!result.direct_sound_spl.is_empty());
3468        assert!(!result.early_reflections_spl.is_empty());
3469        assert!(!result.late_reverb_spl.is_empty());
3470
3471        // All frequency vectors should have the requested number of points
3472        assert_eq!(result.direct_sound_freq.len(), 200);
3473        assert_eq!(result.early_reflections_freq.len(), 200);
3474        assert_eq!(result.late_reverb_freq.len(), 200);
3475
3476        // Time boundaries should match
3477        assert!((result.direct_end_ms - 5.0).abs() < 0.01);
3478        assert!((result.early_end_ms - 40.0).abs() < 0.01);
3479
3480        // Direct sound should be roughly flat above the resolution limit.
3481        // Short window = poor LF resolution, but mid-HF should be flat.
3482        // Filter to frequencies above 500 Hz where the 240-sample window has resolution
3483        let mid_hf: Vec<f32> = result
3484            .direct_sound_freq
3485            .iter()
3486            .zip(result.direct_sound_spl.iter())
3487            .filter(|&(&f, _)| f > 500.0 && f < 18000.0)
3488            .map(|(_, &spl)| spl)
3489            .collect();
3490        if mid_hf.len() > 2 {
3491            let max = mid_hf.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
3492            let min = mid_hf.iter().fold(f32::INFINITY, |a, &b| a.min(b));
3493            let range = max - min;
3494            assert!(
3495                range < 12.0,
3496                "Direct sound mid-HF range too large: {:.1} dB",
3497                range
3498            );
3499        }
3500    }
3501
3502    #[test]
3503    fn test_windowed_fr_empty_window() {
3504        // If early_end == direct_end (no early reflections), that window should be empty/silent
3505        let sr = 48000;
3506        let mut ir = vec![0.0f32; 2048];
3507        // Place impulse away from window edges so fading doesn't zero it out
3508        ir[50] = 1.0;
3509
3510        let result = compute_windowed_fr(&ir, 200, 200, sr, 200).unwrap();
3511
3512        // Early reflections window is zero-length — SPL should be very low
3513        assert_eq!(result.early_reflections_spl.len(), 200);
3514        for &spl in &result.early_reflections_spl {
3515            assert!(
3516                spl <= -199.0,
3517                "Expected silent early reflections, got {:.1} dB",
3518                spl
3519            );
3520        }
3521
3522        // Direct and late should still have content
3523        let direct_max = result
3524            .direct_sound_spl
3525            .iter()
3526            .fold(f32::NEG_INFINITY, |a, &b| a.max(b));
3527        assert!(
3528            direct_max > -100.0,
3529            "Direct sound should have content, max was {:.1} dB",
3530            direct_max
3531        );
3532    }
3533}