TorchLPC
June 3, 2025 · View on GitHub
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 and time-varying LPC coefficients with an order of , the LPC filter is defined as:
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)
# Return the delay values similar to `scipy.signal.lfilter`
y, zf = sample_wise_lpc(x, A, zi=zi, return_zf=True)
Installation
pip install torchlpc
or from source
pip install git+https://github.com/DiffAPF/torchlpc.git
If you want to run it on NVIDIA GPU, make sure you have CUDA toolkit installed, with a verion compatible with your PyTorch installation.
MacOS
To compile with OpenMP support on MacOS, you need to install libomp via Homebrew.
Also, use llvm@15 as the C++ compiler to ensure compatibility with OpenMP.
brew install libomp
export CXX=$(brew --prefix llvm@15)/bin/clang++
export LDFLAGS="-L/usr/local/opt/libomp/lib"
export CPPFLAGS="-I/usr/local/opt/libomp/include"
After performing the above steps, you can install torchlpc as usual.
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 where is the loss function, the gradients of the LPC filter with respect to the input signal and the filter coefficients can be expresssed also through a time-varying filter:
Gradients for the initial condition
The initial conditions provide an entry point at for filtering, as we cannot evaluate . Let us assume so , which also means . Thus, the initial condition gradients are
In practice, we pad and zeros to the beginning of and before evaluating . The first outputs are the gradients to and the rest are to .
Time-invariant filtering
In the time-invariant setting, and the filter is simplified to
The gradients are filtering with backwards in time, same as in the time-varying case. is simply doing a vector-matrix multiplication:
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.
- Support Metal for MacOS.
- Add examples.
Related Projects
- torchcomp: differentiable compressors that use
torchlpcfor 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},
}