Skip to content
This repository was archived by the owner on Aug 11, 2026. It is now read-only.

Commit 3b30cfd

Browse files
committed
fix: add logarithmic frequency scale to spectrogram matplotlib plots
- Add log_freq parameter to plot_on_axis() and to_matplotlib() (default: True) - Fixes issue where CQT log-spaced frequency bins were displayed with linear scale - Sets y-axis to log scale automatically for proper CQT representation
1 parent 8114035 commit 3b30cfd

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

idtap/spectrogram.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ def plot_on_axis(self, ax: 'Axes',
313313
cmap: str = 'viridis',
314314
alpha: float = 1.0,
315315
zorder: int = 0,
316+
log_freq: bool = True,
316317
**imshow_kwargs) -> 'AxesImage':
317318
"""Plot spectrogram on an existing matplotlib axis (for overlays).
318319
@@ -325,6 +326,7 @@ def plot_on_axis(self, ax: 'Axes',
325326
cmap: Matplotlib colormap name
326327
alpha: Transparency (0.0-1.0), useful for subtle underlays
327328
zorder: Drawing order (0 = background, higher = foreground)
329+
log_freq: Whether to use logarithmic frequency scale (default: True)
328330
**imshow_kwargs: Additional arguments passed to ax.imshow()
329331
330332
Returns:
@@ -353,6 +355,12 @@ def plot_on_axis(self, ax: 'Axes',
353355
**imshow_kwargs
354356
)
355357

358+
# Set log scale for frequency axis (CQT is log-spaced)
359+
if log_freq:
360+
ax.set_yscale('log')
361+
# Set reasonable y-axis limits
362+
ax.set_ylim(self.freq_range[0], self.freq_range[1])
363+
356364
return im
357365

358366
def to_image(self, width: Optional[int] = None,
@@ -417,7 +425,8 @@ def to_matplotlib(self, figsize: Tuple[float, float] = (12, 6),
417425
power: float = 1.0,
418426
cmap: str = 'viridis',
419427
show_colorbar: bool = True,
420-
show_axes: bool = True) -> 'Figure':
428+
show_axes: bool = True,
429+
log_freq: bool = True) -> 'Figure':
421430
"""Generate standalone matplotlib Figure for publication.
422431
423432
Use this for quick visualization. For overlays and custom plots,
@@ -429,6 +438,7 @@ def to_matplotlib(self, figsize: Tuple[float, float] = (12, 6),
429438
cmap: Matplotlib colormap name
430439
show_colorbar: Whether to show colorbar
431440
show_axes: Whether to show frequency/time axis labels
441+
log_freq: Whether to use logarithmic frequency scale (default: True)
432442
433443
Returns:
434444
Matplotlib Figure object
@@ -438,7 +448,7 @@ def to_matplotlib(self, figsize: Tuple[float, float] = (12, 6),
438448
fig, ax = plt.subplots(figsize=figsize)
439449

440450
# Use plot_on_axis internally
441-
im = self.plot_on_axis(ax, power=power, cmap=cmap)
451+
im = self.plot_on_axis(ax, power=power, cmap=cmap, log_freq=log_freq)
442452

443453
if show_axes:
444454
ax.set_xlabel('Time (s)')

0 commit comments

Comments
 (0)