Class: OpenC3::PolynomialConversion
- Defined in:
- lib/openc3/conversions/polynomial_conversion.rb,
ext/openc3/ext/polynomial_conversion/polynomial_conversion.c
Overview
Performs a polynomial conversion on the value
Instance Attribute Summary collapse
-
#coeffs ⇒ Array<Float>
The polynomial coefficients.
Instance Method Summary collapse
-
#call(value, myself, buffer) ⇒ Object
Calling this method performs a polynomial conversion on the given value.
-
#initialize(*coeffs) ⇒ PolynomialConversion
constructor
Initializes the conversion with the given polynomial coefficients.
-
#to_config(read_or_write) ⇒ String
Config fragment for this conversion.
-
#to_s ⇒ String
Class followed by the list of coefficients.
Constructor Details
#initialize(*coeffs) ⇒ PolynomialConversion
Initializes the conversion with the given polynomial coefficients. Sets the converted_type to :FLOAT and the converted_bit_size to 64.
31 32 33 34 35 36 37 |
# File 'lib/openc3/conversions/polynomial_conversion.rb', line 31 def initialize(*coeffs) super() @coeffs = coeffs.map { |coeff| coeff.to_f } @converted_type = :FLOAT @converted_bit_size = 64 @params = @coeffs end |
Instance Attribute Details
Instance Method Details
#call(value, myself, buffer) ⇒ Object
Calling this method performs a polynomial conversion on the given value.
conversion.call(1, packet) #=> 2.5
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/openc3/conversions/polynomial_conversion.rb', line 42 def call(value, myself, buffer) # Return nil if value is nil (item outside buffer bounds) return nil if value.nil? value = value.to_f # Handle C0 result = @coeffs[0] # Handle Coefficients raised to a power raised_to_power = 1.0 @coeffs[1..-1].each do |coeff| raised_to_power *= value result += (coeff * raised_to_power) end return result end |
#to_config(read_or_write) ⇒ String
Returns Config fragment for this conversion.
79 80 81 |
# File 'lib/openc3/conversions/polynomial_conversion.rb', line 79 def to_config(read_or_write) " POLY_#{read_or_write}_CONVERSION #{@coeffs.join(' ')}\n" end |
#to_s ⇒ String
Returns Class followed by the list of coefficients.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/openc3/conversions/polynomial_conversion.rb', line 63 def to_s result = "" @coeffs.length.times do |index| if index == 0 result << "#{@coeffs[index]}" elsif index == 1 result << " + #{@coeffs[index]}x" else result << " + #{@coeffs[index]}x^#{index}" end end result end |