def test_preemphasis(): for dtype in [np.float32, np.float64]: np.random.seed(1234) x = np.random.rand(16000 * 5).astype(dtype) y = preemphasis(x, 0.97) assert x.shape == y.shape assert x.dtype == y.dtype x_hat = inv_preemphasis(y, 0.97) assert x_hat.dtype == x.dtype assert np.allclose(x_hat, x, atol=1e-5) x = np.random.rand(16000) assert np.allclose(P.preemphasis(x, 0), x) assert np.allclose(P.inv_preemphasis(P.preemphasis(x, 0), 0), x)
def preemphasis(x): # allow disable preemphasis if hparams.preemphasis == -1.0: return x else: from nnmnkwii.preprocessing import preemphasis return preemphasis(x, hparams.preemphasis)
def melspectrogram(y, ref_level_db, allow_clipping_in_normalization, min_level_db): D = _lws_processor().stft(preemphasis(y)).T S = _amp_to_db(_linear_to_mel(np.abs(D))) - ref_level_db if not allow_clipping_in_normalization: assert S.max() <= 0 and S.min() - min_level_db >= 0 return _normalize(S)
def wave2spec( wave, fs, frame_period, window, nperseg=None, nmels=80, preemphasis_coef=None, f_min=0, f_max=None, dtype='float32', return_t=False): stft_kwargs = make_stft_args( frame_period, fs, nperseg=nperseg, window=window) htk, norm = True, "slaney" if preemphasis_coef is not None: spec_wave = preemphasis(wave, preemphasis_coef) else: spec_wave = wave _, t, Zxx = signal.stft( spec_wave, **stft_kwargs) pspec = np.abs(Zxx) mspec = melspectrogram( sr=fs, S=pspec, n_mels=nmels, fmin=f_min, fmax=f_max, power=1.0, htk=htk, norm=norm) pspec = pspec.T.astype(dtype) mspec = mspec.T.astype(dtype) upsample = fs // (1000 // frame_period) length = (len(wave) // upsample) * upsample wave = wave[:length] mspec = mspec[:length // upsample] spec = pspec[:length // upsample] if return_t: return wave, spec, mspec, upsample, t else: return wave, spec, mspec, upsample
def spectrogram(y): D = librosa.stft(preemphasis(y), n_fft=hparams.fft_size, hop_length=hparams.hop_size, win_length=hparams.fft_wsize) S = _amp_to_db(np.abs(D)) - hparams.spec_ref_level_db #S = librosa.amplitude_to_db(np.abs(D)) - hparams.spec_ref_level_db return _normalize(S)
def melspectrogram(y): D = librosa.stft(preemphasis(y), n_fft=hparams.fft_size, hop_length=hparams.hop_size, win_length=hparams.fft_wsize) S = _amp_to_db(_linear_to_mel(np.abs(D))) - hparams.spec_ref_level_db #S = librosa.amplitude_to_db(_linear_to_mel(np.abs(D))) - hparams.spec_ref_level_db if not hparams.allow_clipping_in_normalization: assert S.max() <= 0 and S.min() - hparams.min_level_db >= 0 return _normalize(S)
def melspectrogram(y): a = preemphasis(y) # stft fails if len(a.shape) != 1, not sure how it got there if len(a.shape) != 1: a = np.squeeze(a) D = _lws_processor().stft(a).T S = _amp_to_db(_linear_to_mel(np.abs(D))) - hparams.ref_level_db if not hparams.allow_clipping_in_normalization: assert S.max() <= 0 and S.min() - hparams.min_level_db >= 0 return _normalize(S)
def spectrogram(y, power, pcen=False): global _mel_freqs stftS = librosa.stft(y, n_fft=hparams.fft_size, hop_length=hparams.hop_size) if hparams.use_preemphasis: y = preemphasis(y) S = librosa.stft(y, n_fft=hparams.fft_size, hop_length=hparams.hop_size) if _mel_freqs is None: _mel_freqs = librosa.mel_frequencies(S.shape[0], fmin=hparams.fmin) _S = librosa.perceptual_weighting(np.abs(S)**power, _mel_freqs, ref=hparams.ref_level_db) return _normalize(_S - hparams.ref_level_db), stftS
def melspectrogram(y): D = _stft(preemphasis(y)) S = _amp_to_db(_linear_to_mel(np.abs(D)**hparams.magnitude_power)) - hparams.ref_level_db if not hparams.allow_clipping_in_normalization: assert S.max() <= 0 and S.min() - hparams.min_level_db >= 0 return _normalize(S)
def melspectrogram(y): D = _stft(preemphasis(y)) S = _amp_to_db(_linear_to_mel(np.abs(D))) - hparams.ref_level_db return _normalize(S)
def preemphasis(x, coef=0.85): return P.preemphasis(x, coef)
def preemphasis(x): from nnmnkwii.preprocessing import preemphasis return preemphasis(x, preemphasis_factor)
def melspectrogram(y): D = _lws_processor().stft(preemphasis(y)).T S = _amp_to_db(_linear_to_mel(np.abs(D))) return _normalize(S)
def stft(y, preemp=False): if preemp: y = preemphasis(y) return librosa.stft(y, n_fft=hparams.fft_size, hop_length=hparams.hop_size)
def preemphasis(x): from nnmnkwii.preprocessing import preemphasis return preemphasis(x, hparams.preemphasis)
def spectrogram(y): D = _lws_processor().stft(preemphasis(y)).T S = _amp_to_db(np.abs(D)) - hparams.ref_level_db return _normalize(S)
def _preemphasis(self, x): from nnmnkwii.preprocessing import preemphasis return preemphasis(x, self.hparams.preemphasis)