# LabVIEW

cancel
Showing results for
Did you mean:

## Problem in computing derivative using Savitzky Golay filter in LabView.

We are trying to measure the exit flow rate from a tank by calculating the rate of change of mass of the tank. We are continuously acquiring mass of tank using a weighing balance in LabView and trying to compute the derivative. For this purpose we are using the Savitzky Golay filter coefficient VI to provide the first order differentiation filter. We have made a VI for acquiring data and computing the flowrate based on derivative of mass. But while running the VI we are getting the value of derivative of mass equal to the mass which is completely wrong. I have tried to figure out the problem but couldn't find one in the VI. I have attached the VI please have a look at it and suggest any solution if possible. Also, please suggest any other scheme to find out derivative in LabView.

Message 1 of 13
(1,389 Views)

## Re: Problem in computing derivative using Savitzky Golay filter in LabView.

I could not find the "attached VI".  Please attach it again.  Also, if this is part of a LabVIEW Project, and the VI uses sub-VIs from the Project, it would be even better if you (a) clicked the folder containing the Project file and the rest of the Project "files", right-clicked "Send to:" and specified Compressed (zipped) Folder, and attached the .zip file of the entire Project.

Bob Schor

Message 2 of 13
(1,375 Views)

## Re: Problem in computing derivative using Savitzky Golay filter in LabView.

Hi Anshuman,

@Anshuman18 wrote:

We are continuously acquiring mass of tank using a weighing balance in LabView and trying to compute the derivative. For this purpose…

I would use a shift register to store the previous mass sample.

Then you can calculate the difference between current and previous mass.

Now all you need to do is to divide by the time between those two samples!

Why do you need a SavitzkyGolayFilter here?

Best regards,
GerdW

using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 3 of 13
(1,363 Views)

## Re: Problem in computing derivative using Savitzky Golay filter in LabView.

The Savitzky Golay filter will need an additional scaling if used to derivate ...

here I posted my version  https://forums.ni.com/t5/LabVIEW/Derivative-problem/m-p/2545553#M770893

usefull if you have noisy signals otherwise GerdW pointed the solution and there is a derivate x vi in the math section

Greetings from Germany
Henrik

LV since v3.1

“ground” is a convenient fantasy

'˙˙˙˙uıɐƃɐ lɐıp puɐ °06 ǝuoɥd ɹnoʎ uɹnʇ ǝsɐǝld 'ʎɹɐuıƃɐɯı sı pǝlɐıp ǝʌɐɥ noʎ ɹǝqɯnu ǝɥʇ'

Message 4 of 13
(1,352 Views)

Plz check now.

Message 5 of 13
(1,331 Views)

## Re: Problem in computing derivative using Savitzky Golay filter in LabView.

Hi anshuman,

can you please provide a much smaller example which is reduced to just show your problem?

Right now the block diagram is HUGE and full of Rube-Goldberg (like FOR loops set to iterate just once)! It's hard to even find the part of code you seem to have problems with…

You also forgot to explain why you need to use the SG-Filter function here!

Why is there a 1000ms wait function when there is no loop around all the code? DON'T use RunContinuously - that is only meant for debugging!

Why do you use the BLAS-ASum function in CDB mode on a DBL array? (What's wrong with the Abs function followed by SumArray?)

Why don't you use subVIs instead of duplicating code?

See this for inspiration:

Best regards,
GerdW

using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 6 of 13
(1,324 Views)

## Re: Problem in computing derivative using Savitzky Golay filter in LabView.

First:Open the serial port once at start,  continously read the results in a (while) loop (producer), Timestamp the results and do a error handling/check!  use a queue to feed valid results to another (while) loop (consumer)    , close it when you stop the program. See examples and tutorials about producer consumer architecture.

you have no control of the time periode the scale is sending the values, you hope the dt is constant and known, but is it? (the scale has it's own clock  and firmware) Did you read every value send? At the rigth time? (internal buffers in the hardware/driver)

Now you have a series of scale values  and timestamps , (starting point to do error/failure handling .. and maybe start alarming)

Start calculating the massflow.  The SG-filter way will give you a delay of sidepoint length measurements. is that OK?

Maybe a linear fit over the last n values (or all values within a timeslot, if timestamps are available) , how far differs the last value from the fit?

Greetings from Germany
Henrik

LV since v3.1

“ground” is a convenient fantasy

'˙˙˙˙uıɐƃɐ lɐıp puɐ °06 ǝuoɥd ɹnoʎ uɹnʇ ǝsɐǝld 'ʎɹɐuıƃɐɯı sı pǝlɐıp ǝʌɐɥ noʎ ɹǝqɯnu ǝɥʇ'

Message 7 of 13
(1,314 Views)

## Re: Problem in computing derivative using Savitzky Golay filter in LabView.

Hi Henrik,

I am using the SG filter coefficient VI for computing the derivative. Right now I am facing problem with respect to scaling with respect to time. I went through your approach as indicated in the link:

https://forums.ni.com/t5/LabVIEW/Derivative-problem/m-p/2545553#M770893

But the issue is that your solution is only valid for waveform, whereas the data points that I am using doesn't follow any particular trend they are just numbers collected from a machine. So please suggest me an approach that I can use for calculating derivative of random data points. I have also attached a screenshot of how I am computing the derivative from an array of data. Thanks in advance.

Message 8 of 13
(1,232 Views)

## Re: Problem in computing derivative using Savitzky Golay filter in LabView.

@Anshuman18 wrote:

But the issue is that your solution is only valid for waveform, whereas the data points that I am using doesn't follow any particular trend they are just numbers collected from a machine.

In order to (numerically) estimate a derivative of a (sampled) signal, you need to know not only the value of the signal, but also the intervals at which the values are obtained.  If I handed you a bag of pennies and said "Heads is 1, tails is 0" and asked you to find the derivative of the pennies in the bag, you would be hard-pressed to define what that means, whereas if I opened the bag, put the pennies down in a straight line touching each other, you could define a derivative as +1 if (reading from left to right) the penny sequence is "tails, heads", -1 for the opposite arrangement, and 0 if they were the same.

So your "just numbers collected from a machine" need to have a "time of collection" associated with each one.  The LabVIEW Waveform data type is one way to provide that, but an array where you know the time interval ("dt") between the samples, or a 2D array of "sample" and "time" could also be used (but the estimate of the derivative is much trickier in this case).

Bob Schor

Message 9 of 13
(1,222 Views)

## Re: Problem in computing derivative using Savitzky Golay filter in LabView.

@Anshuman18 wrote:

So please suggest me an approach that I can use for calculating derivative of random data points. I have also attached a screenshot of how I am computing the derivative from an array of data. Thanks in advance.

As a first step you need to define "randomly". The methods work if the spacing is evenly, no matter what the spacing is. That's just a scaling factor. If your points are spaced unevenly, things get more complicated. Your picture shows 1D arrays which is just data vs. array index. For randomly spaced data you also need a second array that contains timing information for each data point. One possible solution might be to resample the data at fixed intervals by e.g. interpolation before filtering.

Instead of showing us a useless picture, please attach your actual VI and actual sample data so we get a better idea of what you are trying to do. How noisy is the data?

Message 10 of 13
(1,219 Views)