voicesauce / opensauce-python

Voice analysis software (Python port of VoiceSauce)
Apache License 2.0
53 stars 16 forks source link

Rounding issue on Python 3 #19

Closed terriyu closed 7 years ago

terriyu commented 7 years ago

Apparently, there is an issue with rounding in Python 3. This comes up in the implementation of shr_pitch():

# "time locations rounded to nearest ms"
#
# XXX numpy uses round-half-even, while matlab appears to use
# round-away-from-zero.  We can emulate this in python2 by using
# python2 round function instead of numpy's, but in python3 the
# rounding will have to take a detour through the Decimal module,
# which fortunately in python3 has a C accelerator.  Or perhaps
# we can ultimately just use round-half-even, if it turns out
# not to affect the results.  (This matters here because the time
# intervals produced by shrp are normally x.5 values.)
import sys
if sys.version_info.major > 2:
    raise NotImplementedError("python3 rounding will produce bad results")
t = np.vectorize(round)(f0_time)

Quoted from this commit: https://github.com/voicesauce/opensauce-python/commit/7dcb050c1d6be9138e57613c2c44a6882656cb12#diff-7953eb3de436b3d0e74a60971a8b027aR78

Currently, the code passes all tests except for anything involving shr_pitch() because of this rounding problem.

terriyu commented 7 years ago

The solution is to write our own rounding function using NumPy functions. Since it uses NumPy, it works in both Python2 and Python3.

See this commit for the solution https://github.com/voicesauce/opensauce-python/commit/f2cfe4a8900542d43c0ddf8140f4f5292195d76d