Calculates spectrograms for a list of waveforms using the Short-Time Fourier Transform, optimized for batch processing. This function extends the capabilities of the `spectrogram` function to handle multiple waveforms efficiently by leveraging broadcasting. It supports generating vario
(
waveform_list: List[np.ndarray],
window: np.ndarray,
frame_length: int,
hop_length: int,
fft_length: Optional[int] = None,
power: Optional[float] = 1.0,
center: bool = True,
pad_mode: str = "reflect",
onesided: bool = True,
preemphasis: Optional[float] = None,
mel_filters: Optional[np.ndarray] = None,
mel_floor: float = 1e-10,
log_mel: Optional[str] = None,
reference: float = 1.0,
min_value: float = 1e-10,
db_range: Optional[float] = None,
remove_dc_offset: Optional[bool] = None,
dtype: np.dtype = np.float32,
)
| 582 | |
| 583 | |
| 584 | def spectrogram_batch( |
| 585 | waveform_list: List[np.ndarray], |
| 586 | window: np.ndarray, |
| 587 | frame_length: int, |
| 588 | hop_length: int, |
| 589 | fft_length: Optional[int] = None, |
| 590 | power: Optional[float] = 1.0, |
| 591 | center: bool = True, |
| 592 | pad_mode: str = "reflect", |
| 593 | onesided: bool = True, |
| 594 | preemphasis: Optional[float] = None, |
| 595 | mel_filters: Optional[np.ndarray] = None, |
| 596 | mel_floor: float = 1e-10, |
| 597 | log_mel: Optional[str] = None, |
| 598 | reference: float = 1.0, |
| 599 | min_value: float = 1e-10, |
| 600 | db_range: Optional[float] = None, |
| 601 | remove_dc_offset: Optional[bool] = None, |
| 602 | dtype: np.dtype = np.float32, |
| 603 | ) -> List[np.ndarray]: |
| 604 | """ |
| 605 | Calculates spectrograms for a list of waveforms using the Short-Time Fourier Transform, optimized for batch processing. |
| 606 | This function extends the capabilities of the `spectrogram` function to handle multiple waveforms efficiently by leveraging broadcasting. |
| 607 | |
| 608 | It supports generating various types of spectrograms: |
| 609 | |
| 610 | - amplitude spectrogram (`power = 1.0`) |
| 611 | - power spectrogram (`power = 2.0`) |
| 612 | - complex-valued spectrogram (`power = None`) |
| 613 | - log spectrogram (use `log_mel` argument) |
| 614 | - mel spectrogram (provide `mel_filters`) |
| 615 | - log-mel spectrogram (provide `mel_filters` and `log_mel`) |
| 616 | |
| 617 | How this works: |
| 618 | |
| 619 | 1. The input waveform is split into frames of size `frame_length` that are partially overlapping by `frame_length |
| 620 | - hop_length` samples. |
| 621 | 2. Each frame is multiplied by the window and placed into a buffer of size `fft_length`. |
| 622 | 3. The DFT is taken of each windowed frame. |
| 623 | 4. The results are stacked into a spectrogram. |
| 624 | |
| 625 | We make a distinction between the following "blocks" of sample data, each of which may have a different lengths: |
| 626 | |
| 627 | - The analysis frame. This is the size of the time slices that the input waveform is split into. |
| 628 | - The window. Each analysis frame is multiplied by the window to avoid spectral leakage. |
| 629 | - The FFT input buffer. The length of this determines how many frequency bins are in the spectrogram. |
| 630 | |
| 631 | In this implementation, the window is assumed to be zero-padded to have the same size as the analysis frame. A |
| 632 | padded window can be obtained from `window_function()`. The FFT input buffer may be larger than the analysis frame, |
| 633 | typically the next power of two. |
| 634 | |
| 635 | Note: This function is designed for efficient batch processing of multiple waveforms but retains compatibility with individual waveform processing methods like `librosa.stft`. |
| 636 | |
| 637 | Args: |
| 638 | waveform_list (`List[np.ndarray]` with arrays of shape `(length,)`): |
| 639 | The list of input waveforms, each a single-channel (mono) signal. |
| 640 | window (`np.ndarray` of shape `(frame_length,)`): |
| 641 | The windowing function to apply, including zero-padding if necessary. |