User Tools

Site Tools


curves

This is an old revision of the document!


Curves

Curve is a way to define output value from a pre-defined pair table of a discrete set of known data points. Such as standard or desired value.

It can be represented as a black-box, where is some INPUT x have corresponding OUTPUT y = f(x)

Curve is using a piecewise linear interpolation, is a curve fitting which is a allow to using linear polynomials to construct new data points within the range of a discrete set of known data points.

Curves can :

  • be edited at any time.
  • have a static register for storing it.
  • be read from string register.
  • be copied to another string register using dashboard element.

To create a curve you need to define Y and X value for known points using visual editor or digital input form.

There are several options for using curve:

  • The dependence of Y on X (function) is given by the table of pairs.
  • The function should be empirical defined.
  • There is strict algebraic function and value should be calculated.
  • The function should be sequentually or time-to-time changed.

To define curve algebraically, it is linear interpolation between two known points for every piece used. Its string reprentation is a JSON formatted linear polynomial factor “k”, syllable “b” and function definition domain “range” values for every piece of a curve, so that inside the range of INPUT value OUTPUT value can be calculated as
y = k × x + b

For example:
At minimum it can be two points.

Binary logic function.

Table parameter definition

Saturation function

Algebraic defined calculated function. For example sin(x) with 6 points approximation.

Let's create a new curve.
Click on “New curve” button.

Type in a Title and go on the next tab Curve

Here is a curve editor. There is a domain of a function definition, range of a function and every point X and Y coordinates.

To create a new point click twice on a line. To remove it click with holding Alt button.

Then you can both move point or specify its value using form in the right.

At the end, when all of parameters set it can be a 7 point defined sin(x):

To get the OUTPUT y value from existing(pre-created) curve, you need:

  1. a Lua script to work with string register and
  2. include a Lua lib, which will provide access to that function.
  3. create a register for dashboard element.
  4. create a dashboard, with “curve” element, where you can select which curve will be stored in the bound register.
  5. Call a GetCurveValue function and pass to it this register and INPUT x.
- curves.lib
function GetCurveValue ( curve_register, x_to_find_y  )
    --[[   
    --Curve handler 
    INPUT:
        Put curve register as first argument and X coordinate as second.
    OUTPUT:
        Get as result status if it is inside curve range of outside(false) and the Y value as second output argument
 
    EXAMPLE:
        curve_status, value = GetCurveValue( "curve_for_current_hour", current_hour )
    --]]
    table = cjson.decode( R ( curve_register ) )
    -- inRangeCurve( table, x_to_find_y )
    for _, value in ipairs( table ) do --piecewise handler inside range of the curve
        if ( value.range[1] <= x_to_find_y and x_to_find_y < value.range[2] ) then 
            -- in_range_status, index_curve_piece, y = true, index, curveLinearCalc( x_to_find_y, value.k, value.b )
            in_range_status, y = true, ( x_to_find_y * value.k + value.b )
            return in_range_status, y
        end
    end
    if  x_to_find_y < table[1].range[1] then -- behaivior for outside left-sided
        -- in_range_status, index_curve_piece, y = false, 1, curveLinearCalc( table[1].range[1], table[1].k*0, table[1].b ) --table[1].b
        in_range_status, index_curve_piece, y = false, 1, ( table[1].range[1] * table[1].k + table[1].b ) --table[1].b
        return in_range_status, y
    elseif x_to_find_y > table[#table].range[2] then -- behaivior for outside right-sided
        -- in_range_status, index_curve_piece, y = false,#table,curveLinearCalc( table[#table].range[2], table[#table].k, table[#table].b ) --table[#table].b
        in_range_status,  y = false,#table,( table[#table].range[2] * table[#table].k + table[#table].b)
        return in_range_status, y
    end
 
    return in_range_status, y 
end

Example of usage:

- Getter
include "curves.lib"
 
function main (userId)
 
    local t = os.date("*t", os.time()) 
    current_hour = tonumber(t.hour)
    curve_status, y = GetCurveValue( "curve_for_current_hour", current_hour )
    WriteReg("value_for_current_hour", y) 
 
end

Of course, you can fork a your own lib or use the given one.

curves.1596122157.txt.gz · Last modified: 2020/07/30 15:15 by atolstov

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki