logo

10 Signal Scripts

Signal scripts allow the users to analyze and interpret signals in many ways. Combine signals using mathematical operations, generate references, implement protocol parsers, extract statistical informations or search for conflicts automatically.

How to create a signal script ?

Signal scripts can be created either using the template menu Add->Script Example Templates or by changing an existing configuration item into a script production, using the configuration dialogs Production combo box.

Configuration of the production output

Scripts are executed on the fly, as soon as the signal data is required for further processing, and re-executed when settings or input signal have been changed. Before executing a script, the system needs to know the source signals, the type and the domain of the signal. All these informations need to be entered into the configuration dialog.

Primary Signal
The primary signal is the first source signal of the production.
Additional Signals
If more than 1 source signal is required, add them into this table. Instead of using the table you may drag and drop (with a closed dialog) signals onto the configuration item.
Process type
Usually the discrete process type is chosen. Just (at the moment) float signals support the continuous type.
Signal type
Select the output signal type of your script (Float, String, Logic, Integer, ..).
Signal descriptor
The signal descriptor describes the signal type in more details (e.g the bit width of a logic vector (default)). See below for more details. But in most case, you will use the standard settings (default<>) - Press CTRL-Space to view content proposals.
Domain Class/Base
The domain base is just required if your output signal has a different domain than the source signals. If not, just don't touch these settings. If you select a domain base (before you need to select the domain class (e.g. time, frequency,..), you also need to set the domain range, so the minimum and maximum value of the domain (e.g. 0 .. 1000 Hz)

How to get started with scripting ?

Have a look at the script examples. Select a script that is close to your requirements, than copy script and settings into the dialog. Most script exampels are also available at the template menu.

Script Examples

Use the build-in script editor

You may use the built-in script editor instead of the text field in the dialog. Click on "Edit in js editor" to open a new eclipse editor. As soon as you save the content, its signals will be updated and changes get visible in a parallel impulse viewer. If there are errors in the script, log messages will be send to the console view.

The script itself

Impulse uses JavaScript as its default language (other languages might be added in future). Rhino interpreter us used up to Java 6 and above the Nashorn interpreter that uses the byte code engine of java and improves performance a lot. The available scripts are compatible for both interpreters.

The goal of a script is to read the source signals and create one output signal. A typical script look like this:

// input: an array of all input signals
// in0: primary input of type ISamplePointer,IReadableSamples
// in1..: additional inputs of type ISamplePointer,IReadableSamples
// out: output signal of type IFloatSamplesWriter

for (var iter  =  new SamplesIterator(input);iter.hasNext();){
      var current = iter.next();
      out.write(current,false,in1.floatValue()
                   *in2.floatValue()*Math.sin(current/1000.));
}

SamplesIterator class allows to iterate over events of multiple source signals. 'iter.next()' returns the current domain value (e.g current time). All input variable can be accessed as signal pointers: For each event found, SamplesIterator set the pointers to the current position, e.g. floatValue() returns the float value at that position.

Variable out represents a samples writer of the configured type (e.g. IFloatSamplesWriter). You create the new signal by writing consecutively new values at given domain positions (write(domainValue,conflict,value).

If there is just one input, you can simply iterate in the following way. Instead of floatValue(), you use floatValueAt(eventIdx).

for (var i=0;i<in0.getCount();i++){ 
	real[i] = in0.floatValueAt(i);
}

Java and JavaScript

Impulse is build on Java and signal script uses JavaScript to operate on Java object (the input signal pointer, the output writer,...).

Its up to the user to work with JavaScript objects but its usually not required and to our understanding not useful. Primitives from java are converted into JavaScript primitives. But there are limits: JavaScript has just one number type (64 float), so a long value can not be converted and will be given as a Long objects.

If Nashorn is used (up from Java 7), impulse automatically loads the Rhino compatibility library. But have in mind that Rhina and Nashorn still have a slightly different interface to java. Please see Scripting Java (Rhino) and Rhino Migration Guide (to Nashorn).

Content assist and psoido typing

In the 1.4 version templates, you will find snippets like this:<:IFloatSamplesWriter:>.

These snippets are doing psoido typing - They tell the environment the java type of a variable to let the system prepare content assist proposals. The snipperts do not have any impact on the java engine and are removed before processing. You don't need to use them, but they aar equite helpful if you don't want to read reference docs all the time.

Java reference for scripts Show examples More about Extending impulse