computing first and second derivative of a continous cv signal.

I got a question for the calc geeks here: Suppose the first derivative and second derivative of a continous CV signal (lfo … whatever) F(t) = x are of musical interest to me, perhaps for coupling the direction and acceleration of the signal to modulation parameters accordingly.

How would I get dF/dt(t) and d^2F/dt(t) in real time and as smooth as the original signal? Are there modules doing this out of the box? Any ideas how to cobble together a patch doing it?

You could use the backward finite difference

\frac{dF}{dt} \approx \frac{\Delta F}{T} = \frac{F(t) - F(t - T)}{T}

For higher order derivatives, just apply the formula recursively, e.g.

\frac{d^2F}{dt^2} \approx \frac{F(t) - 2F(t-T) + F(t - 2T)}{T^2}

Or you could use a higher order method. https://en.wikipedia.org/wiki/Numerical_differentiation

I don’t know a module that does this, but you could use VCV Prototype or write something in C++.

2 Likes

I don’t know of an existing module that does it but a C++ module could work something like this (not tested yet, might contain some embarrassing mistakes) :

class DerivatorModule : public rack::Module
{

public:

    DerivatorModule()

    {

        config(0,1,2);

    }

    void process(const ProcessArgs& args) override

    {

        float involt = inputs[0].getVoltage();

        float deriv1 = involt-m_history[1];

        float deriv2 = involt-2.0f*m_history[1]+m_history[0];

        outputs[0].setVoltage(deriv1);

        outputs[1].setVoltage(deriv2);

        m_history[0] = m_history[1];

        m_history[1] = involt;

    }

private:

    float m_history[2] = {0.0f,0.0f};

};

Here it’s assumed the time difference is 1 (sample) because that is the rate at which the input signal changes anyway.

1 Like

Thank you! I got an idea what a difference quotient is, but I n ever looked into Prototype in detail until now. (: I’d write something like F(block.inputs[i][block.bufferSize])-F(block.inputs[i][0])/block.bufferSize no?

Well you’d have to store the last N samples in a global array in order to compute the N th derivative. Something like

block.outputs[0][0] = (block.inputs[0][0] - lastInput) * block.sampleTime
lastInput = block.inputs[0][0]
1 Like

Looks fine to me but I’m no dev. (:

I like the name …Derivator

So why don’t we have modules simulating analog derviation/integration? Are they just boring or useless? I would imagine the opposite, if you put them in series or feed them back on themselves. (:

got it. ty. I’ll try that. hargh gotta look up some basic lua syntax too. (:

Maybe it hasn’t been a popular thing to try because the numeric range of the derivative can vary so wildly. For example, it’s well known that sin(x) just becomes cos(x), but sin(x * 440.0) becomes 440.0 * cos(x * 440.0)! So it would be hard to scale the output of the module to the allowed or useful voltage range.

Yes, most integrator/differentiator “modules” in 1950’s analog computers included a calibrated scale knob/multiswitch. For musical purposes, you’d just need a knob with a wide exponential range.

if we consider the 1 sample delay of cables you can do it using 3 sections of nysthi::constaddmult

2 Likes

Yes actually spot on. The notion of trying this in rack occured to me browsing through a signal processing book from the fifties. ((: The idea of using analog differentiator circuits as lego blocks to model all sorts of (differential) equations is appealing. It’s awfully closely related to what we play with here, yet we don’t use those simple thingies directly.

actually, yah I thought about your const add mult or one of Submarines math thingies first, but I figured it’d be too crude.

1 Like

So I typed the line of code @Vortico suggested into a .js file just to see what happens. And yah, if I feed a sine lfo into it and observe the output through scope it doesn’t too far off from a cosine …if I multiply Andrews formula by 40000000. lol. Forgive me if this is a stupid question to ask but I don’t understand what I need to do to clean this up a little.

Prototype examples and the thread dedicated to it here help a little, but it’s still a bit fumbling in the dark. Here’s the script, its testament to my ineptness so pls bear with me. (:

/** I have no idea what I’m doing but this should become 7177’s little pocket differentiator. */

let last =0 config.bufferSize = 32

function process(block) { for (let i = 0; i < block.bufferSize-1; i++) { block.outputs[0][i] = (block.inputs[0][i+1] - block.inputs[0][i]) * block.sampleTime * 40000000 } block.outputs[0][0] = (block.inputs[0][0] - last) * block.sampleTime * 4000000 last = block.inputs[0][block.bufferSize-1] display("Input " + last + " out " + block.outputs[0][0] ) }

how would derivatives be musically significant? serious question here.

1 Like

They tell how the speed and/or direction of the signal changes. Potentially useful as a transformation of the signal.

how would derivatives be musically significant?

honestly: I don’t know yet. (;

Yenakios got it right. First derivative is something like ‘direction’ and speed the original signal is going, second is the ‘acceleration’ change of velocity of the signal in this direction. I hope at least for closely related but distinct control signals to create movement with a organic feel when mapped to different parameters of the same sound.

What I’m really after would be going off the rails completely in this direction: https://en.wikipedia.org/wiki/Analog_computer#Electronic_analog_computers_2 http://chalkdustmagazine.com/features/analogue-computing-fun-differential-equations/ Can you imagine generative patches controlled by oscillating solutions to patched up variants of differential equations? A sound controlled by the heat equation in 2d? I don’t know, you know. That’s why I asked. (:

A sawtooth vco is an integrator that takes the pitch constant and turns it into a ramp (sort of). So at it’s simplest, the derivative would be the reverse of that.

1 Like

True!