Returns an array containing the specified window. This window is intended to be used with `stft`. The following window types are supported: - `"boxcar"`: a rectangular window - `"hamming"`: the Hamming window - `"hann"`: the Hann window - `"povey"`: the Pov
(
window_length: int,
name: str = "hann",
periodic: bool = True,
frame_length: Optional[int] = None,
center: bool = True,
)
| 317 | |
| 318 | |
| 319 | def window_function( |
| 320 | window_length: int, |
| 321 | name: str = "hann", |
| 322 | periodic: bool = True, |
| 323 | frame_length: Optional[int] = None, |
| 324 | center: bool = True, |
| 325 | ) -> np.ndarray: |
| 326 | """ |
| 327 | Returns an array containing the specified window. This window is intended to be used with `stft`. |
| 328 | |
| 329 | The following window types are supported: |
| 330 | |
| 331 | - `"boxcar"`: a rectangular window |
| 332 | - `"hamming"`: the Hamming window |
| 333 | - `"hann"`: the Hann window |
| 334 | - `"povey"`: the Povey window |
| 335 | |
| 336 | Args: |
| 337 | window_length (`int`): |
| 338 | The length of the window in samples. |
| 339 | name (`str`, *optional*, defaults to `"hann"`): |
| 340 | The name of the window function. |
| 341 | periodic (`bool`, *optional*, defaults to `True`): |
| 342 | Whether the window is periodic or symmetric. |
| 343 | frame_length (`int`, *optional*): |
| 344 | The length of the analysis frames in samples. Provide a value for `frame_length` if the window is smaller |
| 345 | than the frame length, so that it will be zero-padded. |
| 346 | center (`bool`, *optional*, defaults to `True`): |
| 347 | Whether to center the window inside the FFT buffer. Only used when `frame_length` is provided. |
| 348 | |
| 349 | Returns: |
| 350 | `np.ndarray` of shape `(window_length,)` or `(frame_length,)` containing the window. |
| 351 | """ |
| 352 | length = window_length + 1 if periodic else window_length |
| 353 | |
| 354 | if name == "boxcar": |
| 355 | window = np.ones(length) |
| 356 | elif name in ["hamming", "hamming_window"]: |
| 357 | window = np.hamming(length) |
| 358 | elif name in ["hann", "hann_window"]: |
| 359 | window = np.hanning(length) |
| 360 | elif name in ["povey"]: |
| 361 | window = np.power(np.hanning(length), 0.85) |
| 362 | else: |
| 363 | raise ValueError(f"Unknown window function '{name}'") |
| 364 | |
| 365 | if periodic: |
| 366 | window = window[:-1] |
| 367 | |
| 368 | if frame_length is None: |
| 369 | return window |
| 370 | |
| 371 | if window_length > frame_length: |
| 372 | raise ValueError( |
| 373 | f"Length of the window ({window_length}) may not be larger than frame_length ({frame_length})" |
| 374 | ) |
| 375 | |
| 376 | padded_window = np.zeros(frame_length) |
no outgoing calls