A Rust implementation of the specparam (fooof). Procedures are identical to specparam in Python, with the exception that argmin is used for optimization, rather than scipy's curve_fit. Performance tests show that this implementation is 30-50x faster than the python implementation, depending on simulation parameters.
Install Rust
brew install rust
Python bindings via PyO3:
git clone [email protected]:ryanhammonds/specparam-rs.git
cd specparam-rs
pip install maturin
maturin develop --release
from specparam import SpecParam
sp = SpecParam(max_n_peaks=5, aperiodic_mode='linear')
res = sp.fit(freqs, powers)
Or, using pure Rust:
use ndarray::{Array1};
mod gen;
mod optimization;
mod specparam;
use gen::{lorentzian, linear, peak, noise};
use specparam::SpecParam;
fn main() {
// Frequencies
let freqs : Array1<f64> = Array1::range(1., 101., 1.0);
// Powers : lorentzian + oscillation + noise
let mut powers : Array1<f64> = lorentzian(&freqs, 10.0, 2.0, 100.0);
let sig_noise : Array1<f64> = noise(&freqs, 0.05);
powers = (powers + sig_noise).mapv(|p| (10.0_f64).powf(p));
powers = powers + peak(&freqs, 20.0, 20.0, 2.0);
// Initialize & fit
let mut sp = SpecParam{
max_n_peaks: 1,
aperiodic_mode: "lorentzian".to_string(),
..Default::default()
};
let results = sp.fit(&freqs, &powers);
}
And then to excute from a shell:
cargo run --release