Linear Interval Approximation for Smart Sensors and IoT Devices

In this work, we introduce and use an innovative approach for adaptive piecewise linear interval approximation of sensor characteristics, which are differentiable functions. The aim is to obtain a discreet type of inverse sensor characteristic, with a predefined maximum approximation error, with minimization of the number of points defining the characteristic, which in turn is related to the possibilities for using microcontrollers with limited energy and memory resources. In this context, the results from the study indicate that to overcome the problems arising from the resource constraints of smart devices, appropriate “lightweight” algorithms are needed that allow efficient connectivity and intelligent management of the measurement processes. The method has two benefits: first, low-cost microcontrollers could be used for hardware implementation of the industrial sensor devices; second, the optimal subdivision of the measurement range reduces the space in the memory of the microcontroller necessary for storage of the parameters of the linearized characteristic. Although the discussed computational examples are aimed at building adaptive approximations for temperature sensors, the algorithm can easily be extended to many other sensor types and can improve the performance of resource-constrained devices. For prescribed maximum approximation error, the inverse sensor characteristic is found directly in the linearized form. Further advantages of the proposed approach are: (i) the maximum error under linearization of the inverse sensor characteristic at all intervals, except in the general case of the last one, is the same; (ii) the approach allows non-uniform distribution of maximum approximation error, i.e., different maximum approximation errors could be assigned to particular intervals; (iii) the approach allows the application to the general type of differentiable sensor characteristics with piecewise concave/convex properties.


Resource-Constrained Smart Sensor Devices-Definitions
In recent years, there has been a strong increase in the interest of scientists in smart, energy-saving sensor technologies. Many promising developments have been made in both sensor production and wireless communications. The implementation of complex sensor systems is already possible due to the high degree of miniaturization of the series of classical measurement methods. Research in the field of smart sensors and the Internet of Things (IoT) has been growing. Unfortunately, there is still a lack of clarity in the terminology used in the specialized literature [1].

Smart Sensors
It is often considered that the term 'smart sensors' was created along with the term IoT. However, IEEE introduced a definition of smart transducers (sensors and actuators) in

Basic Resources of a Typical Smart Sensor/IoT Device and Their Limitations
Despite the nuances and differences in the definitions and characteristics of various devices, their main distinguishing feature is their limited resources.
A typical autonomously powered smart sensor/IoT device includes two basic groups of resources. Hardware resources: data storage, processing, and communication resources, as well as power supply. Software resources: operating systems, system software, preloaded applications, and lightweight services. The optimal distribution of these resources is crucial in most applications [5,6].

Limitations Based on Hardware
(1) Energy and computational constraints: Usually, IoT devices are battery-powered and typically use low-power processors. Therefore, computationally extensive algorithms that require significant resources cannot be transferred directly to such devices.
(2) Memory constraints: IoT devices are made with limited RAM and Flash memory compared to conventional digital systems (e.g., desktop computers, laptops, etc.). They usually use mobile lightweight software tools and operating systems. Therefore, calculation schemes require approaches for efficient memory use. However, the traditional computational algorithms are not specifically designed for this purpose and that is why such algorithms cannot be used directly for IoT devices.

Limitations Based on Software
(1) Firmware constraints: The sensor-IoT operating systems that are embedded in the devices have thin stacks of network protocols. (2) Flexibility: Remote reprogramming may not always be possible for devices, as the operating system may not be able to receive and integrate a new code.

Sensor Transfer Functions
A sensor device generates an output signal in response to a specific measurand/stimulus (input) by activating one or more physical effects (ergo, Seebeck's effect, piezoresistive effect, etc.). The output from a sensor is usually an electrical quantity (e.g., voltage, current, frequency), denoted here by y, while the input, x, is some physical quantity of interest (e.g., temperature, chemical concentration, pressure). If the input-output function is time-invariant, it is commonly called a static transfer function/characteristic or simply a transfer function.
The sensors are parts of a measuring system. The main function of these systems is to derive the unknown value x from the measured value y. Thus, the measurement system shall employ an inverse sensor transfer function x = x(y) to obtain (calculate) the value of the stimulus x [7].
In the context of the system (control) theory, the dependence y = y(x) represents the nonlinear scalar-valued characteristic of the sensor while its inverse x = x(y) represents the inverse transfer function of the sensor (Figure 1a).

Approaches for Sensor Characteristics Linearization
Linearization is a major step in sensor signal processing. It can be considered as a nonlinearity correction between the output signal of the sensor and the associated measured quantity. The nonlinearity of the sensor can be reduced by using linearization schemes or a linearization algorithm [8]. Linearization methods can be grouped into three main classes: • analog hardware linearization circuits; • software-based algorithms for linearization; • analog-digital mixed approaches [9].
An analog hardware method of linearization is performed by connecting a nonlinear analog circuit between the sensor and the ADC [10].
Software-based methods require the use of PCs or DSPs with significant computing resources [11]. Using these methods on low-cost controllers that support only simple mathematical operations is a major challenge due to the limited resources of the controller. Various software-based linearization methods are discussed in the literature. The most applied method is linearization based on the traditional look-up table (LUT), which can be implemented on any microcontroller [12]. In this method, the result of digital processing is used as an index in an array that stores the corrected data points [9,13].
The linearized sensor characteristics can greatly simplify the design, calibration, and measurement accuracy. This work elaborates further on the method presented in [14], used for the adaptive linear approximation of sensor characteristics.
In general, a scalar-valued inverse sensor transfer function is mathematically represented by a nonlinear regression model (e.g., exponential, power-law, or polynomial) constructed through least-squares error minimization (fit) of a statistically representative experimental data set. The identification of the inverse transfer function is often complicated and unreliable due to uncertainty in the choice of the analytic form of the function and reductions in its parametrization. This of course leads to unwanted errors in the calibrated sensor response and hence should be avoided [15].
A way to avoid the uncertainty issues emerging from nonlinear regression-based identification of the inverse sensor response is provided by piecewise linearization (segmentation) of its transfer function. The latter is essentially a (simplified) polygonal approximation of x = x(y) with algorithmic control of the approximation error ( Figure 1b).

Piecewise Linear Approximation of Sensor Transfer Functions
Analog methods for linear approximation are still popular in engineering practice. However, thanks to advances in digital technology, the implementation of software methods can be completed easily, fast, at a low cost, and with a guarantee of high accuracy. Therefore, digital methods combined with software technologies solve linearization problems with better flexibility and efficiency.
Over the last two decades, different piecewise linear approximation (PLA) techniques have been proposed for the representation of sensor transfer functions. These techniques are reported in various application areas of sensor technology, such as data mining [16], data collection in wireless sensor networks [17,18], and activity recognition applications with wearable sensor nodes [19][20][21].
PLA algorithms for data volume reduction by approximating the signals collected in wireless sensor networks (WSN) are presented in [16,17]. These algorithms use buffers, and the calculation time depends on the length of the buffer. In [22], a fast alternative for time series segmentation is considered, in which the reduction in the calculation time significantly increases at the expense of the used memory. This is not an appropriate approach for resource-constrained architectures such as mobile sensor nodes. In contrast, the proposed approach has constant computation and memory complexity.
The SWAB (Sliding Window and Bottom-Up) algorithm is proposed in [16]. The approach is a combination of two steps, the first of which is offline segmentation. Based on this segmentation, in the second step, an online algorithm is created. An optimization of this algorithm, called mSWAB, is presented in [19]. A further improved implementation (called emSWAB) for limited resources, especially suitable for mobile sensor systems is presented in [20]. These approaches are based on linear interpolation, while the approach proposed in the paper is based on linear regression and can also be performed in offline and online modes.
In [23] another segmentation approach was introduced, based on polynomial leastsquares approximation with polynomials of arbitrary order. From the presented experiments with this approach when first-order polynomials are used, results like the Sliding Window (SW) method are obtained in terms of the approximation error and the degree of data compression.
Another buffer-based PLA algorithm is presented in [24]. The proposed approach is a combination of either connected or disconnected segments. In contrast, the proposed algorithm for approximating the sensor characteristics uses connected segments, which is a more appropriate solution to be implemented in architectures with constraint memory and processing resources.
In addition to providing a reliable representation of transfer function, PLA approaches have two more attractive features: a.
They create a simple and fast-to-identify characteristic shape of the transfer function (see [19,21,25]) that could be used for just-in-time classification purposes in large-scale control systems. b.
They require a reduced amount of memory for data storage of the signal from the sensor device and hence possess significant economic potential for realization in smart sensors with reliable, low-cost microcontrollers. This is a crucial issue for two application domains: data mining when huge amounts of sensor data should be preprocessed under the constraint of short system-response times; and acquisition followed by wireless transmission of long-term sensor data.

Main Error Components of the Smart Sensor/IoT Device
As already mentioned, the goal of this study is to limit the memory footprint without violating system accuracy requirements. The basic structure of the smart sensor and its main components are shown in Figure 2. To achieve the required accuracy levels for a smart sensor, the main components of the compound must be considered. They can be divided into error components from the analog part δ AS and components from the digital part δ DP . Details are given in [26]. Here δ AS -relative compound analog sensor error; δ DP -relative compound digital processing error caused by the digital domain signal; δ APR = ∆ x x FS -relative approximation error; ∆ x -predefined absolute approximation error; x FS -full scale of microcontroller output value x*; δ ADC -relative analog-digital conversion error. Based on the considered serial structure, the total system error budget can be determined as: The main components of the digital processing error δ DP can also be attributed to δ ADC and δ APR . When an analog sensor and ADC are selected, the error levels δ AS and δ ADC cannot be affected. High-resolution ADCs are most often used in smart sensors and IoT implementation and therefore the δ ADC the component is negligibly small which proves δ APR to be a key component through which the total error levels can be managed, which will be discussed below.
To achieve typical goals in system design, where δ SS ≈ δ AS , the aim is to have: Published look-up tables, which are used in several applications, are often limited in the number of digits and this can be an additional source of rounding errors [26].

Proposed Approach
The proposed approach can be used for piecewise adaptive linearization of piecewise convex/concave sensor characteristics. As the name suggests, a typical piecewise convex/concave sensor response is characterized by a function consisting of a finite number of linked convex/concave segments with a strictly monotonous slope and hence a constant sign of the second derivative. Within the full measurement range of the sensor, the bounds of these segments can be identified from the condition of removing the second derivative, i.e., as the inflection points of the sensor characteristics.
The specificity of the proposed approach in comparison with the approach presented in [11] is illustrated in Figure 3. An example of such a sensor characteristic is the relationship between resistance and temperature in the platinum temperature sensors set by the Callendar-van Dusen equation.
In [11,27,28], a linearization approach to sensor characteristics was developed in cases where the characteristic is a differentiable function. Its inverse feature is determined in a discrete form (freeform) based on an iterative algorithm with defined sensor characteristics and maximum approximation error. After that, based on the developed adaptive approach with a defined maximum approximation error, the discrete inverse sensor characteristic is replaced by a new discrete characteristic (also freeform), aimed at minimizing the number of points defining the characteristic, which in turn is related to the possibilities of using cheaper microcontrollers.
With the specified sensor, the transfer characteristic is of the type y = y(x) and the first task is to determine the inverse transfer sensor characteristic x = x(y). In cases where the inverse function cannot be obtained in an explicit form, due to the nonlinear relation between y(x) and x, using a discrete linear change of x, the corresponding values of y(x) are obtained, which upon receipt of the inverse function results in a discrete relationship between y(x) and x, but with a nonlinear discrete change of argument s. This problem can be solved by the splitting method [3], the Newton method [29], as well as by the iterative algorithm proposed in [11,30]. As a result, given a maximum approximation error, the inverse sensor characteristic is obtained in a discrete form x i = x i (y i ), i = 1, n, with a uniform changing step of the argument s.
The second task is related to the approximation of the obtained discrete inverse sensor characteristic in the form of x i = x i (y i ), i = 1, n, a new characteristic x j = x j y j , j = 1, k with a specified maximum approximation error and minimization of k. One approach for solving the problem was developed in [2,11].
The purpose of the present work is to develop a new approach for solving the task of linearization of sensor characteristics (differentiable functions with a non-changing sign of curvature), which simultaneously solves the tasks of finding the inverse sensor characteristic and its linearization.

The Analytical Framework of the Proposed Approach for Linearization of Sensor Characteristics
Let a differentiable sensor characteristic of the type y = y(x), x ∈ x A i , x B , i = 1, n − 1, be given. For each such sensor, the characteristic is fulfilled y (x) = 0, as the function y = y(x) is strictly monotonous (y (x) > 0 or y (x) < 0).
In the case considered here, if the sign of curvature remains unchanged, one of the following two conditions should be satisfied: sign(y (x)) > 0 or sign(y (x)) < 0.
Under linear approximation of the function within the specified range, the error ∆ where is the slope of the straight line A i B (Figure 4). From the necessary condition for extremum, [∆ y i x E i ] = 0 the following is obtained: as the condition is also sufficient, y (x) = 0. The extremum always exists (Role Theorem [31]-∆ y i x A i = ∆ y i (x B ) = 0), as at the same time it is unique-the function (y (x) − k i ) is monotonous and has different signs at the ends of the range.
With Equation (1), when x = x E i , the extremum value is obtained The extremum of the error ∆ x i (y) at a linear approximation of the inverse function x(y) in the given range is determined by In the context of this work, of practical interest is the following problem case-the maximum approximation error ∆ x is specified, the absolute value of which is less than the absolute value of the defined by Equation (3) error in the given range x A i , x B . The goal is to find a subrange From Equations (2) and (3) it follows that The last equation defines x E i , then the right end x A i+1 of the range is determined by the equation

Essence of the Proposed Approach for Linearization of Sensor Characteristics
Let a differentiable sensor characteristic in the form y = y(x), x ∈ x A i , x B , i = 1, n − 1 be given as the sign of curvature does not change (sign(y (x)) > 0 or sign(y (x)) < 0). The aim is to obtain the inverse sensor characteristic x(y) in the linearized form at a specified maximum approximation error ∆ x .
The approach is realized in the following way. With Equation (3) the extremum value of the ∆ x 1 in the range x ∈ x A 1 , x B is determined. If |∆ x 1 | < |∆ x | then the function is approximated throughout the range with segment A 1 B and the procedure ends. In the opposite case with Equations (4) and (5), the subrange Then, through Equation (3), the extremum value of ∆ x 2 in the range x ∈ x A 2 , x B is determined. If |∆ x 2 | < |∆ x |, then the function is approximated throughout the range with the segment A 2 B and the procedure ends, or otherwise through Equations (4) and (5)

The inverse sensor characteristic x(y) is approximated by the broken line
Generally, in all intervals, except for the last one, the maximum approximation errors are equal and equal to the maximum specified approximation error ∆ x . The error ∆ x i (x) in each of the intervals is determined by the following equation: Considering the next equation and Equation (7) the error ∆ x i (y) can be found. Illustrative example: At a specified maximum approximation error ∆ x , find the linearized inverse characteristic of the characteristic y( Then y(x) = 2 + 0.02(3 − 2) 2 = 2.02, and y (x) = 0.04(x − 2), y (x) = 0.04. So y x A i = 2 + 0.02.
From (4) follows The first of the two solutions 2.171572875 does not belong to the considered interval (2.171572875 < x A i = 3). Only the second solution 7.828427125 belongs to the considered interval. Therefore, x E i = 7.828427125.
From (5) follows Respectively, x 2 A i+1 − 15.65685425x A i+1 + 37.97056275 = 0. The first of the two solutions (3 and 12.65685425) corresponds to the starting point. Therefore, x A i = 3 and x A i = 12.65685425.
As a result of this approach, the inverse sensor characteristic x(y) is approximated by the broken line A 1 A 2 A 3 B, as shown in Table 1, where the coordinates of the points are given, also the corresponding intervals of the argument variation, the values of the argument in the extrema in the corresponding interval (as a result of the properties of the square parabola they are always in the middle of the interval) and the corresponding error ∆ x i . The illustrative example is selected to enable good graphical visualization of the results shown in Figure 5.

Linearization of the Inverse Sensor Characteristics of Temperature Sensors
The relation between the resistance and the temperature in the platinum temperature sensors is set by the equation of Callendar-van Dusen [32,33]. Using the proposed approach, the task of interval linearization of the inverse characteristic will be solved.
Under the denotations R(T) = R T R 0 and a 3 = −A (2B) , Equation (9) takes the form Differentiating (10) twice, concerning temperature T, leads to the first and second derivatives, respectively, The graphs of function R(T), its first derivative R (T), and its second derivative R (T) in the range T ∈ [0, 661] • C defined by Equations (10)- (12), respectively, are shown in Figure 6. Since R (T) does not change its sign (the curve is concave in the considered range), the proposed approach for linear interval linearization can be applied because the function T(R) can be found in the linearized form.
Equations (2)-(5) and (7), required for the approach, accordingly take the form: The numerical results from the application of the approach, when ∆ T = −0.375 • C in the given temperature range T A 1 = 0 • C, T B = 661 • C, are shown in Table 2 and in graphical form in Figure 7. The graph of the function ∆ T (T) from (17) is shown in Figure 8. Table 2. Results in the range T ∈ [0, 661] • C.
Under the denotation, R(T) = R T R 0 , the Equation (18) takes the form Differentiating (19) twice, concerning the temperature T, leads to the first and second derivatives, respectively, The graphs of R(T), R (T), and R (T) are shown in Figure 9. Since R (T) does not change its sign (the curve is concave in the range considered), the proposed approach for linear interval linearization can be applied because function T(R) can be found in the linearized form.
Equations (2)-(5) needed for the implementation of the approach take the form, respectively, The proposed approach will be applied when ∆ T = −0.375 • C in the temperature range Using Equation (22), the value of the argument T E 1 is determined, under which the (23) has an extremum in the range T B = −200 • C < T < T A 1 = 0 • C. The third power Equation (22) has two complex and one real root T E 1 = −110.583 • C.
When T E 1 = −110.583 • C, ∆ T 1 = −2.472 • C is obtained through Equation (23). As ∆ T 1 > ∆ T , Equations (24) and (25) determine the subrange T ∈ T A 2 , T A 1 where ∆ T 1 = ∆ T . The fourth power Equation (24) has two real and two complex roots as the real root T E 1 = −47.977 • C belongs to the range T B , T A 1 . The Equation (25) is also a fourth power equation and has two real and two complex roots as one of the real roots is T A 1 and the other one is T A 2 = −92.292 • C.
Then the same steps are applied to the range T B = −200 • C < T < T A 2 = −92.292 • C. The Equation (22) has two complex and one real root T E 2 = −149.314 • C. When T E 2 = −149.314 • C, ∆ T 2 = −0.915 • C is obtained by Equation (23). As ∆ T 2 > ∆ T , Equations (24) and (25) define the subrange T ∈ T A 3 , T A 2 where ∆ T 2 = ∆ T . Equation (24) has two real and two complex roots as one of the real roots belongs to the range T B , T A 2 is T E 2 = −130.207 • C. Equation (25) has two real and two complex roots as one of the real roots is T A 2 and the other one is T A 3 = −165.17 • C.
The numerical results of the application of the approach when ∆ T = −0.375 • C are shown in Table 3 and in graphical form in Figure 10. The graph of the function ∆ T (T) (17) is shown in Figure 11. Table 3. Results in the range T ∈ [−200, 0] • C.   This design uses a Lolin 32 development board based on Espressif Systems' ESP32 microcontroller. Its main features include integrated Wi-Fi, flash memory, and USB communication, and is often used for IoT designs [36].
The linearization is performed by segmentation, in which the range is divided into the corresponding number of segments (from 1 to End) to guarantee the requirement for the maximum approximation error. The linearization algorithm for low-cost microcontrollers is presented in a pseudo-code form below (see Algorithm 1).

Algorithm 1 Linearization
Input: Measured RTD resistance, R_T, floating-point type Output: Calculated temperature, Temperature, floating-point type Initialization: defining the coordinates of the points bounding each interval Ra_1, Ra_2... Ra_End and Ta_1, Ta_2... Ta_End 1: Determine the interval (n) in which the measured resistance is located, and whether it is in the respective temperature range If ((R_T > Ra_n) and (R_T ≤ Ra_(n + 1))) 2: Calculate the temperature according to the formula Temperature = (R_T − Ra_n) * ((Ta_(n + 1) − Ta_n)/(Ra_(n + 1) − Ra_n)) + Ta_n; 3: Return Temperature The algorithm is implemented as a function in the Lolin D32 development system for the ESP32 microcontroller. When variables and constants are represented with a floatingpoint data type, the memory footprint of the function is 36 bytes of flash and 8 bytes of the system's RAM. The function is compiled with both Os and g3 optimizations-for size and debug information, respectively.
For comparison, in an implementation based on an optimized lookup table using an unsigned integer, the occupied program memory is about 3k bytes and 8 bytes of RAM [37]. The thermoelectric voltage E in microvolts, as a function of temperature T in degrees Celsius, is defined by: where the coefficients c i are specified in NIST ITS-90 [38]. The graphs of function E(T), its first derivative E (T), and its second derivative E (T) in the range, T ∈ [−270, 0] • C, are shown in Figure 12. Since E (T) does not change sign (the curve is convex in the considered range), the proposed approach for linear interval linearization can be applied and the sensor characteristic can be determined in linearized form.
The numerical results from the application of the approach, when ∆ T = 0.04 • C in the given temperature range T A 1 = 0 • C, T B = T A 48 = −200 • C, are shown in Table 4 and in graphical form in Figure 13. The graph of the function ∆ T (T) is shown in Figure 14. Table 4. Results in the range T ∈ [−200, 0] • C.   The linearization of the k-type thermocouple in the range [−200, 0] • C is implemented in the Lolin D32 development system for the ESP32 microcontroller. Variables and constants are represented by floating-point data type, the memory footprint of the function is about 145.3 kB, with activated size optimization "−Os". This is about 10% of the system's resources.

Conclusions
This paper presented an approach for linear interval approximation of sensor characteristics y = y(x) where the characteristics are differentiable functions in which the sign of curvature does not change, i.e., they are either "concave" or "convex" functions. The aim is to obtain a discreet type of inverse sensor characteristic x i = x i (y i ), i = 1, n, with a predefined maximum approximation error ∆ x , with minimization of the number of points defining the characteristic, which in turn is related to the possibilities for using cheaper microcontrollers. The approach is characterized by the fact that when the maximum approximation error ∆ x under the linearization of the inverse sensor characteristic is given, the inverse sensor characteristic x i = x i (y i ), i = 1, n is found directly in the linearized form. The advantages of the developed approach are as follows: • the approach is applied at intervals, with each subsequent step (each successive interval) resulting in a similar solution of the problem under the new initial conditions to obtain the directly sought solution, which in turn contains the initial conditions for the next step; • the maximum error under linearization of the inverse sensor characteristic at all intervals, except in the general case of the last one, is the same; • the approach allows that different maximum approximation errors are set at each subsequent interval; • the approach allows the application to general types of differentiable sensor characteristics with piecewise concave/convex properties.
The proposed approach is verified through linearization of the inverse sensory characteristics of the Pt100 sensor and k-type thermocouple. The proposed approach to linear interval approximation of sensor characteristics y = y(x), which are differentiable functions, can be also extended to sensor characteristics with inflection points (characteristics where the sign of curvature changes, in some subintervals they are "concave", and in others, they are "convex" functions). A typical example of such a sensor characteristic is the Type K Thermocouple.
To overcome the problems arising from resource constraints, appropriate algorithms are needed that allow efficient connectivity and intelligent management of the measurement processes. Although the discussed computational examples are aimed at building adaptive approximations for temperature sensors, the algorithm can be applied easily to many other sensor types.