Skip to content

Commit

Permalink
Merge branch 'develop' into issue_792_no_default_shape
Browse files Browse the repository at this point in the history
  • Loading branch information
sammlapp committed Aug 20, 2023
2 parents 0fd0252 + afc84f0 commit 751f93f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions opensoundscape/spectrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ def from_audio(
raise TypeError("Class method expects Audio class as input")

# Generate a linear-frequency spectrogram
# with raw stft values rather than decibels
# with linear stft values rather than dB values (dB_scale=False)
linear_spec = Spectrogram.from_audio(
audio,
window_type=window_type,
Expand All @@ -743,7 +743,7 @@ def from_audio(
overlap_fraction=overlap_fraction,
fft_size=fft_size,
decibel_limits=decibel_limits,
dB_scale=dB_scale,
dB_scale=False,
scaling=scaling,
)

Expand All @@ -753,7 +753,7 @@ def from_audio(
filter_bank = librosa.filters.mel(
sr=audio.sample_rate, n_fft=n_fft, n_mels=n_mels, norm=norm, htk=htk
)
# normalize filter bank: rows should sum to 1 #TODO: is this correct?
# normalize filter bank: rows should sum to 1
fb_constant = np.sum(filter_bank, 1).mean()
filter_bank = filter_bank / fb_constant

Expand Down
15 changes: 15 additions & 0 deletions tests/test_spectrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def veryshort_wav_str():
return "tests/audio/veryshort.wav"


@pytest.fixture()
def cswa_str():
return "tests/audio/aru_1.wav"


@pytest.fixture()
def spec():
return Spectrogram(
Expand Down Expand Up @@ -202,6 +207,16 @@ def test_to_image_with_bandpass():
)


def test_melspectrogram_underflow(cswa_str):
"""
Fixed a bug where log transform was applied twice.
Added a test to check the max value of dB scaled spec is as expected
"""
audio = Audio.from_file(cswa_str)
mel_spec = MelSpectrogram.from_audio(audio)
assert math.isclose(mel_spec.spectrogram.max(), -30.914056301116943, abs_tol=1e-4)


def test_to_image_shape(spec):
img = spec.to_image(shape=[5, 6], channels=2, return_type="torch")
assert list(img.shape) == [2, 5, 6] # channels, height, width
Expand Down

0 comments on commit 751f93f

Please sign in to comment.