dargaud
dargaud

Reputation: 2581

Finding the center of 1D data

Say I have data: 0 (or near 0), 0, 0, ..., 1, 10, 52, 80, 100, 100, 100, 100 (for a while), 90, 45, 5, 0, 0, 0... I want to find the index (not necessarily an int, I want more precision) of the 'center' of my plateau of data.

My first thought was to do a gaussian fit, but the data is rather flat for a while in the center. So maybe some kind of square (?) fit. I've been looking at minimization with gsl also, but I don't know what the simplest way to do this would be.

A simple way would be to find the index corresponding to the median value, but that gives me only a precision of 1. With a curve fitting I can do better.

Note: I'm in C and can use GSL, but a general math solution would work too!

Upvotes: 0

Views: 661

Answers (2)

ryyker
ryyker

Reputation: 23226

Weighted Mean Center of a line, with an array similar to your data:

int w[] = {0, 0, 0, 1, 10, 52, 80, 100, 100, 100, 100, 90, 45, 5, 0, 0}

...is calculated by multiplying the x and y coordinate by the weight for that feature and summing all for both x and y individually, and then dividing this by the sum of all the weights.

Because this is a 1D array, position is expressed using the position within the array, i.e. the index, and looks like this:

weighted mean center = sum(w[i]*i)/sum(w[i])  //for all i

in pseudo code:

double sum_w=0;//sum of all values (weights)
double prod_wx=0;//product of all corresponding weights and positions
double wmc=0; //weighted mean center
for(int i=0;i<sizeof(w)/sizeof(w[0]);i++)
{
    prod_wx += w[i]*i;
    sum_w += w[i];
}
wmc = prod_wx/sum_w;

Upvotes: 0

chux
chux

Reputation: 154255

Suggested algorithm:

  1. Optionally filter data: median of 3, low pass, etc.

  2. Find average value: Avg

  3. Find average index of values above Avg: Center_index.

  4. Average a few of the "values above" near Center_index.

Upvotes: 1

Related Questions