## Finding the slope of an array of values

6 posts / 0 new
Author
Message

Hi,

Suppose I have an array of 1000 values, the values are generally monotonic, the values in the array are from an ADC. Suppose I want to find the slope between sample 0 and sample 999, I could disregard all the samples in between and just use 2 points but that sounds horribly dumb. The 2nd least dumb approach I can think of is to put the array through an IIR/FIR filter and then pick two distant points to compute the slope.

Does anyone have suggestions?

Last Edited: Wed. Jan 8, 2020 - 06:13 AM

if sample 1 has a higher level than sample 2 the slope is falling, else it is rising or steady(sample 1 and 2 are equal) .

But I guess you are looking for something more sophisticated.

if you really are not interested in the other samples, then just tread things like you have only 2 samples to work with and forget about the rest. If you want to know how fast things are moving you need the other samples to see if your slope is getting steeper or not and you need a second and perhaps even a 3rd order for that to become visible. But this is still basic meth that should be abundantly discussed on the internet I think as it will be q question asked in highschool.

well, the least square best estimate is

Where the bars are the average values.  Since the X values are prob uniform spaced (1 2 3 4...etc), part of the equation (such as the denominator) can likely be simplified

For 1 to 1000 the denominator is 83333250

The bottom of this link (see graph at bottom) may give some tips:

https://www.embeddedrelated.com/showarticle/1163.php

Of course, it may be better (fast) if you convert to integer calcs, by scaling the numbers

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Wed. Jan 8, 2020 - 08:20 AM

First and last points are almost as good as anything else. However, whether or not this is good enough depends on your application.

The "proper" way to do it would be a least-squares fit which determines the coefficients of the straight line that optimally fits the data. See "Simple Linear Regression model" in: https://en.wikipedia.org/wiki/Or...  or better: https://en.wikipedia.org/wiki/Si...

Jim

Until Black Lives Matter, we do not have "All Lives Matter"!