/// Calculates the average normalized hamming distance for the given input bytes
///
/// The average normalized hamming distance is calculated by
///
/// 1. Pick a keysize s
/// 2. Take 2 chunks each of size s
/// 3. Calculate the hamming distance between these 2 chunks
/// 4. Normalize the hamming distance by dividing by s
/// 5. Repeat 1-4 until there are no more chunks left
/// 6. Calculate the mean average of the normalized hamming distances calculated from the above.
///
/// Returns a HashMap that maps keysize to average normalized hamming distance for that keysize.
Calculates the average normalized hamming distance for the given input bytes
The average normalized hamming distance is calculated by

1. Pick a keysize s
2. Take 2 chunks each of size s
3. Calculate the hamming distance between these 2 chunks
4. Normalize the hamming distance by dividing by s
5. Repeat 1-4 until there are no more chunks left
6. Calculate the mean average of the normalized hamming distances calculated from the above.

Returns a HashMap that maps keysize to average normalized hamming distance for that keysize.
Calculates the average normalized hamming distance for the given input bytes The average normalized hamming distance is calculated by 1 . Pick a keysize s 2 . Take 2 chunks each of size s 3 . Calculate the hamming distance between these 2 chunks 4 . Normalize the hamming distance by dividing by s 5 . Repeat 1 - 4 until there are no more chunks left 6 . Calculate the mean average of the normalized hamming distances calculated from the above . Returns a HashMap that maps keysize to average normalized hamming distance for that keysize .
pub fn avg_normalized_hamming_distance(input : &Vec<u8>, max_keysize : usize) -> HashMap<usize, f32> {
    let mut keysize_to_avg_hamming_dist = HashMap::new();
    for keysize in 1..(max_keysize+1) {
        let mut chunks = input.chunks(keysize);
        let mut num_chunks_compared = 0;
        let mut average_hamming_dist = 0.0_f32;
        // Calculate the mean normalized hamming distance over a
        // number of samples to try to improve accuracy.
        for _ in 1..3 {
            let left_chunk = chunks.next();
            let right_chunk = chunks.next();
            if left_chunk.is_none() {
                break;
            }
            if right_chunk.is_none() {
                break;
            }
            let left = left_chunk.unwrap();
            let right = right_chunk.unwrap();
            if left.len() != right.len() {
                break;
            }
            let hamming_dist = distance(left, right);
            let normalized_hamming = hamming_dist as f32 / keysize as f32;
            average_hamming_dist += normalized_hamming;
            debug!("{:4.3} is the normalized hamming distance for keysize {} and block {}", normalized_hamming, keysize, num_chunks_compared);
            num_chunks_compared += 1;
        }
        if num_chunks_compared != 0 {
            average_hamming_dist = average_hamming_dist / num_chunks_compared as f32;
            keysize_to_avg_hamming_dist.insert(keysize, average_hamming_dist);
        } else {
            debug!("Not enough data in input file to check a keysize of '{}'", keysize);
        }
    }
    keysize_to_avg_hamming_dist
}
pub fn avg_normalized_hamming_distance ( input : & Vec < u8 > , max_keysize : usize ) -> HashMap < usize , f32 > { let mut keysize_to_avg_hamming_dist = HashMap :: new ( ) ; for keysize in 1 .. ( max_keysize + 1 ) { let mut chunks = input . chunks ( keysize ) ; let mut num_chunks_compared = 0 ; let mut average_hamming_dist = 0.0_f32 ; // Calculate the mean normalized hamming distance over a // number of samples to try to improve accuracy. for _ in 1 .. 3 { let left_chunk = chunks . next ( ) ; let right_chunk = chunks . next ( ) ; if left_chunk . is_none ( ) { break ; } if right_chunk . is_none ( ) { break ; } let left = left_chunk . unwrap ( ) ; let right = right_chunk . unwrap ( ) ; if left . len ( ) != right . len ( ) { break ; } let hamming_dist = distance ( left , right ) ; let normalized_hamming = hamming_dist as f32 / keysize as f32 ; average_hamming_dist += normalized_hamming ; debug ! ( " " normalized_hamming keysize num_chunks_compared ) ; num_chunks_compared += 1 ; } if num_chunks_compared != 0 { average_hamming_dist = average_hamming_dist / num_chunks_compared as f32 ; keysize_to_avg_hamming_dist . insert ( keysize , average_hamming_dist ) ; } else { debug ! ( " " keysize ) ; } } keysize_to_avg_hamming_dist }
Calculates the average normalized hamming distance for the given input bytes
The average normalized hamming distance is calculated by
Calculates the average normalized hamming distance for the given input bytes The average normalized hamming distance is calculated by
input
&Vec<u8>
max_keysize
usize