Class: Digiproc::Strategies::XORDifferentialEncodingStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/strategies/code/xor_differential_encoding_strategy.rb

Overview

Class used as a strategy for differentially encoding a bitstream by XORing it with a time-delayed version of itself: Differentially encode the binary stream using the XOR operator, and then map the binary stream using the equation βn = (2l−1)π %2π, l⩽M, which gives you a mapping of 0=> −π ,1=> π The Differentially Encoded Signal => Cn = Dn ⨁ Cn−1 is the signal mapped to phase as described above. The XOR operation in seeded with a 1

Class Method Summary collapse

Class Method Details

.decode(bits) ⇒ Object

Input an array of encoded bits (as strings) Output an array of decoded bits



74
75
76
77
78
79
80
# File 'lib/strategies/code/xor_differential_encoding_strategy.rb', line 74

def self.decode(bits)
    encoded = []  
    for i in 1...bits.length do 
        encoded << (bits[i - 1].to_i.to_s.to_i(2) ^ bits[i].to_i.to_s.to_i(2)).to_s
    end
    encoded
end

.decode_bits(bits) ⇒ Object

Input is an integer The method calls the ‘decode_str` method below and inputs a string of the binary of the input integer. The output will be the original bitstream encoded by this encoding strategy in string form



53
54
55
# File 'lib/strategies/code/xor_differential_encoding_strategy.rb', line 53

def self.decode_bits(bits)
    dencode_str(bits.to_s(2))
end

.decode_str(bits) ⇒ Object

Input an encoded binary bit stream in string form Output a string of the original bitstream



62
63
64
65
66
67
68
69
# File 'lib/strategies/code/xor_differential_encoding_strategy.rb', line 62

def self.decode_str(bits)
    bits_arr = bits.split("")
    encoded = []  
    for i in 1...bits_arr.length do 
        encoded << (bits_arr[i - 1].to_i(2) ^ bits_arr[i].to_i(2)).to_s
    end
    encoded.join
end

.encode(arr, m = 2, beginning_val = "1") ⇒ Object

Encoding an incoming array of bits (as strings) into an array of XOR’d bits Requires an input of an array, and has optional arguments of m (number of bits per symbol) And a beginning value (a starting reference phase angle). Outputs an array of XOR’d bits



38
39
40
41
42
43
44
45
# File 'lib/strategies/code/xor_differential_encoding_strategy.rb', line 38

def self.encode(arr, m = 2, beginning_val = "1")
    beginning_val = beginning_val.to_s(2) unless beginning_val.is_a? String
    encoded = [beginning_val]
    for i in 0...arr.length do 
        encoded << (encoded.last.to_i(2) ^ arr[i].to_i(2)).to_s
    end
    encoded
end

.encode_bits(bits) ⇒ Object

Accept an int reprisenting a bit stream. Encode the bits by XORing it with a time delay of itself via the ‘self.encode_str` method below. The first bit is seeded with a “1”



17
18
19
# File 'lib/strategies/code/xor_differential_encoding_strategy.rb', line 17

def self.encode_bits(bits)
    encode_str(bits.to_s(2))
end

.encode_str(bits) ⇒ Object

Input a string of bits Outupt the XOR’d version of the bits, seeded with a beginning “1”



24
25
26
27
28
29
30
31
# File 'lib/strategies/code/xor_differential_encoding_strategy.rb', line 24

def self.encode_str(bits)
    bits_arr = bits.split("")
    encoded = ["1"]  
    for i in 0...bits_arr.length do 
        encoded << (encoded.last.to_i(2) ^ bits_arr[i].to_i(2)).to_s
    end
    encoded.join
end

.phase_shift_eqn(m) ⇒ Object

Return a lambda which transforms a bit into a phase Input an integer specifying the number of bits in a symbol Input to the returned lambda should be the symbol value in decimal form Ouptut of the lambda is the encoded phase angle



87
88
89
# File 'lib/strategies/code/xor_differential_encoding_strategy.rb', line 87

def self.phase_shift_eqn(m)
    ->(l){ (((2.0 * (l+1) - 1.0) / m) % 2) * Math::PI }
end

.phase_to_sym(m) ⇒ Object

Return a lambda which transforms a pahse into a symbol Input an integer specifying the number of bits in a symbol Input to the returned lambda is the encoded phase angle Output of the lambda is the binary symbol



96
97
98
# File 'lib/strategies/code/xor_differential_encoding_strategy.rb', line 96

def self.phase_to_sym(m)
    ->(code){ (((code / (Math::PI)) * m) + 1.0) / 2.0 - 1.0 }
end