Julius (PyTorch DSP Library)
Julius is a Python library providing fast, differentiable Digital Signal Processing (DSP) algorithms implemented with PyTorch, offering CUDA support. It specializes in functionalities like sinc resampling, FFT-based convolutions, and FIR filter banks for audio and 1D signals. The current version is 0.2.7, with releases addressing compatibility and performance improvements.
Warnings
- gotcha The `resample_frac` function is highly optimized for fractional changes in sample rates where the `old_sr` and `new_sr` reduce to a small irreducible fraction (e.g., 2000 to 3000 simplifies to 2:3). Performance can degrade significantly if the sample rates do not simplify to small integers (e.g., 20001 to 30001).
- gotcha The `julius.fftconv` modules (e.g., `FFTConv1d`, `fft_conv1d`) are optimized for convolutions with *large kernels* (typically >= 128) and a stride of 1. For smaller kernels or different strides, `torch.nn.Conv1d` might be faster or more memory efficient. Dilation and groups are not supported by `julius.fftconv`.
- deprecated As of January 2021, the `julius` implementation of resampling has been officially integrated into `torchaudio`. Users primarily focused on resampling may consider using `torchaudio.functional.resample` for potentially more robust or integrated solutions within the PyTorch ecosystem.
- breaking Version 0.2.2 (released January 2021) introduced changes to filter normalization in `lowpass` and `resample` and switched from zero padding to replicate padding. This could subtly alter output signals, especially at the edges or for very low frequencies, compared to previous versions.
Install
-
pip install julius
Imports
- julius
import julius
- resample_frac
import julius signal = torch.randn(1000) resampled_signal = julius.resample_frac(signal, old_sr=44100, new_sr=16000)
- ResampleFrac
from julius.resample import ResampleFrac resampler = ResampleFrac(old_sr=44100, new_sr=16000)
- fft_conv1d
import julius x = torch.randn(1, 1, 1024) w = torch.randn(1, 1, 256) y = julius.fftconv.fft_conv1d(x, w)
Quickstart
import julius
import torch
# Create a dummy audio signal (batch_size, channels, time)
signal = torch.randn(2, 1, 44100) # 2 batches, 1 channel, 44100 samples (1 second at 44.1kHz)
old_sample_rate = 44100
new_sample_rate = 16000
# Resample the signal
resampled_signal = julius.resample_frac(signal, old_sr=old_sample_rate, new_sr=new_sample_rate)
print(f"Original signal shape: {signal.shape}")
print(f"Resampled signal shape: {resampled_signal.shape}")
# For FFT-based convolution:
x = torch.randn(1, 1, 2048) # Input (batch, channels, time)
w = torch.randn(1, 1, 512) # Kernel (out_channels, in_channels, kernel_size)
y = julius.fftconv.fft_conv1d(x, w)
print(f"FFT Conv output shape: {y.shape}")