How to define break points in a numeric array in MATLAB

Good afternoon guys, I have this new question, I hope you can help me again:

I have a vector that you can find in the following link:

https://drive.google.com/file/d/0B4WGV21GqSL5Y09GU240N3F1YkU/edit?usp=sharing

The configured vector is as follows:

enter image description here

As you can see, there are some parts on the graph where the data has an almost linear behavior. This is what I'm talking about:

enter image description here

I need to find these breakpoints based on the linearity of some pieces of data. And you are probably asking yourself what happens when a part of the data is not linear, well, the algorithm will not accept this part.

I hope you can help me, thanks.

+6
source share
1 answer

What you are trying to do is called linear time series segmentation.

There are many methods to solve this problem, which are distinguished by their complexity and accuracy.

Here is the simplest one called sliding window segmentation:

function [breaks vals] = segment( data, max_error ) breaks = []; vals = []; left = 1; for right = 2:length(data) err = linear_regresion(data(left:right)); if max(abs(err)) > max_error breaks(end+1) = right-1; vals(end+1) = data(right-1); left = right; end end end function err = linear_regresion( data ) n = length(data); x = (1:n)' - (n+1)/2; y = data - mean(data); k = sum(x.*y) ./ sum(x.^2); err = y - k*x; end 

linear_regresion is an implementation of a simple linear regression algorithm .

In my example, I used the maximum absolute error as a stopping criterion, but you can replace it with any other fitting function, for example. means square error .

Here is an example of segmenting your data using max_error = 0.04 :

segmentation results

You can find about this and other segmentation methods in this review article .

+7
source

Source: https://habr.com/ru/post/972603/


All Articles