You are on page 1of 26

Measurement library

(LMeasure)
Siemens
S7-PLC Industry
Online
https://support.industry.siemens.com/cs/ww/en/view/81662739 Support
Legal information

Legal information
Use of application examples
Application examples illustrate the solution of automation tasks through an interaction of several
components in the form of text, graphics and/or software modules. The application examples are
a free service by Siemens AG and/or a subsidiary of Siemens AG ("Siemens"). They are
non-binding and make no claim to completeness or functionality regarding configuration and
equipment. The application examples merely offer help with typical tasks; they do not constitute
customer-specific solutions. You yourself are responsible for the proper and safe operation of the
products in accordance with applicable regulations and must also check the function of the
respective application example and customize it for your system.
Siemens grants you the non-exclusive, non-sublicensable and non-transferable right to have the
application examples used by technically trained personnel. Any change to the application
examples is your responsibility. Sharing the application examples with third parties or copying the
application examples or excerpts thereof is permitted only in combination with your own products.
The application examples are not required to undergo the customary tests and quality inspections
of a chargeable product; they may have functional and performance defects as well as errors. It is
your responsibility to use them in such a manner that any malfunctions that may occur do not
result in property damage or injury to persons.

Disclaimer of liability
Siemens shall not assume any liability, for any legal reason whatsoever, including, without
limitation, liability for the usability, availability, completeness and freedom from defects of the
application examples as well as for related information, configuration and performance data and
any damage caused thereby. This shall not apply in cases of mandatory liability, for example
under the German Product Liability Act, or in cases of intent, gross negligence, or culpable loss of
life, bodily injury or damage to health, non-compliance with a guarantee, fraudulent
non-disclosure of a defect, or culpable breach of material contractual obligations. Claims for
damages arising from a breach of material contractual obligations shall however be limited to the
© Siemens AG 2019 All rights reserved

foreseeable damage typical of the type of agreement, unless liability arises from intent or gross
negligence or is based on loss of life, bodily injury or damage to health. The foregoing provisions
do not imply any change in the burden of proof to your detriment. You shall indemnify Siemens
against existing or future claims of third parties in this connection except where Siemens is
mandatorily liable.
By using the application examples you acknowledge that Siemens cannot be held liable for any
damage beyond the liability provisions described.

Other information
Siemens reserves the right to make changes to the application examples at any time without
notice. In case of discrepancies between the suggestions in the application examples and other
Siemens publications such as catalogs, the content of the other documentation shall have
precedence.
The Siemens terms of use (https://support.industry.siemens.com) shall also apply.

Security information
Siemens provides products and solutions with Industrial Security functions that support the secure
operation of plants, systems, machines and networks.
In order to protect plants, systems, machines and networks against cyber threats, it is necessary
to implement – and continuously maintain – a holistic, state-of-the-art industrial security concept.
Siemens’ products and solutions constitute one element of such a concept.
Customers are responsible for preventing unauthorized access to their plants, systems, machines
and networks. Such systems, machines and components should only be connected to an
enterprise network or the Internet if and to the extent such a connection is necessary and only
when appropriate security measures (e.g. firewalls and/or network segmentation) are in place.
For additional information on industrial security measures that may be implemented, please visit
https://www.siemens.com/industrialsecurity.
Siemens’ products and solutions undergo continuous development to make them more secure.
Siemens strongly recommends that product updates are applied as soon as they are available
and that the latest product versions are used. Use of product versions that are no longer
supported, and failure to apply the latest updates may increase customer’s exposure to cyber
threats.
To stay informed about product updates, subscribe to the Siemens Industrial Security RSS Feed
at: https://www.siemens.com/industrialsecurity.

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 2
Table of contents

Table of contents
Legal information ......................................................................................................... 2
1 Introduction ........................................................................................................ 4
1.1 Overview............................................................................................... 4
1.2 Components used ................................................................................ 5
2 Block description ............................................................................................... 6
2.1.1 FB LMeasure_Boxplot .......................................................................... 6
2.1.2 FB LMeasure_Histogram ................................................................... 10
2.1.3 FB/FC LMeasure_SimpleSmoothing .................................................. 14
2.1.4 FB/FC LMeasure_SmoothByPolynom ............................................... 17
2.1.5 FB/FC LMeasure_DifferenceQuotient ................................................ 20
2.1.6 FC LMeasure_RegressionLine .......................................................... 23
3 Appendix .......................................................................................................... 25
3.1 Service and support ........................................................................... 25
3.2 Links and literature ............................................................................. 26
3.3 Change documentation ...................................................................... 26
© Siemens AG 2019 All rights reserved

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 3
1 Introduction

1 Introduction
1.1 Overview
The SIMATIC S7 PLCs are also ideally suited for preprocessing measured value
data. This article provides some standard functions for statistical processing and
display of measured values.
As a rule, the raw data of the measured values read in by the periphery must be
pre-processed in order to make statements regarding the quality. Standard
functions here are statements on statistical distribution, various mean value
calculations, smoothing functions, regressions, etc.. In this article you will find
suitable functions for the S7-1200 and S7-1500 PLCs as well as visualization
components for WinCC RT Professional.
To be able to process measured values from processes already in the S7-PLC, you
receive a library for measured value processing.
This library is integrated into the "Measurement library (LMeasure)".
The following blocks are contained:
• LMeasure_Boxplot
Calculates the statistical information of the minimum, 25% quartile, median,
75% quartile and the maximum value of a series of measured values
necessary for the representation of a boxplot. In addition, the block calculates
outlier statistics and a measure for the skewness of a distribution.
© Siemens AG 2019 All rights reserved

• LMeasure_Histogram
Represents the frequency distribution of a series of measured values in the
form of a histogram according to parameterizable classes.
• LMeasure_SimpleSmoothingFC
Performs a smoothing of the measured values by calculating the linear mean
value by three points.
• LMeasure_SmoothByPolynomFB
Carries out a smoothing of the measured values by a 3rd degree
compensating polynomial.
• LMeasure_DifferenceQuotientFC
Carries out a numerical differentiation of the equidistantly sampled measured
values. This function uses a 3rd degree polynomial to smooth noisy input
signals.
• LMeasure_RegressionLine
Calculates a linear regression with the slope and the intersection point with the
Y-axis as initial parameters from a measured value data set.

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 4
1 Introduction

1.2 Components used


This application example has been created with the following hardware and
software components:
Table 1-1
Component Number Article number Note
SIMATIC STEP 7 Basic 1 6ES7822-0AA05-0YA5 Or later version
V15.1
CPU 1214C DC/DC/DC 1 6ES7214-1AG40-0XB0 You can use any CPU.

This application example consists of the following components:


Table 1-2
Component File name Note
Documentation 81662739_Measurement_Library_DOC_V11_en.pdf V1.1
Library 81662739_Measurement_Library_CODE_V11.zip V1.1
© Siemens AG 2019 All rights reserved

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 5
2 Block description

2 Block description
2.1.1 FB LMeasure_Boxplot

Short description
If you want to get an overview of existing data, you can use a Boxplot diagram. A
Boxplot shows you in which area the data is located and how it is distributed over
this area. A Boxplot consists of the following parameters:
• Minimum (smallest occurring value of the sample)
• Lower or first quartile (below this value are 25% of the sample values)
• Median or second quartile (below this value are 50% of the sample values)
• Upper or third quartile (below this value are 75% of the sample values)
• Maximum (largest occurring value of the sample)

Figure 2-1: Boxplot


MIN 2.QUARTILE / MEDIAN MAX
© Siemens AG 2019 All rights reserved

1. QUARTILE 3. QUARTILE

WinCC-Control
To visualize the Boxplot, the Siemens Industry Online Support offers you a Net-
Control, which you can use in conjunction with WinCC Runtime Professional. The
download is available at the entry ID: 81662739.
Figure 2-2: .Net Control "Boxplot"

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 6
2 Block description

Block
Figure 2-3 LMeasure_Boxplot
FB LMeasure_Boxplot
BOOL execute outlierMax REAL
Real rangeOutlier max REAL
VARIANT array (InOut) q75 REAL
median REAL
q25 REAL
min REAL
outlierMin REAL
skewness REAL
busy BOOL
done BOOL
error BOOL
status WORD
statusID UINT
© Siemens AG 2019 All rights reserved

Input parameters
Table 2-1: Input parameters
Parameters Data type Description
execute Bool Activation of the calculation with each positive edge.
rangeOutlier Real Outlier detection:
• 0: Outlier detection is deactivated
• 0-1: Invalid value
• >1: Outlier detection is activated

Output parameters
Table 2-2: Output parameters
Parameters Data type Description
outlierMax Real Upper outliers in %.
max Real Maximum Value, not an outlier.
q75 Real 3rd quartile or Q75 of the data series.
median Real 2nd quartile or Median of the data series.
q25 Real 1st quartile or Q25 of the data series.
min Real Minimum Value, not an outlier.
outlierMin Real Lower outliers in %.
skewness Real Skewness of the data series.
busy Bool Processing of the block is active.
done Bool Processing was completed successfully.
error Bool An error occurred during processing.
status Word Outputs the status code of the processing (see table).

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 7
2 Block description

Parameters Data type Description


statusID UInt Outputs the ID of the block that reports the status. See table
below.

Input/output parameters (InOut)


Table 2-3: Input/output parameters (InOut)
Parameters Data type Description
array Variant The data series that is to be used for the calculation.
The following data types are supported:
• Int
• UInt
• Real

Status and error displays


Table 2-4: Status/error codes
statusID status Meaning Remedy / notes
1 16#0000 No error Processing was completed successfully.
1 16 #7000 No error The block waits for a positive edge at the
© Siemens AG 2019 All rights reserved

"execute" input.
1 16#7001 No error First call.
1 16#7002 No error Processing is active.
1 16#9100 Wrong data type Use one of the following data types:
• Int
• UInt
• Real
1 16#9101 The parameter "rangeOutlier" Enter a valid "rangeOutlier" value for the
type is invalid. parameter.
• 0: Outlier detection is deactivated
• >1 Valid value.
1 16#9102 Do not pass an array Pass an array to the block at the InOut
parameter "array".
1 16#8xxx Error when processing the Use the TIA Portal Online Help for
statement troubleshooting.
"MOVE_BLK_VARIANT".
2 16#8xxx Error when processing the Information on this block can be found in
block "LGF_Shellsort". the "Library of General Functions" (\3\).

Principle of operation
The block sorts the data series and then calculates the so-called "five-point
summary":
Table 2-5: Five-point summary
Characteristic value of the five-point summary Output parameter of the block
Minimum (smallest occurring value of the sample) min
Lower or first quartile (below this value are 25% of q25
the sample values)

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 8
2 Block description

Characteristic value of the five-point summary Output parameter of the block


Median or second quartile (below this value are median
50% of the sample values)
Upper or third quartile (below this value are 75% of q75
the sample values)
Maximum (largest occurring value of the sample) max

If outlier detection is activated, the block first calculates the limits. From these limit
values, the values are recognized as outliers:
𝐵𝑜𝑢𝑛𝑑𝑙𝑜𝑤𝑒𝑟 = 𝑞25 − 𝑟𝑎𝑛𝑔𝑒𝑂𝑢𝑡𝑙𝑖𝑒𝑟 ∗ (𝑞75 − 𝑞25)
𝐵𝑜𝑢𝑛𝑑𝑢𝑝𝑝𝑒𝑟 = 𝑞75 + 𝑟𝑎𝑛𝑔𝑒𝑂𝑢𝑡𝑙𝑖𝑒𝑟 ∗ (𝑞75 − 𝑞25)
The block then calculates new values for the parameters "max" and "min", which lie
within the outlier limits. The outliers are counted and output as a percentage.
To make it easier to judge how the data is distributed, the block also calculates the
skewness. The skewness lies between the values -1 and 1 with the following
meaning:
• -1: extreme left-skew distribution
• 0: Symmetrical distribution
• 1: extreme right-skew distribution
The elements of the passed array are sorted in ascending order by the block. The
© Siemens AG 2019 All rights reserved

"LGF_Shellsort" blocks are used for sorting. (see \3\).

The characteristic values are calculated as follows:


Table 2-6: Boxplot formulas
Parameters Formula
1
q25 (1st quartile) {[ (𝑛+1)]+1} 𝑛+3
𝑞25 = 𝑥(𝑘) with 𝑘 = 2
=
2 4
median / q50 (2nd 𝑞50 = 𝑥([𝑛+1]/2)
quartile)
q75 (3rd quartile) 3𝑛+1
𝑞75 = 𝑥(𝑛+1−𝑘) with 𝑛 + 1 − 𝑘 =
4
n := number of samples (size of array)

If the result of the element to be determined (from which the quartiles can be
derived) is not an integer, the quartile is calculated from the linear fraction
between the two adjacent samples.
skewness (𝑞25 + 𝑞75 ) − 2 ∗ q50
skewness =
q75 − q25
Note: This is just an approximation.

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 9
2 Block description

2.1.2 FB LMeasure_Histogram

Short description
The histogram shows the frequency distribution of a sample by class.
A class describes a value interval in which the individual frequencies are added
together. After specifying the number of classes, the class width and the respective
class center are calculated. The number of classes is limited to 15.
The distribution is represented as a rectangle around the class mean with the class
width and the cumulated frequency as height.

Figure 2-4: Range

Range
9 8
8
Number of Elements

7 6
6 5 5
5
4 3
3 2
2
© Siemens AG 2019 All rights reserved

1
1
0
154,00 158,93 163,86 168,79 173,72 178,65 183,58
Middle of Classes

WinCC-Control
To visualize the histogram, Siemens Industry Online Support offers you a .net
control that you can use in conjunction with WinCC Runtime Professional. The
download is available at the entry ID: 81662739.
Figure 2-5: .Net Control

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 10
2 Block description

Block
Figure 2-6 LMeasure_Histogram
FB LMeasure_Histogram
BOOL execute busy BOOL
UINT numberOfClasses done BOOL
VARIANT array (InOut) error BOOL
status WORD
statusID UINT
Array [0..1, 0..14]
histValues of REAL
Array [0..3]
axis of REAL
classWidth REAL

Input parameters
Table 2-7: Input parameters
Parameters Data type Description
execute Bool Activation of the calculation with each positive edge.
© Siemens AG 2019 All rights reserved

NumberOfClasses UInt Number of desired classes.

Output parameters
Table 2-8: Output parameters
Parameters Data type Description
busy Bool Processing of the block is active.
done Bool Processing was completed successfully.
error Bool An error occurred during processing.
status Word Outputs the status code of the processing. (see table).
statusID UInt Outputs the ID of the block that reports the status. See table
below.
histValues Array[0..1, 0..14] Outputs the calculated values in a two-dimensional array.
of Real • HistValues[0,0..14] displays the relative frequency of the
individual classes.
• HistValues[1,0..14] displays the class centers.
If fewer than 15 classes are desired, the array elements that are
not required are output with 0.
axis Array[0..3] of Specifies the axis values:
Real • Lower X axis value
• Upper X axis value
• Lower Y axis value
• Upper Y axis value
classWidth Real Returns the calculated class width.

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 11
2 Block description

Input/output parameters (InOut)


Table 2-9: Input/output parameters (InOut)
Parameters Data type Description
array Variant The data series that is to be used for the calculation.
The following data types are supported:
• Int
• UInt
• Real

Status and error displays


Table 2-10: Status/error codes
statusID status Meaning Remedy / notes
1 16#0000 No error Processing was completed successfully.
1 16 #7000 No error The block waits for a positive edge at the "execute"
input.
1 16#7001 No error First call
1 16#7002 No error Processing is active.
1 16#9100 Wrong data type Use one of the following data types:
• Int

© Siemens AG 2019 All rights reserved

UInt
• Real
1 16#9101 Incorrect number of Give the parameter "NumberOfClasses" a valid
classes value (1 to 15).
1 16#9102 Do not pass an array Pass an array to the block at the InOut parameter
"array".
2 16#8xxx Error when processing Use the TIA Portal Online Help for troubleshooting.
the statement
"MOVE_BLK_VARIANT".
3 16#8xxx Error when processing Information on this block can be found in the
the block "Library of General Functions" (\3\).
"LGF_Shellsort".

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 12
2 Block description

Principle of operation
The block sorts the passed data and calculates the general class width using the
passed class count and data range. The block then counts the values that lie within
a class. In order to draw a histogram, the block also calculates the necessary X
and Y coordinates.
The elements of the passed array are sorted in ascending order by the block. The
"LGF_Shellsort" blocks are used for sorting (see \3\).
The number of classes can be specified using the following rule of thumb:
𝑁𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝐶𝑙𝑎𝑠𝑠𝑒𝑠 = √𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡𝑠
e.g. 100 values -> 𝑁𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝐶𝑙𝑎𝑠𝑠𝑒𝑠 = √100 = 10

Formulas
The block uses the following formula to calculate the class width:
𝑚𝑎𝑥 − 𝑚𝑖𝑛
𝑐𝑙𝑎𝑠𝑠𝑊𝑖𝑑𝑡ℎℎ =
𝑁𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝐶𝑙𝑎𝑠𝑠𝑒𝑠
© Siemens AG 2019 All rights reserved

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 13
2 Block description

2.1.3 FB/FC LMeasure_SimpleSmoothing

Short description
The simplest form of smoothing a sequence of measured values is to calculate the
linear mean value by three points.

The block is implemented as a function and as a function block.

Table 2-11: Comparison FC-FB


Function (FC) Function block (FB)
The function calculates the linear mean value The function block calculates the linear mean value
acyclically. cyclically.
The function reads an array that is smoothed. N-2 The function block reads in a value with each
smoothed measured values can be calculated from positive edge at the "insert" input. As soon as three
N measured values. Therefore, the output array in values have been read in, the block calculates a
the index (0) and index (N) contains the value 0. smoothed value and outputs it.

Function block (FB)


Figure 2-7: LMeasure_SimpleSmoothingFB
FB LMeasure_SimpleSmoothingFB
© Siemens AG 2019 All rights reserved

BOOL enable smoothedValue REAL


BOOL insert busy BOOL
REAL value done BOOL
error BOOL
status WORD

Input parameters
Table 2-12: Input parameters
Parameters Data type Description
enable Bool Activates the block. As long as "enable" is "true", the block can accept
values at the "value" parameter.
insert Bool Accepts the value at input "value" and outputs a "smoothedValue" if three
values have been read in.
value Real Value that is to be included in the smoothing.

Output parameters
Table 2-13: Output parameters
Parameters Data type Description
smoothedValue Real The smoothed values.
busy Bool Processing of the block is active.
done Bool Processing was completed successfully.
error Bool An error occurred during processing.

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 14
2 Block description

Parameters Data type Description


status Word Outputs the status code of the processing (see table).

Status and error displays


Table 2-14: Status/error codes
status Meaning Remedy / notes
16#0000 No error Processing was completed successfully.
16 #7000 No error The block waits for activation by the parameter "enable".
16#7001 No error First call.
16#7002 No error Processing is active.
16#9000 Too few values The block requires three values to calculate a smoothed value.
Pass further values with a positive edge at the "insert" input.

Function (FC)
Figure 2-8: LMeasure_SimpleSmoothingFC
FC LMeasure_SimpleSmoothingFC
ARRAY [*] of
REAL values (InOut) error BOOL
© Siemens AG 2019 All rights reserved

ARRAY [*] of
REAL smoothedValues(InOut) status WORD

Output parameters
Table 2-15: Output parameters
Parameters Data type Description
error Bool An error occurred during processing.
status Word Outputs the status code of the processing (see table).

Input/output parameters (InOut)


Table 2-16: Input/output parameters (InOut)
Parameters Data type Description
values Array[*] of Real Values that are to be included in the smoothing.
smoothedValues Array[*] of Real The smoothed values.

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 15
2 Block description

Status and error displays


Table 2-17: Status/error codes
status Meaning Remedy / notes
16#0000 No error Processing was completed successfully.
16#7001 No error First call
16#7002 No error Processing is active.
16#9000 Too few values The block requires three values to calculate a smoothed value.
Increase the size of the array at the input parameter "values".
16#9001 Unequal array The arrays "values" and "smoothedValues" must have the same
sizes size.

Principle of operation
The block calculates the smoothed values using the following formula:

𝑦(𝑛 − 1) + 𝑦(𝑛) + 𝑦(𝑛 + 1)


̅̅̅̅̅̅
𝑦(𝑛) =
3
The calculated value is output or the calculated values are output at output
"smoothedValue".
Based on this formula, the FC cannot calculate values for the elements 0 and N.
© Siemens AG 2019 All rights reserved

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 16
2 Block description

2.1.4 FB/FC LMeasure_SmoothByPolynom

Short description
For smoothing, a 3rd degree polynomial is placed through five value points. The
error squares of the distances between polynomial and real value are minimized.
The smoothed values can be determined from the polynomial parameters obtained
in this way.
The block is implemented as a function and as a function block.

Table 2-18: Comparison FC-FB


Function (FC) Function block (FB)
The function calculates the smoothed values The function block cyclically displays the smoothed
acyclically. values.
The function reads an array that is smoothed. N-4 The function block reads in a value with each
smoothed measured values can be calculated from positive edge at the "insert" input. As soon as five
N measured values. The output array contains the values have been read in, the block calculates a
value 0 in the index (0,1,N-1,N). However, smoothed value and outputs it.
replacement values can be calculated.

Function block (FB)


© Siemens AG 2019 All rights reserved

Figure 2-9: LMeasure_SmoothByPolynomFB


FB LMeasure_SmoothByPolynomFB
BOOL enable smoothedValue REAL
BOOL insert busy BOOL
REAL value done BOOL
error BOOL
status WORD

Input parameters
Table 2-19: Input parameters
Parameters Data type Description
enable Bool Activates the block. As long as "enable" is "true", the block can accept
values at the "value" parameter.
insert Bool Accepts the value at input "value" and outputs a "smoothedValue" if five
values have been read in.
value Real Value that is to be included in the smoothing.

Output parameters
Table 2-20: Output parameters
Parameters Data type Description
smoothedValue Real Smoothed value.
busy Bool Processing of the block is active.

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 17
2 Block description

Parameters Data type Description


done Bool Processing was completed successfully.
error Bool An error occurred during processing.
status Word Outputs the status code of the processing (see table).

Status and error displays


Table 2-21: Status/error codes
status Meaning Remedy / notes
16#0000 No error Processing was completed successfully.
16 #7000 No error The block waits for activation by the parameter "enable".
16#7001 No error First call
16#7002 No error Processing is active.
16#9000 Too few values The block requires five values to calculate a smoothed value.
Pass further values with a positive edge at the "insert" input.

Function (FC)
Figure 2-10: LMeasure_CompensationPolynomialFC
FC LMeasure_SmoothByPolynomFC
© Siemens AG 2019 All rights reserved

ARRAY [*] of
REAL values (InOut) error BOOL
ARRAY [*] of
REAL smoothedValues(InOut) status WORD

Output parameters
Table 2-22: Output parameters
Parameters Data type Description
error Bool An error occurred during processing.
status Word Returns the status code of the processing.

Input/output parameters (InOut)


Table 2-23: Input/output parameters (InOut)
Parameters Data type Description
values Array[*] of Real Values that are to be included in the smoothing.
smoothedValues Array[*] of Real The smoothed values.

Status and error displays


Table 2-24: Status/error codes
status Meaning Remedy / notes
16#0000 No error Processing was completed successfully.

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 18
2 Block description

status Meaning Remedy / notes


16#7001 No error First call
16#7002 No error Processing is active.
16#9000 Too few values The block requires five values to calculate a smoothed value.
Increase the size of the array at the input parameter "values".
16#9001 Unequal array The arrays "values" and "smoothedValues" must have the same
sizes size.

Principle of operation
The 3rd degree compensation polynomial is calculated as follows:
̅̅̅̅̅̅ 1
𝑦(𝑛) = ∗ (−3 ∗ 𝑦(𝑛 − 2) + 12 ∗ 𝑦(𝑛 − 1) + 17 ∗ 𝑦(𝑛) + 12 ∗ 𝑦(𝑛 + 1) − 3 ∗ 𝑦(𝑛 + 2))
35

N-4 smoothed measured values can thus be calculated from the N measured
values. The output array contains the value 0 in the index (0,1,N-1,N).
These "missing" values are calculated with the following formalisms:

̅̅̅̅̅̅̅̅̅̅̅ 1
𝑦(𝑛 − 2) = ∗ (69 ∗ 𝑦(𝑛 − 2) + 4 ∗ 𝑦(𝑛 − 1) − 6 ∗ 𝑦(𝑛) + 4 ∗ 𝑦(𝑛 + 1) − 𝑦(𝑛 + 2))
70

̅̅̅̅̅̅̅̅̅̅̅ 2
© Siemens AG 2019 All rights reserved

𝑦(𝑛 − 1) = ∗ (2 ∗ 𝑦(𝑛 − 2) + 27 ∗ 𝑦(𝑛 − 1) + 12 ∗ 𝑦(𝑛) − 8 ∗ 𝑦(𝑛 + 1) + 2 ∗ 𝑦(𝑛 + 2))


70

̅̅̅̅̅̅̅̅̅̅̅ 2
𝑦(𝑛 + 1) = ∗ (2 ∗ 𝑦(𝑛 − 2) − 8 ∗ 𝑦(𝑛 − 1) + 12 ∗ 𝑦(𝑛) + 27 ∗ 𝑦(𝑛 + 1) + 2 ∗ 𝑦(𝑛 + 2))
70

̅̅̅̅̅̅̅̅̅̅̅ 1
𝑦(𝑛 + 2) = ∗ (−𝑦(𝑛 − 2) + 4 ∗ 𝑦(𝑛 − 1) − 6 ∗ 𝑦(𝑛) + 4 ∗ 𝑦(𝑛 + 1) + 69 ∗ 𝑦(𝑛 + 2))
70

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 19
2 Block description

2.1.5 FB/FC LMeasure_DifferenceQuotient

Short description
This block numerically differentiates a signal sampled equidistantly in time. For
example, the velocity can be calculated from a measured locus curve, or the
acceleration can be calculated from the measured velocity. In order to minimize the
effects of a scattering measurement signal, this algorithm uses a compensating
polynomial.
The block is implemented as a function and as a function block.

Table 2-25: Comparison FC-FB


Function (FC) Function block (FB)
The function calculates the derivated values The function block cyclically displays the derivated
acyclically. values.
The function reads an array that is derivated. N-4 The function block reads in a value with each
derivated measured values can be calculated from positive edge at the "insert" input. As soon as five
N measured values. The output array contains the values have been read in, the block calculates a
value 0 in the index (0,1,N-1,N). However, derivated value and outputs it.
replacement values can be calculated.

Function block (FB)


© Siemens AG 2019 All rights reserved

Figure 2-11: LMeasure_DifferenceQuotientFB


FB LMeasure_DifferenceQuotientFB
BOOL enable derivatedValue REAL
BOOL insert busy BOOL
REAL value done BOOL
REAL deltaT error BOOL
status WORD

Input parameters
Table 2-26: Input parameters
Parameters Data type Description
enable Bool Activates the block. As long as "enable" is "true", the block can accept
values at the "value" parameter.
insert Bool Accepts the value at input "value" and outputs a "derivatedValue" if five
values have been read in.
value Real Value that is to be included in the differentiation.
deltaT Real Equidistant distance between two measured values. (e.g. 1s)

Output parameters
Table 2-27: Output parameters
Parameters Data type Description
derivatedValue Real Differentiated value.

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 20
2 Block description

Parameters Data type Description


busy Bool Processing of the block is active.
done Bool Processing was completed successfully.
error Bool An error occurred during processing.
status Word Outputs the status code of the processing (see table).

Status and error displays


Table 2-28: Status/error codes
status Meaning Remedy / notes
16#0000 No error Processing was completed successfully.
16 #7000 No error The block waits for activation by the parameter "enable".
16#7001 No error First call
16#7002 No error Processing is active.
16#9000 Too few values The block requires five values to calculate a differentiated value.
Pass further values with a positive edge at the "insert" input.

Function (FC)
Figure 2-12: LMeasure_DifferenceQuotientFC
© Siemens AG 2019 All rights reserved

FC LMeasure_DifferenceQuotientFC
REAL deltaT error BOOL
ARRAY [*] of
REAL values (InOut) status WORD
ARRAY [*] of
REAL derivatedValues(InOut)

Input parameters
Table 2-29: Input parameters
Parameters Data type Description
deltaT Real equidistant distance between two measured values. (e.g. 1s)

Output parameters
Table 2-30: Output parameters
Parameters Data type Description
error Bool An error occurred during processing.
status Word Outputs the status code of the processing (see table).

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 21
2 Block description

Input/output parameters (InOut)


Table 2-31: Input/output parameters (InOut)
Parameters Data type Description
values Array[*] of Real Values that are to be included in the differentiation.
derivatedValues Array[*] of Real The differentiated values.

Status and error displays


Table 2-32: Status/error codes
status Meaning Remedy / notes
16#0000 No error Processing was completed successfully.
16#7001 No error First call
16#7002 No error Processing is active.
16#9000 Too few values The block requires five values to calculate a differentiated value.
Increase the size of the array at the input parameter "values".
16#9001 Unequal array The arrays "values" and "derivatedValues" must have the same
sizes size.

Principle of operation
© Siemens AG 2019 All rights reserved

To calculate the difference quotient of a scattering signal, a third degree


compensation polynomial is first placed through the measured values. This
polynomial is then differentiated. With this method, even a distorted input signal
can be sensibly differentiated.

The difference quotient is calculated using the following formula:


1
𝑦̅̇ (𝑛) = (𝑦(𝑛 − 2) − 8𝑦(𝑛 − 1) + 8𝑦(𝑛 + 1) − 𝑦(𝑛 + 2))
12𝑑𝑒𝑙𝑡𝑎𝑇
deltaT: equidistant distance between two measured values (e.g. 1s).

The function (FC) can calculate differentiated and smoothed measured values from
N measured values N-4. The output array would be assigned 0 in the index (0,1,N-
1,N). However, the following formalisms can be used to calculate substitute values:
1
𝑦̅̇ (𝑛 − 2) = (−125𝑦(𝑛 − 2) + 136𝑦(𝑛 − 1) + 48𝑦(𝑛) − 88𝑦(𝑛 + 1)
84𝑑𝑒𝑙𝑡𝑎𝑇
+ 29𝑦(𝑛 + 2))
1
𝑦̅̇ (𝑛 − 1) = (−38𝑦(𝑛 − 2) − 2𝑦(𝑛 − 1) + 24𝑦(𝑛) + 26𝑦(𝑛 + 1)
84𝑑𝑒𝑙𝑡𝑎𝑇
− 10𝑦(𝑛 + 2))
1
𝑦̅̇ (𝑛 + 1) = (10𝑦(𝑛 − 2) − 26𝑦(𝑛 − 1) − 24𝑦(𝑛) + 2𝑦(𝑛 + 1) + 38𝑦(𝑛 + 2))
84𝑑𝑒𝑙𝑡𝑎𝑇
1
𝑦̅̇ (𝑛 + 2) = (−29𝑦(𝑛 − 2) + 88𝑦(𝑛 − 1) − 48𝑦(𝑛) − 136𝑦(𝑛 + 1)
84𝑑𝑒𝑙𝑡𝑎𝑇
+ 125𝑦(𝑛 + 2))
deltaT: equidistant distance between two measured values (e.g. 1s).

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 22
2 Block description

2.1.6 FC LMeasure_RegressionLine

Short description
The simplest case of a regression is the regression line. This means that the
assumed relationship between input and output signal is a linear straight line.

Figure 2-13: Regressionline


© Siemens AG 2019 All rights reserved

Block
Figure 2-14: LMeasure_RegressionLine
FC LMeasure_RegressionLine
ARRAY [*, *] of
LMeasure_typeRegressionLine values (InOut) slope REAL
intercept REAL
busy BOOL
done BOOL
error BOOL
status WORD

Input parameters
Table 2-33: Input parameters
Parameters Data type Description
values Array[*] of The data points are passed with their X- and Y-values.
LMeasure_typeRegressionLine The data type "LMeasure_typeRegressionLine" has
the following structure:
• x (Real)
• y (Real)

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 23
2 Block description

Output parameters
Table 2-34: Output parameters
Parameters Data type Description
slope LReal Gradient of straight line
intercept LReal The intersection with the Y axis
error Bool An error occurred during processing.
status Word Outputs the status code of the processing (see table).

Status and error displays


Table 2-35: Status/error codes
status Meaning Remedy / notes
16#0000 No error Processing was completed successfully.
16#9000 Too few values The block requires at least two pairs of values to calculate a
regression line. Increase the size of the array at the input
parameter "values" in the second dimension.

Principle of operation
The block calculates the regression line with the following line equation:
© Siemens AG 2019 All rights reserved

𝑓(𝑥) = 𝑎 + 𝑏 ∗ 𝑥
b: Gradient of straight line
a: Intersection with y-axis

The gradient b is calculated using the following equation:


𝑛 ∗ ∑𝑁 𝑁 𝑁
1 (𝑥(𝑛) ∗ 𝑦(𝑛)) − (∑1 𝑥(𝑛) ∗ ∑1 𝑦(𝑛))
𝑏=
𝑛 ∗ ∑𝑁 𝑁 𝑁
1 𝑥 (𝑛) − (∑1 𝑥(𝑛) ∗ ∑1 𝑥(𝑛))
2

The intersection with the Y axis is calculated using the following equation:
∑𝑁
1 𝑦(𝑛) ∑𝑁
1 𝑥(𝑛)
𝑎= −𝑏∗
𝑁 𝑁

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 24
3 Appendix

3 Appendix
3.1 Service and support
Industry Online Support
Do you have any questions or need assistance?
Siemens Industry Online Support offers round the clock access to our entire
service and support know-how and portfolio.
The Industry Online Support is the central address for information about our
products, solutions and services.
Product information, manuals, downloads, FAQs, application examples and videos
– all information is accessible with just a few mouse clicks:
support.industry.siemens.com

Technical Support
The Technical Support of Siemens Industry provides you fast and competent
support regarding all technical queries with numerous tailor-made offers
– ranging from basic support to individual support contracts. Please send queries
to Technical Support via Web form:
www.siemens.com/industry/supportrequest
© Siemens AG 2019 All rights reserved

SITRAIN – Training for Industry


We support you with our globally available training courses for industry with
practical experience, innovative learning methods and a concept that’s tailored to
the customer’s specific needs.
For more information on our offered trainings and courses, as well as their
locations and dates, refer to our web page:
www.siemens.com/sitrain

Service offer
Our range of services includes the following:
• Plant data services
• Spare parts services
• Repair services
• On-site and maintenance services
• Retrofitting and modernization services
• Service programs and contracts
You can find detailed information on our range of services in the service catalog
web page:
support.industry.siemens.com/cs/sc

Industry Online Support app


You will receive optimum support wherever you are with the "Siemens Industry
Online Support" app. The app is available for Apple iOS, Android and Windows
Phone:
support.industry.siemens.com/cs/ww/en/sc/2067

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 25
3 Appendix

3.2 Links and literature


Table 3-1
No. Topic
\1\ Siemens Industry Online Support
https://support.industry.siemens.com
\2\ Link to this entry page of this application example
https://support.industry.siemens.com/cs/ww/en/view/81662739
\3\ Library of general functions (LGF) for STEP 7 (TIA Portal) and S7-1200 / S7-1500
https://support.industry.siemens.com/cs/ww/en/view/109479728

3.3 Change documentation


Table 3-2
Version Date Modifications
V1.0 01/2019 First version
V1.1 06/2019 Bugfixing of LMeasure_DifferenceQuotientFB
© Siemens AG 2019 All rights reserved

Measurement library
Entry-ID: 81662739, V1.1, 06/2019 26

You might also like