diff --git a/fave/extractFormants.py b/fave/extractFormants.py index 21c404b..8e2f76a 100755 --- a/fave/extractFormants.py +++ b/fave/extractFormants.py @@ -1068,20 +1068,14 @@ def maximumIntensity(intensities, times): def mean_stdv(valuelist): """returns the arithmetic mean and sample standard deviation (N-1 in the denominator) of a list of values""" - n = len(valuelist) - if n > 0: - if n == 1: + np_valuelist = np.array(valuelist,dtype=np.float64) + if len(np_valuelist) > 0: + if len(np_valuelist) == 1: mean = valuelist[0] stdv = 0 else: - sum_i = 0 - for i in range(n): - sum_i += valuelist[i] - mean = sum_i / n - diffsum_i = 0 - for i in range(n): - diffsum_i += (valuelist[i] - mean) ** 2 - stdv = math.sqrt(diffsum_i / (n - 1)) + mean = np.nanmean(np_valuelist) + stdv = np.nanstd(np_valuelist, ddof=1) else: # empty list mean = None diff --git a/tests/fave/extract/test_extractFormants.py b/tests/fave/extract/test_extractFormants.py index e30ebe6..362dbd1 100644 --- a/tests/fave/extract/test_extractFormants.py +++ b/tests/fave/extract/test_extractFormants.py @@ -1,4 +1,5 @@ +from cmath import nan import logging import pytest import numpy as np @@ -42,7 +43,7 @@ def provide_valuelist(): [-1], np.mean([-1]), 0 - ] + ], [ [3.5, 2.6, 11.6, None, 34.66, 2.8, 4.7], np.nanmean(np.array([3.5, 2.6, 11.6, None, 34.66, 2.8, 4.7],