(
y,
*,
n_fft=2048,
hop_length=None,
win_length=None,
window="hann",
center=True,
dtype=None,
pad_mode="reflect",
)
| 167 | |
| 168 | |
| 169 | def stft( |
| 170 | y, |
| 171 | *, |
| 172 | n_fft=2048, |
| 173 | hop_length=None, |
| 174 | win_length=None, |
| 175 | window="hann", |
| 176 | center=True, |
| 177 | dtype=None, |
| 178 | pad_mode="reflect", |
| 179 | ): |
| 180 | # By default, use the entire frame |
| 181 | if win_length is None: |
| 182 | win_length = n_fft |
| 183 | |
| 184 | # Set the default hop, if it's not already specified |
| 185 | if hop_length is None: |
| 186 | hop_length = int(win_length // 4) |
| 187 | |
| 188 | fft_window = get_window(window, win_length, fftbins=True) |
| 189 | |
| 190 | # Pad the window out to n_fft size |
| 191 | fft_window = pad_center(fft_window, size=n_fft) |
| 192 | |
| 193 | # Reshape so that the window can be broadcast |
| 194 | fft_window = expand_to(fft_window, ndim=1 + y.ndim, axes=-2) |
| 195 | |
| 196 | # Pad the time series so that frames are centered |
| 197 | if center: |
| 198 | if n_fft > y.shape[-1]: |
| 199 | print( |
| 200 | "n_fft={} is too small for input signal of length={}".format(n_fft, y.shape[-1]), |
| 201 | stacklevel=2, |
| 202 | ) |
| 203 | |
| 204 | padding = [(0, 0) for _ in range(y.ndim)] |
| 205 | padding[-1] = (int(n_fft // 2), int(n_fft // 2)) |
| 206 | y = np.pad(y, padding, mode=pad_mode) |
| 207 | |
| 208 | elif n_fft > y.shape[-1]: |
| 209 | raise RuntimeError( |
| 210 | "n_fft={} is too large for input signal of length={}".format(n_fft, y.shape[-1]) |
| 211 | ) |
| 212 | |
| 213 | # Window the time series. |
| 214 | |
| 215 | y_frames = extract_frames(y, frame_length=n_fft, hop_length=hop_length, axis=-1) |
| 216 | |
| 217 | if dtype is None: |
| 218 | dtype = dtype_r2c(y.dtype) |
| 219 | |
| 220 | # Pre-allocate the STFT matrix |
| 221 | shape = list(y_frames.shape) |
| 222 | shape[-2] = 1 + n_fft // 2 |
| 223 | stft_matrix = np.empty(shape, dtype=dtype, order="F") |
| 224 | |
| 225 | # Constrain STFT block sizes to 256 KB |
| 226 | MAX_MEM_BLOCK = 2**8 * 2**10 |
no test coverage detected