# Adaptive Segmentation of Streaming Sensor Data on Edge Devices
## 4 Proposed Solution
### The Algorithm  
Algorithm 1 presents a high-level view of the whole computational process (see also Appendix A). The _sliding window_-based streaming segmentation that we propose is clearly reflected in its structure. It is also worth noting that:  
* the sliding window buffer size, \(h\), can be either fixed upfront (e.g., depending on the input signal characteristics and/or real-time constraints), or constantly adapted (e.g., using ML algorithms); a good strategy for the first approach is to use the value of \(h\) corresponding to the maximum acceptable buffering delay (latency),
* to increase the readability of the pseudo-code, checking for exceptional/corner cases (e.g., too few data points at the end of the sliding window buffer to build one more segment) was omitted in some places,
* the algorithm presents one possible way of handling the end of the stream (\(s^{(3)}_{Best}\), was computed with no looking-ahead); again, if necessary, this computation can be more sophisticated (e.g., the stream can be \"artificially\" extended),
* the number of sub-intervals that a given interval is divided into can be either fixed upfront or variable (e.g., simple dependence on the length of the interval, or ML-based). **Remark 3**: _The streaming characteristic of the algorithm means that it requires \(O(n)\) time and \(O(h)\) space, where \(n\) stands for the length of \(\mathbf{S}_{in}\) (potentially \(n\rightarrow\infty\)). See also Appendix B._```
Input: Non-empty stream of data points,  Sliding window buffer size Output: Stream of cubic spline segments which form a \(C^{2}\)-continuous curve
1 Open the input and output streams
2 Estimate initial conditions for the first segment
3 Initialize the buffer offset and search interval (see Equation (15))
4 whilenot end of input streamdo
5 Fill the sliding window buffer
6 whilenot end of sliding window bufferdo
7 Compute the new search interval (see Equation (15))
8 Find \(s_{Best}\) - the best splinelet in this interval
9 Add the first segment of \(s_{Best}\) to the output stream
10 Compute the new initial conditions
11 Add the remaining two segments of \(s_{Best}\) to the output stream
12 Close the input and output streams
```

**Algorithm 1**Adaptive segmentation of streaming data (see also Appendix A)

## 5 Results and Discussion

To evaluate the proposed algorithm, a series of numerical experiments was carried out, mostly in the form of a comparative analysis. As a point of reference, the results obtained from R function smooth.spline (accessed on 15 July 2021) were used. It is worth noting that this function--an example of state-of-the-art solutions--_is global_ (i.e., the whole stream must be given as its input). A summary of the evaluation process used is given in Section 5.1 and the results of the experiments are presented in Sections 5.2 and 5.3. ### Evaluation Process Overview

The key aspects of the evaluation process--_test streams, algorithm performance descriptors_, and _the reference function (algorithm) used_--are briefly described in this section. #### 5.1.1 Test Streams

The test data sets were generated using the following function \(g\) (its graph in the interval [0, 500] is shown in Figure 2) as the \"true signal\":

\[g(q)=\sin(\pi/2-0.1\;q)+\sin(0.025\;q)+2\sin(0.15\;q) \tag{23}\]

to which four levels of Gaussian noise was added, resulting in the following four test streams (see Figure 3):

\[\left\{ \begin{array}{l}f_{1}(q_{k})=g(q_{k})+\epsilon_{1_{k}}\\ f_{2}(q_{k})=g(q_{k})+\epsilon_{2_{k}}\\ f_{3}(q_{k})=g(q_{k})+\epsilon_{3_{k}}\\ f_{4}(q_{k})=g(q_{k})+\epsilon_{4_{k}}\\ \end{array} \right.