xref: /aosp_15_r20/external/libopus/dnn/torch/fargan/rc.py (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1import torch
2
3
4
5def rc2lpc(rc):
6    order = rc.shape[-1]
7    lpc=rc[...,0:1]
8    for i in range(1, order):
9        lpc = torch.cat([lpc + rc[...,i:i+1]*torch.flip(lpc,dims=(-1,)), rc[...,i:i+1]], -1)
10        #print("to:", lpc)
11    return lpc
12
13def lpc2rc(lpc):
14    order = lpc.shape[-1]
15    rc = lpc[...,-1:]
16    for i in range(order-1, 0, -1):
17        ki = lpc[...,-1:]
18        lpc = lpc[...,:-1]
19        lpc = (lpc - ki*torch.flip(lpc,dims=(-1,)))/(1 - ki*ki)
20        rc = torch.cat([lpc[...,-1:] , rc], -1)
21    return rc
22
23if __name__ == "__main__":
24    rc = torch.tensor([[.5, -.5, .6, -.6]])
25    print(rc)
26    lpc = rc2lpc(rc)
27    print(lpc)
28    rc2 = lpc2rc(lpc)
29    print(rc2)
30