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
Outputs
Name | Type | Description |
---|---|---|
ipResultMessage | 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 |
bNewResult | BOOL | When a new result has been calculated, the output is |
bConfigured | BOOL | Displays |
bBusy | BOOL |
|
eState | Current state of the FB due to asynchronous file accesses. | |
bExecutingCompare | BOOL |
|
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. |
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:](Images/png/6842566411__Web.png)
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 |