Class: Noyes::GolombRiceEncoder

Inherits:
Object
  • Object
show all
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)

Constructor Details

- (GolombRiceEncoder) initialize(m = 8)

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

- (Object) <<(data)

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

- (Object) encode(integer_array)

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

- (Object) interleave(x)

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