luns The best filter to use in place of the boxcar depends on what we know of the signal we’re trying to detect. For a pure sine wave, the ideal filter would be a narrow bandpass filter tuned to the frequency of the sine wave. For a hammer position which is the second integral of what can look like a spikey acceleration, I would go for some second order lowpass filter, or basically a simple biquad filter.
I should add, the above statements are assuming everything is LTI, but the process we're sensing is not LTI to begin with (ff is not simply an amplitude scaled pp). The simplifying assumption is to keep things tractable. The suggestion of a biquad filter isn't necessarily ideal either, but is also to keep things from getting too complicated.
I've not heard of Savitzky-Golay, but if we're to allow things to get more complicated, elements of what I remember of the Kalman filter seem appropriate. The filter maintains an estimate of the state (for us, the hammer position and velocity) that it can use to predict the input, but also keeps track of how good those estimates are (the covariance). Between the covariance and prediction error, the filter uses the sampled input in varying proportion relative to the estimate from past samples - in effect, there's something of a dynamic averaging time.
This matches with what we'd empirically want to do anyway: to estimate the velocity between two thresholds crossings, we can take the average of the individual step velocities through the entire interval - we'd effectively average few samples for a fast key strike, and many for a slow one.
This is not to say I suggest implementing a Kalman filter: what benefit it might have over the other approaches is likely not enough to be worth the additional complexity, and if that marginal benefit is needed, it's probably more fruitful to invest in higher sampling rates or more bits of an ADC.
The LTI filtering (fixed bandwidth, or equivalently, fixed averaging time) would have to have a wide enough bandwidth to settle in a fast strike, but for a slow strike, this would see more noise. Whether this is a problem depends on how bad the noise is, but of course more bits help. Without a surplus of bits, the time-between-crossings approach (with interpolation to estimate where crossings are between samples) is probably the best.
xooorx Aha... I tried various ways of filtering the position signal before differentiating but got better results by, effectively, differentiating the position and low-pass filtering the result. (Am no longer sure which way round @stem_piano was doing it).
Assuming no noise is introduced along the way, the filtering operations, and the filter coefficients themselves, are commutative. I think @stem_piano convolves the differentiation and the boxcar together into a single FIR which does the same non-adjacent-sample differentiation you did, just looking even farther back than your 4 samples.
xooorx For quantisation noise it might be that prevention is better than cure i.e. use higher bit converters. @stem_piano's posted data was quantised to 3-digit decimal but came from a system that will soon be doing 24 bits. OTOH my own plans currently involve a 12-bit converter so I need to work out if that's enough or if I need to switch it up.
24 bits feels like overkill while 12 feels appropriate to me. Just my gut feeling though - would have to play with the data to really tell.