|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
from scipy.io import wavfile
|
|
import torch
|
|
|
|
|
|
def minmax_norm_diff(tensor: torch.Tensor, vmax: float = 2.5, vmin: float = -12) -> torch.Tensor:
|
|
tensor = torch.clip(tensor, vmin, vmax)
|
|
tensor = 2 * (tensor - vmin) / (vmax - vmin) - 1
|
|
return tensor
|
|
|
|
|
|
def reverse_minmax_norm_diff(tensor: torch.Tensor, vmax: float = 2.5, vmin: float = -12) -> torch.Tensor:
|
|
tensor = torch.clip(tensor, -1.0, 1.0)
|
|
tensor = (tensor + 1) / 2
|
|
tensor = tensor * (vmax - vmin) + vmin
|
|
return tensor
|
|
|
|
|
|
def scale_shift(x, scale, shift):
|
|
return (x+shift) * scale
|
|
|
|
|
|
def scale_shift_re(x, scale, shift):
|
|
return (x/scale) - shift
|
|
|
|
|
|
def align_seq(source, target_length, mapping_method='hard'):
|
|
source_len = source.shape[1]
|
|
if mapping_method == 'hard':
|
|
mapping_idx = np.round(np.arange(target_length) * source_len / target_length)
|
|
output = source[:, mapping_idx]
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
return output |