Class: Cosmos::SegmentedPolynomialConversion
- Inherits:
-
Conversion
- Object
- Conversion
- Cosmos::SegmentedPolynomialConversion
- Defined in:
- lib/cosmos/conversions/segmented_polynomial_conversion.rb
Overview
Segmented polynomial conversions consist of polynomial conversions that are applied for a range of values.
Defined Under Namespace
Classes: Segment
Instance Attribute Summary collapse
-
#segments ⇒ Array<Segment>
readonly
Segments which make up this conversion.
Attributes inherited from Conversion
#converted_array_size, #converted_bit_size, #converted_type
Instance Method Summary collapse
-
#add_segment(lower_bound, *coeffs) ⇒ Object
Add a segment to the segmented polynomial.
- #as_json ⇒ Object
-
#call(value, packet, buffer) ⇒ Float
The value with the polynomial applied.
-
#initialize(segments = []) ⇒ SegmentedPolynomialConversion
constructor
Initialize the converted_type to :FLOAT and converted_bit_size to 64.
-
#to_config(read_or_write) ⇒ String
Config fragment for this conversion.
-
#to_s ⇒ String
The name of the class followed by a description of all the polynomial segments.
Constructor Details
#initialize(segments = []) ⇒ SegmentedPolynomialConversion
Initialize the converted_type to :FLOAT and converted_bit_size to 64.
91 92 93 94 95 96 97 |
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 91 def initialize(segments = []) super() @segments = [] segments.each { |lower_bound, coeffs| add_segment(lower_bound, *coeffs) } @converted_type = :FLOAT @converted_bit_size = 64 end |
Instance Attribute Details
Instance Method Details
#add_segment(lower_bound, *coeffs) ⇒ Object
Add a segment to the segmented polynomial. The lower bound is inclusive, but is ignored for the segment with the lowest lower_bound.
106 107 108 109 |
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 106 def add_segment(lower_bound, *coeffs) @segments << Segment.new(lower_bound, coeffs) @segments.sort! end |
#as_json ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 161 def as_json params = [] @segments.each do |segment| params << [segment.lower_bound, segment.coeffs] end { 'class' => self.class.name.to_s, 'params' => [params] } end |
#call(value, packet, buffer) ⇒ Float
Returns The value with the polynomial applied.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 113 def call(value, packet, buffer) # Try to find correct segment @segments.each do |segment| return segment.calculate(value) if value >= segment.lower_bound end # Default to using segment with smallest lower_bound segment = @segments[-1] if segment return @segments[-1].calculate(value) else return nil end end |
#to_config(read_or_write) ⇒ String
Returns Config fragment for this conversion.
152 153 154 155 156 157 158 159 |
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 152 def to_config(read_or_write) config = '' @segments.each do |segment| config << " SEG_POLY_#{read_or_write}_CONVERSION #{segment.lower_bound} #{segment.coeffs.join(' ')}\n" end config end |
#to_s ⇒ String
Returns The name of the class followed by a description of all the polynomial segments.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/cosmos/conversions/segmented_polynomial_conversion.rb', line 130 def to_s result = '' count = 0 @segments.each do |segment| result << "\n" if count > 0 result << "Lower Bound: #{segment.lower_bound} Polynomial: " segment.coeffs.length.times do |index| if index == 0 result << "#{segment.coeffs[index]}" elsif index == 1 result << " + #{segment.coeffs[index]}x" else result << " + #{segment.coeffs[index]}x^#{index}" end end count += 1 end result end |