Programmer Guide/Command Reference/NUM: Difference between revisions

From STX Wiki
Jump to navigationJump to search
No edit summary
No edit summary
 
(20 intermediate revisions by 3 users not shown)
Line 11: Line 11:
|0<BR>warning code
|0<BR>warning code
|}
|}
;Description: The ''expression'' will be evaluated numerically, and the result (textual representation) is the return value. The following operators and functions are available.


The ''expression'' will be evaluated numerically, and the result (textual representation) will be returned. The ''expression'' may consist of the following parts:
;decimal numbers: <code>123.456</code>, <code>17.5e3</code>, <code>100</code>, <code>-312.123</code>, <code>1e-3</code>,&hellip;
;hexa-decimal numbers:<code>0x1234</code>, <code>0xabc</code>, <code>0XabC</code>, &hellip;
;special constants:<code>pi</code> (=3.1415...), <code>e</code> (=2.71828...),
:<code>rand</code> (a linear distributed pseudo random number ''r''; -1&le;''r''&lt;1)
:<code>lran</code> (a linear distributed pseudo random number ''r''; 0&le;''r''&lt;1)
;numerical operators: <code>-''a''</code> (negate), <code>''a''+''b''</code> (add), <code>''a''-''b''</code> (subtract), <code>''a''*''b''</code> (multiply), <code>''a''/''b''</code> (divide), <code>''a''%''b''</code> (modulo), <code>''a''^''b''</code> (power)
;logical operators (bitwise): <code>!''a</code> (not), <code>''a''&''b''</code> (and), <code>''a''|''b''</code> (or), <code>''a''^''b''</code> (exclusive or)
;brackets:<code>(''expression'')</code>
;functions:
:{|class="einrahmen"
|<code>setlran(''seed'')</code>
|linear distributed pseudo random number ''r'' (0&le;''r''&lt;1)<BR>''seed'' is the start value (0&le;''seed''&lt<1)
|-
|<code>sin(''x'')</code>, <code>cos(''x'')</code>, <code>tan(''x'')</code>
|sine, cosine or tangent of ''x''
|-
|<code>asin(''x'')</code>, <code>acos(''x'')</code>, <code>atan(''x'')</code>
|inverse sine, cosine or tangent of ''x''
|-
|<code>exp(''x'')</code>
|computes <code>e<sup>''x''</sup></code>
|-
|<code>log(''x'')</code>
|common logarithm of ''x'' (base <code>10</code>)
|-
|<code>ln(''x'')</code>
|natural logarithm of ''x'' (base <code>e</code>)
|-
|<code>sqrt(''x'')</code>
|square root of ''x'' (base 10)
|-
|<code>abs(''x'')</code>
|absolute value of ''x'' (base 10)
|-
|<code>int(''x'')</code>
|integer part of ''x'', the fractional part is truncated<BR>(range of ''x'': 32bit signed integer)
|-
|<code>round(''x'')</code>
|round to the nearest integer of ''x''<BR>(range of ''x'': 32bit signed integer)
|-
|<code>db(''x'')</code>
|convert level to factor (<code>10<sup>''x''/20</sup></code>)
|-
|<code>sinc(''x'')</code>, <code>sinx(''x'')</code>
|sinc function: <code>sin(''x'') / ''x''</code>
|-
|<code>sign(''x'')</code>
|sign of ''x''; returns -1 if <code>''x''&lt;0</code>, otherwise 1
|-
|<code>hz2bark(''x'')</code>
|convert ''x'' from Hertz to Bark
|-
|<code>bark2hz(''x'')</code>
|convert ''x'' from Bark to Hertz
|-
|<code>bit(''n'')</code>
|returns the integer with only bit ''n'' (0&le;''n''&lt;32) is set and all other are cleared; can be used to generate bit masks
|-
|<code>floor(''x'')</code>
|returns a floating-point value representing the largest integer that is less than or equal to ''x''<BR>(range of ''x'': 64bit float)
|-
|<code>iserr(''rc'')</code>, <code>iserror(''rc'')</code>
|returns 1 if ''rc'' is a {{STX}} error code, otherwise 0<BR>this function can be used to test the completion code <code>RC</code> of a command
|-
|<code>iswarn(''rc'')</code>, <code>iswarning(''rc'')</code>
|returns 1 if ''rc'' is a {{STX}} warning code, otherwise 0<BR>this function can be used to test the completion code <code>RC</code> of a command
|-
|<code>npow2(''n'')</code>
|next power of 2; returns the smallest possible value <code>2<sup>m</sup> &ge; ''n''</code>
|}
;Notes:
;Notes:
:* The <code>INT</code> command converts the result to an integer by ''truncating'' it. There is no rounding involved, no rounding at all.
*In case of the expression being syntactically ill-formed, an error (<code>NUM</code>) or warning (<code>NUMCHECK</code>) is reported.
:* In case of the expression being syntactically ill-formed, an error (<code>INT</code>) or warning (<code>INTCHECK</code>) is reported.
*The special numerical objects (vectors, matrices) available in [[../EVAL|EVAL]]-expressions, can not be used in the <code>NUM</code> expressions. Only the use of numerical constants and variables is possible.
;See also: [[../NUM|NUM, NUMCHECK]], [[../EVAL|EVAL, EVALCHECK]]
*The same expression syntax is used for the [[../INT|INT and INTCHECK]], for the numerical expressions in [[Programmer_Guide/Introduction#Conditional_Expressions|conditions]] (e.g. [[../IF|IF]] or [[../WHILE|WHILE]]) and for the evaluation of numerical arguments of commands.
*Numerical expressions are evaluated in 64bit floating point precission.
*For logical (operators <code>!, &, |, ^</code>) and integer expressions (functions <code>round, int, bit</code>) 32bit signed integers are used.


;See also: [[../INT|INT, INTCHECK]], [[../EVAL|EVAL, EVALCHECK]], [[../SEGMENT|SEGMENT]]


;Examples:
;Examples:
  #result := int 3.1          // #result is set to 3
// #wave = wave item
  #result := int 3.9          // #result is set to 3, too
  #t := num $#wave[!length] / $#wave[!srate]          // duration in seconds
  #result := int 3.9 * 3.9    // #result is set to 15 (note that calculation
// #t = frame length in seconds, #o = overlap in percent
                              // is done in floating point, resulting in 15.21,
  #lfrm := int $#t * $#wave[!srate]                  // frame length in samples
                              // and truncation occurs only on assigning
  #lhop := int $#t * (1 - $#o / 100) * $#wave[!srate] // hopsize in samples
  // compare the preceding example with the following one:
  #lfft := int npow2($#lfrm)                          // fft length
  #result := int 3 * int(3.9) // here, #result will be assigned 9 - cool, isn't it?
  #df  := num $#wave[!srate] / $#lfft                // frequency resolution
 
  // using expressions in a IF statement
if 'db($#level)' > 1 then
    conlog 'level must be lower or equal 0dB'
    #level := 0
end

Latest revision as of 16:51, 25 April 2014

command return value value of RC
NUM expression value of expression or
empty string if the evaluation fails
0
error code
NUMCHECK expression value of expression or
empty string if the evaluation fails
0
warning code

The expression will be evaluated numerically, and the result (textual representation) will be returned. The expression may consist of the following parts:

decimal numbers
123.456, 17.5e3, 100, -312.123, 1e-3,…
hexa-decimal numbers
0x1234, 0xabc, 0XabC, …
special constants
pi (=3.1415...), e (=2.71828...),
rand (a linear distributed pseudo random number r; -1≤r<1)
lran (a linear distributed pseudo random number r; 0≤r<1)
numerical operators
-a (negate), a+b (add), a-b (subtract), a*b (multiply), a/b (divide), a%b (modulo), a^b (power)
logical operators (bitwise)
!a (not), a&b (and), a|b (or), a^b (exclusive or)
brackets
(expression)
functions
setlran(seed) linear distributed pseudo random number r (0≤r<1)
seed is the start value (0≤seed&lt<1)
sin(x), cos(x), tan(x) sine, cosine or tangent of x
asin(x), acos(x), atan(x) inverse sine, cosine or tangent of x
exp(x) computes ex
log(x) common logarithm of x (base 10)
ln(x) natural logarithm of x (base e)
sqrt(x) square root of x (base 10)
abs(x) absolute value of x (base 10)
int(x) integer part of x, the fractional part is truncated
(range of x: 32bit signed integer)
round(x) round to the nearest integer of x
(range of x: 32bit signed integer)
db(x) convert level to factor (10x/20)
sinc(x), sinx(x) sinc function: sin(x) / x
sign(x) sign of x; returns -1 if x<0, otherwise 1
hz2bark(x) convert x from Hertz to Bark
bark2hz(x) convert x from Bark to Hertz
bit(n) returns the integer with only bit n (0≤n<32) is set and all other are cleared; can be used to generate bit masks
floor(x) returns a floating-point value representing the largest integer that is less than or equal to x
(range of x: 64bit float)
iserr(rc), iserror(rc) returns 1 if rc is a STx error code, otherwise 0
this function can be used to test the completion code RC of a command
iswarn(rc), iswarning(rc) returns 1 if rc is a STx warning code, otherwise 0
this function can be used to test the completion code RC of a command
npow2(n) next power of 2; returns the smallest possible value 2mn
Notes
  • In case of the expression being syntactically ill-formed, an error (NUM) or warning (NUMCHECK) is reported.
  • The special numerical objects (vectors, matrices) available in EVAL-expressions, can not be used in the NUM expressions. Only the use of numerical constants and variables is possible.
  • The same expression syntax is used for the INT and INTCHECK, for the numerical expressions in conditions (e.g. IF or WHILE) and for the evaluation of numerical arguments of commands.
  • Numerical expressions are evaluated in 64bit floating point precission.
  • For logical (operators !, &, |, ^) and integer expressions (functions round, int, bit) 32bit signed integers are used.
See also
INT, INTCHECK, EVAL, EVALCHECK, SEGMENT
Examples
// #wave = wave item
#t := num $#wave[!length] / $#wave[!srate]          // duration in seconds
// #t = frame length in seconds, #o = overlap in percent
#lfrm := int $#t * $#wave[!srate]                   // frame length in samples
#lhop := int $#t * (1 - $#o / 100) * $#wave[!srate] // hopsize in samples
#lfft := int npow2($#lfrm)                          // fft length
#df   := num $#wave[!srate] / $#lfft                // frequency resolution
// using expressions in a IF statement
if 'db($#level)' > 1 then
    conlog 'level must be lower or equal 0dB'
    #level := 0
end

Navigation menu

Personal tools