/analytic_wfm

from a gist.

Primary LanguagePython

Musings about the peakdetect functions by Sixten Bergman
Note that this code should work with both python 2.7 and python3.x.


All the peak detection functions in __all__ of peakdetect.py will work on
consistent waveforms, but only peakdetect.peakdetect can properly handle
offsets.

The most accurate method for pure sine seems to be peakdetect_parabola,
which for a 50Hz sine wave lasting 0.1s with 10k samples has an error in
the order of 1e-10, whilst a naive most extreme sample will have an error
in the order of 7e-5 for the position and 4e-7 for the amplitude

Do note that this accuracy most likely doesn't stay true for any real world
data where you'll have noise and harmonics in the signal which may produce
errors in the functions, which may be smaller or larger then the error of
naively using the highest/lowest point in a local maxima/minima.


The sine fit function seem to perform even worse than a just retrieving the
highest or lowest data point and is as such not recommended. The reason for
this as far as I can tell is that the scipy.optimize.curve_fit can't optimize
the variables. 


For parabola fit to function well, it must be fitted to a small section of the
peak as the curvature will start to mismatch with the function, but this also
means that the parabola should be quite sensitive to noise


FFT interpolation has between 0 to 2 orders of magnitude improvement over a 
raw peak fit. To obtain this improvement the wave needs to be heavily padded
in length

Spline seems to have similar performance to a FFT interpolation of the time
domain. Spline does however seem to be better at estimating amplitude than the
FFT method, but is unknown if this will hold true for wave-shapes that are
noisy.

It should also be noted that the errors as given in "Missmatch data.txt"
generated by the test routine are for pure functions with no noise, so the only
error being reduced by the "non-raw" peakdetect functions are errors stemming
low time resolution and are in no way an indication of how the functions can
handle any kind of noise that real signals will have.


Automatic tests for sine fitted peak detection is disabled due to it's problems



Avoid using the following functions as they're questionable in performance:
    peakdetect_sine
    peakdetect_sine_locked