Finding array's linear part

Hi there, my question is about sensor values.
I made array from analogRead() values by the time.
Example of results:

300,420,250,350,352,356,220,280,330
0, 1,  2 , 3 , 4,   5,  6,  7,  8

as you can see values are not sequential in all. the there values "350,352,356" are most sequential values.
if we graph all values by time, we can see kinda linear line in 4,5,6 seconds.

Are there any algorithm to find these linear seconds with arduino?

You're going to need to look at a lot of data from your sensor to figure out the right answer. But why not just use the middle three elements of the sorted array as a first guess?

-br

Yes, but you need more detail about what sort of relationship you are trying to detect.

In this example, the samples to either side are substantially different to the 'linear' ones. Are you really looking for linear samples, or just ones that are sufficiently close to each other? How close, or how nearly linear, do they need to be to be accepted? Is the number of samples you're looking for fixed at three, or are you looking for longer potential sequences? In that case, how long? Do you know (or control) the interval between samples? If this is unknown and varying then you have much less scope to detect patterns in the data.

Is that two array's or just one with indices below it? If the latter, array indices start at 0.

If you're just looking for 3 items in a row that form a line:

Use a for loop to take i from the lower bound to the upper bound less 2. Calculate the difference between array item i+1 and i. Add the difference to array item i+1 and see if it equals i+2

PeterH:
Yes, but you need more detail about what sort of relationship you are trying to detect.

In this example, the samples to either side are substantially different to the 'linear' ones. Are you really looking for linear samples, or just ones that are sufficiently close to each other? How close, or how nearly linear, do they need to be to be accepted? Is the number of samples you're looking for fixed at three, or are you looking for longer potential sequences? In that case, how long? Do you know (or control) the interval between samples? If this is unknown and varying then you have much less scope to detect patterns in the data.

I am looking for ones that sufficiently close to each other. The rate of closeness is not known, will defined by looking all items in array. I'm looking for closest ones. And i'm looking for longest potential sequences. Length is three at least maybe. Interval is one.

If it's linear, it doesn't matter how many elements you use, right?

I don't think you want to get into calculating piecewise linear regressions of all contiguous subsets of the data, but that's the hammer you'd use in a formal statistical analysis.

Often the central tendency tells you enough to get the job done. Speaking of which, what is this for? It might help understand your requirements better.

-br

feveran:
I am looking for ones that sufficiently close to each other. The rate of closeness is not known, will defined by looking all items in array. I'm looking for closest ones. And i'm looking for longest potential sequences. Length is three at least maybe. Interval is one.

Unless you can define what you want better than that, it won't be possible to get anywhere. You need to define when a sequence of elements is considered 'sufficiently close'.

There are almost unlimited ways that this could be approached, and unless / until we know what you are trying to achieve it's a waste of time suggesting how to implement it.

linearity can be detected by creating the first derivative of the signal. This is not really difficult, see sketch below. If you want to include more points the condition just grows. The example sketch has a THRESHOLD to accept noise in the signal (again, you can make this threshold formula more complex if needed).

(code not tested or compiled)

#define THRESHOLD 2

void setup()
{
  Serial.begin(115200);
}
void loop()
{
  int signal[100];
  int der1[100];

  for (int i =0; i<100; i++) 
  {
    signal[i] = analogRead(A0);
  }

  for (int i =0; i<99; i++) 
  {
    der1[i] = signal[i+1] - signal[i];
  }

  // linearity detection loop
  for (int i=0; i< 99; i++)
  {
    if (abs(der[i] - der[i+1]) < THRESHOLD)
    {
      Serial.print("linear part found around index: ");
      Serial.print(i);
      Serial.print("\t");
      Serial.print(signal[i]);
      Serial.print("\t");
      Serial.print(signal[i+1]);
    }
  }
}

I will back, thanks all.