#dct #transform #discrete #signal #wavelet #cosine #dct2

signal-transforms

A comprehensive Rust library for discrete and wavelet transforms, including DCT, Dct_2d, DWT2, and more

5 releases

0.1.4 Oct 21, 2024
0.1.3 Oct 21, 2024
0.1.2 Oct 18, 2024
0.1.1 Oct 17, 2024
0.1.0 Oct 17, 2024

#312 in Math

Download history 318/week @ 2024-10-13 311/week @ 2024-10-20 9/week @ 2024-10-27

178 downloads per month

MIT license

20KB
182 lines

signal-transforms

Crates.io Build Status Docs.rs License GitHub stars Forks Rust Crates.io Downloads GitHub Discussions

signal-transforms is a Rust library dedicated to implementing various signal transformation algorithms, including:

  • Discrete Cosine Transform (DCT)
  • Inverse Discrete Cosine Transform (IDCT)
  • Two-Dimensional Discrete Cosine Transform (DCT2)
  • Inverse Two-Dimensional Discrete Cosine Transform (IDCT2)
  • Future plans to support more signal processing algorithms

Installation

Add the following dependency to your Cargo.toml file:

[dependencies]
signal-transforms = "0.1.3"

How To Use

Discrete Cosine Transform (DCT)

One-Dimensional DCT

fn example_dct_1d() {
    use nalgebra::DMatrix;
    use signal_transforms::dct::Dct;

    // Create a new DCT instance with size 4
    let dct = Dct::new(4);

    // Define a vector of sample data
    let vec1 = vec![52.0, 55.0, 61.0, 66.0];
    let vec1 = DMatrix::from_vec(1, 4, vec1);

    // Perform the 1D DCT
    let dct_res = dct.dct_1d(&vec1);

    println!("DCT result = {}", dct_res);

    // Perform the inverse 1D DCT
    let idct_res = dct.idct_1d(&dct_res);

    println!("Inverse DCT result = {}", idct_res);
}

Two-Dimensional DCT

fn example_dct_2d() {
    use nalgebra::DMatrix;
    use signal_transforms::dct::Dct2D;

    // Define a 4x4 matrix of sample data
    let matrix = vec![
        52.0, 55.0, 61.0, 66.0,
        70.0, 61.0, 64.0, 73.0,
        63.0, 59.0, 55.0, 90.0,
        67.0, 61.0, 68.0, 104.0,
    ];

    let matrix = DMatrix::from_row_slice(4, 4, &matrix);

    // Create a new 2D DCT instance with dimensions 4x4
    let dct = Dct2D::new(4, 4);

    // Perform the 2D DCT
    let dct_res = dct.dct_2d(&matrix);

    println!("DCT result = {}", dct_res);

    // Perform the inverse 2D DCT
    let idct_res = dct.idct_2d(&dct_res);
    println!("Inverse DCT result = {}", idct_res);
}

Dct4x4 is 20x faster than Dct2D

#[test]
fn example_dct_4x4() {
    use nalgebra::Matrix4;
    use signal_transforms::dct::Dct4x4;
    let matrix = Matrix4::new(
        52.0, 55.0, 61.0, 66.0,
        70.0, 61.0, 64.0, 73.0,
        63.0, 59.0, 55.0, 90.0,
        67.0, 61.0, 68.0, 104.0,
    );

    let dct = Dct4x4::new();

    let dct_res = dct.dct_2d(&matrix);

    println!("dct result = {}", dct_res);

    let idct_res = dct.idct_2d(&dct_res);
    println!("idct result = {}", idct_res);
}

Future Enhancements

  • Support for additional signal processing algorithms.
  • Optimization for performance and memory usage.
  • Comprehensive documentation and examples for advanced use cases.

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

License

This project is licensed under the MIT License. See the LICENSE file for details.


For more information, visit the official documentation.

Dependencies

~3MB
~57K SLoC