def time_norm(data, method, clip_factor=1, clip_set_zero=False,
mute_parts=48, mute_factor=2):
"""
Calculate normalized data, see e.g. Bensen et al. (2007)
:param data: numpy array with data to manipulate
:param str method:
1bit: reduce data to +1 if >0 and -1 if <0\n
clip: clip data to the root mean square (rms)\n
mute_envelope: calculate envelope and set data to zero where envelope
is larger than specified
:param float clip_factor: multiply std with this value before cliping
:param bool clip_mask: instead of clipping, set the values to zero and mask
them
:param int mute_parts: mean of the envelope is calculated by dividing the
envelope into several parts, the mean calculated in each part and
the median of this averages defines the mean envelope
:param float mute_factor: mean of envelope multiplied by this
factor defines the level for muting
:return: normalized data
"""
mask = np.ma.getmask(data)
if method == '1bit':
data = np.sign(data)
elif method == 'clip':
std = np.std(data)
args = (data < -clip_factor * std, data > clip_factor * std)
if clip_set_zero:
ind = np.logical_or(*args)
data[ind] = 0
else:
np.clip(data, *args, out=data)
elif method == 'mute_envelope':
N = next_fast_len(len(data))
envelope = np.abs(hilbert(data, N))[:len(data)]
levels = [np.mean(d) for d in np.array_split(envelope, mute_parts)]
level = mute_factor * np.median(levels)
data[envelope > level] = 0
else:
msg = 'The method passed to time_norm is not known: %s.' % method
raise ValueError(msg)
return _fill_array(data, mask=mask, fill_value=0.)
# http://azitech.wordpress.com/
# 2011/03/15/designing-a-butterworth-low-pass-filter-with-scipy/
评论列表
文章目录