signal_transforms/
lib.rs

1/*!
2# signal-transforms
3
4- Documentation: [https://docs.rs/signal-transforms/](https://docs.rs/signal-transforms/)
5- Source code: [https://github.com/guofei9987/signal-transforms](https://github.com/guofei9987/signal-transforms)
6
7[![Crates.io](https://img.shields.io/crates/v/signal-transforms)](https://crates.io/crates/signal-transforms)
8[![Build Status](https://github.com/guofei9987/signal-transforms/actions/workflows/rust.yml/badge.svg)](https://github.com/guofei9987/signal-transforms/actions)
9[![Docs.rs](https://docs.rs/signal-transforms/badge.svg)](https://docs.rs/signal-transforms)
10[![License](https://img.shields.io/crates/l/signal-transforms)](https://github.com/your-username/your-repo/blob/master/LICENSE)
11[![GitHub stars](https://img.shields.io/github/stars/guofei9987/signal-transforms.svg?style=social&label=Star)](https://github.com/guofei9987/signal-transforms)
12[![Forks](https://img.shields.io/github/forks/guofei9987/signal-transforms.svg?style=social&label=Fork)](https://github.com/guofei9987/signal-transforms/fork)
13![Rust](https://img.shields.io/badge/Rust-1.60+-orange.svg)
14[![Crates.io Downloads](https://img.shields.io/crates/d/signal-transforms)](https://crates.io/crates/signal-transforms)
15[![GitHub Discussions](https://img.shields.io/github/discussions/guofei9987/signal-transforms)](https://github.com/guofei9987/signal-transforms/discussions)
16
17
18**signal-transforms** is a Rust library dedicated to implementing various signal transformation algorithms, including:
19
20- Discrete Cosine Transform (DCT)
21- Inverse Discrete Cosine Transform (IDCT)
22- Two-Dimensional Discrete Cosine Transform (DCT2)
23- Inverse Two-Dimensional Discrete Cosine Transform (IDCT2)
24- Future plans to support more signal processing algorithms
25
26## Installation
27
28Add the following dependency to your `Cargo.toml` file:
29
30```toml
31[dependencies]
32signal-transforms = "0.1.3"
33```
34
35# How To Use
36
37## Discrete Cosine Transform (DCT)
38
39### One-Dimensional DCT
40
41```rust
42fn example_dct_1d() {
43    use nalgebra::DMatrix;
44    use signal_transforms::dct::Dct;
45
46    // Create a new DCT instance with size 4
47    let dct = Dct::new(4);
48
49    // Define a vector of sample data
50    let vec1 = vec![52.0, 55.0, 61.0, 66.0];
51    let vec1 = DMatrix::from_vec(1, 4, vec1);
52
53    // Perform the 1D DCT
54    let dct_res = dct.dct_1d(&vec1);
55
56    println!("DCT result = {}", dct_res);
57
58    // Perform the inverse 1D DCT
59    let idct_res = dct.idct_1d(&dct_res);
60
61    println!("Inverse DCT result = {}", idct_res);
62}
63```
64
65### Two-Dimensional DCT
66
67```rust
68fn example_dct_2d() {
69    use nalgebra::DMatrix;
70    use signal_transforms::dct::Dct2D;
71
72    // Define a 4x4 matrix of sample data
73    let matrix = vec![
74        52.0, 55.0, 61.0, 66.0,
75        70.0, 61.0, 64.0, 73.0,
76        63.0, 59.0, 55.0, 90.0,
77        67.0, 61.0, 68.0, 104.0,
78    ];
79
80    let matrix = DMatrix::from_row_slice(4, 4, &matrix);
81
82    // Create a new 2D DCT instance with dimensions 4x4
83    let dct = Dct2D::new(4, 4);
84
85    // Perform the 2D DCT
86    let dct_res = dct.dct_2d(&matrix);
87
88    println!("DCT result = {}", dct_res);
89
90    // Perform the inverse 2D DCT
91    let idct_res = dct.idct_2d(&dct_res);
92    println!("Inverse DCT result = {}", idct_res);
93}
94```
95
96
97### `Dct4x4` is 20x faster than `Dct2D`
98
99```rust
100#[test]
101fn example_dct_4x4() {
102    use nalgebra::Matrix4;
103    use signal_transforms::dct::Dct4x4;
104    let matrix = Matrix4::new(
105        52.0, 55.0, 61.0, 66.0,
106        70.0, 61.0, 64.0, 73.0,
107        63.0, 59.0, 55.0, 90.0,
108        67.0, 61.0, 68.0, 104.0,
109    );
110
111    let dct = Dct4x4::new();
112
113    let dct_res = dct.dct_2d(&matrix);
114
115    println!("dct result = {}", dct_res);
116
117    let idct_res = dct.idct_2d(&dct_res);
118    println!("idct result = {}", idct_res);
119}
120```
121
122
123## Tests and Benchmark
124
125### Tests
126
127```shell
128cargo test
129```
130
131Or:
132
133```shell
134cargo test --all-features
135```
136
137### Benchmark
138
139```bash
140cargo bench
141```
142
143The result is in `./target/criterion`
144
145
146## Future Enhancements
147
148- Support for additional signal processing algorithms.
149- Optimization for performance and memory usage.
150- Comprehensive documentation and examples for advanced use cases.
151
152## Contributing
153
154Contributions are welcome! Please feel free to submit issues and pull requests.
155
156## License
157
158This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
159
160---
161
162For more information, visit the [official documentation](https://github.com/your-repo/signal-transforms).
163*/
164
165
166
167pub mod dct;
168
169#[cfg(feature = "dct_v0")]
170pub mod dct_v0;
171
172#[cfg(feature = "dct_v0")]
173pub mod dct_v1;