I receive a buffer of data periodically that contains a number of values that are a fixed distance in time apart. I need to differentiate them. It is soo long since I did calculus at school ....
What I have come up with is this:
function DifferentiateBuffer(
ABuffer: TDoubleDynArray; AVPS: integer): TDoubleDynArray;
var
i: integer;
dt, dy: double;
begin
if (AVPS = 0) then exit;
// calc the delta time
dt := 1 / AVPS;
// run through the whole buffer
for i := 0 to high(ABuffer) do begin
if (i = 0) then
if (IsNan(FLastDiffValue) = false) then
dy := ABuffer[0] - FLastDiffValue
else
dy := ABuffer[0]
else
dy := Abuffer[i] - ABuffer[i -1];
result[i] := dy / dt
end;
// remember the last value for next time
FLastDiffValue := ABuffer[high(ABuffer)];
end;
AVPS is values per second. A typical value for this would be between 10 and 100. The length of the buffers would typically be 500 to 1000 values.
I call the buffer time after time with new data which is continuous with the previous block of data, hence keeping the last value of the block for next time. That last value is set to NAN in a constructor.
Is what I have done correct? ie, will it differentiate the values properly?
I also need to integrate similar data ... what is that likely to look like?