Skip to content

Commit

Permalink
Merge pull request #46 from hadware/type-hinting
Browse files Browse the repository at this point in the history
Type hinting and general code review
  • Loading branch information
SuperKogito committed Dec 30, 2022
2 parents 050ae0c + 1941686 commit 378d194
Show file tree
Hide file tree
Showing 26 changed files with 1,155 additions and 1,071 deletions.
1 change: 1 addition & 0 deletions requirements.txt
@@ -1,2 +1,3 @@
numpy>=1.21
scipy>=1.7.3
typing_extensions
8 changes: 4 additions & 4 deletions setup.py
Expand Up @@ -7,11 +7,11 @@
here = Path(__file__).parent

# get readme text
with open(here / "README.md") as filey:
LONG_DESCRIPTION = filey.read()
with open(here / "README.md") as readme_file:
LONG_DESCRIPTION = readme_file.read()

with open(here / "requirements.txt") as reqfile:
requirements = reqfile.read().split()
with open(here / "requirements.txt") as req_file:
requirements = req_file.read().split()

setup(
name="spafe",
Expand Down
31 changes: 17 additions & 14 deletions spafe/fbanks/bark_fbanks.py
Expand Up @@ -6,19 +6,22 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""
from typing import Optional

import numpy as np
from ..utils.filters import scale_fbank
from ..utils.converters import hz2bark, bark2hz

from ..utils.converters import hz2bark, bark2hz, BarkConversionApproach
from ..utils.exceptions import ParameterError, ErrorMsgs
from ..utils.filters import scale_fbank, ScaleType


def Fm(fb, fc):
def Fm(fb: float, fc: float) -> float:
"""
Compute a Bark filter around a certain center frequency in bark [Hermansky]_.
Args:
fb (int): frequency in Bark.
fc (int): center frequency in Bark.
fb (float): frequency in Bark.
fc (float): center frequency in Bark.
Returns:
(float) : associated Bark filter value/amplitude.
Expand All @@ -38,13 +41,13 @@ def Fm(fb, fc):


def bark_filter_banks(
nfilts=24,
nfft=512,
fs=16000,
low_freq=0,
high_freq=None,
scale="constant",
conversion_approach="Wang",
nfilts: int = 24,
nfft: int = 512,
fs: int = 16000,
low_freq: float = 0,
high_freq: Optional[float] = None,
scale: ScaleType = "constant",
conversion_approach: BarkConversionApproach = "Wang",
):
"""
Compute Bark filter banks. The filters are stored in the rows, the columns
Expand All @@ -57,9 +60,9 @@ def bark_filter_banks(
(Default is 512).
fs (int) : sample rate/ sampling frequency of the signal.
(Default 16000 Hz).
low_freq (int) : lowest band edge of mel filters.
low_freq (float) : lowest band edge of mel filters.
(Default 0 Hz).
high_freq (int) : highest band edge of mel filters.
high_freq (float) : highest band edge of mel filters.
(Default is fs/2).
scale (str) : monotonicity behavior of the filter banks.
(Default is "constant").
Expand Down
42 changes: 24 additions & 18 deletions spafe/fbanks/gammatone_fbanks.py
Expand Up @@ -6,25 +6,29 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""
from typing import Optional, Tuple

import numpy as np
from ..utils.converters import hz2erb
from ..utils.filters import scale_fbank
from ..utils.exceptions import ParameterError, ErrorMsgs

from ..utils.converters import hz2erb, ErbConversionApproach
from ..utils.exceptions import ParameterError, ErrorMsgs
from ..utils.filters import scale_fbank, ScaleType

# Slaney's ERB Filter constants
EarQ = 9.26449
minBW = 24.7


def generate_center_frequencies(min_freq, max_freq, nfilts):
def generate_center_frequencies(
min_freq: float, max_freq: float, nfilts: int
) -> np.ndarray:
"""
Compute center frequencies in the ERB scale.
Args:
min_freq (int) : minimum frequency of the center frequencies domain.
max_freq (int) : maximum frequency of the center frequencies domain.
nfilts (int) : number of filters <=> number of center frequencies to compute.
min_freq (float) : minimum frequency of the center frequencies' domain.
max_freq (float) : maximum frequency of the center frequencies' domain.
nfilts (int) : number of filters <=> number of center frequencies to compute.
Returns:
(numpy.ndarray) : array of center frequencies.
Expand All @@ -40,7 +44,9 @@ def generate_center_frequencies(min_freq, max_freq, nfilts):
return center_freqs[::-1]


def compute_gain(fcs, B, wT, T):
def compute_gain(
fcs: np.ndarray, B: np.ndarray, wT: np.ndarray, T: float
) -> Tuple[np.ndarray, np.ndarray]:
"""
Compute Gain and matrixify computation for speed purposes [Ellis-spectrogram]_.
Expand Down Expand Up @@ -79,14 +85,14 @@ def compute_gain(fcs, B, wT, T):


def gammatone_filter_banks(
nfilts=24,
nfft=512,
fs=16000,
low_freq=0,
high_freq=None,
scale="constant",
order=4,
conversion_approach="Glasberg",
nfilts: int = 24,
nfft: int = 512,
fs: int = 16000,
low_freq: float = 0,
high_freq: Optional[float] = None,
scale: ScaleType = "constant",
order: int = 4,
conversion_approach: ErbConversionApproach = "Glasberg",
):
"""
Compute Gammatone-filter banks. The filters are stored in the rows, the columns
Expand All @@ -99,9 +105,9 @@ def gammatone_filter_banks(
(Default is 512).
fs (int) : sample rate/ sampling frequency of the signal.
(Default is 16000 Hz).
low_freq (int) : lowest band edge of mel filters.
low_freq (float) : lowest band edge of mel filters.
(Default is 0 Hz).
high_freq (int) : highest band edge of mel filters.
high_freq (float) : highest band edge of mel filters.
(Default samplerate/2).
scale (str) : monotonicity behavior of the filter banks.
(Default is "constant").
Expand Down
41 changes: 22 additions & 19 deletions spafe/fbanks/linear_fbanks.py
Expand Up @@ -6,36 +6,39 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""
from typing import Optional

import numpy as np

from .mel_fbanks import mel_filter_banks_helper
from ..utils.exceptions import ParameterError, ErrorMsgs
from ..utils.filters import ScaleType


def linear_filter_banks(
nfilts=24,
nfft=512,
fs=16000,
low_freq=0,
high_freq=None,
scale="constant",
nfilts: int = 24,
nfft: int = 512,
fs: int = 16000,
low_freq: float = 0,
high_freq: Optional[float] = None,
scale: ScaleType = "constant",
):
"""
Compute linear-filter banks. The filters are stored in the rows, the columns
correspond to fft bins.
Args:
nfilts (int) : the number of filters in the filter bank.
(Default 20).
nfft (int) : the FFT size.
(Default is 512).
fs (int) : sample rate/ sampling frequency of the signal.
(Default 16000 Hz).
low_freq (int) : lowest band edge of linear filters.
(Default 0 Hz).
high_freq (int) : highest band edge of linear filters.
(Default samplerate/2).
scale (str) : monotonicity behavior of the filter banks.
(Default is "constant").
nfilts (int) : the number of filters in the filter bank.
(Default 20).
nfft (int) : the FFT size.
(Default is 512).
fs (int) : sample rate/ sampling frequency of the signal.
(Default 16000 Hz).
low_freq (float) : lowest band edge of linear filters.
(Default 0 Hz).
high_freq (float) : highest band edge of linear filters.
(Default samplerate/2).
scale (str) : monotonicity behavior of the filter banks.
(Default is "constant").
Returns:
(tuple) :
Expand Down
62 changes: 32 additions & 30 deletions spafe/fbanks/mel_fbanks.py
Expand Up @@ -6,21 +6,24 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""
from typing import Optional

import numpy as np
from ..utils.filters import scale_fbank
from ..utils.converters import hz2mel, mel2hz

from ..utils.converters import hz2mel, mel2hz, MelConversionApproach
from ..utils.exceptions import ParameterError, ErrorMsgs
from ..utils.filters import scale_fbank, ScaleType


def mel_filter_banks_helper(
nfilts=24,
nfft=512,
fs=16000,
low_freq=0,
high_freq=None,
scale="constant",
nfilts: int = 24,
nfft: int = 512,
fs: int = 16000,
low_freq: float = 0,
high_freq: Optional[float] = None,
scale: ScaleType = "constant",
fb_type="mel",
conversion_approach="Oshaghnessy",
conversion_approach: MelConversionApproach = "Oshaghnessy",
):
"""
Compute Mel-filter banks.The filters are stored in the rows, the columns
Expand All @@ -33,9 +36,9 @@ def mel_filter_banks_helper(
(Default is 512).
fs (int) : sample rate/ sampling frequency of the signal.
(Default 16000 Hz).
low_freq (int) : lowest band edge of mel filters.
low_freq (float) : lowest band edge of mel filters.
(Default 0 Hz).
high_freq (int) : highest band edge of mel filters.
high_freq (float) : highest band edge of mel filters.
(Default samplerate/2).
scale (str) : monotonicity behavior of the filter banks.
(Default is "constant").
Expand Down Expand Up @@ -102,7 +105,6 @@ def mel_filter_banks_helper(
for j, (center, lower, upper) in enumerate(
zip(center_freqs_hz, lower_edges_hz, upper_edges_hz)
):

left_slope = (freqs >= lower) == (freqs <= center)
fbank[j, left_slope] = (freqs[left_slope] - lower) / (center - lower)

Expand All @@ -117,13 +119,13 @@ def mel_filter_banks_helper(


def mel_filter_banks(
nfilts=24,
nfft=512,
fs=16000,
low_freq=0,
high_freq=None,
scale="constant",
conversion_approach="Oshaghnessy",
nfilts: int = 24,
nfft: int = 512,
fs: int = 16000,
low_freq: float = 0,
high_freq: Optional[float] = None,
scale: ScaleType = "constant",
conversion_approach: MelConversionApproach = "Oshaghnessy",
):
"""
Compute Mel-filter banks.The filters are stored in the rows, the columns
Expand All @@ -136,9 +138,9 @@ def mel_filter_banks(
(Default is 512).
fs (int) : sample rate/ sampling frequency of the signal.
(Default 16000 Hz).
low_freq (int) : lowest band edge of mel filters.
low_freq (float) : lowest band edge of mel filters.
(Default 0 Hz).
high_freq (int) : highest band edge of mel filters.
high_freq (float) : highest band edge of mel filters.
(Default samplerate/2).
scale (str) : monotonicity behavior of the filter banks.
(Default is "constant").
Expand Down Expand Up @@ -211,13 +213,13 @@ def mel_filter_banks(


def inverse_mel_filter_banks(
nfilts=24,
nfft=512,
fs=16000,
low_freq=0,
high_freq=None,
scale="constant",
conversion_approach="Oshaghnessy",
nfilts: int = 24,
nfft: int = 512,
fs: int = 16000,
low_freq: float = 0,
high_freq: Optional[float] = None,
scale: ScaleType = "constant",
conversion_approach: MelConversionApproach = "Oshaghnessy",
):
"""
Compute inverse Mel-filter banks. The filters are stored in the rows, the columns
Expand All @@ -230,9 +232,9 @@ def inverse_mel_filter_banks(
(Default is 512).
fs (int) : sample rate/ sampling frequency of the signal.
(Default 16000 Hz).
low_freq (int) : lowest band edge of mel filters.
low_freq (float) : lowest band edge of mel filters.
(Default 0 Hz).
high_freq (int) : highest band edge of mel filters.
high_freq (float) : highest band edge of mel filters.
(Default samplerate/2).
scale (str) : monotonicity behavior of the filter banks.
(Default is "constant").
Expand Down

0 comments on commit 378d194

Please sign in to comment.