-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtransformData.py
50 lines (45 loc) · 1.96 KB
/
transformData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import numpy as np
quantization_channels=256 #discretize the value to 256 numbers
def y_mu_law_encode(audio, quantization_channels=256):
'''Quantizes waveform amplitudes.'''
mu = (quantization_channels - 1)*1.0
# Perform mu-law companding transformation (ITU-T, 1988).
# Minimum operation is here to deal with rare large amplitudes caused
# by resampling.
safe_audio_abs = np.minimum(np.abs(audio), 1.0)
magnitude = np.log1p(mu * safe_audio_abs) / np.log1p(mu)
signal = np.sign(audio) * magnitude
# Quantize signal to the specified number of levels.
return ((signal + 1) / 2 * mu + 0.5).astype(int) #discretize to 0~255
def x_mu_law_encode(audio, quantization_channels=256):
'''Quantizes waveform amplitudes.'''
mu = (quantization_channels - 1)*1.0
# Perform mu-law companding transformation (ITU-T, 1988).
# Minimum operation is here to deal with rare large amplitudes caused
# by resampling.
safe_audio_abs = np.minimum(np.abs(audio), 1.0)
magnitude = np.log1p(mu * safe_audio_abs) / np.log1p(mu)
signal = np.sign(audio) * magnitude
# Quantize signal to the specified number of levels.
return signal
def mu_law_decode(output, quantization_channels=256):
'''Recovers waveform from quantized values.'''
mu = quantization_channels - 1
# Map values back to [-1, 1].
signal = 2 * ((output*1.0) / mu) - 1
# Perform inverse of mu-law transformation.
magnitude = (1 / mu) * ((1 + mu)**np.abs(signal) - 1)
return np.sign(signal) * magnitude
def onehot(a,mu=quantization_channels):
b = np.zeros((a.shape[0], mu))
b[np.arange(a.shape[0]), a] = 1
return b
def cateToSignal(output, quantization_channels=256,stage=0):
mu = quantization_channels - 1
if stage == 0:
# Map values back to [-1, 1].
signal = 2 * ((output*1.0) / mu) - 1
return signal
else:
magnitude = (1 / mu) * ((1 + mu)**np.abs(output) - 1)
return np.sign(output) * magnitude