Download code/omini/data/staqa_preprocessing.py from zhizeng115/EchoScout-FourTask: direct link, hf CLI and curl.
- Browser
- Download file 2.01 kB
-
https://huggingface.co/datasets/zhizeng115/EchoScout-FourTask/resolve/main/code/omini/data/staqa_preprocessing.py
- Command line
-
hf download hf://datasets/zhizeng115/EchoScout-FourTask/code/omini/data/staqa_preprocessing.py
-
curl -L -o staqa_preprocessing.py https://huggingface.co/datasets/zhizeng115/EchoScout-FourTask/resolve/main/code/omini/data/staqa_preprocessing.py
2.01 kB
| """Canonical waveform preprocessing, extracted without numerical changes.""" | |
| import numpy as np | |
| import torch | |
| from scipy import signal | |
| def prepare_staqa_waveform( | |
| wav: np.ndarray, | |
| sample_rate: int, | |
| *, | |
| target_sample_rate: int = 32_000, | |
| target_length: int = 320_000, | |
| normalize: bool = True, | |
| ) -> torch.Tensor: | |
| """Return the canonical two-channel, fixed-length StAQA waveform.""" | |
| if ( | |
| isinstance(sample_rate, bool) | |
| or not isinstance(sample_rate, int) | |
| or sample_rate <= 0 | |
| ): | |
| raise ValueError("sample_rate must be a positive integer") | |
| if target_sample_rate <= 0: | |
| raise ValueError("target_sample_rate must be positive") | |
| if target_length <= 0: | |
| raise ValueError("target_length must be positive") | |
| waveform = np.asarray(wav).T.astype(np.float32) | |
| if waveform.ndim != 2: | |
| raise ValueError( | |
| "StAQA waveform must be [samples, channels], " | |
| f"got shape {tuple(np.asarray(wav).shape)}" | |
| ) | |
| if waveform.shape[0] == 1: | |
| waveform = np.repeat(waveform, 2, axis=0) | |
| elif waveform.shape[0] > 2: | |
| waveform = waveform[:2] | |
| if int(sample_rate) != int(target_sample_rate): | |
| waveform = signal.resample_poly( | |
| waveform, | |
| int(target_sample_rate), | |
| int(sample_rate), | |
| axis=-1, | |
| ).astype(np.float32) | |
| if normalize: | |
| for channel in range(waveform.shape[0]): | |
| rms = float(np.sqrt(np.mean(waveform[channel] ** 2))) | |
| if rms > 0: | |
| waveform[channel] = waveform[channel] * 10 ** ( | |
| (-14.0 - 20 * np.log10(rms)) / 20 | |
| ) | |
| if waveform.shape[1] >= target_length: | |
| waveform = waveform[:, :target_length] | |
| else: | |
| pad = np.zeros( | |
| (waveform.shape[0], target_length - waveform.shape[1]), | |
| dtype=np.float32, | |
| ) | |
| waveform = np.concatenate([waveform, pad], axis=1) | |
| return torch.from_numpy(waveform) | |