Algorithm Information

Back to the Waveform Analysis Package - Main Page

Algorithm Page Index:

    A note on different algorithms
( Shared Algorithms )
    Maximum Voltage
( Waveform Algorithms )
    Baseline
    Peak Maker
( Peak Algorithms )
    Leading Edge
    Peak Area





A note on calling different algorithms:

    For any generic value that can be calculated by different algorithms, there are two methods one can use to get it:

                    getValue() or getValue( int algorithm )

The getValue() method with no arguement will use the default algorithm to calculate the value.  The getValue( algorithm ) method will use the algorithm represented by the integer sent to it to calculate the value.  Check the Peak and Waveform classes for final static integer fields that show what integer to send for each algorithm.  Once an algorithm is specified, and a value returned, future calls to getValue() will return the previously calculated value.  In order to get a new value calculated by the default algorithm, one must specify that algorithm in the method call:  getValue( int algorithm ).  The getValue() method returns to the default algorithm once a new pulse has been entered.

Back to top



Algorithms shared by Peak and Waveform:



Maximum Voltage
==============

    The purpose of this algorithm is to find the maximum value that the voltage attains over the waveform or peak object's pulse.  These algorithms can work without any previous information about the Signal object.  They also always return a value and never fail.  There are three algorithms written to serve this purpose:  Defalut, Parabolic, and Three-Level.  The steps of each algorithm follw:

Default
This algorithm simply loops over each voltage tick in the pulse and checks it's current maximum value against it.  If a voltage tick exceeds the current maximum value, then the current maximum value is set to the value of that voltage tick.  The current maximum value is initiallized to -100000, so that it can be assumed that a voltage tick in the pulse will excced that level.  The error is set to 2% of the returned value, an assumed digitization error.

         //  Initiallizes the maxVoltage, so that it can be assumed that a voltage tick
        //  will exceed it.
        signal.maxVoltage = -100000;

        //  Loops through the length of the signal to find the highest
        //  voltage value.
        for(int iTick = 0; iTick < signal.pulse.length; iTick++){

            //  If a voltage tick is greater than the current maximum voltgage, the
            // current maximum voltage is set to that tick.
            if( signal.maxVoltage < signal.pulse[iTick]){
                signal.maxVoltage = signal.pulse[iTick];
            }
        }

        //  The error is set to a 2% error on the value and the isDone parameter is
        //  set to one.
        signal.maxVoltageError = 0.02 * signal.maxVoltage;
        signal.maxVoltageIsDone = signal.DEFAULT;

Parabolic
This algorithm first loops over the pulse just as in the default algorithm to find the tick where the maximum voltage value occurs.  The two voltage values around it and the maximum voltage value itself are then used as ( x, y ) coordinates to determine a parabola in the form y - y0 = k ( x - x0 ) ^ 2.  The maximum of that parabola ( y0 ) is then returned as the maximum voltage.  The error is set to 2% of the returned value, an assumed digitization error.

        //  This loop finds at what tick the maximum value occurs.
        int maxVoltageTick = 0;
        double standardMaxVoltage = -100000;
        for(int iTick = 0; iTick < signal.pulse.length; iTick++){
            if( standardMaxVoltage < signal.pulse[iTick]){
                maxVoltageTick = iTick;
                standardMaxVoltage = signal.pulse[iTick];
            }
        }

        //  Here are the three point that determine the parabola
        double x1 = maxVoltageTick - 1, x2 = maxVoltageTick, x3 = maxVoltageTick + 1;
        double y1 = signal.pulse[ (int) x1 ], y2 = signal.pulse[ (int) x2 ], y3 = signal.pulse[ (int) x3 ];

        //  Here, various constants are calculated according to the formula for a parabola:
        //          y - y0 = k * ( x - x0 ) ^ 2
        //  Each constant is calculated in turn, till we get the one we want:  y0.
        double k = ( ( y1 - y2 ) / ( x1 - x2 ) - ( y1 - y3 ) / ( x1 - x3 ) ) / ( x2 - x3 );
        double x0 = ( x1 + x2 - ( y1 - y2 ) / ( x1 - x2 ) / k ) / 2;
        double y0 = ( y1 + y2 + y3 - k * ( ( x1 - x0 ) * ( x1 - x0 ) + ( x2 - x0 ) * ( x2 - x0 )
                + ( x3 - x0 ) * ( x3 - x0 ) ) ) / 3 ;

        signal.maxVoltage = y0;
        signal.maxVoltageError = 3;
        signal.maxVoltageIsDone = 2;

Three Level
The motivation for this algrorithm is to be able to handle peaks that contain a subPeak before their actual maximum value.  This algorithm first loops over the pulse just as in the default algorithm to find the tick where the maximum voltage occurs.  The two voltage values around it and the maximum voltage value itself are then averaged to find the middle mean.  The three voltage values to the left and right of the middle mean are then averaged to find the left and right means.  If the left mean is greater than the right mean, the four voltage values to the left of the maximum and the maximum itself are averaged to find the returned value.  Otherwise, the middle mean is returned.  The error is set to 2% of the returned value, an assumed digitization error.

        //  The algorithm finds at what tick the maximum value occurs.
        int maxVoltageTick = 0;
        double standardMaxVoltage = -100000;
        for(int iTick = 0; iTick < signal.pulse.length; iTick++){
               if( standardMaxVoltage < signal.pulse[iTick]){
                    maxVoltageTick = iTick;
                    standardMaxVoltage = signal.pulse[iTick];
               }
        }

        //  The means of three values to the left and right are calculate
        double leftMean = ( signal.pulse[maxVoltageTick - 2] + signal.pulse[maxVoltageTick - 3]
                + signal.pulse[maxVoltageTick - 4] ) / 3;
        double rightMean = ( signal.pulse[maxVoltageTick + 2] + signal.pulse[maxVoltageTick + 3]
                + signal.pulse[maxVoltageTick + 4] ) / 3;

        //  If the left mean is greater than the right mean, the four
        //  values to the left of the highest and the highest value itself are
        //  averaged to find the pulse height.
        //  Otherwise the three values around the peak are averaged to find the
        //  pulse height.
        //  The error is estimated to be a propogation of +/- 3 digitization error.
        if(leftMean > rightMean){

            signal.maxVoltage = ( signal.pulse[maxVoltageTick - 4] + signal.pulse[maxVoltageTick - 3]
                + signal.pulse[maxVoltageTick - 2] + signal.pulse[maxVoltageTick - 1] +
                    signal.pulse[maxVoltageTick]) / 5;
            signal.maxVoltageError = Math.sqrt( 45 );
        }else{

            signal.maxVoltage = ( signal.pulse[maxVoltageTick - 1] + signal.pulse[maxVoltageTick]
                    + signal.pulse[maxVoltageTick + 1] ) / 3;
            signal.maxVoltageError = Math.sqrt( 27 );
        }

        signal.maxVoltageIsDone = 3;

Back to top




Algorithms for Waveform Objects:


Baseline
======

Forced Iterative
Use this algorithm if you are sure that there are peaks on the waveforms that you are analyzing.

Forced Noise
Use this algorithm if you are sure that there are no peaks on the waveform that you are analyzing.

Default
This algorithm tries to decide if there are peaks on the waveform or not, and uses a different algorithm for each situation.  If you want to make that decision yourself, use one of the "Force (blank)" algorithms above.


Baseline Algorithm Description  


        //  Sets up a temporary waveform where values will can be erased and
        //  manipulated.
        int lengthTempWaveform = waveform.pulse.length;
        double[] tempWaveform = new double[ waveform.pulse.length ];
        for( int iTick = 0; iTick < waveform.pulse.length; iTick++ ){

            tempWaveform[ iTick ] = waveform.pulse[ iTick ];
        }

        //  Finds necessary properties of the waveform:  the highest and lowest value.
        //  These values are used to set the size of the voltage histogram array (see
        //  next step)
        int peak = (int) waveform.maxVoltage;
        int trough = peak;
        for( int iTick = 0; iTick < tempWaveform.length; iTick++){
            if( tempWaveform[ iTick ] < trough ){
                trough = (int) Math.floor( tempWaveform[ iTick ] );
            }
        }

        //  Sets up a histogram of voltage ticks.
        int[] voltageTickHist = new int[ peak - trough + 1 ];
        for( int iTick = 0; iTick < tempWaveform.length; iTick++){

            //  Since the array indicies cannot drop below zero, the bottom of the
            //  range of voltage ticks is translated to zero.
            voltageTickHist[ (int) Math.floor( tempWaveform[ iTick ] ) - trough ]++;
        }


        //  Finds the voltage tick that is found most numerous in the waveform.
        int max = 0, iMax = 0;
        for( int iTick = 0; iTick < voltageTickHist.length; iTick++ ){

            if( voltageTickHist[iTick] > max ){

                max = voltageTickHist[ iTick ];
                iMax = iTick;
            }
        }

        //  Adjustes the most numerous tick according to the above translation.
        iMax += trough;


        //  Index that counts the number of times the baseline and pedastalRMS is calculated.
        int iCalc = 0;

        //  Each calculation of the mean and pedastalRMS is saved in an array.
        double[] mean = new double[15], RMS = new double[15];

        //  Here is the first iteration of the calculation loop.  One of the conditions
        //  of exiting the loop requires two iterations to have been completed, so the
        //  first iteration is here, and the second is inside the do statemnet.

        //  This loop adds up the voltages and their squares.
        for( int iTick = 0; iTick < tempWaveform.length; iTick++ ){

            mean[ iCalc ] += tempWaveform[ iTick ];
            RMS[ iCalc ] += tempWaveform[ iTick ] * tempWaveform[ iTick ];
        }

        //  The average and pedastalRMS is calculated from the preceding sums.
        mean[ iCalc ] /= lengthTempWaveform;
        RMS[ iCalc ] = lengthTempWaveform / ( lengthTempWaveform - 1 ) *
                ( RMS[ iCalc ] / lengthTempWaveform + mean[ iCalc ] * mean[ iCalc ] );
        RMS[ iCalc ] = Math.sqrt( RMS[iCalc] );

        //  This loop removes values from the waveform that exceed the average by
        //  two times the pedastalRMS.  These values are set to .9999, which is assumed to
        //  not occur in the waveform.  The length of the waveform is also adjusted
        //  to take into account this removal
        for( int iTick = 0; iTick < tempWaveform.length; iTick++ ){

            if( tempWaveform[ iTick ] > mean[ iCalc ] + 2 *  RMS[ iCalc ]){

                tempWaveform[ iTick ] = 0.9999;
                lengthTempWaveform--;
            }
        }

        //  End first iteration of calculation loop.

        do{

            //  Stepping the calculation index.
            iCalc++;

            //  THese iterations of the calculation loop are similar to the first
            //  iteration, which appears before the "do" statement.

            //  The voltage sum and sum of the squares are calculated.  This loop
            //  throws out values that are equal to 0.9999 (ie. they have been thrown
            //  out by previous iterations).
            for( int iTick = 0; iTick < tempWaveform.length; iTick++ ){

                if( tempWaveform[ iTick ] != 0.9999 ){

                    mean[ iCalc ] += tempWaveform[ iTick ];
                    RMS[ iCalc ] += tempWaveform[ iTick ] * tempWaveform[ iTick ];
                }
            }

            //  Calculation of the average and pedastalRMS from the above sums.
            mean[ iCalc ] /= lengthTempWaveform;
            RMS[ iCalc ] = lengthTempWaveform / ( lengthTempWaveform - 1 ) *
                    ( RMS[ iCalc ] / lengthTempWaveform + mean[ iCalc ] * mean[ iCalc ] );
            RMS[ iCalc ] = Math.sqrt( RMS[iCalc] );

            //  Throwing out values that exceed the average by twice the pedastalRMS.
            for( int iTick = 0; iTick < tempWaveform.length; iTick++ ){

                if( tempWaveform[ iTick ] > mean[ iCalc ] + 2 *  RMS[ iCalc ]){

                    tempWaveform[ iTick ] = 0.9999;
                    lengthTempWaveform--;
                }
            }

            //  The loop is repeated until one of the following conditions:  the twentieth
            //  calculation, the mean calculated is the same (ie. no values were thrown
            //  out), the mean drops below the most numerous voltage value (iMax), or over
            //  a third of the values in the waveform have been removed.
        }while( iCalc < 14 &&
                mean[ iCalc - 1 ] != mean[ iCalc ] &&
                mean[ iCalc ] > iMax &&
                lengthTempWaveform > tempWaveform.length / 3. );

        //  The value is set to the latest calculation of the mean.
        waveform.baseline = mean[ iCalc  ];
        //  The error on the mean is the RMS divided by the square root of the number of ticks
        //  used to calculated it.
        waveform.baselineError = RMS[ iCalc ] / Math.sqrt( lengthTempWaveform );
        //  The RMS is set to the latest calculation of it.
        waveform.pedestalRMS = RMS[ iCalc ];
        //  The error on the RMS is given by this formula:
        waveform.pedestalRMSError = 2 * Math.pow( waveform.baseline, 4) / (float) ( lengthTempWaveform - 1. );
        //  Sets isDone to true, skipping recalculation on same waveform.
        waveform.baselineIsDone = 1;

Back to top



Peak Maker
=========

    The purpose of this algorithm is to look for peaks in the pulse.  The code then creates a Peak object with various specifications, and adds it to the Waveform object's vector of Peaks.  The nextTriggerTool( double[] pulse ) is a method that the algorithm utilizes.  It's purpose is to find each new peak, by locating where the voltage values first exceed two times the RMS.  Another voltage value must also exceed this threshold level in order for the method to count it as a pulse.  The method is displayed below:

    private int nextTriggerTool( double[] pulse ){

        //  The thresholdLevel is then set to 3 times the pedastalRMS added to the baseline.
        double thresholdLevel = this.baseline + 3 * this.pedestalRMS;

        //  This parameter represents the number of subsequent ticks that need to
        //  exceed the threshold level in order to show a peak.
        int subsequentTicks = 2;

        //  A flag is set to false while no trigger has yet been found
        boolean realPeak = false;

        // triggerTick is the tick we search for
        // peakTick is an index that counts the ticks after the triggerTick
        int triggerTick = 0, peakTick = 0;

        //  No trigger found so far
        while( !realPeak &&
                // prevent from accessing elements out of bounds
                triggerTick <= ( pulse.length - subsequentTicks ) ) {

            //  Checks if the current tick exceeds the threshold level
            if ( pulse[triggerTick] > thresholdLevel ){

                //  If the current tick exceeds the threshold level, the inner
                //  loop starts to check the next few ticks, up to the parameter
                //  subsequent ticks.
                peakTick = triggerTick + 1;
                realPeak = true;

                //  If a subsequent tick falls below the threshold level, realPeak
                //  is set to false.  If the subsequent ticks remain above the
                //  threshold level, the both loops are exited with realPeak true.
                while ( ( peakTick < ( triggerTick + subsequentTicks ) ) &&
                        realPeak ){

                    if( pulse[peakTick] > thresholdLevel ){

                        peakTick++;
                    }else{

                        realPeak = false;
                        triggerTick = peakTick + 1;
                    }
                }

                //  If the current tick is not above the threshold level, the loop
                // continues to the next tick.
            } else {

                triggerTick++;
            }
        }

        //  This code throws the exception if the loop found no trigger, or
        //  sets the valuer, if a trigger was found.
        if( !realPeak ){

            return ( pulse.length + 1 );
        }else{

            //  In order to find a peak, the threshold level is set to a high three times
            //  the RMS, but the trigger is then backed up to the tick before the peak first
            //  crossed two times the RMS
            do{

                triggerTick--;
            }while( pulse[ triggerTick ] > this.baseline + 2 * this.pedestalRMS );

            return  triggerTick;
        }
    }

When a trigger tick is returned to the peak maker, it adds each of the voltage ticks in the peak to a new array of doubles, and then makes a Peak object with the new array.  After voltage ticks have been read into a Peak object, they are set to the baseline, so that nextTrigger( pulse ) will not find the same peak twice.  Peaks are then added to the Waveform object's peak Vector:

        //  Initiallizes all variables
        waveform.peaks = new Vector();
        this.baseline = waveform.baseline; this.pedestalRMS = waveform.pedestalRMS;
        int nextTrigger = 0;

        //  Sets up a temperory waveform that the algorithm can safetly manipulate.
        double[] temporaryWaveform = new double[ waveform.pulse.length ];
        for( int iTick = 0; iTick < waveform.pulse.length; iTick++ ){
            temporaryWaveform[ iTick ] = waveform.pulse[ iTick ];
        }

        //  Finds the tick that begins the first peak
        //  Returns a value of tempWaveform.length + 2 if there are no more peaks
        //  in the waveform.
        nextTrigger = nextTriggerTool( temporaryWaveform ) + 1;

        //  Loops through the waveform until there are no more peaks.
        while( nextTrigger < temporaryWaveform.length + 2 ){

            //  iTock counts the ticks inside a peak.
            int iTock = 0;
            //  Where the peaks voltage values are saved.
            double[] tempPeakPulse = new double[ temporaryWaveform.length ];

            //  Loops until the end of the peak.
            while( iTock < temporaryWaveform.length && temporaryWaveform[ nextTrigger + iTock ] > baseline ){

                //  Saves the voltage values in the peak to an array of doubles.
                tempPeakPulse[ iTock ] = temporaryWaveform[ nextTrigger + iTock ];

                //  Sets voltage values already saved to the baseline, so that the
                //  nextTriggerTool will not find the same peak twice.
                temporaryWaveform[ nextTrigger + iTock ] = baseline;

                //  Counts through the ticks of the peak
                iTock++;
            }

            //  This is the peakPulse of the correct length that will be used in
            //  the Peak object constructor.
            double[] peakPulse = new double[ iTock ];

            //  Enters the peak into the array of the correct length.
            for( int iTick = 0; iTick < iTock; iTick++ ){
                peakPulse[ iTick ] = tempPeakPulse[ iTick ];
            }

            //  Creates the new peak object.
            Peak peakObject = new Peak( peakPulse, nextTrigger, waveform.baseline,
                    waveform.baselineError, waveform.pedestalRMS, waveform.pedestalRMSError );

            //  Adds the peak object to the waveforms vector of peaks.
            waveform.peaks.add( peakObject );

            //  Finds the tick that begins the first peak
            //  Returns a value of tempWaveform.length + 2 if there are no more peaks
            //  in the waveform.
            nextTrigger = nextTriggerTool( temporaryWaveform ) + 1;

        }

        waveform.peaksIsDone = true;


Back to top



Algorithms for Peak Objects:


Leading Edge
==========

    The purpose of this algorithm is to find an accurate value for where the pulse actually begins.  First, the first tick to exceed the RMS level is found.  That voltage tick and the previous one are then used to determine a line ( y - y0 = m ( x - x0 ) ).  The basline in then plugged into the y value, and the equation is solved for x.


        //  Finds where the pulse first crosses the baseline plus the RMS.
        int iTick = 0;
        while( peak.pulse[ iTick ] < peak.baseline + peak.pedestalRMS ) iTick++;

        //  Plugs the point found above and the previous point into the equation
        //  of a line through two points:
        //          y - y0 = m ( x - x0 )
        //  y is then set to the baseline, and the equation is solved for x.
        peak.leadingEdge = peak.pulse[ iTick ] - ( peak.pulse[ iTick ] - peak.baseline ) /
                ( peak.pulse[ iTick ] - peak.pulse[ iTick - 1 ] );

        //  Since the indicies on the peak pulses all start at zero, the calculated
        //  leading edge should be added to the triggerTick, so that the leading edge
        //  has meaning with respect to the waveform.
        peak.leadingEdge += peak.triggerTick;

        //  The error on this quantity is set to three.
        peak.leadingEdgeError = 3;


Back to top



Peak Area
========

    This algorithm simply finds the area underneath a Peak object.  The first algorith ( DEFAULT ), simply sums each voltage tick.  The second ( WITH_SLOPES ) accounts for the triangles formed by connecting the voltage ticks with lines.  The error on both is set to the propogation of a 2% digitization error on each voltage value.

DEFAULT
        //  Sets where the area should begin to be summed.  If the leading edge is less
        //  than zero, it is set to zero instead ( avoiding index out of bouds exceptions).
        int startPoint = (int) Math.ceil( peak.leadingEdge );
        startPoint = ( startPoint < 0 ) ? 0 : startPoint;

        //  Loops through the entire peak.
        for( int iTick = startPoint; iTick < peak.pulse.length; iTick++ ) {

            //  Sums each voltage value.
            peak.peakArea += ( peak.pulse[iTick] - peak.baseline );

            // Assuming a 2% error on the digitisation and propogates that error.
            peak.peakAreaError += 0.0004 * ( peak.baseline - peak.pulse[iTick] ) *
                    ( peak.baseline - peak.pulse[iTick] );
        }

        //  Sets the error according to the propogation.
        peak.peakAreaError = Math.sqrt( peak.peakAreaError );
        //  Sets the value's isDone parameter to this algorithm's number.
        peak.peakAreaIsDone = 1;


WITH_SLOPES
        //  Sets where the area should begin to be summed.  If the leading edge is less
        //  than zero, it is set to zero instead ( avoiding index out of bouds exceptions).
        int startPoint = (int) Math.ceil( peak.leadingEdge );
        startPoint = ( startPoint < 0 ) ? 0 : startPoint;

        //  Loops through the entire peak.
        for( int iTick = startPoint; iTick < peak.pulse.length - 1; iTick++ ) {

            //  Sums each voltage value.
            peak.peakArea += ( peak.pulse[iTick] - peak.baseline );

            //  Also sums the triangular region between the top of two voltage values.
            peak.peakArea += ( peak.pulse[ iTick + 1 ] - peak.pulse[ iTick ] ) / 2.;

            // Assuming a 2% error on the digitisation:
            peak.peakAreaError += 0.0004 * ( peak.baseline - peak.pulse[iTick] ) *
                    ( peak.baseline - peak.pulse[iTick] );
        }

        //  Sets the error to a propogation of the 2% digitisation
        peak.peakAreaError = Math.sqrt( peak.peakAreaError );
        //  Sets the isDone parameter.
        peak.peakAreaIsDone = 1;

Back to top