Signal-rate Derivative
I was wondering what the best way to implement a signal rate derivative is? I'm aware that [delta~] gives the slope between two samples. Is this the only option?
I was trying to implement the trapezoidal rule:
x'[n] = 2*fs*(x[n] - x[n-1]) - x'[n-1]
I know the x[n] - x[n-1] component could just be replaced by a x->[delta~] but was getting infinite recursion errors when trying to feed back x' with only a 1 sample delay. I was trying to use [delay~ 1 1] for this delay.
I know I probably could do this with gen~ (z^-1 and all that) but I haven't had the cash to get a licence yet.
Any thoughts? Is there a better way?
Thanks!
the only alternative to gen~ would be to use a poly~ vs 2 inside a poly up 2 - and then implement your algorithm to work only with samples 1,3,5 and ignore every other sample.
then you can make a feedbackloop inside this thing using tapout~, which will now allow a delay of only 2 samples, of which you skip every second after the process. voila, x and x-1 at your hands, recursive.
you can build a ladder filter in MSP like that. :) but it is nonsense, it is far to expensive and the method will not work with many things.
if i would fully understand x'[n] = 2*fs*(x[n] - x[n-1]) - x'[n-1] we might be able to find out if it can be done with factory objects somehow.
ah ok, now i know what this is. :)
why recursive? and not parallel? is a bit of latency out of question?
Thanks for the reply, Roman! Wow that's an approach I never would have thought of! Color me impressed you were able to make that work for a ladder filter!
I honestly think I will have to bite the bullet and get the full license soon so I can use gen~ and codebox. I was trying to implement Jatin Chowhury's adaptation of the Jiles-Antherton hysteresis equations.
https://jatinchowdhury18.medium.com/complex-nonlinearities-episode-3-hysteresis-fdeb2cd3e3f6
https://github.com/jatinchowdhury18/ComplexNonlinearities/tree/master/Hysteresis
(I was referencing his python code as well as his c++ juce code in the "plugin" folder)
I realized after posting that the Runge-Kutta solver (RK-2 in this case) also requires the previous samples, not just the one derivation, so it definitely would have been impractical to implement in the baseline max environment. I was hoping at the time there was some factory objects that I wasn't aware of lol.
The recursion in the case of the trapezoidal rule derivation is just because the x'[n-1] is the previous sample of the output. x' being the derivative of x... not sure if that notation was clear or not. So its pretty standard to reference the previous sample of the output, but it max doesn't seem to like recursion of anything less than a vector length, right? 1 sample seems out of the question outside of gen~ (and your poly~ method perhaps?).
as far as i understand the generic math (this is usually not very far) there is always a "maximum error" anyway.
so it can in practice eventually not make a bigger difference if you calculate that thing "infinite" or only with the last/next 25 samples, which means that you can set it up parallel, or in a tree structure.
there is a proposed method for realtime audio to build it like so:

which suggest that if your application of that allowed a latency of one vector, you could do it using tapout~. or maybe a windowing system with parallel +=~´s.
intersample values interpolation will probably not work with my silly poly~ method, where you need to skip every second sample, but i havent tried it. but a solution which is a complete CPU hog is not what you want, even where it works.
atm i rather think we are missing that there is an external which could be used.
for example if the signal was unipolar, maybe one could make an approximation by using slide~ and then distort the signal or something like that.