4-10. Formulas

4-10-1. What are formulas in MUSTIG language ?
4-10-2. When to use formulas ?
4-10-3. How to use formulas ?
Pin numbers and labels
Syntax of the formula text
Syntax errors in formulas
Special constants
Calculation operators
Logical operators
Example of formula : thresholding data

 

4-10-1. What are formulas in MUSTIG language ?

Although the usual programming mode in MUSTIG language is building graphs, the Formula macro provides the user with a text interface, allowing to replace a portion of a graph by command lines, in a C or Fortran style.

When compiling the graph, MUSTIG replaces the formula by an equivalent graph, and this graph is compiled the usual way.

Formulas are able to carry out scalar operations only. That is, they perform the same operations on all the input data. Logical operations may be included.

The Formula module is available in the "<->" section of the standard Library. This section contains all the interface tools proposed with MUSTIG.

4-10-2. When to use formulas ?

As a matter of graph, it is sometimes simpler to explicitly write a formula than to build the corresponding graph. For instance, to convert a time base into a sine wave, you may find it more convenient to write :

Y = sin( 2*PI*nu*t + phi )

than to build the following MUSTIG graph :

Thus, algorithms containing lots of successive simple scalar or logical operations may be described more clearly using formulas than using MUSTIG graphs.

4-10-3. How to use formulas ?

Pin numbers and labels

The Formula macro has 8 pins, numbered clockwise from 1 to 8 as shown below :

Slide one in your program window. You may add or delete pins if necessary (see Creating a pin and Deleting a pin). In this case, all the pins are re-numbered clockwise.

You may also label one or several pins to refer to them more easily : click on the pin to select it and hit Alt+L. Click where the label should appear to enter the Edit mode and enter the label. Click anywhere else to exit the Edit mode. You may then move the label. Unlabeled pins are referred to using their number.

The pin number or label is used to refer to the corresponding data. Any pin may be an input or an output pin, MUSTIG checks the validity of the formula script accordingly.

You may rename the Formula module into something more explicit, such as Custom thresholding, or Frequency calculation, etc.

Syntax of the formula text

Double-click on the Formula module : a text window appears. Hit Shift+Ctrl+Click to enter the Edit mode (see Editing a text). You are now ready to enter the text of the formula. Click anywhere outside the formula window to exit.

This formula text may contain several lines, each line being separated from the previous one by a carriage return. Each line must be made of two parts, separated by the "=" sign :

Identifier = Value

The identifier may be :

The Value may be :

 

Examples :

In the examples below, the text variables may be either intermediate variables or labeled input or output pins.

Threshold=$1

Loads the input value(s) on pin 1 into an intermediate variable named Threshold.

Or sends the values on pin 1 onto a pin labeled "Threshold" (which must exist).

Tmp=Threshold

Copies a variable (or labeled pin) into another variable (or labeled pin).

x=2*$2+0.5

Loads variable (or labeled pin) x with 0.5 plus twice the input value(s) of pin 2.

$5=result

Sends the value(s) of variable (or labeled pin) result to the output pin number 5.

Syntax errors in formulas

Syntax error inside formula

Pin not connected

Special constants

Some particular scalar values may be used directly in the formula text :

PI

3.14159…

DPI

Same as above, but in 8-byte real type

J

Elementary (0,1) complex number

Calculation operators

The following calculation operators may be used in the formula text :

FIX(), FLOAT()

Type conversion

-

Unary minus

SIN(), COS(), TAN()

Trigonometric operators

ASIN(), ACOS(), ATAN()

Inverse trigonometric operators

LOG(), LOG10()

Logarithmic operators

EXP(), EXP10()

Exponential operators

+ - * /

Arithmetical operations

Logical operators

The following logical operators may be used in the formula text :

>, <

Comparison : a<b returns 0 or 1.

=

Equality test : a=b returns 0 or 1

.. ? .. : ..

Conditional commutation syntax

Examples :

a>b

Returns an integer equal to 1 if a>b, or equal to 0 if a<=b

a<b

Returns an integer equal to 1 if a<b, or equal to 0 if a>=b

a=b

Returns an integer equal to 1 if a=b, or equal to 0 otherwise

a ? b : c

Returns b if a is not equal to zero, returns c otherwise

Example of formula : thresholding data

The following graph and formula may be used to threshold an input signal :

MUSTIG graph

The thresholding process is carried out by the formula (see formula text on the right). The View2C/t module allows to display both original and thresholded signal at a time :

Text entered in the Formula module

Here, intermediate variables are used to handle the input data (signal and threshold value). The thresholded signal is sent to pin 6.

 

Note : Since only scalar operations can be carried out, here is no need to specify whether the data on a given pin is a scalar, or a vector, or a matrix, etc. The described operations are carried out on every input sample.

 

An even simpler formula is obtained by using pin labels :

MUSTIG graph

Formula text

 

When you ask for the results (right-click), MUSTIG internally builds and compiles an equivalent graph :