FB_ALY_CorrelationFunctionReference

The Correlation Function Reference function block calculates the discrete correlation function between a recorded signal (referred to below as reference signal), which is read from a tcab file, and one or more input signals (Channel 00, ..., Channel 0n).

The correlation coefficients are calculated for time shifts of m cycles between the two signals, with the maximum and minimum values for m being limited by the parameters Minimum Lag (negative integer) and Maximum Lag (positive integer).

The Step Size parameter determines the number of cycles by which the signals are moved to calculate two consecutive correlation coefficients. I.e. m is always a multiple of the Step Size. Accordingly, only multiples of the Step Size are also allowed for Minimum Lag and Maximum Lag. If the Step Size is set to one, Minimum Lag to -6 and Maximum Lag to +4, for example, coefficients are calculated for shifts by -6, -5, -4, -3, -2, -1, 0, +1, +2, +3, and +4 cycles. If the Step Size is set to two, coefficients are calculated for shifts by -6, -4, -2, 0, +2, and +4 cycles.

From the start of the analysis, a value is processed from the read-in signal per cycle. When the end of the file is reached, the process starts again with the first value of the file. The read-in signal is therefore assumed to be periodic. If you only want to correlate certain periods of the input signal with the reference signal, you can control this via the Enable Execution and Reset inputs as well as via the New Result output.

The correlation coefficients can be calculated over different timeframes. which are set by the Window Mode parameter. In Continuous mode, all values since the beginning of the analysis are included in the calculations. In SlidingWindow mode, the calculation runs continuously for the last number of cycles set by the Window Size. In FixWindow mode, the calculation is done over the length of the reference signal and the output values are always updated at the end of the recorded sequence.

If m is not zero, the window for the corresponding signal shifts by m cycles. The number of values included in the calculation of the coefficients is the same for all values of m. The only exceptions to this are the results from the first cycles after the start of the analysis. If the number of elapsed cycles is less than |m|, correspondingly fewer values are included in the calculation.

For positive values of m, the values of the reference signal (Channel Ref) are stored in a ring memory, so that the respective value of the reference signal received before m cycles can be compared with the current values from Channel 00 to Channel 0n. This corresponds to a shift of the reference signal into the past. For negative values of m, the reference signal would accordingly have to be moved into the future. Since this is obviously not possible, the second signal (Channel 00, .. Channel0n) is saved and moved backward instead.

The correlation coefficients can be calculated according to different calculation rules. This is determined by the Correlation Mode. In the Base and Normed modes, the coefficients are calculated analogously to the definition from signal processing, which calculates the correlation over the convolution. In Normed mode, the coefficients are also divided by the number of summands. Covariance and CovarianceBessel calculate the covariance without and with Bessel correction. Pearson mode uses the definition of the Pearson correlation coefficient commonly used in statistics. The exact calculation rules are mathematically listed in the configuration options. Here, xn denotes the value of the reference signal and yn denotes the value of the input signal (in each case Channel00, ..., Channel0n) at the timestamp tn (corresponding to the nth cycle since the start of the analysis or since reset, except for the cycles in which Enable Execution = FALSE). The value of N depends on the WindowMode you select. For SlidingWindow mode and FixWindow mode, N is equal to the WindowSize, provided a corresponding number of cycles have already elapsed since the start of the analysis, so that xn-N-m (or yn-N+m) has been recorded, otherwise N will be reduced to n-m+1 or n+m+1 respectively. In FixWindow mode, the window size is not to be set manually, but corresponds to the length of the read-in signal section (reference signal) and the output values are updated at the end of the signal section. In Continuous mode, N = n+1 always applies.

The illustration shows the different input and output values of the function block for a configuration (Correlation Mode = Pearson, Window Mode = FixWindow, Window Size = 120 (= number of values in the file), Step Size = 5, Maximum Lag = 20, Minimum Lag = -20) and an input channel. On the left side, the input signals Channel 00 and Reset are shown in the two lower plots, above which the read-in sequence is shown on the same timeline, according to its processing. The signal starts to be read in at the beginning of the analysis. If the last value from the file is processed in the 120th (or 240th) cycle, the process starts again with the first one. In the 300th cycle, a reset is performed and the process starts again with the first value of the file. In addition, all values are deleted from the internal memories and the calculation of the coefficients begins again. For example, it was detected here that Channel 00 did not contain valid values in the previous cycles and the vibration has now been re-energized. In this area the analysis could also be interrupted by Enable Execution = FALSE. Since WindowMode = FixWindow, the outputs are updated only in the 120th, 240th and 420th cycle. The corresponding coefficients (from Output00) are shown in the right plot. From the value pairs (Maximizing Lag, Maximum Coefficient) you can read how much the input signal is shifted with respect to the reference signal. For example, in the 420th cycle (Maximizing Lag, Maximum Coefficient) = (-10.1). This means that if the Reset had taken place 10 cycles earlier, the reference signal and Channel 00 would have matched each other exactly in this window.

FB_ALY_CorrelationFunctionReference 1:

Syntax

Definition:

FUNCTION_BLOCK FB_ALY_CorrelationFunctionReference 
VAR_OUTPUT
    ipResultMessage: Tc3_EventLogger.I_TcMessage;
    bError: BOOL;
    bNewResult: BOOL;
    bConfigured: BOOL;
    bBusy: BOOL;
    eState: E_ALY_ReadState;
    fValueRead: LREAL;
    stFileHeader: ST_ALY_FileHeader;
END_VAR

FB_ALY_CorrelationFunctionReference 2: Outputs

Name

Type

Description

ipResultMessage

I_TcMessage

Contains more detailed information on the current return value. This special interface pointer is internally secured so that it is always valid/assigned.

bError

BOOL

This output is TRUE if an error occurs.

bNewResult

BOOL

When a new result has been calculated, the output is TRUE.

bConfigured

BOOL

Displays TRUE when the function block is successfully configured.

bBusy

BOOL

TRUE if the FB is active due to file accesses.

eState

E_ALY_ReadState

Current state of the FB due to asynchronous file access.

fValueRead

LREAL

Value of the data point that is read from a file.

stFileHeader

ST_ALY_FileHeader

Header information of the file that was read.

FB_ALY_CorrelationFunctionReference 3:Methods

Name

Definition location

Description

Call()

Local

Method for calculating the outputs for a specific configuration.

Configure()

Local

General configuration of the algorithm with its parameterized conditions.

FB_init()

Local

Initializes the number of input channels.

Reset()

Local

Resets all internal states or the calculations performed so far.

SetChannelValue()

Local

Method for passing values to the algorithm.

GetChannelOutputArray()

Local

Retrieves the result array for a specific channel without adding new values.

GetChannelOutputValues()

Local

Retrieves the results (individual values) for a specific channel without adding new values.

SwitchState_Idle()

Local

Initiating the change from state Read to state Idle. See state diagram.

SwitchState_Read()

Local

Initiating the change from state Idle to state Read. See state diagram.

UpdateState()

Local

Updating the state after a state change has been initiated and while the target state has not been reached.

GetBusyState()

Local

Returns TRUE if the FB is active.

State diagram

Due to the asynchronous file access in real-time applications, this function block requires a state machine to prepare and complete the file access.

At startup the function block is in Idle state. To read the data from the file, the state must switch to Read. Therefore, the method SwitchState_Read() must be called once to set the function block to state PendingRead. Then the method UpdateState() must be called until the function block is in state Read. In this state the function block can run through one or more cycles. If the function block is not to compare any further cycles, it can be returned to the Idle state. To initiate the state switch, the method SwitchState_Idle() must be called. Then the method UpdateState() must be called until the function block is in state Idle.

State diagram for the data read operation:

FB_ALY_CorrelationFunctionReference 4:

Sample

VAR
    fbCorrFctRef : FB_ALY_CorrelationFunctionReference(
        nNumChannels := 1,
        nMinLag := 0,
        nMaxLag := 10,
        nStepSize := 1,
        eWindowMode := E_ALY_WindowMode.FixWindow,
        eCorrelationMode := E_ALY_CorrelationMode.Normed,
        nWindowSize := 120,
        nSegmentSize := 400,
        tTimeout := TIME#5S,
        sFilePath := 'C:\TwinCAT\3.1\Boot\UnitTest_CorrelationFunction.tas');

    // State
    eState : E_ALY_ReadState := E_ALY_ReadState.Idle;
    bRead : BOOL;

    // Inputs
    fInputCh1 : LREAL;

    // Results
    fMinCoef : LREAL;
    fMaxCoef : LREAL;
    nMinimizingLag : DINT;
    nMaximizingLag : DINT;
    aCoefficients : ARRAY[-10..10] OF LREAL;
END_VAR
// Configure algorithm
IF bConfigure THEN
    bConfigure := FALSE;

    fbCorrFctRef.Configure(eCorrelationMode := eCorrelationMode, sFilePath := sFilePath );
END_IF

// Call algorithm
eState := fbCorrFctRef.eState;
CASE eState OF
E_ALY_ReadState.Idle:
    IF bRead THEN
        fbCorrFctRef.SwitchState_Read();
        fbCorrFctRef.UpdateState();
    END_IF
E_ALY_ReadState.Read:
    fbCorrFctRef.SetChannelValue(1, fInputCh1);
    fbCorrFctRef.Call();
    fbCorrFctRef.GetChannelOutputArray(1, ADR(aCoefficients), SIZEOF(aCoefficients));
    fbCorrFctRef.GetChannelOutputValues(1, fMinCoef, fMaxCoef, nMinimizingLag, nMaximizingLag);
    IF NOT bRead THEN
        fbCorrFctRef.SwitchState_Idle();
        fbCorrFctRef.UpdateState();
    END_IF    
E_ALY_ReadState.Pending,
E_ALY_ReadState.PendingIdle,
E_ALY_ReadState.PendingRead:
    fbCorrFctRef.UpdateState();
    eState := fbCorrFctRef.eState;
END_CASE

Requirements

Development environment

Target platform

Plc libraries to include

TwinCAT v3.1.4024.0

PC or CX (x64, x86)

Tc3_Analytics