Created
May 4, 2013 20:19
-
-
Save dymk/5518612 to your computer and use it in GitHub Desktop.
Haar 1D and Haar 2D implementations for Rust.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use core::vec; | |
| pub fn haar2d<T : NumCast>(in_mat: ~[~[T]]) -> ~[~[float]] { | |
| let in_mat_rows = in_mat.len(); | |
| // Initialize a vector n items long with the value 0 | |
| //Perform on each row | |
| let mut in_mat = do vec::map_consume(in_mat) |vec| { | |
| haar1d(vec) | |
| }; | |
| //And now on each column | |
| for uint::range(0, in_mat_rows) |y| { | |
| let mut temp_row = vec::with_capacity(in_mat_rows); | |
| for uint::range(0, in_mat_rows) |x| { | |
| temp_row.push(in_mat[x][y]); | |
| } | |
| let temp_row = haar1d(temp_row); | |
| for uint::range(0, in_mat_rows) |x| { | |
| in_mat[x][y] = temp_row[x]; | |
| } | |
| } | |
| in_mat | |
| } | |
| /* | |
| * Based on the pseudocode found at http://www.cs.ucf.edu/~mali/haar/ | |
| */ | |
| pub fn haar1d<T : NumCast>(mut in_vec: ~[T]) -> ~[float] { | |
| //map to float types | |
| let mut in_vec = do in_vec.map |&i| { i.to_float() }; | |
| let in_v_len = in_vec.len(); | |
| let sqrt2 = float::consts::sqrt2; | |
| // Initialize a vector n items long with the value 0 | |
| let mut out_vec: ~[float] = vec::from_elem(in_v_len, 0f); | |
| //How far we itterate over the output vector each time | |
| let mut out_vec_bound = in_v_len; | |
| while out_vec_bound > 1 { | |
| out_vec_bound /= 2; | |
| let ovb = out_vec_bound; | |
| for uint::range(0, ovb) |i| { | |
| let i2 = i*2; | |
| out_vec[i] =(in_vec[i2] + in_vec[i2+1]) / sqrt2; | |
| out_vec[i+ovb]=(in_vec[i2] - in_vec[i2+1]) / sqrt2; | |
| } | |
| if out_vec_bound > 1 { | |
| // Going to itterate over again; copy | |
| // relevant changes into input vector | |
| for uint::range(0, out_vec_bound*2) |i| { | |
| in_vec[i] = out_vec[i]; | |
| } | |
| } | |
| } | |
| out_vec | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment