Exemple #1
0
def mfcc(samples,
         winlen=400,
         winshift=200,
         preempcoeff=0.97,
         nfft=512,
         nceps=13,
         samplingrate=20000,
         liftercoeff=22):
    """Computes Mel Frequency Cepstrum Coefficients.

    Args:
        samples: array of speech samples with shape (N,)
        winlen: lenght of the analysis window
        winshift: number of samples to shift the analysis window at every time step
        preempcoeff: pre-emphasis coefficient
        nfft: length of the Fast Fourier Transform (power of 2, >= winlen)
        nceps: number of cepstrum coefficients to compute
        samplingrate: sampling rate of the original signal
        liftercoeff: liftering coefficient used to equalise scale of MFCCs

    Returns:
        N x nceps array with lifetered MFCC coefficients
    """
    mspec_ = mspec(samples, winlen, winshift, preempcoeff, nfft, samplingrate)
    ceps = cepstrum(mspec_, nceps)
    return lifter(ceps, liftercoeff)
Exemple #2
0
 def test_lmfcc(self):
     lmfcc = lifter(self.example['mfcc'])
     assert_allclose(lmfcc, self.example['lmfcc'])
Exemple #3
0
    # 4.5: Mel filterbank log spectrum
    print("Applying Mel filterbank log spectrum...")
    MSPEC = logMelSpectrum(FFT, 20000)
    if compare(MSPEC, example['mspec']):
        print("The result matches the example.")
    else:
        print("The result doesn't match the example.")

    # 4.6: Cosine transform and liftering
    print("Applying cosine transform...")
    MFCC = cepstrum(MSPEC, 13)
    if compare(MFCC, example['mfcc']): print("The result matches the example.")
    else: print("The result doesn't match the example.")

    print("Applying liftering...")
    LMFCC = tools.lifter(MFCC)
    if compare(LMFCC, example['lmfcc']):
        print("The result matches the example.")
    else:
        print("The result doesn't match the example.")

    # Apply to data
    # First step
    mfcc_features = mfcc(data[0]['samples'])  # liftered
    mspec_features = mspec(data[0]['samples'])

    # Compute the mspec and mfcc for all utterances and stack them
    for i in range(1, len(data)):
        mfcc_features = np.vstack(
            (mfcc_features, mfcc(data[i]['samples'])))  # liftered
        mspec_features = np.vstack((mspec_features, mspec(data[i]['samples'])))
pre_emph = preemp(window_frames)
if (np.array_equal(example['preemph'], pre_emph)):
    customPlot(pre_emph, 'preemph: preemphasis', True)
windowed = windowing(pre_emph)
if (np.allclose(example['windowed'], windowed, atol=1e-08)):
    customPlot(windowed, 'windowed: hamming window', True)
fft_ = powerSpectrum(windowed, 512)
if (np.allclose(example['spec'], fft_, atol=1e-08)):
    customPlot(fft_, 'spec:abs(FFT)^2', True)
logMel = logMelSpectrum(fft_, sampling_rate, 512)
if (np.allclose(example['mspec'], logMel, atol=1e-08)):
    customPlot(logMel, 'mspec:Mel Filterbank', True)
mfcc_ = cepstrum(logMel, 13)
if (np.allclose(example['mfcc'], mfcc_, atol=1e-08)):
    customPlot(mfcc_, 'mfcc:MFCCs', True)
lmfcc_ = lifter(mfcc_)
if (np.allclose(example['lmfcc'], lmfcc_, atol=1e-08)):
    customPlot(lmfcc_, 'lmfcc:Liftered MFCCs', True)

from lab1_proto import mfcc, mspec
data = np.load('lab1_data.npz', allow_pickle=True)['data']
for i in range(data.shape[0]):
    samples = data[i]['samples']
    s = mfcc(samples)
    t = mspec(samples)
    if (i == 0):
        data_mfcc = s
        data_mspec = t
    else:
        data_mfcc = np.append(data_mfcc, s, axis=0)
        data_mspec = np.append(data_mspec, t, axis=0)