Skip to content

Fast and differentiable time domain all-pole filter in PyTorch.

License

Notifications You must be signed in to change notification settings

DiffAPF/torchlpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TorchLPC

PyPI version

torchlpc provides a PyTorch implementation of the Linear Predictive Coding (LPC) filter, also known as all-pole filter. It's fast, differentiable, and supports batched inputs with time-varying filter coefficients.

Given an input signal x R T and time-varying LPC coefficients A R T × N with an order of N , the LPC filter is defined as:

y t = x t i = 1 N A t , i y t i .

Usage

import torch
from torchlpc import sample_wise_lpc

# Create a batch of 10 signals, each with 100 time steps
x = torch.randn(10, 100)

# Create a batch of 10 sets of LPC coefficients, each with 100 time steps and an order of 3
A = torch.randn(10, 100, 3)

# Apply LPC filtering
y = sample_wise_lpc(x, A)

# Optionally, you can provide initial values for the output signal (default is 0)
zi = torch.randn(10, 3)
y = sample_wise_lpc(x, A, zi=zi)

Installation

pip install torchlpc

or from source

pip install git+https://github.com/DiffAPF/torchlpc.git

Derivation of the gradients of the LPC filter

The details of the derivation can be found in our preprints12. We show that, given the instataneous gradient L y t where L is the loss function, the gradients of the LPC filter with respect to the input signal x and the filter coefficients A can be expresssed also through a time-varying filter:

L x t = L y t i = 1 N A t + i , i L x t + i

L A = | L x 1 0 0 0 L x 2 0 0 0 L x t | | y 0 y 1 y N + 1 y 1 y 0 y N + 2 y T 1 y T 2 y T N | .

Gradients for the initial condition y t | t 0

The initial conditions provide an entry point at t = 1 for filtering, as we cannot evaluate t = . Let us assume A t , : | t 0 = 0 so y t | t 0 = x t | t 0 , which also means L y t | t 0 = L x t | t 0 . Thus, the initial condition gradients are

L y t = L x t = i = 1 t N A t + i , i L x t + i for  N < t 0.

In practice, we pad N and N × N zeros to the beginning of L y and A before evaluating L x . The first N outputs are the gradients to y t | t 0 and the rest are to x t | t > 0 .

Time-invariant filtering

In the time-invariant setting, A t , i = A 1 , i t [ 1 , T ] and the filter is simplified to

y t = x t i = 1 N a i y t i , a = A 1 , : .

The gradients L x are filtering L y with a backwards in time, same as in the time-varying case. L a is simply doing a vector-matrix multiplication:

L a T = L x T | y 0 y 1 y N + 1 y 1 y 0 y N + 2 y T 1 y T 2 y T N | .

This algorithm is more efficient than 3 because it only needs one pass of filtering to get the two gradients while the latter needs two.

TODO

  • Use PyTorch C++ extension for faster computation.
  • Use native CUDA kernels for GPU computation.
  • Add examples.

Related Projects

  • torchcomp: differentiable compressors that use torchlpc for differentiable backpropagation.
  • jaxpole: equivalent implementation in JAX by @rodrigodzf.

Citation

If you find this repository useful in your research, please cite our work with the following BibTex entries:

@inproceedings{ycy2024diffapf,
    title={Differentiable All-pole Filters for Time-varying Audio Systems},
    author={Chin-Yun Yu and Christopher Mitcheltree and Alistair Carson and Stefan Bilbao and Joshua D. Reiss and György Fazekas},
    booktitle={International Conference on Digital Audio Effects (DAFx)},
    year={2024},
    pages={345--352},
}

@inproceedings{ycy2024golf,
    title     = {Differentiable Time-Varying Linear Prediction in the Context of End-to-End Analysis-by-Synthesis},
    author    = {Chin-Yun Yu and György Fazekas},
    year      = {2024},
    booktitle = {Proc. Interspeech},
    pages     = {1820--1824},
    doi       = {10.21437/Interspeech.2024-1187},
}

Footnotes

  1. Differentiable All-pole Filters for Time-varying Audio Systems.

  2. Differentiable Time-Varying Linear Prediction in the Context of End-to-End Analysis-by-Synthesis.

  3. Singing Voice Synthesis Using Differentiable LPC and Glottal-Flow-Inspired Wavetables.