solvr 0.2.0

Advanced computing library for real-world problem solving - optimization, differential equations, interpolation, statistics, and more
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! CPU implementation of signal analysis algorithms.
//!
//! Some algorithms (hilbert, resample) are GPU-accelerable and delegate to impl_generic.
//! Others (decimate, find_peaks, savgol) are CPU-only due to their sequential access patterns.

use crate::signal::impl_generic::{
    apply_butter_lowpass, apply_fir_lowpass, compute_prominences, compute_savgol_coeffs,
    filter_by_distance, hilbert_impl, resample_impl,
};
use crate::signal::traits::analysis::{
    DecimateFilterImpl, DecimateParams, HilbertResult, PeakParams, PeakResult,
    SignalAnalysisAlgorithms,
};
use numr::error::{Error, Result};
use numr::runtime::cpu::{CpuClient, CpuRuntime};
use numr::tensor::Tensor;

impl SignalAnalysisAlgorithms<CpuRuntime> for CpuClient {
    fn hilbert(&self, x: &Tensor<CpuRuntime>) -> Result<HilbertResult<CpuRuntime>> {
        hilbert_impl(self, x)
    }

    fn resample(
        &self,
        x: &Tensor<CpuRuntime>,
        num: usize,
        den: usize,
    ) -> Result<Tensor<CpuRuntime>> {
        resample_impl(self, x, num, den)
    }

    fn decimate(
        &self,
        x: &Tensor<CpuRuntime>,
        q: usize,
        params: DecimateParams,
    ) -> Result<Tensor<CpuRuntime>> {
        decimate_cpu(x, q, params)
    }

    fn find_peaks(
        &self,
        x: &Tensor<CpuRuntime>,
        params: PeakParams,
    ) -> Result<PeakResult<CpuRuntime>> {
        find_peaks_cpu(x, params)
    }

    fn savgol_filter(
        &self,
        x: &Tensor<CpuRuntime>,
        window_length: usize,
        polyorder: usize,
        deriv: usize,
    ) -> Result<Tensor<CpuRuntime>> {
        savgol_filter_cpu(x, window_length, polyorder, deriv)
    }
}

/// Decimate a signal (CPU implementation).
///
/// This is CPU-only because it uses IIR/FIR filtering which is inherently sequential.
fn decimate_cpu(
    x: &Tensor<CpuRuntime>,
    q: usize,
    params: DecimateParams,
) -> Result<Tensor<CpuRuntime>> {
    // CPU-specific: extract data for processing
    let x_data: Vec<f64> = x.to_vec();
    let n = x_data.len();
    let device = x.device();

    if n == 0 {
        return Err(Error::InvalidArgument {
            arg: "x",
            reason: "Input signal cannot be empty".to_string(),
        });
    }

    if q == 0 {
        return Err(Error::InvalidArgument {
            arg: "q",
            reason: "Decimation factor must be positive".to_string(),
        });
    }

    if q == 1 {
        return Ok(x.clone());
    }

    // Design anti-aliasing filter
    // Cutoff at 0.8 * Nyquist / q to prevent aliasing
    let cutoff = 0.8 / q as f64;

    // Apply filter
    let filtered = match params.ftype {
        DecimateFilterImpl::Iir => {
            // Use simple IIR Butterworth lowpass
            apply_butter_lowpass(&x_data, cutoff, params.n, params.zero_phase)
        }
        DecimateFilterImpl::Fir => {
            // Use FIR lowpass
            let fir_len = params.n * 2 * q + 1;
            apply_fir_lowpass(&x_data, cutoff, fir_len)
        }
    };

    // Downsample by taking every q-th sample
    let output_len = n.div_ceil(q);
    let mut result = Vec::with_capacity(output_len);

    for i in 0..output_len {
        let idx = i * q;
        if idx < filtered.len() {
            result.push(filtered[idx]);
        }
    }

    Ok(Tensor::from_slice(&result, &[result.len()], device))
}

/// Find peaks in a signal (CPU implementation).
///
/// This is CPU-only because it requires element-wise comparisons and
/// sequential filtering operations.
fn find_peaks_cpu(x: &Tensor<CpuRuntime>, params: PeakParams) -> Result<PeakResult<CpuRuntime>> {
    // CPU-specific: extract data for processing
    let x_data: Vec<f64> = x.to_vec();
    let n = x_data.len();
    let device = x.device();

    if n < 3 {
        // Need at least 3 points to find a peak
        return Ok(PeakResult {
            indices: vec![],
            heights: Tensor::from_slice(&[] as &[f64], &[0], device),
            prominences: None,
        });
    }

    // Find all local maxima
    let mut peaks: Vec<usize> = Vec::new();

    for i in 1..n - 1 {
        if x_data[i] > x_data[i - 1] && x_data[i] > x_data[i + 1] {
            peaks.push(i);
        }
    }

    // Filter by height
    if let Some(min_height) = params.height {
        peaks.retain(|&i| x_data[i] >= min_height);
    }

    // Filter by threshold (difference from neighbors)
    if let Some(threshold) = params.threshold {
        peaks.retain(|&i| {
            let left_diff = x_data[i] - x_data[i - 1];
            let right_diff = x_data[i] - x_data[i + 1];
            left_diff >= threshold && right_diff >= threshold
        });
    }

    // Filter by distance (keep highest peak when peaks are too close)
    if let Some(min_distance) = params.distance
        && min_distance > 0
    {
        peaks = filter_by_distance(&peaks, &x_data, min_distance);
    }

    // Compute prominences if requested
    let prominences = if params.prominence.is_some() {
        let proms = compute_prominences(&peaks, &x_data);

        // Filter by prominence
        if let Some(min_prom) = params.prominence {
            let filtered: Vec<(usize, f64)> = peaks
                .iter()
                .zip(proms.iter())
                .filter(|(_, p)| **p >= min_prom)
                .map(|(i, p)| (*i, *p))
                .collect();

            peaks = filtered.iter().map(|(i, _)| *i).collect();
            let new_proms: Vec<f64> = filtered.iter().map(|(_, p)| *p).collect();
            Some(Tensor::from_slice(&new_proms, &[new_proms.len()], device))
        } else {
            Some(Tensor::from_slice(&proms, &[proms.len()], device))
        }
    } else {
        None
    };

    // Extract heights
    let heights: Vec<f64> = peaks.iter().map(|&i| x_data[i]).collect();

    Ok(PeakResult {
        indices: peaks,
        heights: Tensor::from_slice(&heights, &[heights.len()], device),
        prominences,
    })
}

/// Apply Savitzky-Golay filter (CPU implementation).
///
/// This is CPU-only because it requires convolution with computed coefficients
/// and mirror boundary handling.
fn savgol_filter_cpu(
    x: &Tensor<CpuRuntime>,
    window_length: usize,
    polyorder: usize,
    deriv: usize,
) -> Result<Tensor<CpuRuntime>> {
    // CPU-specific: extract data for processing
    let x_data: Vec<f64> = x.to_vec();
    let n = x_data.len();
    let device = x.device();

    if window_length.is_multiple_of(2) {
        return Err(Error::InvalidArgument {
            arg: "window_length",
            reason: "window_length must be odd".to_string(),
        });
    }

    if window_length < polyorder + 2 {
        return Err(Error::InvalidArgument {
            arg: "window_length",
            reason: "window_length must be greater than polyorder + 1".to_string(),
        });
    }

    if deriv > polyorder {
        return Err(Error::InvalidArgument {
            arg: "deriv",
            reason: "deriv must be <= polyorder".to_string(),
        });
    }

    let half_window = window_length / 2;

    // Compute Savitzky-Golay coefficients
    let coeffs = compute_savgol_coeffs(window_length, polyorder, deriv);

    // Apply filter
    let mut result = Vec::with_capacity(n);

    for i in 0..n {
        let mut sum = 0.0;

        for (j, &coeff) in coeffs.iter().enumerate() {
            let k = i as isize + j as isize - half_window as isize;
            // Mirror boundary conditions
            let idx = if k < 0 {
                (-k) as usize
            } else if k >= n as isize {
                2 * n - 2 - k as usize
            } else {
                k as usize
            };

            if idx < n {
                sum += coeff * x_data[idx];
            }
        }

        result.push(sum);
    }

    Ok(Tensor::from_slice(&result, &[n], device))
}

#[cfg(test)]
mod tests {
    use super::*;
    use numr::runtime::cpu::CpuDevice;
    use std::f64::consts::PI;

    fn setup() -> (CpuClient, CpuDevice) {
        let device = CpuDevice::new();
        let client = CpuClient::new(device.clone());
        (client, device)
    }

    #[test]
    fn test_hilbert_sine() {
        let (client, device) = setup();

        let n = 256;
        let freq = 5.0;
        let signal: Vec<f64> = (0..n)
            .map(|i| (2.0 * PI * freq * i as f64 / n as f64).sin())
            .collect();

        let x = Tensor::from_slice(&signal, &[n], &device);
        let result = client.hilbert(&x).unwrap();

        let env = result.envelope().unwrap();
        let env_data: Vec<f64> = env.to_vec();

        // Ignore edge effects (first and last 10%)
        let start = n / 10;
        let end = n - n / 10;
        for &e in &env_data[start..end] {
            assert!((e - 1.0).abs() < 0.15, "Envelope should be ~1.0, got {}", e);
        }
    }

    #[test]
    fn test_hilbert_envelope() {
        let (client, device) = setup();

        let n = 512;
        let carrier_freq = 50.0;
        let mod_freq = 5.0;

        let signal: Vec<f64> = (0..n)
            .map(|i| {
                let t = i as f64 / n as f64;
                (1.0 + 0.5 * (2.0 * PI * mod_freq * t).sin()) * (2.0 * PI * carrier_freq * t).sin()
            })
            .collect();

        let x = Tensor::from_slice(&signal, &[n], &device);
        let result = client.hilbert(&x).unwrap();
        let env = result.envelope().unwrap();
        let env_data: Vec<f64> = env.to_vec();

        let env_mean: f64 = env_data.iter().sum::<f64>() / n as f64;
        assert!((env_mean - 1.0).abs() < 0.2, "Mean envelope should be ~1.0");
    }

    #[test]
    fn test_resample_upsample() {
        let (client, device) = setup();

        let signal = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let n = signal.len();
        let x = Tensor::from_slice(&signal, &[n], &device);

        let result = client.resample(&x, 2, 1).unwrap();
        let result_data: Vec<f64> = result.to_vec();

        assert_eq!(result_data.len(), n * 2);
    }

    #[test]
    fn test_resample_downsample() {
        let (client, device) = setup();

        let n = 100;
        let signal: Vec<f64> = (0..n).map(|i| (i as f64 * 0.1).sin()).collect();
        let x = Tensor::from_slice(&signal, &[n], &device);

        let result = client.resample(&x, 1, 2).unwrap();
        let result_data: Vec<f64> = result.to_vec();

        assert_eq!(result_data.len(), n.div_ceil(2));
    }

    #[test]
    fn test_decimate() {
        let (client, device) = setup();

        let n = 256;
        let signal: Vec<f64> = (0..n)
            .map(|i| (2.0 * PI * 2.0 * i as f64 / n as f64).sin())
            .collect();
        let x = Tensor::from_slice(&signal, &[n], &device);

        let params = DecimateParams::default();
        let result = client.decimate(&x, 4, params).unwrap();
        let result_data: Vec<f64> = result.to_vec();

        assert_eq!(result_data.len(), n.div_ceil(4));

        let max_val = result_data.iter().fold(0.0_f64, |a, &b| a.max(b.abs()));
        assert!(max_val > 0.1, "Decimated signal should preserve content");
    }

    #[test]
    fn test_find_peaks_simple() {
        let (client, device) = setup();

        let signal = vec![0.0, 1.0, 0.0, 2.0, 0.0, 1.5, 0.0];
        let x = Tensor::from_slice(&signal, &[signal.len()], &device);

        let params = PeakParams::new();
        let result = client.find_peaks(&x, params).unwrap();

        assert_eq!(result.indices, vec![1, 3, 5]);

        let heights: Vec<f64> = result.heights.to_vec();
        assert!((heights[0] - 1.0).abs() < 1e-10);
        assert!((heights[1] - 2.0).abs() < 1e-10);
        assert!((heights[2] - 1.5).abs() < 1e-10);
    }

    #[test]
    fn test_find_peaks_with_height() {
        let (client, device) = setup();

        let signal = vec![0.0, 1.0, 0.0, 2.0, 0.0, 0.5, 0.0];
        let x = Tensor::from_slice(&signal, &[signal.len()], &device);

        let params = PeakParams::new().with_height(1.0);
        let result = client.find_peaks(&x, params).unwrap();

        assert_eq!(result.indices, vec![1, 3]);
    }

    #[test]
    fn test_find_peaks_with_distance() {
        let (client, device) = setup();

        let signal = vec![0.0, 1.0, 0.5, 1.2, 0.0, 0.0, 2.0, 0.0];
        let x = Tensor::from_slice(&signal, &[signal.len()], &device);

        let params = PeakParams::new().with_distance(3);
        let result = client.find_peaks(&x, params).unwrap();

        assert!(result.indices.len() <= 3);
        assert!(result.indices.contains(&6));
    }

    #[test]
    fn test_savgol_smoothing() {
        let (client, device) = setup();

        let n = 101;
        let signal: Vec<f64> = (0..n)
            .map(|i| {
                let t = i as f64 / (n - 1) as f64;
                t * t + 0.1 * ((i * 7) as f64).sin()
            })
            .collect();

        let x = Tensor::from_slice(&signal, &[n], &device);

        let result = client.savgol_filter(&x, 11, 2, 0).unwrap();
        let result_data: Vec<f64> = result.to_vec();

        assert_eq!(result_data.len(), n);

        for (i, &val) in result_data.iter().enumerate().take(80).skip(20) {
            let t = i as f64 / (n - 1) as f64;
            let expected = t * t;
            assert!(
                (val - expected).abs() < 0.15,
                "Smoothed value at {} should be close to {} (got {})",
                i,
                expected,
                val
            );
        }
    }

    #[test]
    fn test_savgol_derivative() {
        let (client, device) = setup();

        let n = 101;
        let signal: Vec<f64> = (0..n)
            .map(|i| {
                let t = (i as f64 - 50.0) / 50.0;
                t * t
            })
            .collect();

        let x = Tensor::from_slice(&signal, &[n], &device);

        let result = client.savgol_filter(&x, 11, 3, 1).unwrap();
        let result_data: Vec<f64> = result.to_vec();

        let scale = 50.0;
        for (i, &val) in result_data.iter().enumerate().take(80).skip(20) {
            let t = (i as f64 - 50.0) / 50.0;
            let expected_deriv = 2.0 * t / scale;
            assert!(
                (val - expected_deriv).abs() < 0.1,
                "Derivative at {} should be ~{} (got {})",
                i,
                expected_deriv,
                val
            );
        }
    }

    #[test]
    fn test_find_peaks_with_prominence() {
        let (client, device) = setup();

        let signal = vec![0.0, 1.0, 0.8, 2.0, 0.5, 0.6, 0.0];
        let x = Tensor::from_slice(&signal, &[signal.len()], &device);

        let params = PeakParams::new().with_prominence(0.5);
        let result = client.find_peaks(&x, params).unwrap();

        assert!(
            result.indices.contains(&3),
            "Should find the prominent peak"
        );

        assert!(result.prominences.is_some());
    }
}