Skip to content
Permalink
Browse files

tests: Enable melfilter tests with very lax tolerances

There is some quantization error or similar going on
  • Loading branch information...
jonnor committed Dec 29, 2018
1 parent dfb070f commit fd053e5bec5ec2c62c6a64ee913f5aa277bf02bd
Showing with 36 additions and 6 deletions.
  1. +36 −6 test/test_audio.py
@@ -7,8 +7,10 @@

import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
import librosa
import librosa.display
import pandas

import emlearn
import eml_audio
@@ -35,8 +37,30 @@ def test_rfft_not_power2_length():
eml_audio.rfft(numpy.array([0,1,3,4,5]))


@pytest.mark.skip('broken')
def test_melfilter():
def test_melfilter_basic():
n_mels = 32
n_fft = 512
length = 1 + n_fft//2
sr = 22050
fmin = 0
fmax = sr//2

input = numpy.ones(shape=length)
out = eml_audio.melfilter(input, sr, n_fft, n_mels, fmin, fmax)
ref = librosa.feature.melspectrogram(S=input, htk=True, norm=None, sr=sr, n_fft=n_fft, n_mels=n_mels, fmin=fmin, fmax=fmax)
diff = out - ref

fig, (ref_ax, out_ax, diff_ax) = plt.subplots(3)
pandas.Series(out).plot(ax=out_ax)
pandas.Series(ref).plot(ax=ref_ax)
pandas.Series(diff).plot(ax=diff_ax)
fig.savefig('melfilter.basic.png')

assert ref.shape == out.shape
numpy.testing.assert_allclose(out, ref, rtol=0.30) # FIXME: should be 0.01 or better


def test_melfilter_librosa():
filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename, offset=1.0, duration=0.3)
n_fft = 1024
@@ -48,11 +72,19 @@ def test_melfilter():
spec = numpy.abs(librosa.core.stft(y, n_fft=n_fft, hop_length=hop_length))**2
spec1 = spec[:,0]

ref = librosa.feature.melspectrogram(S=spec1, sr=sr, htk=True, n_fft=n_fft, n_mels=n_mels, fmin=fmin, fmax=fmax)
ref = librosa.feature.melspectrogram(S=spec1, sr=sr, norm=None, htk=True, n_fft=n_fft, n_mels=n_mels, fmin=fmin, fmax=fmax)
out = eml_audio.melfilter(spec1, sr, n_fft, n_mels, fmin, fmax)

fig, (ref_ax, out_ax) = plt.subplots(2)
def specshow(d, ax):
s = librosa.amplitude_to_db(d, ref=numpy.max)
librosa.display.specshow(s, ax=ax, x_axis='time')
specshow(ref.reshape(-1, 1), ax=ref_ax)
specshow(out.reshape(-1, 1), ax=out_ax)
fig.savefig('melfilter.librosa.png')

assert ref.shape == out.shape
numpy.testing.assert_array_almost_equal(ref, out)
numpy.testing.assert_allclose(ref, out, rtol=0.9, atol=1) # FIXME: should be 0.01 or better


@pytest.mark.skip('broken')
@@ -82,7 +114,6 @@ def test_melspectrogram():

assert out.shape == ref.shape

from matplotlib import pyplot as plt
fig, (ref_ax, out_ax) = plt.subplots(2)
def specshow(d, ax):
s = librosa.amplitude_to_db(d, ref=numpy.max)
@@ -102,4 +133,3 @@ def specshow(d, ax):
#print(out-ref)
numpy.testing.assert_allclose(out, ref, rtol=1e-6);


0 comments on commit fd053e5

Please sign in to comment.
You can’t perform that action at this time.