Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter an array based on density

I have a sample graph like one below.., which I plotted with set of (x,y) values in an array X.

http://bubblebird.com/images/t.png

As you can see the image has dense peak values between 4000 to 5100

My exact question is can I programmatically find this range where the graph is most dense?
ie.. with Array X how can I find range within which this graph is dense?
for this array it would be 4000 - 5100.

Assume that the array has only one dense region for simplicity.
Thankful if you can suggest a pseudocode/code.

like image 483
everlasto Avatar asked Nov 27 '12 13:11

everlasto


People also ask

How do you filter an array of objects based on an array?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.

How do I filter an NP array?

In NumPy, you filter an array using a boolean index list. A boolean index list is a list of booleans corresponding to indexes in the array. If the value at an index is True that element is contained in the filtered array, if the value at that index is False that element is excluded from the filtered array.


1 Answers

You can use the variance of the signal on a moving window. Here is an example (see the graph attached where the test signal is red, the windowed variance is green and the filtered signal is blue) :

simple example :

test signal generation :

import numpy as np
X = np.arange(200) - 100.  
Y = (np.exp(-(X/10)**2) + np.exp(-((np.abs(X)-50.)/2)**2)/3.) * np.cos(X * 10.)

compute moving window variance :

window_length = 30 # number of point for the window
variance = np.array([np.var(Y[i-window_length / 2.: i+window_length/2.]) for i in range(200)])

get the indices where the variance is high (here I choose the criterion variance superior to half of the maximum variance... you can adapt it to your case) :

idx = np.where(variance > 0.5 * np.max(variance))

X_min = np.min(X[idx])
# -14.0
X_max = np.max(X[idx])
# 15.0

or filter the signal (set to zero the points with low variance)

Y_modified = np.where(variance > 0.5 * np.max(variance), Y, 0)
like image 107
Thomas Leonard Avatar answered Sep 19 '22 11:09

Thomas Leonard