modusa 0.4.30__tar.gz → 0.4.31__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: modusa
3
- Version: 0.4.30
3
+ Version: 0.4.31
4
4
  Summary: A modular signal analysis python library.
5
5
  Author-Email: Ankit Anand <ankit0.anand0@gmail.com>
6
6
  License: MIT
@@ -16,7 +16,7 @@ dependencies = [
16
16
  ]
17
17
  requires-python = ">=3.11"
18
18
  readme = "README.md"
19
- version = "0.4.30"
19
+ version = "0.4.31"
20
20
 
21
21
  [project.license]
22
22
  text = "MIT"
@@ -11,4 +11,7 @@ from modusa.tools import dist_plot, hill_plot, plot, fig
11
11
  # Synthsizing related
12
12
  from modusa.tools import synth_f0
13
13
 
14
- __version__ = "0.4.30" # This is dynamically used by the documentation, and pyproject.toml; Only need to change it here; rest gets taken care of.
14
+ # Audio features related
15
+ from modusa.tools import stft
16
+
17
+ __version__ = "0.4.31" # This is dynamically used by the documentation, and pyproject.toml; Only need to change it here; rest gets taken care of.
@@ -16,4 +16,7 @@ from .plotter import Fig as fig
16
16
  from .plotter import dist_plot, hill_plot, plot
17
17
 
18
18
  # Synthesizing related
19
- from .synth import synth_f0
19
+ from .synth import synth_f0
20
+
21
+ # Audio features
22
+ from .audio_stft import stft
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env python3
2
+
3
+ #---------------------------------
4
+ # Author: Ankit Anand
5
+ # Date: 23/10/25
6
+ # Email: ankit0.anand0@gmail.com
7
+ #---------------------------------
8
+
9
+ import numpy as np
10
+
11
+ def stft(y, sr, winlen=None, hoplen=None, gamma=None):
12
+ """
13
+ Compute spectrogram with just numpy.
14
+
15
+ Parameters
16
+ ----------
17
+ y: ndarray
18
+ - Audio signal.
19
+ sr: int
20
+ - Sampling rate of the audio signal.
21
+ winlen: int
22
+ - Window length in samples.
23
+ - Default: None => set at 0.064 sec
24
+ hoplen: int
25
+ - Hop length in samples.
26
+ - Default: None => set at one-forth of winlen
27
+ gamma: int | None
28
+ - Log compression factor.
29
+ - Add contrast to the plot.
30
+
31
+ Returns
32
+ -------
33
+ ndarray:
34
+ - Spectrogram matrix, complex is gamma is None else real
35
+ ndarray:
36
+ - Frequency bins in Hz.
37
+ ndarray:
38
+ - Timeframes in sec.
39
+ """
40
+
41
+ if winlen is None:
42
+ winlen = 2 ** int(np.log2(0.064 * sr))
43
+ if hoplen is None:
44
+ hoplen = int(winlen * 0.25)
45
+
46
+ # Estimating the shape of the S matrix
47
+ M = int(np.ceil(winlen / 2))
48
+ N = int(np.ceil((y.size - winlen) / hoplen))
49
+
50
+ # Initialize the S matrix
51
+ S = np.empty((M, N), dtype=np.complex64) # M X N => freq bin X time frame
52
+
53
+ # We will need a hann window
54
+ hann = np.hanning(winlen)
55
+
56
+ # Get the frames
57
+ frames = np.lib.stride_tricks.sliding_window_view(y, window_shape=winlen)[::hoplen] # frame X chunk
58
+
59
+ # Apply window to the frame
60
+ frames_windowed = frames * hann # frame X chunk
61
+
62
+ # Compute fft for each frame
63
+ S = np.fft.rfft(frames_windowed, n=winlen, axis=1).T # transpose to match shape (freq_bins, frame)
64
+
65
+ # Magnitude spectrogram
66
+ Sf = np.fft.rfftfreq(winlen, d=1/sr) # Frequency bins (Hz)
67
+ St = np.arange(N) * hoplen / sr # Time bins (sec)
68
+
69
+ if gamma is not None:
70
+ S = np.log1p(gamma * np.abs(S))
71
+
72
+ return S, Sf, St
File without changes
File without changes