コード例 #1
0
def _get_mfcc_and_spec(wav, preemphasis_coeff, n_fft, win_length, hop_length):

    # Pre-emphasis
    y_preem = preemphasis(wav, coeff=preemphasis_coeff)

    # Get spectrogram
    D = librosa.stft(y=y_preem,
                     n_fft=n_fft,
                     hop_length=hop_length,
                     win_length=win_length)
    mag = np.abs(D)

    # Get mel-spectrogram
    # Must have librosa==0.6.3
    # https://github.com/librosa/librosa/issues/1160
    # https://blog.csdn.net/Drifter_Galaxy/article/details/105077454
    mel_basis = librosa.filters.mel(hp.default.sr, hp.default.n_fft,
                                    hp.default.n_mels)  # (n_mels, 1+n_fft//2)
    mel = np.dot(mel_basis, mag)  # (n_mels, t) # mel spectrogram

    # Get mfccs, amp to db
    mag_db = amp2db(mag)
    mel_db = amp2db(mel)

    mfccs = np.dot(librosa.filters.dct(hp.default.n_mfcc, mel_db.shape[0]),
                   mel_db)

    # Normalization (0 ~ 1)
    mag_db = normalize_0_1(mag_db, hp.default.max_db, hp.default.min_db)
    mel_db = normalize_0_1(mel_db, hp.default.max_db, hp.default.min_db)

    return mfccs.T, mag_db.T, mel_db.T  # (t, n_mfccs), (t, 1+n_fft/2), (t, n_mels)
コード例 #2
0
ファイル: data_load.py プロジェクト: guang/morgan-freeman
def _get_mfcc_and_spec(wav, preemphasis_coeff, n_fft, win_length, hop_length):

    # Pre-emphasis
    y_preem = preemphasis(wav, coeff=preemphasis_coeff)

    # Get spectrogram
    D = librosa.stft(y=y_preem,
                     n_fft=n_fft,
                     hop_length=hop_length,
                     win_length=win_length)
    mag = np.abs(D)

    # Get mel-spectrogram
    mel_basis = librosa.filters.mel(hp.default.sr, hp.default.n_fft,
                                    hp.default.n_mels)  # (n_mels, 1+n_fft//2)
    mel = np.dot(mel_basis, mag)  # (n_mels, t) # mel spectrogram

    # Get mfccs, amp to db
    mag_db = amp2db(mag)
    mel_db = amp2db(mel)
    mfccs = np.dot(librosa.filters.dct(hp.default.n_mfcc, mel_db.shape[0]),
                   mel_db)

    # Normalization (0 ~ 1)
    mag_db = normalize_0_1(mag_db, hp.default.max_db, hp.default.min_db)
    mel_db = normalize_0_1(mel_db, hp.default.max_db, hp.default.min_db)

    return mfccs.T, mag_db.T, mel_db.T  # (t, n_mfccs), (t, 1+n_fft/2), (t, n_mels)
コード例 #3
0
def _get_mfcc_and_spec(wav, preemphasis_coeff, n_fft, win_length, hop_length):

    # Pre-emphasis
    y_preem = preemphasis(wav, coeff=preemphasis_coeff)

    # Get spectrogram and power energy
    D = librosa.stft(y=y_preem,
                     n_fft=n_fft,
                     hop_length=hop_length,
                     win_length=win_length)
    mag = np.abs(D)
    mag_e = np.sum(mag**2, axis=0)
    mag_e = np.where(mag_e == 0, np.finfo(float).eps, mag_e)

    # Get mel-spectrogram
    mel_basis = librosa.filters.mel(hp.default.sr, hp.default.n_fft,
                                    hp.default.n_mels)  # (n_mels, 1+n_fft//2)
    mel = np.dot(mel_basis, mag)  # (n_mels, t) # mel spectrogram
    mel = np.where(mel == 0, np.finfo(float).eps, mel)

    # Get mfccs, amp to db
    mag_db = amp2db(mag)
    mel_db = amp2db(mel)
    mfccs = np.dot(librosa.filters.dct(hp.default.n_mfcc, mel_db.shape[0]),
                   mel_db)

    mfccs[0, :] = np.log(mag_e)
    """
    # Normalizing mfcc so that has zero mean and unit variance
    mfccs_norm = (mfccs - np.mean(mfccs)) / np.std(mfccs)
    """

    # Normalization (0 ~ 1)
    mag_db = normalize_0_1(mag_db, hp.default.max_db, hp.default.min_db)
    mel_db = normalize_0_1(mel_db, hp.default.max_db, hp.default.min_db)

    return mfccs.T, mag_db.T, mel_db.T  # (t, n_mfccs), (t, 1+n_fft/2), (t, n_mels)