joepetrowski / rust-psd

Calculation of power spectrum from time signal.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PSD Calculation in Rust

This program essentially copies the pwelch function from MATLAB. It will:

  1. Remove mean from signal.
  2. Apply a Hanning window.
  3. Compute a Cooley-Tukey FFT on an n-length sample, where n is the power of 2 that results in the closest possible window to 1 second.
  4. Compute the PSD up to the Nyquist frequency of the signal.
  5. Convert to 1/6-octave band (1/3 and 1/6 OB are by convention in vibration analysis).
  6. Compute a final envelope for the entire signal.

My sample time series were free and came from Mide, whose blog also provides a great into to vibration analysis.

My PSD and scale factor calculation came from equation 23 of this paper.

Instructions

Modify the file path in your main.rs to point to a CSV file with time in the first column and signal in the second. This program makes the following assumptions:

  • Time is in seconds and all points are evenly spaced.
  • Signal is stochastic and stationary.
  • Data has been cleaned (viz, all time points have a corresponding signal point that can be read as an f64).
  • Signal is in unit acceleration (g), although this is mostly related to plots.
cargo build --release
cargo run --release

The --release build will run significantly faster than the debug build.

To Do

  • Make this a Parity Substrate off-chain worker.
  • Change this to a library that accepts a Vec<_> and returns the envelope.
  • Add traits to allow more input types.
  • Add more error handling (e.g. if signal contains NaN values).
  • Add more features (e.g. input different units, plot options, window options).
  • Implement FFT algorithms that accept non-power-of-two signal lengths.
  • Performance comparisons against Python, MATLAB, etc.

Additional inspiration

Friendly Warning

This has not been peer-reviewed at all. I have compared FFT results to other libraries and compared time-based and PSD-based RMS results, and everything looks reasonable. However, this was much more about learning more Rust and eventually integrating into Substrate as a demo.

About

Calculation of power spectrum from time signal.


Languages

Language:Rust 100.0%