Exemple #1
0
def test_cens():
    # load CQT data from Chroma Toolbox
    ct_cqt = load(os.path.join('data', 'features-CT-cqt.mat'))

    fn_ct_chroma_cens = [
        'features-CT-CENS_9-2.mat', 'features-CT-CENS_21-5.mat',
        'features-CT-CENS_41-1.mat'
    ]

    cens_params = [(9, 2), (21, 5), (41, 1)]

    for cur_test_case, cur_fn_ct_chroma_cens in enumerate(fn_ct_chroma_cens):
        win_len_smooth = cens_params[cur_test_case][0]
        downsample_smooth = cens_params[cur_test_case][1]

        # plug into librosa cens computation
        lr_chroma_cens = librosa.feature.chroma_cens(
            C=ct_cqt['f_cqt'],
            win_len_smooth=win_len_smooth,
            fmin=librosa.core.midi_to_hz(1),
            bins_per_octave=12,
            n_octaves=10)

        # leaving out frames to match chroma toolbox behaviour
        # lr_chroma_cens = librosa.resample(lr_chroma_cens, orig_sr=1, target_sr=1/downsample_smooth)
        lr_chroma_cens = lr_chroma_cens[:, ::downsample_smooth]

        # load CENS-41-1 features
        ct_chroma_cens = load(os.path.join('data', cur_fn_ct_chroma_cens))

        assert np.allclose(ct_chroma_cens['f_CENS'],
                           lr_chroma_cens,
                           rtol=1e-15,
                           atol=1e-15)
def test_cens():
    # load CQT data from Chroma Toolbox
    ct_cqt = load(os.path.join('tests', 'data', 'features-CT-cqt.mat'))

    fn_ct_chroma_cens = ['features-CT-CENS_9-2.mat',
                         'features-CT-CENS_21-5.mat',
                         'features-CT-CENS_41-1.mat']

    cens_params = [(9, 2), (21, 5), (41, 1)]

    for cur_test_case, cur_fn_ct_chroma_cens in enumerate(fn_ct_chroma_cens):
        win_len_smooth = cens_params[cur_test_case][0]
        downsample_smooth = cens_params[cur_test_case][1]

        # plug into librosa cens computation
        lr_chroma_cens = librosa.feature.chroma_cens(C=ct_cqt['f_cqt'],
                                                     win_len_smooth=win_len_smooth,
                                                     fmin=librosa.core.midi_to_hz(1),
                                                     bins_per_octave=12,
                                                     n_octaves=10)

        # leaving out frames to match chroma toolbox behaviour
        # lr_chroma_cens = librosa.resample(lr_chroma_cens, orig_sr=1, target_sr=1/downsample_smooth)
        lr_chroma_cens = lr_chroma_cens[:, ::downsample_smooth]

        # load CENS-41-1 features
        ct_chroma_cens = load(os.path.join('tests', 'data', cur_fn_ct_chroma_cens))

        maxdev = np.abs(ct_chroma_cens['f_CENS'] - lr_chroma_cens)
        assert np.allclose(ct_chroma_cens['f_CENS'], lr_chroma_cens, rtol=1e-15, atol=1e-15), maxdev
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(os.path.join('tests', DATA['wavfile'][0]),
                             sr=None,
                             mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)

        S = DATA['S']

        # normalization is disabled here, since the precomputed S is already
        # normalized
        # metlab uses p=1, other folks use p=2
        bw = librosa.feature.spectral_bandwidth(S=S,
                                                sr=sr,
                                                n_fft=n_fft,
                                                hop_length=hop_length,
                                                centroid=DATA['centroid'],
                                                norm=False,
                                                p=1)

        # METlab implementation takes the mean, not the sum
        assert np.allclose(bw, S.shape[0] * DATA['bw'])
Exemple #4
0
def test_spectral_bandwidth(infile):
    DATA = load(infile)

    y, sr = librosa.load(os.path.join("tests", DATA["wavfile"][0]),
                         sr=None,
                         mono=True)

    n_fft = DATA["nfft"][0, 0].astype(int)
    hop_length = DATA["hop_length"][0, 0].astype(int)

    S = DATA["S"]

    # normalization is disabled here, since the precomputed S is already
    # normalized
    # metlab uses p=1, other folks use p=2
    bw = librosa.feature.spectral_bandwidth(
        S=S,
        sr=sr,
        n_fft=n_fft,
        hop_length=hop_length,
        centroid=DATA["centroid"],
        norm=False,
        p=1,
    )

    # METlab implementation takes the mean, not the sum
    assert np.allclose(bw, S.shape[0] * DATA["bw"])
Exemple #5
0
def deprecated_test_beat(infile):

    DATA = load(infile)

    (bpm, beats) = librosa.beat.beat_track(y=None, sr=8000, hop_length=32, onset_envelope=DATA["onsetenv"][0])

    beat_times = librosa.frames_to_time(beats, sr=8000, hop_length=32)
    assert np.allclose(beat_times, DATA["beats"])
Exemple #6
0
def test_onset_strength(infile):

    DATA = load(infile)

    # Compute onset envelope using the same spectrogram
    onsets = librosa.onset.onset_strength(
        y=None, sr=8000, S=DATA["D"], lag=1, max_size=1, center=False, detrend=True, aggregate=np.mean
    )

    assert np.allclose(onsets[1:], DATA["onsetenv"][0])
Exemple #7
0
    def __test(infile):

        DATA = load(infile)

        (bpm, beats) = librosa.beat.beat_track(y=None,
                                               sr=8000,
                                               hop_length=32,
                                               onset_envelope=DATA['onsetenv'][0])

        beat_times = librosa.frames_to_time(beats, sr=8000, hop_length=32)
        assert np.allclose(beat_times, DATA['beats'])
Exemple #8
0
    def __test(infile):

        DATA = load(infile)

        (bpm, beats) = librosa.beat.beat_track(y=None,
                                               sr=8000,
                                               hop_length=32,
                                               onset_envelope=DATA['onsetenv'][0])

        beat_times = librosa.frames_to_time(beats, sr=8000, hop_length=32)
        assert np.allclose(beat_times, DATA['beats'])
Exemple #9
0
    def __test(infile):
        DATA = load(infile)

        # Estimate tempo from the given onset envelope
        tempo = librosa.beat.estimate_tempo(DATA['onsetenv'][0],
                                            sr=8000,
                                            hop_length=32,
                                            start_bpm=120.0)

        assert (np.allclose(tempo, DATA['t'][0, 0])
                or np.allclose(tempo, DATA['t'][0, 1]))
Exemple #10
0
    def __test(infile):
        DATA = load(infile)

        # Estimate tempo from the given onset envelope
        tempo = librosa.beat.estimate_tempo(DATA['onsetenv'][0],
                                            sr=8000,
                                            hop_length=32,
                                            start_bpm=120.0)

        assert (np.allclose(tempo, DATA['t'][0, 0]) or
                np.allclose(tempo, DATA['t'][0, 1]))
Exemple #11
0
    def __test(infile):
        DATA = load(infile)

        # Compute onset envelope using the same spectrogram
        onsets = librosa.onset.onset_strength(y=None,
                                              sr=8000,
                                              S=DATA['D'],
                                              centering=False,
                                              detrend=True)

        assert np.allclose(onsets[1:], DATA['onsetenv'][0])

        pass
Exemple #12
0
    def __test(infile):
        DATA = load(infile)

        # Compute onset envelope using the same spectrogram
        onsets = librosa.onset.onset_strength(y=None,
                                              sr=8000,
                                              S=DATA['D'],
                                              centering=False,
                                              detrend=True)

        assert np.allclose(onsets[1:], DATA['onsetenv'][0])

        pass
Exemple #13
0
    def __test(infile):
        DATA = load(infile)

        # Compute onset envelope using the same spectrogram
        onsets = librosa.onset.onset_strength(y=None,
                                              sr=8000,
                                              S=DATA['D'],
                                              lag=1,
                                              max_size=1,
                                              center=False,
                                              detrend=True,
                                              aggregate=np.mean)

        assert np.allclose(onsets[1:], DATA['onsetenv'][0])

        pass
Exemple #14
0
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)

        # spectralCentroid uses normalized spectra
        S = met_stft(y, n_fft, hop_length, n_fft, True)

        centroid = librosa.feature.spectral_centroid(S=S,
                                                     sr=sr,
                                                     n_fft=n_fft,
                                                     hop_length=hop_length)

        assert np.allclose(centroid, DATA['centroid'])
Exemple #15
0
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)

        # spectralCentroid uses normalized spectra
        S = met_stft(y, n_fft, hop_length, n_fft, True)

        centroid = librosa.feature.spectral_centroid(S=S,
                                                     sr=sr,
                                                     n_fft=n_fft,
                                                     hop_length=hop_length)

        assert np.allclose(centroid, DATA['centroid'])
Exemple #16
0
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)

        # spectralContrast uses normalized spectra
        S = met_stft(y, n_fft, hop_length, n_fft, True)

        contrast = librosa.feature.spectral_contrast(S=S, sr=sr,
                                                     n_fft=n_fft,
                                                     hop_length=hop_length,
                                                     linear=True)

        assert np.allclose(contrast, DATA['contrast'], rtol=1e-3, atol=1e-2)
Exemple #17
0
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)

        # spectralContrast uses normalized spectra
        S = met_stft(y, n_fft, hop_length, n_fft, True)

        contrast = librosa.feature.spectral_contrast(S=S, sr=sr,
                                                     n_fft=n_fft,
                                                     hop_length=hop_length,
                                                     linear=True)

        assert np.allclose(contrast, DATA['contrast'], rtol=1e-3, atol=1e-2)
Exemple #18
0
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)
        pct = DATA['pct'][0, 0]

        # spectralRolloff uses normalized spectra
        S = met_stft(y, n_fft, hop_length, n_fft, True)

        rolloff = librosa.feature.spectral_rolloff(S=S, sr=sr,
                                                   n_fft=n_fft,
                                                   hop_length=hop_length,
                                                   roll_percent=pct)

        assert np.allclose(rolloff, DATA['rolloff'])
Exemple #19
0
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)
        pct = DATA['pct'][0, 0]

        # spectralRolloff uses normalized spectra
        S = met_stft(y, n_fft, hop_length, n_fft, True)

        rolloff = librosa.feature.spectral_rolloff(S=S, sr=sr,
                                                   n_fft=n_fft,
                                                   hop_length=hop_length,
                                                   roll_percent=pct)

        assert np.allclose(rolloff, DATA['rolloff'])
Exemple #20
0
def test_spectral_centroid(infile):
    DATA = load(infile)

    y, sr = librosa.load(os.path.join("tests", DATA["wavfile"][0]),
                         sr=None,
                         mono=True)

    n_fft = DATA["nfft"][0, 0].astype(int)
    hop_length = DATA["hop_length"][0, 0].astype(int)

    # spectralCentroid uses normalized spectra
    S = met_stft(y, n_fft, hop_length, n_fft, True)

    centroid = librosa.feature.spectral_centroid(S=S,
                                                 sr=sr,
                                                 n_fft=n_fft,
                                                 hop_length=hop_length)

    assert np.allclose(centroid, DATA["centroid"])
Exemple #21
0
def test_spectral_contrast(infile):
    DATA = load(infile)

    y, sr = librosa.load(os.path.join("tests", DATA["wavfile"][0]),
                         sr=None,
                         mono=True)

    n_fft = DATA["nfft"][0, 0].astype(int)
    hop_length = DATA["hop_length"][0, 0].astype(int)

    # spectralContrast uses normalized spectra
    S = met_stft(y, n_fft, hop_length, n_fft, True)

    contrast = librosa.feature.spectral_contrast(S=S,
                                                 sr=sr,
                                                 n_fft=n_fft,
                                                 hop_length=hop_length,
                                                 linear=True)

    assert np.allclose(contrast, DATA["contrast"], rtol=1e-3, atol=1e-2)
Exemple #22
0
def test_spectral_rolloff(infile):
    DATA = load(infile)

    y, sr = librosa.load(os.path.join("tests", DATA["wavfile"][0]),
                         sr=None,
                         mono=True)

    n_fft = DATA["nfft"][0, 0].astype(int)
    hop_length = DATA["hop_length"][0, 0].astype(int)
    pct = DATA["pct"][0, 0]

    # spectralRolloff uses normalized spectra
    S = met_stft(y, n_fft, hop_length, n_fft, True)

    rolloff = librosa.feature.spectral_rolloff(S=S,
                                               sr=sr,
                                               n_fft=n_fft,
                                               hop_length=hop_length,
                                               roll_percent=pct)

    assert np.allclose(rolloff, DATA["rolloff"])
Exemple #23
0
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)

        S = DATA['S']

        # normalization is disabled here, since the precomputed S is already
        # normalized
        # metlab uses p=1, other folks use p=2
        bw = librosa.feature.spectral_bandwidth(S=S, sr=sr,
                                                n_fft=n_fft,
                                                hop_length=hop_length,
                                                centroid=DATA['centroid'],
                                                norm=False,
                                                p=1)

        # METlab implementation takes the mean, not the sum
        assert np.allclose(bw, S.shape[0] * DATA['bw'])