Class: Noyes::GolombRiceEncoder
- Inherits:
-
Object
- Object
- Noyes::GolombRiceEncoder
- Defined in:
- lib/ruby_impl/compression.rb
Overview
Takes disassembled floats and compresses them. We want them dissassembled because compressing exponents and signs have you unique properties and can be efficiently compressed with rice coding. The same is not true of the significand.
Instance Method Summary collapse
-
#<<(data) ⇒ Object
data is a list of disassembled floats.
-
#encode(integer_array) ⇒ Object
Rice encoding returned as a BitArray.
-
#initialize(m = 8) ⇒ GolombRiceEncoder
constructor
A new instance of GolombRiceEncoder.
-
#interleave(x) ⇒ Object
Map negative nubers to odd postive numbers and postive numbers to even positive numbers.
Constructor Details
#initialize(m = 8) ⇒ GolombRiceEncoder
Returns a new instance of GolombRiceEncoder.
140 141 142 143 |
# File 'lib/ruby_impl/compression.rb', line 140 def initialize m = 8 @M = m @b = Math.log2(m).to_i end |
Instance Method Details
#<<(data) ⇒ Object
data is a list of disassembled floats
146 147 148 149 150 151 |
# File 'lib/ruby_impl/compression.rb', line 146 def << data data.map do |b,e,s| exp_sign_combo = b |(e << 1) [interleave(exp_sign_combo), s] end end |
#encode(integer_array) ⇒ Object
Rice encoding returned as a BitArray.
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/ruby_impl/compression.rb', line 161 def encode integer_array integer_array = integer_array.clone bits = BitArray.new integer_array.each do |x| q = x/@M q.times {bits.push 1} bits.push 0 r = x % @M (@b-1).downto(0){|i| bits.push r[i]} end bits end |
#interleave(x) ⇒ Object
Map negative nubers to odd postive numbers and postive numbers to even positive numbers. We need to do this because negative numbers don’t compress well with rice encoding.
156 157 158 |
# File 'lib/ruby_impl/compression.rb', line 156 def interleave x x < 0 ? 2 * x.abs - 1 : 2 * x.abs end |