def mel2linear(melspectrogram, *, freq_min, freq_max): """ Convert mel-frequency into linear-frequency w/o dimension compression (with linear interpolation in linear-frequency domain) Args: melspectrogram (numpy.ndarray 2D): magnitude, IF and any other melspectrogram Returns: numpy.ndarray 2D: linear-nized spectrogram """ time = range(melspectrogram.shape[1]) even_spaced_mel = np.linspace(start=hz2mel(freq_min, htk=True), stop=hz2mel(freq_max, htk=True), num=melspectrogram.shape[0], endpoint=True) mel_in_freq = [mel2hz(mel, htk=True) for mel in even_spaced_mel] linearnizer = interp2d(time, mel_in_freq, melspectrogram) linear_freq = np.linspace(start=freq_min, stop=freq_max, num=melspectrogram.shape[0], endpoint=True) spectrogram = linearnizer(time, linear_freq) return spectrogram
def linear2melD(spectrogram, *, freq_min, freq_max): """ Convert linear-frequency into mel-frequency w/o dimension compression (with linear interpolation in linear-frequency domain) Args: spectrogram (numpy.ndarray 2D): magnitude, IF and any other mel-compatible spectrogram Returns: numpy.ndarray 2D: mel-nized spectrogram """ linear_freq = np.linspace(start=freq_min, stop=freq_max, num=spectrogram.shape[0], endpoint=True) time = range(spectrogram.shape[1]) melnizer = interp2d(time, linear_freq, spectrogram) even_spaced_mel = np.linspace(start=hz2mel(freq_min, htk=True), stop=hz2mel(freq_max, htk=True), num=spectrogram.shape[0] * scaler, endpoint=True) mel_in_freq = [mel2hz(mel, htk=True) for mel in even_spaced_mel] mel_spectrogram = melnizer(time, mel_in_freq) return mel_spectrogram