A PyTorch library for time-varying IIR filters.
Project description
PhilTorch
A PyTorch package for fast automatic differentiation of discrete time linear filters.
Our principle design goals are:
- Provide fast and differentiable version of
scipy.signal.*functions. - Focus on time-domain implementation without using FFT.
- Support batch processing, parameter-varying filters, and GPU acceleration.
- Pure functional implementation and no stateful objects.
Installation
Stable release
pip install philtorch
Development version
pip install -i https://test.pypi.org/simple/ philtorch
# or
pip install git+https://github.com/yoyolicoris/philtorch.git
Note:
- The installation process compiles C++/CUDA extensions, so make sure you have a working C++ compiler and CUDA toolkit (if you want to use GPU acceleration) installed.
- We recommend using
--no-build-isolationflag to avoid potential issues with building the package in an isolated environment, especially when installing with CUDA support.
Module overview
philtorch: Root module.lpv: Functions under it are for linear parameter-varying filters.fir:- Finite Impulse Response filters.
allpole:- All-pole filters.
lfilter:- Parameter-varying version of
scipy.signal.lfilter. It supports not only transposed direct form II but also transposed direct form I, direct form I, and direct form II structures.
- Parameter-varying version of
state_space:- Parameter-varying state-space models.
state_space_recursion:- The core recursion function for state-space models.
linear_recurrence:- A linear recurrence function with scalar coefficients.
lti: Functions under it are for linear time-invariant filters.fir:- Finite Impulse Response filters.
lfilter:- A differentiable version of
scipy.signal.lfilter. It supports not only transposed direct form II but also transposed direct form I, direct form I, and direct form II structures.
- A differentiable version of
filtfilt:- A differentiable version of
scipy.signal.filtfilt.
- A differentiable version of
lfilter_zi:- A differentiable version of
scipy.signal.lfilter_zi.
- A differentiable version of
lfiltic:- A differentiable version of
scipy.signal.lfiltic.
- A differentiable version of
state_space:- State-space models.
diag_state_space:- State-space models with diagonalisable state matrix.
state_space_recursion:- The core recursion function for state-space models.
linear_recurrence:- A linear recurrence function with scalar coefficients.
utils: Utility functions.mat: Matrix operations.poly: Polynomial operations.
For detailed API reference, please refer to the docstring of each function.
Examples
Recreating scipy.signal.lfilter example
import torch
from philtorch.lti import lfilter, lfilter_zi, filtfilt
from scipy.signal import butter
x = torch.randn(201)
b_np, a_np = butter(3, 0.05)
# note that in philtorch a_0 is always 1
b_np /= a_np[0]
a_np = a_np[1:] / a_np[0]
b, a = torch.from_numpy(b_np), torch.from_numpy(a_np)
# note that the position of a and b are swapped compared to scipy
zi = lfilter_zi(a, b)
z, _ = lfilter(b, a, x, zi=zi * x[0])
z2 = filtfilt(b, a, x)
If lfilter is imported from philtorch.lpv, it can also handle parameter-varying filters, where a and b are at least 2D tensors with an additional time dimension.
Computing the first 10 Fibonacci numbers using state_space
The function philtorch.lti.state_space compute the following recursion:
\begin{aligned}
\mathbf{h}_{n+1} &= \mathbf{A} \mathbf{h}_n + \mathbf{B} \mathbf{x}_n \\
\mathbf{y}_n &= \mathbf{C} \mathbf{h}_n + \mathbf{D} \mathbf{x}_n
\end{aligned}
We can use it to compute the Fibonacci numbers by setting:
\begin{aligned}
\mathbf{A} = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}, \quad
\mathbf{C} = \begin{bmatrix} 1 & 0 \end{bmatrix}, \quad
\mathbf{B} = \mathbf{D} = 0, \\
\mathbf{h}_0 = \begin{bmatrix} 1 \\ 0 \end{bmatrix}
\end{aligned}
import torch
from philtorch.lti import state_space
A = torch.tensor([[1, 1], [1, 0]])
C = torch.tensor([1, 0])
x = torch.zeros(1, 10).long()
h0 = torch.tensor([1, 0])
y, _ = state_space(A, x, C=C, zi=h0)
print(y)
tensor([[ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]])
The result is the first 10 Fibonacci numbers, which has the following recursion relation:
F_n = F_{n-1} + F_{n-2}, \quad F_0 = 1, \quad F_1 = 1
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file philtorch-0.1.tar.gz.
File metadata
- Download URL: philtorch-0.1.tar.gz
- Upload date:
- Size: 43.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
649db61aa0abf020775520fc06e727c8be24d9217a6b28805073db4045d6bdbf
|
|
| MD5 |
29ee61219d92069058d7aac56ba295c8
|
|
| BLAKE2b-256 |
4402e5b443fa337c9f5c37182582ded1e277138e4416e3a55a18dadeca79d022
|
Provenance
The following attestation bundles were made for philtorch-0.1.tar.gz:
Publisher:
python-publish-stable.yml on yoyolicoris/philtorch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
philtorch-0.1.tar.gz -
Subject digest:
649db61aa0abf020775520fc06e727c8be24d9217a6b28805073db4045d6bdbf - Sigstore transparency entry: 551669163
- Sigstore integration time:
-
Permalink:
yoyolicoris/philtorch@2b2f5b10491788ef4a4f6b00bcb572dcf90e13c8 -
Branch / Tag:
refs/tags/v0.1 - Owner: https://github.com/yoyolicoris
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-stable.yml@2b2f5b10491788ef4a4f6b00bcb572dcf90e13c8 -
Trigger Event:
push
-
Statement type: