def merlin_post_filter(mgc, alpha, minimum_phase_order=511, fftlen=2048, coef=1.4, weight=None): _, D = mgc.shape if weight is None: weight = np.ones(D) * coef weight[:2] = 1 assert len(weight) == D mgc_r0 = pysptk.c2acr(pysptk.freqt(mgc, minimum_phase_order, alpha=-alpha), 0, fftlen).flatten() mgc_p_r0 = pysptk.c2acr( pysptk.freqt(mgc * weight, minimum_phase_order, -alpha), 0, fftlen).flatten() mgc_b0 = pysptk.mc2b(weight * mgc, alpha)[:, 0] mgc_p_b0 = np.log(mgc_r0 / mgc_p_r0) / 2 + mgc_b0 mgc_p_mgc = pysptk.b2mc( np.hstack((mgc_p_b0[:, None], pysptk.mc2b(mgc * weight, alpha)[:, 1:])), alpha) return mgc_p_mgc
def test_merlin_post_filter(): root = join(DATA_DIR, "merlin_post_filter") mgc = np.fromfile(join(root, "arctic_b0539.mgc"), dtype=np.float32).reshape(-1, 60) weight = np.fromfile(join(root, "weight"), dtype=np.float32) alpha = 0.58 minimum_phase_order = 511 fftlen = 1024 coef = 1.4 # Step 1 mgc_r0 = np.fromfile(join(root, "arctic_b0539.mgc_r0"), dtype=np.float32) mgc_r0_hat = pysptk.c2acr(pysptk.freqt( mgc, minimum_phase_order, alpha=-alpha), 0, fftlen).flatten() assert np.allclose(mgc_r0, mgc_r0_hat) # Step 2 mgc_p_r0 = np.fromfile( join(root, "arctic_b0539.mgc_p_r0"), dtype=np.float32) mgc_p_r0_hat = pysptk.c2acr(pysptk.freqt( mgc * weight, minimum_phase_order, -alpha), 0, fftlen).flatten() assert np.allclose(mgc_p_r0, mgc_p_r0_hat) # Step 3 mgc_b0 = np.fromfile(join(root, "arctic_b0539.mgc_b0"), dtype=np.float32) mgc_b0_hat = pysptk.mc2b(weight * mgc, alpha)[:, 0] assert np.allclose(mgc_b0, mgc_b0_hat) # Step 4 mgc_p_b0 = np.fromfile( join(root, "arctic_b0539.mgc_p_b0"), dtype=np.float32) mgc_p_b0_hat = np.log(mgc_r0_hat / mgc_p_r0_hat) / 2 + mgc_b0_hat assert np.allclose(mgc_p_b0, mgc_p_b0_hat) # Final step mgc_p_mgc = np.fromfile( join(root, "arctic_b0539.mgc_p_mgc"), dtype=np.float32).reshape(-1, 60) mgc_p_mgc_hat = pysptk.b2mc( np.hstack((mgc_p_b0_hat[:, None], pysptk.mc2b(mgc * weight, alpha)[:, 1:])), alpha) assert np.allclose(mgc_p_mgc, mgc_p_mgc_hat) filtered_mgc = merlin_post_filter(mgc, alpha, coef=coef, weight=weight, minimum_phase_order=minimum_phase_order, fftlen=fftlen) assert np.allclose(filtered_mgc, mgc_p_mgc, atol=1e-6)
def __test_synthesis_levdur(filt): # dummy source excitation source = __dummy_source() hopsize = 80 # dummy filter coef. windowed = __dummy_windowed_frames(source, frame_len=512, hopsize=hopsize) c = pysptk.mcep(windowed, filt.order) lpc = pysptk.levdur(pysptk.c2acr(c)) # make sure lpc has loggain lpc[:, 0] = np.log(lpc[:, 0]) # synthesis synthesizer = Synthesizer(filt, hopsize) y = synthesizer.synthesis(source, lpc) assert np.all(np.isfinite(y))
def pitch_shift_on_lpc_residual( wav, sr, shift_in_cent, frame_length=4096, hop_length=240, mgc_order=59, ): assert wav.dtype == np.int16 frames = (librosa.util.frame(wav, frame_length=frame_length, hop_length=hop_length).astype(np.float64).T) frames *= pysptk.blackman(frame_length) alpha = pysptk.util.mcepalpha(sr) mgc = pysptk.mcep(frames, mgc_order, alpha, eps=1e-5, etype=1) c = pysptk.freqt(mgc, mgc_order, -alpha) lpc = pysptk.levdur(pysptk.c2acr(c, mgc_order, frame_length)) # remove gain lpc[:, 0] = 0 # Compute LPC residual synth = Synthesizer(AllZeroDF(mgc_order), hop_length) wav_lpc = synth.synthesis(wav.astype(np.float64), -lpc) residual = wav - wav_lpc # Pitch-shift on LPC residual residual_shifted = librosa.effects.pitch_shift(residual, sr=sr, n_steps=shift_in_cent, bins_per_octave=1200) # Filtering by LPC synth = Synthesizer(AllPoleDF(mgc_order), hop_length) wav_shifted = synth.synthesis(residual_shifted, lpc) return wav_shifted.astype(np.int16)
def __test_fftlen(fftlen): pysptk.c2acr(np.ones(20), 19, fftlen)