secretsauceai/mfcc-rust

fixing dct inside mfcc

Closed this issue · 1 comments

the majority of the errors(according to rust analyzer at least) in feature.rs are mismatched or incorrect types, which are a pretty simple fix. we do have some issue with the wrong arguments for the dct planner we are using, I noticed that ndarrayrust is a wrapper around dct, so we might be able to drop the direct dependency.

Do you mean rustdct?

Regarding the dct itself, this is the original Python code:

feature = dct(feature, type=2, axis=-1, norm='ortho')[:, :num_cepstral]

I've come up with a Rust implementation of that:

    let axis1_len = feature.shape()[1];
    let dct = rustdct::DctPlanner::new().plan_dct2(axis1_len);
    for view in feature.axis_iter_mut(Axis(1)) {
        dct.process_dct2(&mut feature.as_slice_memory_order_mut().unwrap());
    }
    let ortho_factor = (1.0/(2.0 * feature.shape()[1] as f64)).sqrt();
    for dim_val, v in feature.indexed_iter_mut() {
        *v =
            if dim_val.0 == 0 { *v * (1.0/(4.0 * feature.shape()[1] as f64)).sqrt()}
            else {*v * ortho_factor};
    }
    feature = feature.slice(s![.., ..num_cepstral]);

Note that chances are that this won't compile, haven't tested it myself, but should provide a foundation for working code. The rationale here is that we iter over the last axis (here axis 1), this is equivalent to axis=-1. ortho_factor and the loop after that is just for replicating the type='ortho'. In order to get access to the underlying data I put as_slice_memory_order_mut but I could wrong, since this is a view not an array.

Also, this is just an initial implementation, in the future we will want to replace dct.process_dct2 with dct.process_dct2_with_scratchto avoid an allocation (this will mean putting the scratch inside our struct), change the ortho calculation to another more SIMD compatible, like multiply everything with the general case and then override the necessary ones, we would still need to discuss how to preserve the original number or if we can decompose that special case.