Please see this overview page first to understand some basic principles about using scripting functions in SIGVIEW.


Script signal source window provides the possibility to insert signals into SIGVIEW  from the outside, for example from DAQ devices or signal files.

How to create Script Signal Source Window


Select Scripts/Signal window main menu option. A new window will open. At the same time a source code editor will open, opening script template and allowing you to edit a script to define the content of your window. Until you write and save your script, window will be blank, showing only "Not enough data to show the signal" message.


The script template will already contain some content: In the upper part, you will find some help text and in the lower part you a short reference of all important types you can use.  The important part is in the middle - those are the functions update(...) and getUpdateIntervalMSec( ) function:


function update( output_signal:: SigviewSignalWindow )

  #write your code here

  return false

end


function getUpdateIntervalMSec( )

       return 0

end


The purpose of update(...) function is to collect the signal (or the next signal block) from the external source and to insert it into SIGVIEW. If it was successful, i.e. new data has been provided, the function should return true. If there is no new data, the function should return false.


For detailed description of SigviewSignalWindow struct, see Data types chapter.


The second function, getUpdateIntervalMSec, defines if update(...) should be called automatically in specified time interval (in milliseconds). If you return 0 in this function (default), update(...) function will not be called automatically. If you return a value >0, your update function will be called in that time intervals. This is, of course, useful only for data acquisition from live signal sources or for signal generator scripts.


Let's show how this works on an example. We will simply generate sequence of random numbers, 1000 samples long and make it a content of our source signal window:


function update( output_signal:: SigviewSignalWindow )

  output_signal.samples = rand( -100 : 100, 1000 )

  output_signal.samplingRate = 10

  output_signal.xAxisUnit = "sec"

  return true

end


As soon as you save the script from your editor, SIGVIEW will detect that the script has changed, the script will be executed and the result will be displayed in a SIGVIEW window. You can keep editing the script - each time you save it, SIGVIEW will execute it again and update the content of a window.

If your update(...) script returns false, window content will not be cleared as it is the case with signal windows. Instead, the last valid data will be kept in the window.


You can explicitly execute the script again by selecting Execute script...  context menu option. In the above example with random signal, you would get a new signal with new random numbers each time you do it.


If you additionally change the return value of the getUpdateIntervalMSec function to 100ms, your random signal will update 10 times per second.


function getUpdateIntervalMSec( )

       return 100

end


How would you change the x-axis to change accordingly each time you generate a new signal? You can do it by defining a global variable outside your functions to track the current begin of the x-axis:


x_axis_begin = 0


function update( output_signal:: SigviewSignalWindow )

  output_signal.samples = rand( -100 : 100, 1000 )

  output_signal.samplingRate = 10

  output_signal.xAxisUnit = "sec"

  output_signal.xAxisBegin = x_axis_begin

  global x_axis_begin += 100 #we generate 1000 samples at 10 samples/s, i.e. block length of 100s

  return true

end


Note that the global variable x_axis_begin will have a different instance/scope for each window where it is used - it is not really a global variable but only global for your window.


For additional functions you can use to improve your script window, see Advanced topics chapter.

Handling errors and displaying text


If you make an error in your script, for example syntax error, using unknown function or variable etc., you will see the corresponding Julia error message in your SIGVIEW window in its upper left corner:




If you would like to report an error of your own from the script, you can set a corresponding text message to be displayed and return false from the process function. For example, if your script expects to have exactly two parents, you can check it and display appropriate error like this:


  if length( input_windows ) != 2

     output_signal.textToDisplay = "Error:\nThis script can process only 2 inputs signals"

     return false

  end



Even if you provide the content of the window, i.e. return true from the process function, you can still use textToDisplay variable to display a text in the result window, for example for debugging purposes.


Further reading


See scripting examples in this help and this chapter for some additional advanced functions you can implement in your script.