Implementation of PAQM in Python using pytorch for vectorized operations.
Project description
torchpaqm
This repo contains a pytorch implementation of the perceptual audio quality measure (PAQM), published by Beerends et al. in this paper. I made it as a way to study objective audio quality assessment in the context of my master's.
Dependencies
- pytorch
- torchaudio
- scipy
- pytest (for testing)
- matplotlib (for testing)
Installation
For regular use, just install normally from pip.
pip install torchpaqm
For development, git clone this repository and install locally with test dependencies
git clone git@github.com:bvm810/torchpaqm.git
cd torchpaqm
pip install -e .[tests]
Usage
The main PAQM class wraps the processing blocks described in the reference paper. The comparison between references and a test audio signals can be made by time-frequency bin, by frame, or with the whole signals. The average for the whole signal can be converted to a mean opinion score (MOS).
from torchpaqm import PAQM
# test_signal.shape --> (batch, channels, sample)
# reference_signal.shape --> (batch, channels, sample)
evaluator = PAQM(test_signal, reference_signal)
# get MOS scores
mos = evaluator.mean_opinion_score # (batch, channel)
# get raw scores
scores = evaluator.score # (batch, channel)
# get frame scores
frame_scores = evaluator.frame_scores # (batch, channel, frame)
# get bin scores
full_scores = evaluator.full_scores # (batch, channels, bark bin, frame)
References and test signals should have the same size. The module also includes torchpaqm.utils.PAQMDataset, a torch.utils.data.Dataset subclass that can be used to load multiple audio files into a batch. An utility collate function to pad signals with NaNs can be found in torchpaqm.utils.
The dataset class performs some basic validation on the input data. It checks if they all have a specific sampling rate (which is 44.1kHz by default, but can be passed as an optional parameter to PAQMDataset), and if test signals and references have the same shape.
from torchpaqm.utils import PAQMDataset, collate
# test_signals --> list with absolute paths to test signal files
# references --> list with absolute paths to reference files
dataset = PAQMDataset(test_signals, references)
loader = DataLoader(dataset, batch_size=8, shuffle=True, collate_fn=collate)
The intermediate processing steps of PAQM are handled by subclasses which are given as optional arguments to the PAQM class. In order:
SpectrumAnalyzer
Handles the Fourier representation of the signals, and conversion to the perceptual Bark scale.
from torchpaqm import SpectrumAnalyzer
analyzer = SpectrumAnalyzer(
fs = <sampling-rate> # mandatory argument, float in hertz,
frame_duration = 0.04,
window = "hann",
overlap = 0.5,
nfft = 2048 # int,
bark_binwidth = 0.2
)
OuterToInnerTransfer
Handles the transfer function between the outer and inner ears. The default transfer function points were obtained from a previous MATLAB implementation, and can be found as a constant in torchpaqm.transfer
from torchpaqm import OuterToInnerTransfer
from torchpaqm.transfer import DEFAULT_TRANSFER_FUNCTION
transfer = OuterToInnerTransfer(
transfer_function = DEFAULT_TRANSFER_FUNCTION # list of tuples (f_in_hertz, log_magnitude)
)
Masker
Handles time domain and frequency domain spreading. Time spreading is done autoregressively, and frequency spreading follows the log curves of the reference paper. The default parameters were taken from the reference paper, and can be imported from torchpaqm.masker
from torchpaqm import Masker
from torchpaqm.masker import ENERGY_TIME_DECAY_CONSTANT, FREQ_SLOPES_CONSTANTS
masker = Masker(
time_compression = 0.6,
freq_compression = 0.8,
tau_curve = ENERGY_TIME_DECAY_CONSTANT, # tau time decay curve points
freq_spreading_constants = FREQ_SLOPES_CONSTANTS, # freq. spreading slopes (in log scale)
)
LoudnessCompressor
Handles computing compressed loudness from the excitation representations. Outputs the final internal representation used in PAQM. The first two parameters are taken from the reference paper; the hearing threshold is the ISO 226 hearing threshold measured in phons
from torchpaqm import LoudnessCompressor
from torchpaqm.loudness import PHON_HEARING_THRESHOLD
compressor = LoudnessCompressor(
schwell_factor = 0.5,
compression_level = 0.04,
hearing_threshold = PHON_HEARING_THRESHOLD # ISO 226 hearing threshold
)
So, for example, in order to calculate PAQM using a different sampling rate, one would create an evaluator using
from torchpaqm import PAQM, SpectrumAnalyzer
evaluator = PAQM(test_signal, reference_signal, analyzer=SpectrumAnalyzer(fs=44100))
Testing
After cloning the repo and installing it locally, automated tests can be executed running the pytestcommand from the repository folder. The fixtures for the test were extracted from a previous MATLAB implementation created in the Audio Processing Group of the Signals, Multimedia and Telecommunications Lab (SMT) of the Federal University of Rio de Janeiro (UFRJ).
There is one test .py file per class in the source code, plus one test file for the utility functions, one test file to check PAQM's usage for neural network training, and one test file to see if there were considerable numerical error differences between this implementation and the MATLAB reference.
Contributing
Feel free to create an issue to discuss potential changes. Any additions to the code should update tests as appropriate.
Acknowledgements
This project would not be possible without the suggestions from Prof. Luiz Wagner Biscainho and Lucas Simões Maia, from SMT / UFRJ.
License
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torchpaqm-0.0.1.tar.gz.
File metadata
- Download URL: torchpaqm-0.0.1.tar.gz
- Upload date:
- Size: 31.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5378ec769ae9911ed298db6e6930ec07fcdcc668d58fe7cc011a9fc393f8993
|
|
| MD5 |
543e0d38c397a3982ff5179cfbb8e3ba
|
|
| BLAKE2b-256 |
020ab968d7d91196f15f177952985d154da871a3257ae40b178c7d8754d47ea0
|
File details
Details for the file torchpaqm-0.0.1-py3-none-any.whl.
File metadata
- Download URL: torchpaqm-0.0.1-py3-none-any.whl
- Upload date:
- Size: 24.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ad6a1c7630df195a66527c8382beb0bf531c4dcc383d61c793e610f3c03a48d5
|
|
| MD5 |
ee2972d468cb13028972ecf531ba50d6
|
|
| BLAKE2b-256 |
31e2d94cc6c09b7d83119dfcc6d4f1f051c4e093ae9e118099039a7ab9c1426c
|