CyclicnoiseGen_v1 Cyclic noise with a single parameter of beta. Pytorch v1 implementation assumes f_t is also fixed
| 244 | |
| 245 | |
| 246 | class CyclicNoiseGen_v1(torch.nn.Module): |
| 247 | """ CyclicnoiseGen_v1 |
| 248 | Cyclic noise with a single parameter of beta. |
| 249 | Pytorch v1 implementation assumes f_t is also fixed |
| 250 | """ |
| 251 | |
| 252 | def __init__(self, samp_rate, |
| 253 | noise_std=0.003, voiced_threshold=0): |
| 254 | super(CyclicNoiseGen_v1, self).__init__() |
| 255 | self.samp_rate = samp_rate |
| 256 | self.noise_std = noise_std |
| 257 | self.voiced_threshold = voiced_threshold |
| 258 | |
| 259 | self.l_pulse = PulseGen(samp_rate, pulse_amp=1.0, |
| 260 | noise_std=noise_std, |
| 261 | voiced_threshold=voiced_threshold) |
| 262 | self.l_conv = SignalsConv1d() |
| 263 | |
| 264 | def noise_decay(self, beta, f0mean): |
| 265 | """ decayed_noise = noise_decay(beta, f0mean) |
| 266 | decayed_noise = n[t]exp(-t * f_mean / beta / samp_rate) |
| 267 | |
| 268 | beta: (dim=1) or (batchsize=1, 1, dim=1) |
| 269 | f0mean (batchsize=1, 1, dim=1) |
| 270 | |
| 271 | decayed_noise (batchsize=1, length, dim=1) |
| 272 | """ |
| 273 | with torch.no_grad(): |
| 274 | # exp(-1.0 n / T) < 0.01 => n > -log(0.01)*T = 4.60*T |
| 275 | # truncate the noise when decayed by -40 dB |
| 276 | length = 4.6 * self.samp_rate / f0mean |
| 277 | length = length.int() |
| 278 | time_idx = torch.arange(0, length, device=beta.device) |
| 279 | time_idx = time_idx.unsqueeze(0).unsqueeze(2) |
| 280 | time_idx = time_idx.repeat(beta.shape[0], 1, beta.shape[2]) |
| 281 | |
| 282 | noise = torch.randn(time_idx.shape, device=beta.device) |
| 283 | |
| 284 | # due to Pytorch implementation, use f0_mean as the f0 factor |
| 285 | decay = torch.exp(-time_idx * f0mean / beta / self.samp_rate) |
| 286 | return noise * self.noise_std * decay |
| 287 | |
| 288 | def forward(self, f0s, beta): |
| 289 | """ Producde cyclic-noise |
| 290 | """ |
| 291 | # pulse train |
| 292 | pulse_train, sine_wav, uv, noise = self.l_pulse(f0s) |
| 293 | pure_pulse = pulse_train - noise |
| 294 | |
| 295 | # decayed_noise (length, dim=1) |
| 296 | if (uv < 1).all(): |
| 297 | # all unvoiced |
| 298 | cyc_noise = torch.zeros_like(sine_wav) |
| 299 | else: |
| 300 | f0mean = f0s[uv > 0].mean() |
| 301 | |
| 302 | decayed_noise = self.noise_decay(beta, f0mean)[0, :, :] |
| 303 | # convolute |