Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH gtgram: add f_max parameter #9

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/gammatone/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def erb_space(low_freq=DEFAULT_LOW_FREQ, high_freq=DEFAULT_HIGH_FREQ, num=DEFAUL
return erb_point(low_freq, high_freq, np.arange(1, num + 1) / num)


def centre_freqs(fs, num_freqs, cutoff):
def centre_freqs(fs, num_freqs, cutoff, f_max=None):
"""
Calculates an array of centre frequencies (for :func:`make_erb_filters`)
from a sampling frequency, lower cutoff frequency and the desired number of
Expand All @@ -66,9 +66,12 @@ def centre_freqs(fs, num_freqs, cutoff):
:param num_freqs: number of centre frequencies to calculate
:type num_freqs: int
:param cutoff: lower cutoff frequency
:param f_max: upper cutoff frequency (default ``fs / 2``)
:return: same as :func:`erb_space`
"""
return erb_space(cutoff, fs / 2, num_freqs)
if f_max is None:
f_max = fs / 2
return erb_space(cutoff, f_max, num_freqs)


def make_erb_filters(fs, centre_freqs, width=1.0):
Expand Down
12 changes: 6 additions & 6 deletions src/gammatone/gtgram.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ def gtgram_strides(fs, window_time, hop_time, filterbank_cols):
return (nwin, hop_samples, columns)


def gtgram_xe(wave, fs, channels, f_min):
def gtgram_xe(wave, fs, channels, f_min, f_max):
"""Calculate the intermediate ERB filterbank processed matrix"""
cfs = centre_freqs(fs, channels, f_min)
cfs = centre_freqs(fs, channels, f_min, f_max)
fcoefs = np.flipud(make_erb_filters(fs, cfs))
xf = erb_filterbank(wave, fcoefs)
xe = np.power(xf, 2)
return xe


def gtgram(wave, fs, window_time, hop_time, channels, f_min):
def gtgram(wave, fs, window_time, hop_time, channels, f_min, f_max=None):
"""
Calculate a spectrogram-like time frequency magnitude array based on
gammatone subband filters. The waveform ``wave`` (at sample rate ``fs``) is
passed through an multi-channel gammatone auditory model filterbank, with
passed through a multi-channel gammatone auditory model filterbank, with
lowest frequency ``f_min`` and highest frequency ``f_max``. The outputs of
each band then have their energy integrated over windows of ``window_time``
seconds, advancing by ``hop_time`` secs for successive columns. These
Expand All @@ -58,10 +58,10 @@ def gtgram(wave, fs, window_time, hop_time, channels, f_min):
|
| (c) 2013 Jason Heeris (Python implementation)
"""
xe = gtgram_xe(wave, fs, channels, f_min)
xe = gtgram_xe(wave, fs, channels, f_min, f_max)

nwin, hop_samples, ncols = gtgram_strides(fs, window_time, hop_time, xe.shape[1])
channels, ncols = int(channels), int(ncols) # typing fro some compatibility reasons
channels, ncols = int(channels), int(ncols) # typing for some compatibility reasons
y = np.zeros((channels, ncols))

for cnum in range(ncols):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_cfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
((22050, 100, 10), (10, 11025, 100)),
((22050, 1000, 100), (100, 11025, 1000)),
((160000, 500, 200), (200, 80000, 500)),
# add f_max parameter
((22050, 100, 100, 10000), (100, 10000, 100)),
),
)
def test_centre_freqs(erb_space_mock, args, params):
Expand Down
Loading