# dY/dt calculation with variable dt

## Recommended Posts

Hi LAVA people,

Does anybody know a standard LabVIEW vi or has anyone developed a vi to calculate dY/dt (both are double arrays) where dt is a variable step size (but monotonic) and not a constant as usual?

Thanks!

##### Share on other sites

hmmm, this is such a basic problem that I really wonder why you don't try it yourself.

the simplest method would be dY/dt (t(i)) = [y(t(i))-y(t(i+1))]/[(t)i - t(i+1)]

QUOTE(adriaanrijllart @ Jul 26 2007, 03:47 PM)

Hi LAVA people,

Does anybody know a standard LabVIEW vi or has anyone developed a vi to calculate dY/dt (both are double arrays) where dt is a variable step size (but monotonic) and not a constant as usual?

Thanks!

##### Share on other sites

Sure I can use the formula node.

It was just that I wondered ... knowing there are >500 built-in mathematical functions, that there might be something I overlooked.

Thanks for taking away the doubt that I need to use the formula.

##### Share on other sites

A quick thought: you could resample your data so that you end up with a new, constant dt. Then you could use any of the standard analysis VI's.

##### Share on other sites

QUOTE

Sure I can use the formula node.

Why would you? I guess you have a background in text-programming because you want to use a formula node that fast...

You have two arrays of t and y(t), why not use a simple for-loop in labview?

QUOTE(BobHamburger @ Jul 26 2007, 06:34 PM)

A quick thought: you could resample your data so that you end up with a new, constant dt. Then you could use any of the standard analysis VI's.

that would probably require more LV knowledge than solving the problem in G in the first place

##### Share on other sites

QUOTE(Dirk J. @ Jul 26 2007, 09:47 AM)

Why would you? I guess you have a background in text-programming because you want to use a formula node that fast...

You have two arrays of t and y(t), why not use a simple for-loop in labview?

that would probably require more LV knowledge than solving the problem in G in the first place

Going off-track for a bit, there is a very useful re-sample VI buried somewhere in LV (I found it by breaking apart an express VI: Align & Resample Express VI), which is extremely easy to use and works great.. it probably might work with the variable dt as well.

But, I wouldn't recommend using it in this particular case.

Neville.

##### Share on other sites

Thanks for the tips!

I'm not really a line programmer ... that's why I'm looking for a vi. It's just that I thought there must be something out there, ready for me. It was the suggestion that made me think of it.

I've considered resampling, but that would mean putting the whole signal on the same dt. This increases the number of points quite a lot, because there's a slow part and a fast part.

And I've been finger pointed already for using many MBs ... and I had promised a very efficient program, "cute and clean".

So I'm happy with Dirk's suggestion of the For loop.

Thanks again to all who replied! It was very useful.

##### Share on other sites

Hi all,

found a way to do the calculation without loop. Only array functions.

Cheers,

##### Share on other sites

QUOTE(adriaanrijllart @ Aug 8 2007, 09:27 AM)

Hi all,

found a way to do the calculation without loop. Only array functions.

Cheers,

Should the contant be "1"or "0" ?

Ben

##### Share on other sites

QUOTE(Ben @ Aug 8 2007, 03:56 PM)

Should the contant be "1"or "0" ?

Ben

1.

The first (0-index) element of y and t arrays is deleted using the Array Subset function.

this trick works because LV allows you to subtract arrays of different sizes.

drawback here is that ArraySize(Derivatives) = ArraySize(Y) - 1

##### Share on other sites

QUOTE(Dirk J. @ Aug 8 2007, 10:20 AM)

1.

The first (0-index) element of y and t arrays is deleted using the Array Subset function.

this trick works because LV allows you to subtract arrays of different sizes.

drawback here is that ArraySize(Derivatives) = ArraySize(Y) - 1

Thank you Dirk!

I stand corrected.

Ben

##### Share on other sites

QUOTE(Ben @ Aug 8 2007, 04:34 PM)

Thank you Dirk!

I stand corrected.

Ben

I was myopically staring at the screenshot for quite some time to figure out that it was 'array subset'.

all these icons look alike

##### Share on other sites

QUOTE(adriaanrijllart @ Aug 8 2007, 08:27 AM)

found a way to do the calculation without loop. Only array functions.

Be aware that this introduces a bias into your data, namely that you're getting the derivative from the left side of the function. Basically lim(dy/dt) as t -> t_i+. This will only be strictly true if the second derivative of your function is zero. It maybe a small enough effect that you don't care, though.

##### Share on other sites

QUOTE(adriaanrijllart @ Aug 8 2007, 06:27 AM)

Hi all,

found a way to do the calculation without loop. Only array functions.

Cheers,

Though doing the calculation without a loop is possible, the code will now make two extra copies of the arrays Y and t possibly even a third copy for the dY/dt output; this may not be a big issue for "small" arrays, but with very large arrays this could affect performance due to memory manager calls.

I would suggest using the loop approach to limit the memory issue (even if it is not a concern presently). This would prevent possible future performance issues.

Tools>>Profile>>Highlight Buffer allocations

to see what I'm talking about.

Neville.

##### Share on other sites

Hi Guys,

Thanks for all reactions. They are all true.

Neville: It does create a copy of the data, but that's the price to pay for keeping the code simple and fast. For very big arrays I will reconsider the loop.

eaolson: There is a bias to the left side of the function, but also using a loop this will remain. I don't see how to avoid it. I will always get 1 point less in the final array, as I have to use two input elements to produce on output. But I'm open for suggestions.

Ben: Yes the constant is 1. This is the trick to calculate Y(i+1)-Y(i).

One additional point I thought of is to add a criterion of filtering the signal if it contains too much noise. I thought someone would make the remark, but so far no one seems to have had this problem.

Thanks all.

##### Share on other sites

QUOTE(adriaanrijllart @ Aug 14 2007, 10:46 AM)

One additional point I thought of is to add a criterion of filtering the signal if it contains too much noise. I thought someone would make the remark, but so far no one seems to have had this problem.

The derivative of a signal is going to make the noise problem much worse. Discontinuities in your point data will cause spikes in the derivative. That is why its never a good idea to use it in practice. This is also the reason why increasing the derivative gain in a PID loop can cause the loop to go unstable.

Better approach: fit a polynomial curve to the data and take its analytical derivative which should be fairly smooth and continuous.

Neville.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

×