Step 4: Code the Script Calculation

To implement this script, we will subset the data to the left and the right of the data to be removed.  Then, we will concatenate the two subsets to produce the output.

To do so, the built-in EMGscript functions subset and concat are used, as well as some basic math operations.  Code the Calculate function as seen below.

/**
* Data Segment Removal Tutorial Script
* @name Data Segment Removal
* @version 0.0.0.0
* @author Your Name
* @signed 0
* @namespace DSR
*/


DSR = {
Calculate : function()
   {
    // compute the starting and stopping indices of the region to be removed
    var startIndex = (DSR.Parameter.IntervalStart - DSR.Input.XStart) * DSR.Input.Fs;
    var stopIndex = (DSR.Parameter.IntervalStop - DSR.Input.XStart) * DSR.Input.Fs;
    
    // subset and concatenate the data to the left and right of the segment to be removed
    DSR.Output.SegmentRemoved = DSR.Input.subset(0, startIndex)
              .concat(DSR.Input.subset(stopIndex, DSR.Input.NumPoints - stopIndex));

   },

Input : 5,

OutputTemplate:
           {
              SegmentRemoved: {seq: 1, name: "Data Segment Removed"}
           },

Validate : function()
           {
              if (DSR.Parameter.IntervalStart < DSR.Input.XStart)
                  return "Start cannot be less than the start time of the data series";
              if (DSR.Parameter.IntervalStart >= DSR.Parameter.IntervalStop)
                  return "Start cannot be greater than or equal to stop";
              if ((DSR.Parameter.IntervalStop - DSR.Input.XStart) * DSR.Input.Fs > DSR.Input.NumPoints)
                  return "Stop cannot be greater than the length of the data series";      
           },

ParameterTemplate:
           {
             IntervalStart: {seq: 1, name: "Start of interval (sec)", type:"number", initial: 0},
             IntervalStop: {seq: 2, name: "End of interval (sec)", type:"number", initial: 5}
           },
}