def test_lpcc(sig, fs, num_ceps, lifter, normalize):
    """
    test LPCC features module for the following:
        - check that the returned number of cepstrums is correct.
        - check normalization.
        - check liftering.
    """
    lpccs = lpcc(sig=sig,
                 fs=fs,
                 num_ceps=num_ceps,
                 lifter=lifter,
                 normalize=normalize)
    # assert number of returned cepstrum coefficients
    if not lpccs.shape[1] == num_ceps:
        raise AssertionError

    # TO FIX: normalize
    if normalize:
        np.testing.assert_almost_equal(
            lpccs,
            cmvn(
                cms(
                    lpcc(sig=sig,
                         fs=fs,
                         num_ceps=num_ceps,
                         lifter=lifter,
                         normalize=False))), 0)
    else:
        # TO FIX: lifter
        if lifter > 0:
            np.testing.assert_almost_equal(
                lpccs,
                lifter_ceps(
                    lpcc(sig=sig,
                         fs=fs,
                         num_ceps=num_ceps,
                         lifter=False,
                         normalize=normalize), lifter), 0)
def test_pncc(sig, fs, num_ceps, nfilts, nfft, low_freq, high_freq, dct_type,
              use_energy, lifter, normalize):
    """
    test PNCC features module for the following:
        - check if ParameterErrors are raised for:
                - nfilts < num_ceps
                - negative low_freq value
                - high_freq > fs / 2
        - check that the returned number of cepstrums is correct.
        - check the use energy functionality.
        - check normalization.
        - check liftering.
    """

    # check error for number of filters is smaller than number of cepstrums
    with pytest.raises(ParameterError):
        pnccs = pncc(sig=sig,
                     fs=fs,
                     num_ceps=num_ceps,
                     nfilts=num_ceps - 1,
                     nfft=nfft,
                     low_freq=low_freq,
                     high_freq=high_freq)

    # check lifter Parameter error for low freq
    with pytest.raises(ParameterError):
        pnccs = pncc(sig=sig,
                     fs=fs,
                     num_ceps=num_ceps,
                     nfilts=nfilts,
                     nfft=nfft,
                     low_freq=-5,
                     high_freq=high_freq)

    # check lifter Parameter error for high freq
    with pytest.raises(ParameterError):
        pnccs = pncc(sig=sig,
                     fs=fs,
                     num_ceps=num_ceps,
                     nfilts=nfilts,
                     nfft=nfft,
                     low_freq=low_freq,
                     high_freq=16000)

    # compute features
    pnccs = pncc(sig=sig,
                 fs=fs,
                 num_ceps=num_ceps,
                 nfilts=nfilts,
                 nfft=nfft,
                 low_freq=low_freq,
                 high_freq=high_freq,
                 dct_type=dct_type,
                 use_energy=use_energy,
                 lifter=lifter,
                 normalize=normalize)

    # assert number of returned cepstrum coefficients
    if not pnccs.shape[1] == num_ceps:
        raise AssertionError

    # check use energy
    if use_energy:
        pnccs_energy = pnccs[:, 0]
        gfccs_energy = pncc(sig=sig,
                            fs=fs,
                            num_ceps=num_ceps,
                            nfilts=nfilts,
                            nfft=nfft,
                            low_freq=low_freq,
                            high_freq=high_freq,
                            dct_type=dct_type,
                            use_energy=use_energy,
                            lifter=lifter,
                            normalize=normalize)[:, 0]

        np.testing.assert_almost_equal(pnccs_energy, gfccs_energy, 3)

    # check normalize
    if normalize:
        np.testing.assert_almost_equal(
            pnccs,
            cmvn(
                cms(
                    pncc(sig=sig,
                         fs=fs,
                         num_ceps=num_ceps,
                         nfilts=nfilts,
                         nfft=nfft,
                         low_freq=low_freq,
                         high_freq=high_freq,
                         dct_type=dct_type,
                         use_energy=use_energy,
                         lifter=lifter,
                         normalize=False))), 3)
    else:
        # check lifter
        if lifter > 0:
            np.testing.assert_almost_equal(
                pnccs,
                lifter_ceps(
                    pncc(sig=sig,
                         fs=fs,
                         num_ceps=num_ceps,
                         nfilts=nfilts,
                         nfft=nfft,
                         low_freq=low_freq,
                         high_freq=high_freq,
                         dct_type=dct_type,
                         use_energy=use_energy,
                         lifter=False,
                         normalize=normalize), lifter), 3)
def test_cmvn(x):
    # To improve
    x = np.round(np.random.normal(loc=0.0, scale=1.0, size=100), 3)
    y = cmvn(x)
    np.testing.assert_almost_equal(y, cvn(cms(x)), 0)
Beispiel #4
0
def lpcc(sig,
         fs=16000,
         num_ceps=13,
         pre_emph=1,
         pre_emph_coeff=0.97,
         win_type="hann",
         win_len=0.025,
         win_hop=0.01,
         do_rasta=True,
         lifter=1,
         normalize=1,
         dither=1):
    """
    Compute the LINEAR PREDICTIVE CEPSTRAL COEFFICIENTS (LPCC) from an audio signal.

    Args:
        sig            (array) : a mono audio signal (Nx1) from which to compute features.
        fs               (int) : the sampling frequency of the signal we are working with.
                                 Default is 16000.
        num_ceps       (float) : number of cepstra to return (order of the model to compute).
                                 Default is 13.
        pre_emph         (int) : apply pre-emphasis if 1.
                                 Default is 1.
        pre_emph_coeff (float) : apply pre-emphasis filter [1 -pre_emph] (0 = none).
                                 Default is 0.97.
        win_type       (float) : window type to apply for the windowing.
                                 Default is hanning.
        win_len        (float) : window length in sec.
                                 Default is 0.025.
        win_hop        (float) : step between successive windows in sec.
                                 Default is 0.01.
        do_rasta         (int) : if 1 then apply rasta filtering.
                                 Default is 0.
        lifter           (int) : apply liftering if value > 0.
                                 Default is 22.
        normalize        (int) : apply normalization if 1.
                                 Default is 0.
        dither           (int) : 1 = add offset to spectrum as if dither noise.
                                 Default is 0.
    Returns:
        (array) : 2d array of LPCC features (num_frames x num_ceps)
    """
    lpcs = lpc(sig=sig,
               fs=fs,
               num_ceps=num_ceps,
               pre_emph=pre_emph,
               pre_emph_coeff=pre_emph_coeff,
               win_len=win_len,
               win_hop=win_hop,
               do_rasta=True,
               dither=dither)
    lpccs = lpc2cep(lpcs.T)

    # liftering
    if lifter > 0:
        lpccs = lifter_ceps(lpccs, lifter)

    # normalization
    if normalize:
        lpccs = cmvn(cms(lpccs))

    lpccs = lpccs.T
    return lpccs[:, :]