xref: /aosp_15_r20/external/libopus/dnn/torch/neural-pitch/utils.py (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1"""
2Utility functions that are commonly used
3"""
4
5import numpy as np
6from scipy.signal import windows, lfilter
7from prettytable import PrettyTable
8
9
10# Source: https://gist.github.com/thongonary/026210fc186eb5056f2b6f1ca362d912
11def count_parameters(model):
12    table = PrettyTable(["Modules", "Parameters"])
13    total_params = 0
14    for name, parameter in model.named_parameters():
15        if not parameter.requires_grad: continue
16        param = parameter.numel()
17        table.add_row([name, param])
18        total_params+=param
19    print(table)
20    print(f"Total Trainable Params: {total_params}")
21    return total_params
22
23def stft(x, w = 'boxcar', N = 320, H = 160):
24    x = np.concatenate([x,np.zeros(N)])
25    # win_custom = np.concatenate([windows.hann(80)[:40],np.ones(240),windows.hann(80)[40:]])
26    return np.stack([np.fft.rfft(x[i:i + N]*windows.get_window(w,N)) for i in np.arange(0,x.shape[0]-N,H)])
27
28def random_filter(x):
29    # Randomly filter x with second order IIR filter with coefficients in between -3/8,3/8
30    filter_coeff = np.random.uniform(low =  -3.0/8, high = 3.0/8, size = 4)
31    b = [1,filter_coeff[0],filter_coeff[1]]
32    a = [1,filter_coeff[2],filter_coeff[3]]
33    return lfilter(b,a,x)
34
35def feature_xform(feature):
36    """
37    Take as input the (N * 256) xcorr features output by LPCNet and perform the following
38    1. Downsample and Upsample by 2 (followed by smoothing)
39    2. Append positional embeddings (of dim k) coresponding to each xcorr lag
40    """
41
42    from scipy.signal import resample_poly, lfilter
43
44
45    feature_US = lfilter([0.25,0.5,0.25],[1],resample_poly(feature,2,1,axis = 1),axis = 1)[:,:feature.shape[1]]
46    feature_DS = lfilter([0.5,0.5],[1],resample_poly(feature,1,2,axis = 1),axis = 1)
47    Z_append = np.zeros((feature.shape[0],feature.shape[1] - feature_DS.shape[1]))
48    feature_DS = np.concatenate([feature_DS,Z_append],axis = -1)
49
50    # pos_embedding = []
51    # for i in range(k):
52    #     pos_embedding.append(np.cos((2**i)*np.pi*((np.repeat(np.arange(feature.shape[1]).reshape(feature.shape[1],1),feature.shape[0],axis = 1)).T/(2*feature.shape[1]))))
53
54    # pos_embedding = np.stack(pos_embedding,axis = -1)
55
56    feature = np.stack((feature_DS,feature,feature_US),axis = -1)
57    # feature = np.concatenate((feature,pos_embedding),axis = -1)
58
59    return feature
60