Formula

The Formula function block evaluates a mathematical expression on input signals and produces a calculated output for every sample. Use DewesoftX to enter the expression, assign signals, and configure one or more outputs on the device.

Available settings depend on the device firmware and DewesoftX version. This manual describes the Formula block under the device’s Math module.

1. Open the Formula configuration

Select the device in DewesoftX, then open Config → Math. Select Add math, then choose Formula under Formula and scripting. To edit an existing block, select Setup in its row.

The Math overview shows the shared expression and the outputs below it. Use the Used switch to enable or disable the block.

Device Math module with a Formula block and two calculated outputs

Figure 1. Math overview with Formula math 1 and its two outputs.

2. Enter an expression

Enter the calculation in the Formula editor. Here you can use numeric values, input variables, operators, and functions. For example:

"input 1" * 2

This expression multiplies each sample of the assigned input signal by two.

Put input variable names in single or double quotes. Each unique quoted name creates a column in the Input table. The name identifies an input port; assign the actual signal in that column.

Expression Input columns created
'a' * 2 a
'a' + 'b' a and b
'a' * 'a' a
"input 1" + "input 2" input 1 and input 2

Variable names can contain spaces and special characters. When you change the expression, the block adds or removes input ports to match its variables. Check the signal assignments in every row after changing a variable name.

Use a decimal point for numeric values, such as 2.5. Scientific notation is also supported, for example 1e-3.

3. Assign input signals

In the Input table, select the cell below the required variable name and choose a signal from the selector. Repeat this for every input column in the row. A cell showing still needs a signal.

For "input 1" * 2, assign AI 1 to input 1 in the first row. The output Formula 1 then contains twice the value of AI 1.

For a calculation with two inputs, enter an expression such as:

"input 1" + "input 2"

Assign AI 1 to input 1 and AI 2 to input 2 in the same row. The output contains the sum of the two signals.

Two or more signals connected to the same instance must have exactly the same sampling rate and matching linear domains. Check the input rates before combining signals. See the Formula function block reference.

The formula must reference at least one input signal. This input provides the sampling timing, including for expressions that use only time or a sample counter for their calculation.

4. Apply the formula to several signal groups

Select + above the Input table to add an instance. Each row has its own signal assignments and produces one output. All rows use the expression shown in the Formula editor.

For the expression "input 1" * 2, the following assignments create two independent outputs:

Instance input 1 Calculation
Formula 1 AI 1 AI 1 multiplied by 2
Formula 2 AI 3 AI 3 multiplied by 2

Formula setup with two input rows sharing one expression

Figure 2. The same scaling expression applied to AI 1 and AI 3. Each row produces its own output.

Select a row and use − to remove that instance. Check which row is selected before removing it.

Changing the expression changes the calculation for every instance in the block. To use a different expression for another signal group, add a separate Formula block from the Math overview.

5. Check the output

Use the Output selector to choose the calculated signal to inspect. Here you can change its Name and Description, and view its available metadata and last value.

The tested version shows Units, Min, and Max as read-only fields. Use the enabled fields in your version and check the displayed metadata before using the output in a display or recording.

Select OK to close the editor and return to the Math overview. Expand the Formula block to see its output rows and values.

All Formula output values use Float32. Results that are NaN or infinite are replaced with zero. Division by zero also returns zero. A zero output can therefore come from an invalid mathematical operation as well as from a valid calculation.

6. Use operators and functions

The editor provides Basic operators and tabs under Other math functions. Hover over an operator or function to see its syntax and description. The signal list has a search field for finding available channels.

Formula editor showing the Functions tab

Figure 3. Functions tab with logarithms, powers, minimum, maximum, and other mathematical functions.

Arithmetic and grouping

Expression Calculation
x + y, x - y Addition and subtraction
x * y, x / y Multiplication and division
x % y, mod(x, y) Modulo
x ^ y, pow(x, y) Exponentiation
-x Negation
(x + y) * z Grouping with parentheses

Here, x, y, and z stand for values or expressions. Quote input names when using signals, for example ('a' + 'b') * 2.

Multiplication, division, modulo, and ^ share the same precedence in the RT reference. Addition and subtraction have lower precedence, followed by comparisons, then logical operators. Use parentheses to make the intended calculation explicit, for example 'a' * ('b' ^ 2).

Mathematical functions

Functions Calculation
abs(x) Absolute value
sqr(x), sqrt(x) Square and square root
sgn(x) Returns −1, 0, or 1 according to the sign of x
min(x, y), max(x, y) Minimum and maximum
exp(x) Exponential, e to the power x
ln(x), log2(x), log10(x) Natural, base-2, and base-10 logarithms
sin(x), cos(x), tan(x) Trigonometric functions with angles in radians
trunc(x) Truncates toward zero
round(x) Rounds to the nearest integer
floor(x), ceil(x) Rounds down and up

sqrt(x) returns zero for negative inputs. The logarithm functions return zero for inputs less than or equal to zero. The constants pi and e are available without quotes.

Comparisons and logic

Expression Calculation
x = y, x == y Equality
x <> y, x != y Inequality
x < y, x <= y, x > y, x >= y Comparisons
x and y, x or y, x xor y Logical operations
not x, !x Logical negation
if(z, x, y) Returns x when z is non-zero; otherwise returns y

Logical operations treat zero as false and any non-zero value as true. For example, this expression returns 1 when input a is above 5 and 0 otherwise:

if('a' > 5, 1, 0)

7. Use time, the sample counter, and previous values

Open the Signals tab to see the predefined variables and signal-history function.

Expression Value
time Time in seconds since epoch, taken from the input domain
scnt Sample counter starting at 0 and increasing by 1 per sample
prev Previous output value; 0 for the first sample
'a'.data(0) Current sample of input a
'a'.data(-1) Previous sample of input a
'a'.data(1) Next sample of input a

The argument to .data(N) must be an integer constant. Positive values require future samples and add output latency equal to N samples. For example, .data(10) adds 10 samples of latency.

To generate a 1 Hz sine wave using an input signal’s sampling timing, enter:

'a' * 0 + sin(time * 2 * pi)

Assign a signal to a. Multiplication by zero removes its amplitude contribution while retaining the input needed for sampling timing.

8. Build a calculation in several steps

Use := to assign a value to a user variable and ; to separate expressions. Assign each user variable before using it. The final expression provides the output value.

x := 'a' * 2; x + 1

This calculation doubles input a and adds 1. The user variable x is unquoted; the quoted variable 'a' creates an input port.

Example expressions

Purpose Expression
Apply a gain and offset 'a' * 2.5 + 10
Subtract two signals 'a' - 'b'
Calculate a magnitude from two components sqrt(sqr('a') + sqr('b'))
Limit a value to the range 0 to 100 min(max('a', 0), 100)
Calculate the change from the previous sample 'a' - 'a'.data(-1)
Accumulate the input values prev + 'a'

The last example sums sample values. Include the sample interval if the intended calculation is integration over time.

9. Use and record the results

Open Live view and select the calculated outputs for the required display. Check the values against the expected calculation before using the results in a measurement.

To record the results on the device, open Config → Storing and select the Formula outputs for the required storing instance. Configure the recording mode and destination, then check the device mode and storing status when recording starts.

Adding a Formula block creates calculated signals. Select those outputs in the storing configuration to include them in a recording. See the Storing function block reference for recording behavior.

If the output is missing or unexpected

Check Action
An input is unassigned Assign a signal to every variable in the instance row.
Several signals are combined Check that their sampling rates and linear domains match exactly.
The formula uses only constants or predefined variables Include an input, such as 'a' * 0 + time, and assign its signal.
The expression was changed Review the input columns and assignments in every instance.
The result is zero Check the input values and mathematical domain, including division by zero, square roots, and logarithms.
The result arrives later than expected Check for positive .data(N) offsets.
One output has the wrong source Review the assignments in that instance’s row.
The output is absent from a recording Check its selection in the required storing engine.