ndwt 0.1.1

High-performance discrete and lifting wavelet transforms for 1-D and N-D signals, with SIMD acceleration, adjoint operations, and 8 boundary conditions.
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
use ndwt::Wavelet;
use ndwt::boundarys::BoundaryCondition;
use ndwt::iter::LanesIterator;
use ndwt::lwt::driver::WaveletTransform;
use num_complex::{Complex32, Complex64};
use rstest::rstest;

// ── Single-level forward: compare against hand-rolled 1-D transforms ───────────

/// Transform along axis 1 of a 2-D array and verify each row matches a
/// direct `forward_1d` call.
#[test]
pub fn test_lwt_driver_db2_single_level_1d_along() {
    let shape = [30, 35];
    let axes = [1];
    let wvlt = Wavelet::Daubechies2;
    let bc = BoundaryCondition::Periodic;
    let trans = WaveletTransform::new(wvlt, bc);

    let n_ax = shape[axes[0]]; // 35
    let ns = (n_ax + 1) / 2; // 18
    let nd = n_ax / 2; // 17

    let n_total = shape.iter().product();
    let x: Vec<f64> = (0..n_total).map(|i| i as f64).collect();

    let mut sd = vec![0.0f64; n_total];
    trans.forward_nd(&x, &mut sd, &shape, &axes);

    // Hand-roll: apply forward_1d to each row.
    let mut sd2 = vec![0.0f64; n_total];
    x.chunks_exact(shape[1])
        .zip(sd2.chunks_exact_mut(shape[1]))
        .for_each(|(row, out_row)| {
            let (s_out, d_out) = out_row.split_at_mut(ns);
            trans.forward_1d(row, s_out, d_out);
        });

    assert_eq!(ns + nd, shape[1]);
    ndwt::tests::test_approx_equal(&sd, &sd2, 1e-12, 1e-11);
}

/// Transform along axis 0 of a 2-D array and verify each column matches a
/// direct `forward_1d` call.
#[test]
pub fn test_lwt_driver_db2_single_level_1d_across() {
    let shape = [30, 35];
    let axes = [0];
    let wvlt = Wavelet::Daubechies2;
    let bc = BoundaryCondition::Periodic;
    let trans = WaveletTransform::new(wvlt, bc);

    let n_ax = shape[axes[0]]; // 30
    let ns = (n_ax + 1) / 2; // 15
    let nd = n_ax / 2; // 15

    let n_total = shape.iter().product();
    let x: Vec<f64> = (0..n_total).map(|i| i as f64).collect();

    let mut sd = vec![0.0f64; n_total];
    trans.forward_nd(&x, &mut sd, &shape, &axes);

    // Hand-roll: extract each column (strided), apply forward_1d, write back.
    let mut sd2 = vec![0.0f64; n_total];
    let mut x_w = vec![0.0f64; n_ax];
    let mut s_w = vec![0.0f64; ns];
    let mut d_w = vec![0.0f64; nd];
    x.iter_lanes(&shape, axes[0])
        .zip(sd2.iter_lanes_mut(&shape, axes[0]))
        .for_each(|(x_lane, mut sd_lane)| {
            x_lane.pour_into(&mut x_w);
            trans.forward_1d(&x_w, &mut s_w, &mut d_w);
            sd_lane.stack(&s_w, &d_w);
        });

    ndwt::tests::test_approx_equal(&sd, &sd2, 1e-12, 1e-11);
}

/// Two-axis single-level transform equals two sequential single-axis transforms.
#[test]
pub fn test_lwt_driver_db2_single_2d() {
    let shape = [30, 35];
    let axes = [0, 1];
    let wvlt = Wavelet::Daubechies2;
    let bc = BoundaryCondition::Periodic;
    let trans = WaveletTransform::new(wvlt, bc);

    let n_total = shape.iter().product();
    let x: Vec<f64> = (0..n_total).map(|i| (i * i) as f64 / 3.0).collect();

    let mut sd = vec![0.0f64; n_total];
    trans.forward_nd(&x, &mut sd, &shape, &axes);

    // Sequential: axis 0 then axis 1.
    let mut inter = vec![0.0f64; n_total];
    let mut sd2 = vec![0.0f64; n_total];
    trans.forward_nd(&x, &mut inter, &shape, &[axes[0]]);
    trans.forward_nd(&inter, &mut sd2, &shape, &[axes[1]]);

    ndwt::tests::test_approx_equal(&sd, &sd2, 1e-12, 1e-10);
}

/// Three-level forward matches three successive single-level calls.
#[test]
pub fn test_lwt_driver_db2_multi_level_1d() {
    let shape = [30, 35];
    let axes = [1];
    let level = 3;
    let wvlt = Wavelet::Daubechies2;
    let bc = BoundaryCondition::Periodic;
    let trans = WaveletTransform::new(wvlt, bc);

    let n_ax = shape[axes[0]]; // 35
    let ns1 = (n_ax + 1) / 2; // 18
    let nd1 = n_ax / 2; // 17
    let ns2 = (ns1 + 1) / 2; // 9
    let nd2 = ns1 / 2; // 9
    let ns3 = (ns2 + 1) / 2; // 5
    let nd3 = ns2 / 2; // 4

    let n_total = shape.iter().product();
    let x: Vec<f64> = (0..n_total).map(|i| i as f64).collect();

    let mut sd = vec![0.0f64; n_total];
    trans.forward_multilevel_nd(&x, &mut sd, &shape, &axes, level);

    // Hand-roll three nested forward_1d calls per row, then assemble the
    // packed layout [s3 | d3 | d2 | d1].
    let mut sd2 = vec![0.0f64; n_total];
    x.chunks_exact(shape[1])
        .zip(sd2.chunks_exact_mut(shape[1]))
        .for_each(|(row, out)| {
            let mut s1 = vec![0.0f64; ns1];
            let mut d1 = vec![0.0f64; nd1];
            trans.forward_1d(row, &mut s1, &mut d1);

            let mut s2 = vec![0.0f64; ns2];
            let mut d2 = vec![0.0f64; nd2];
            trans.forward_1d(&s1, &mut s2, &mut d2);

            let mut s3 = vec![0.0f64; ns3];
            let mut d3 = vec![0.0f64; nd3];
            trans.forward_1d(&s2, &mut s3, &mut d3);

            // Packed layout: [s3 | d3 | d2 | d1]
            out[..ns3].copy_from_slice(&s3);
            out[ns3..ns3 + nd3].copy_from_slice(&d3);
            out[ns3 + nd3..ns3 + nd3 + nd2].copy_from_slice(&d2);
            out[ns3 + nd3 + nd2..].copy_from_slice(&d1);
        });

    ndwt::tests::test_approx_equal(&sd, &sd2, 1e-12, 1e-11);
}

// ── Inverse round-trip tests ───────────────────────────────────────────────────

#[test]
pub fn test_lwt_driver_inv_db2_single_level_1d_along() {
    let shape = [30, 35];
    let axes = [1];
    let wvlt = Wavelet::Daubechies2;
    let bc = BoundaryCondition::Periodic;
    let trans = WaveletTransform::new(wvlt, bc);

    let n_total = shape.iter().product();
    let x: Vec<f64> = (0..n_total).map(|i| (i + 1) as f64).collect();

    let mut sd = vec![0.0f64; n_total];
    trans.forward_nd(&x, &mut sd, &shape, &axes);

    let mut x2 = vec![0.0f64; n_total];
    trans.inverse_nd(&sd, &mut x2, &shape, &axes);

    ndwt::tests::test_approx_equal(&x2, &x, 1e-12, 1e-11);
}

#[test]
pub fn test_lwt_driver_inv_db2_single_level_1d_across() {
    let shape = [30, 35];
    let axes = [0];
    let wvlt = Wavelet::Daubechies2;
    let bc = BoundaryCondition::Periodic;
    let trans = WaveletTransform::new(wvlt, bc);

    let n_total = shape.iter().product();
    let x: Vec<f64> = (0..n_total).map(|i| (i + 1) as f64).collect();

    let mut sd = vec![0.0f64; n_total];
    trans.forward_nd(&x, &mut sd, &shape, &axes);

    let mut x2 = vec![0.0f64; n_total];
    trans.inverse_nd(&sd, &mut x2, &shape, &axes);

    ndwt::tests::test_approx_equal(&x2, &x, 1e-12, 1e-11);
}

#[test]
pub fn test_lwt_driver_inv_db2_single_level_2d() {
    let shape = [30, 35];
    let axes = [0, 1];
    let wvlt = Wavelet::Daubechies2;
    let bc = BoundaryCondition::Periodic;
    let trans = WaveletTransform::new(wvlt, bc);

    let n_total = shape.iter().product();
    let x: Vec<f64> = (0..n_total).map(|i| (i + 1) as f64).collect();

    let mut sd = vec![0.0f64; n_total];
    trans.forward_nd(&x, &mut sd, &shape, &axes);

    let mut x2 = vec![0.0f64; n_total];
    trans.inverse_nd(&sd, &mut x2, &shape, &axes);

    ndwt::tests::test_approx_equal(&x2, &x, 1e-12, 1e-11);
}

#[test]
pub fn test_lwt_driver_inv_db2_multi_level_1d() {
    let shape = [30, 35];
    let axes = [1];
    let level = 3;
    let wvlt = Wavelet::Daubechies2;
    let bc = BoundaryCondition::Periodic;
    let trans = WaveletTransform::new(wvlt, bc);

    let n_total = shape.iter().product();
    let x: Vec<f64> = (0..n_total).map(|i| (i + 1) as f64).collect();

    let mut sd = vec![0.0f64; n_total];
    trans.forward_multilevel_nd(&x, &mut sd, &shape, &axes, level);

    let mut x2 = vec![0.0f64; n_total];
    trans.inverse_multilevel_nd(&sd, &mut x2, &shape, &axes, level);

    ndwt::tests::test_approx_equal(&x2, &x, 1e-12, 1e-11);
}

#[test]
pub fn test_lwt_driver_inv_db2_multi_level_2d() {
    let shape = [30, 35];
    let axes = [0, 1];
    let level = 3;
    let wvlt = Wavelet::Daubechies2;
    let bc = BoundaryCondition::Periodic;
    let trans = WaveletTransform::new(wvlt, bc);

    let n_total = shape.iter().product();
    let x: Vec<f64> = (0..n_total).map(|i| (i + 1) as f64).collect();

    let mut sd = vec![0.0f64; n_total];
    trans.forward_multilevel_nd(&x, &mut sd, &shape, &axes, level);

    let mut x2 = vec![0.0f64; n_total];
    trans.inverse_multilevel_nd(&sd, &mut x2, &shape, &axes, level);

    ndwt::tests::test_approx_equal(&x2, &x, 1e-12, 1e-11);
}

// ── Zero boundary condition round-trips ───────────────────────────────────────

#[test]
pub fn test_lwt_driver_inv_db2_zero_bc_multi_level_1d() {
    use ndwt::boundarys::ZeroBoundary;

    let shape = [30, 35];
    let axes = [1];
    let level = 3;
    let wvlt = Wavelet::Daubechies2;
    let trans = WaveletTransform::new(wvlt, ZeroBoundary {});

    let n_total = shape.iter().product();
    let x: Vec<f64> = (0..n_total).map(|i| (i + 1) as f64).collect();

    let mut sd = vec![0.0f64; n_total];
    trans.forward_multilevel_nd(&x, &mut sd, &shape, &axes, level);

    let mut x2 = vec![0.0f64; n_total];
    trans.inverse_multilevel_nd(&sd, &mut x2, &shape, &axes, level);

    ndwt::tests::test_approx_equal(&x2, &x, 1e-12, 1e-11);
}

// ── Parametrised wavelet-family tests ─────────────────────────────────────────

/// Forward + inverse round-trip for a variety of wavelet families and levels.
///
/// Uses periodic boundary so that all wavelets have an exact inverse regardless
/// of signal length.
#[rstest]
#[case(Wavelet::Daubechies2, 1)]
#[case(Wavelet::Daubechies4, 1)]
#[case(Wavelet::Daubechies4, 3)]
#[case(Wavelet::Symlet4, 1)]
#[case(Wavelet::Symlet4, 3)]
#[case(Wavelet::Coiflet2, 1)]
#[case(Wavelet::Coiflet2, 3)]
#[case(Wavelet::Bior2_2, 1)]
#[case(Wavelet::Bior2_2, 3)]
#[case(Wavelet::CDF9_7, 1)]
#[case(Wavelet::CDF9_7, 3)]
pub fn test_round_trip_wavelet_families(#[case] wvlt: Wavelet, #[case] level: usize) {
    let shape = [30, 35];
    let axes = [1];
    let bc = BoundaryCondition::Periodic;
    let trans = WaveletTransform::new(wvlt, bc);

    let n_total = shape.iter().product();
    let x: Vec<f64> = (0..n_total).map(|i| (i as f64 + 1.0) * 0.37).collect();

    let mut sd = vec![0.0f64; n_total];
    trans.forward_multilevel_nd(&x, &mut sd, &shape, &axes, level);

    let mut x2 = vec![0.0f64; n_total];
    trans.inverse_multilevel_nd(&sd, &mut x2, &shape, &axes, level);

    ndwt::tests::test_approx_equal(&x2, &x, 1e-12, 1e-10);
}

/// Adjoint property: `<v, T(u)> = <T*(v), u>` where `T` is the forward LWT
/// and `T*` is `adj_forward`.
///
/// Both u and v live in the same space (LWT is shape-preserving).
#[rstest]
#[case(Wavelet::Daubechies2, 1)]
#[case(Wavelet::Daubechies4, 1)]
#[case(Wavelet::Daubechies4, 3)]
#[case(Wavelet::Symlet4, 1)]
#[case(Wavelet::Coiflet2, 1)]
#[case(Wavelet::Bior2_2, 1)]
#[case(Wavelet::CDF9_7, 1)]
pub fn test_adj_forward_wavelet_families(#[case] wvlt: Wavelet, #[case] level: usize) {
    let n: usize = 64;
    let shape = [n];
    let axes = [0];
    let bc = BoundaryCondition::Periodic;
    let trans = WaveletTransform::new(wvlt, bc);

    let u: Vec<f64> = (0..n).map(|i| (i as f64 * 0.31 + 1.0).sin()).collect();
    let v: Vec<f64> = (0..n).map(|i| (i as f64 * 0.17 + 0.5).cos()).collect();

    ndwt::tests::test_approx_adjoint(
        |u, fu| trans.forward_multilevel_nd(u, fu, &shape, &axes, level),
        |v, atv| trans.adj_forward_multilevel_nd(v, atv, &shape, &axes, level),
        &u,
        &v,
        1e-12,
        1e-10,
    );
}

/// Adjoint property: `<v, T^{-1}(u)> = <(T^{-1})*(v), u>` where `T^{-1}` is
/// the inverse LWT and `(T^{-1})*` is `adj_inverse`.
#[rstest]
#[case(Wavelet::Daubechies2, 1)]
#[case(Wavelet::Daubechies4, 1)]
#[case(Wavelet::Daubechies4, 3)]
#[case(Wavelet::Symlet4, 1)]
#[case(Wavelet::Coiflet2, 1)]
#[case(Wavelet::Bior2_2, 1)]
#[case(Wavelet::CDF9_7, 1)]
pub fn test_adj_inverse_wavelet_families(#[case] wvlt: Wavelet, #[case] level: usize) {
    let n: usize = 64;
    let shape = [n];
    let axes = [0];
    let bc = BoundaryCondition::Periodic;
    let trans = WaveletTransform::new(wvlt, bc);

    let u: Vec<f64> = (0..n).map(|i| (i as f64 * 0.31 + 1.0).sin()).collect();
    let v: Vec<f64> = (0..n).map(|i| (i as f64 * 0.17 + 0.5).cos()).collect();

    ndwt::tests::test_approx_adjoint(
        |u, fu| trans.inverse_multilevel_nd(u, fu, &shape, &axes, level),
        |v, atv| trans.adj_inverse_multilevel_nd(v, atv, &shape, &axes, level),
        &u,
        &v,
        1e-12,
        1e-10,
    );
}

// ── Complex element-type round-trips ──────────────────────────────────────────
// LWT uses SimdTransformable which handles complex types by treating f32*C32
// and f64*C64 — the scalar multiplier is broadcast via f32s/f64s SIMD vectors,
// and the complex data rides in c32s/c64s SIMD vectors of the same byte width.

/// Round-trip test using `Complex32` element type.
#[test]
pub fn test_round_trip_complex32() {
    let wvlt = Wavelet::Daubechies4;
    let shape = [32];
    let axes = [0];
    let level = 2;
    let bc = BoundaryCondition::Periodic;

    let n = shape.iter().product::<usize>();
    let x: Vec<Complex32> = (0..n)
        .map(|i| Complex32::new(i as f32 * 0.5, -(i as f32) * 0.3))
        .collect();
    let mut sd = vec![Complex32::new(0.0, 0.0); n];

    // Let the compiler infer T=Complex32 and N=N_C32 from usage.
    let trans = WaveletTransform::new(wvlt, bc);
    trans.forward_multilevel_nd(&x, &mut sd, &shape, &axes, level);

    let mut x2 = vec![Complex32::new(0.0, 0.0); n];
    trans.inverse_multilevel_nd(&sd, &mut x2, &shape, &axes, level);

    let x_re: Vec<f32> = x.iter().map(|c| c.re).collect();
    let x2_re: Vec<f32> = x2.iter().map(|c| c.re).collect();
    let x_im: Vec<f32> = x.iter().map(|c| c.im).collect();
    let x2_im: Vec<f32> = x2.iter().map(|c| c.im).collect();
    ndwt::tests::test_approx_equal(&x2_re, &x_re, 1e-5, 1e-4);
    ndwt::tests::test_approx_equal(&x2_im, &x_im, 1e-5, 1e-4);
}

/// Round-trip test using `Complex64` element type.
#[test]
pub fn test_round_trip_complex64() {
    let wvlt = Wavelet::Daubechies4;
    let shape = [32];
    let axes = [0];
    let level = 2;
    let bc = BoundaryCondition::Periodic;

    let n = shape.iter().product::<usize>();
    let x: Vec<Complex64> = (0..n)
        .map(|i| Complex64::new(i as f64 * 0.5, -(i as f64) * 0.3))
        .collect();
    let mut sd = vec![Complex64::new(0.0, 0.0); n];

    // Let the compiler infer T=Complex64 and N=N_C64 from usage.
    let trans = WaveletTransform::new(wvlt, bc);
    trans.forward_multilevel_nd(&x, &mut sd, &shape, &axes, level);

    let mut x2 = vec![Complex64::new(0.0, 0.0); n];
    trans.inverse_multilevel_nd(&sd, &mut x2, &shape, &axes, level);

    let x_re: Vec<f64> = x.iter().map(|c| c.re).collect();
    let x2_re: Vec<f64> = x2.iter().map(|c| c.re).collect();
    let x_im: Vec<f64> = x.iter().map(|c| c.im).collect();
    let x2_im: Vec<f64> = x2.iter().map(|c| c.im).collect();
    ndwt::tests::test_approx_equal(&x2_re, &x_re, 1e-12, 1e-10);
    ndwt::tests::test_approx_equal(&x2_im, &x_im, 1e-12, 1e-10);
}