Skip to content

Commit

Permalink
Merge pull request #63 from SuperKogito/fix-cqcc-shape
Browse files Browse the repository at this point in the history
fix cqcc size bug
  • Loading branch information
SuperKogito authored May 29, 2024
2 parents c7c8499 + 954e94d commit af577d7
Show file tree
Hide file tree
Showing 23 changed files with 41 additions and 7 deletions.
Empty file modified build.sh
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions spafe/fbanks/bark_fbanks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions spafe/fbanks/gammatone_fbanks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional, Tuple

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions spafe/fbanks/linear_fbanks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions spafe/fbanks/mel_fbanks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions spafe/features/bfcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down
10 changes: 6 additions & 4 deletions spafe/features/cqcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down Expand Up @@ -166,7 +167,7 @@ def cqcc(
normalize: Optional[NormalizationType] = None,
number_of_octaves: int = 7,
number_of_bins_per_octave: int = 24,
resampling_ratio: float = 0.95,
resampling_ratio: float = 1.0,
spectral_threshold: float = 0.005,
f0: float = 120,
q_rate: float = 1.0,
Expand Down Expand Up @@ -208,7 +209,7 @@ def cqcc(
number_of_bins_per_octave (int) : numbers of bins oer occtave.
(Default is 24).
resampling_ratio (float) : ratio to use for the uniform resampling.
(Default is 0.95).
(Default is 1.00).
spectral_threshold (float) : spectral threshold.
(Default is 0.005).
f0 (float) : fundamental frequency.
Expand All @@ -217,7 +218,7 @@ def cqcc(
(Default is 1.0).
Returns:
(numpy.ndarray) : 2d array of BFCC features (num_frames x num_ceps).
(numpy.ndarray) : 2d array of BFCC features (num_frames*resampling_ratio x num_ceps).
Tip:
- :code:`dct` : can take the following options [1, 2, 3, 4].
Expand Down Expand Up @@ -283,7 +284,7 @@ def cqcc(
# -> log(.)
# handle zeros: if feat is zero, we get problems with log
features_no_zero = zero_handling(x=power_spectrum)
log_features = np.log(features_no_zero)
log_features = np.log(features_no_zero.T)

# uniform resampling
resampled_features = resample(
Expand All @@ -300,4 +301,5 @@ def cqcc(
# normalization
if normalize:
cqccs = normalize_ceps(cqccs, normalize)

return cqccs
1 change: 1 addition & 0 deletions spafe/features/gfcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional, Tuple

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions spafe/features/lfcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions spafe/features/lpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions spafe/features/mfcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions spafe/features/msrcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions spafe/features/ngcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions spafe/features/pncc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions spafe/features/psrcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down
5 changes: 2 additions & 3 deletions spafe/features/rplp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import Optional

import numpy as np
Expand Down Expand Up @@ -148,9 +149,7 @@ def __rastaplp(

# equal loudness pre_emphasis
E = lambda w: ((w**2 + 56.8 * 10**6) * w**4) / (
(w**2 + 6.3 * 10**6)
* (w**2 + 0.38 * 10**9)
* (w**6 + 9.58 * 10**26)
(w**2 + 6.3 * 10**6) * (w**2 + 0.38 * 10**9) * (w**6 + 9.58 * 10**26)
)
Y = [E(w) for w in auditory_spectrum]

Expand Down
1 change: 1 addition & 0 deletions spafe/utils/cepstral.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

import numpy as np
from scipy.signal import lfilter
from typing_extensions import Literal
Expand Down
1 change: 1 addition & 0 deletions spafe/utils/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

import numpy as np
from typing_extensions import Literal

Expand Down
1 change: 1 addition & 0 deletions spafe/utils/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

import numpy as np
from scipy import signal
from typing_extensions import Literal
Expand Down
1 change: 1 addition & 0 deletions spafe/utils/spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

from typing import List

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions spafe/utils/vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

import numpy as np

from spafe.utils.converters import hz2mel, hz2bark, hz2erb
Expand Down
1 change: 1 addition & 0 deletions spafe/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
"""

__version__ = "0.3.2"
14 changes: 14 additions & 0 deletions tests/test_features_cqcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import scipy.io.wavfile
from spafe.utils import vis
from spafe.features.cqcc import cqcc
from spafe.utils.preprocessing import framing
from spafe.utils.exceptions import ParameterError
from spafe.utils.cepstral import normalize_ceps, lifter_ceps

Expand All @@ -18,6 +19,7 @@
@pytest.mark.parametrize("dct_type", [1, 2, 4])
@pytest.mark.parametrize("lifter", [None, 0.7, -7])
@pytest.mark.parametrize("normalize", [None, "mvn", "ms"])
@pytest.mark.parametrize("resampling_ratio", [1.0, 0.9, 0.3])
def test_cqcc(
sig,
fs,
Expand All @@ -29,6 +31,7 @@ def test_cqcc(
dct_type,
lifter,
normalize,
resampling_ratio,
):
"""
test cqcc features module for the following:
Expand All @@ -40,6 +43,9 @@ def test_cqcc(
- check normalization.
- check liftering.
"""
# get number of frames
frames, frame_length = framing(sig=sig, fs=fs, win_len=0.025, win_hop=0.01)
num_frames = len(frames)

# check error for number of filters is smaller than number of cepstrums
with pytest.raises(ParameterError):
Expand All @@ -50,6 +56,7 @@ def test_cqcc(
nfft=nfft,
low_freq=low_freq,
high_freq=fs,
resampling_ratio=resampling_ratio,
)

# compute features
Expand All @@ -64,12 +71,17 @@ def test_cqcc(
dct_type=dct_type,
lifter=lifter,
normalize=normalize,
resampling_ratio=resampling_ratio,
)

# assert number of returned cepstrum coefficients
if not cqccs.shape[1] == num_ceps:
raise AssertionError

# assert number of returned cepstrum coefficients
if not cqccs.shape[0] == int(num_frames * resampling_ratio):
raise AssertionError

# check normalize
if normalize:
np.testing.assert_array_almost_equal(
Expand All @@ -86,6 +98,7 @@ def test_cqcc(
dct_type=dct_type,
lifter=lifter,
normalize=None,
resampling_ratio=resampling_ratio,
),
normalize,
),
Expand All @@ -108,6 +121,7 @@ def test_cqcc(
dct_type=dct_type,
lifter=None,
normalize=normalize,
resampling_ratio=resampling_ratio,
),
lifter,
),
Expand Down

0 comments on commit af577d7

Please sign in to comment.