FB_ALY_DynamicTimeWarping

The Dynamic Time Warping algorithm compares input data with previously recorded templates. The special feature of the algorithm is that signals with different speeds or even shifted signals can be compared. As a result, the distance between the input signal and the respective template is output. The smaller the distance, the more equal the compared signals are. If the distance is 0, both signals are identical. The amount of distance depends on the equality but also on the length of the signals.

The comparison starts when the signal of the Start Period flag is TRUE. A result is output if the signal of the Stop Period flag is TRUE or the Start Period flag is TRUE again.

It is recommended not to use Dynamic time Warping simultaneously with Time Based Teach Path 1Ch due to concurrent file access. Instead, a reference signal should first be taught in with the Time Based Teach Path 1Ch and only then should the evaluation be carried out with the aid of the Dynamic time Warping. The templates contain reference signals previously recorded with the Time Based Teach Path 1Ch. As a rule, the templates are a few hundred supporting points. Therefore, a reduction of the data with the function block Downsampling 1Ch is often useful.

Syntax

Definition:

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

FB_ALY_DynamicTimeWarping 1: 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 a file access.

eState

E_ALY_ReadState

Current state of the FB due to asynchronous file accesses.

bExecutingCompare

BOOL

TRUE, if the algorithm processes the envelope, otherwise FALSE. The envelope process begins when the flag is bStartPeriod=TRUE.

nBestMatchIdx

UDINT

Outputs the index of the template with the smallest distance to the input channel.

stFileHeader

ST_ALY_FileHeader

Header information of the last read file.

FB_ALY_DynamicTimeWarping 2: 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.

ConfigureChannel()

Local

Configures the file paths of the templates.

FB_init()

Local

Initializes the number of templates.

GetBusyState()

Local

This method returns the Busy state of the function block.

GetChannelOutputVaue()

Local

Method for getting individual output values from the output array

GetChannelOutputArray()

Local

Method for getting the entire output array.

Reset()

Local

Resets all internal states or the calculations performed so far.

SetChannelValue()

Local

Method for passing values to the algorithm.

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.

State diagram

Due to the asynchronous file access in real time applications this function block needs a state machine to prepare and finish file access.

At startup the function block is in Idle state. To compare the incoming data with data from file it has to be switched to state Read. Therefore the method SwitchState_Read() has to be called once to set the function block to the PendingRead state. After that the method UpdateState() has to be called until the function block is in state Read. In this state one or multiple comparison cycles can be proceeded. If the function block should not compare additional cycles it can be set to Idle state again. To initiate the state switch the method SwitchState_Idle() has to be called. After that the method UpdateState() has to be called until the function block is in state Idle.

State diagram for the read procedure of the data:

FB_ALY_DynamicTimeWarping 3:

Sample

VAR
    fbDynamicTimeWarping : FB_ALY_DynamicTimeWarping(nNumChannels := 3);
    tTimeout : TIME := T#5S;
    sFilePath1 : STRING := 'C:\TwinCAT\3.1\Boot\Template1.tas';
    sFilePath2 : STRING := 'C:\TwinCAT\3.1\Boot\Template2.tas';
    sFilePath3 : STRING := 'C:\TwinCAT\3.1\Boot\Template3.tas';
    
    bConfigure : BOOL := TRUE;
    eState : E_ALY_ReadState := E_ALY_ReadState.Idle;    
    bRead : BOOL;
    fInput : LREAL;
    bStartPeriod : BOOL;
    bStopPeriod : BOOL;
    
    aDistances : ARRAY[1..3] OF LREAL;
END_VAR
// Configure algorithm
IF bConfigure THEN
    bConfigure := FALSE;
    fbDynamicTimeWarping.ConfigureChannel(1, sFilePath1);
    fbDynamicTimeWarping.ConfigureChannel(2, sFilePath2);
    fbDynamicTimeWarping.ConfigureChannel(3, sFilePath3);
    fbDynamicTimeWarping.Configure(tTimeout);    
END_IF

// Call algorithm
eState := fbDynamicTimeWarping.eState;
CASE eState OF
E_ALY_ReadState.Idle:
    IF bRead THEN
        fbDynamicTimeWarping.SwitchState_Read();
        fbDynamicTimeWarping.UpdateState();
    END_IF
E_ALY_ReadState.Read:
    fbDynamicTimeWarping.SetChannelValue(fInput);
    fbDynamicTimeWarping.Call(bStartPeriod:=bStartPeriod, bStopPeriod:=bStopPeriod);
    
    IF NOT bRead THEN
        fbDynamicTimeWarping.SwitchState_Idle();
        fbDynamicTimeWarping.UpdateState();
    END_IF    
E_ALY_ReadState.Pending,
E_ALY_ReadState.PendingIdle,
E_ALY_ReadState.PendingRead:
    fbDynamicTimeWarping.UpdateState();
    eState := fbDynamicTimeWarping.eState;
END_CASE

// Get results
fbDynamicTimeWarping.GetOutputArray(pArrayOut:=ADR(aDistances), nArrayOutSize:=SIZEOF(aDistances));

Requirements

Development environment

Target platform

Plc libraries to include

TwinCAT v3.1.4024.0

PC or CX (x64, x86)

Tc3_Analytics