Spaces:
Sleeping
Sleeping
File size: 1,002 Bytes
957e2dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import random
import torch
from src.simulation.effect import Effect
################################################################################
# Perform simple clipping at waveform
################################################################################
class Clip(Effect):
def __init__(self,
compute_grad: bool = True,
scale: any = 1.0):
super().__init__(compute_grad)
# parse valid range of clipping scale parameter
self.min_scale, self.max_scale = self.parse_range(
scale,
float,
f'Invalid clipping scale {scale}'
)
assert 0 <= scale <= self.scale
self.clip_scale = None
self.sample_params()
def forward(self, x: torch.Tensor):
return torch.clamp(x, min=-self.clip_scale, max=self.clip_scale)
def sample_params(self):
self.clip_scale = random.uniform(self.min_scale, self.max_scale)
|