- changed status to resolved
Centroid calculation is incorrect.
Issue #2
resolved
import numpy as np
from peakutils import centroid
y = np.ones(10.)
x = np.arange(10.)
c = centroid(x, y)
assert(c == 4.5)
y = np.ones(3.)
x = np.array([0., 1., 9.])
c = centroid(x, y)
assert(c == 4.5)
A better calculation is:
def centroid(y, x=None, dx=1.):
'''Computes the centroid for the specified data.
Parameters
----------
y : array_like
Array whose centroid is to be calculated.
x : array_like, optional
The points at which y is sampled.
Returns
-------
(centroid, sd)
Centroid and standard deviation of the data.
'''
yt = np.array(y)
if x is None:
x = np.arange(yt.size, dtype='float') * dx
normaliser = simps(yt, x)
centroid = simps(x * yt, x) / normaliser
var = simps((x - centroid)**2 * yt, x) / normaliser
return centroid, np.sqrt(var)
Comments (1)
-
repo owner - Log in to comment
Thanks. I've added your implementation as centroid2. The original one is not incorrect, but its formulation indeed does not work for your test data.