用Delphi区分缓冲区 [英] Differentiation of a buffer with Delphi

查看:140
本文介绍了用Delphi区分缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会定期接收一个数据缓冲区,该缓冲区包含多个时间间隔固定的值.我需要区分它们.自从我在学校做微积分以来已经很久了....

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 ....

我想出的是这样的:

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是每秒的值.一个典型的值将在10到100之间. 缓冲区的长度通常为500到1000个值.

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.

我用新数据一次又一次地调用缓冲时间,新数据与上一个数据块连续,因此将下一个块的最后一个值保留下来.最后一个值在构造函数中设置为NAN.

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?

推荐答案

我找不到任何故障.

在第一次调用时返回的第一个值将是(ABuffer[0] - 0.0) / dt,它基于信号从零开始的假设.我想这就是您的意图.

The first value returned, on the first call will be (ABuffer[0] - 0.0) / dt which is based on the assumption that the signal starts with a zero. I presume that is what you intend.

现在,您可以使自己做的更好,而不是要求Stack Overflow社区检查您的代码.您应该测试代码以证明它是正确的.使用单元测试框架(例如DUnitX)对其进行测试.输入可以预测其输出的函数值.例如,从y = x 2 或y = sin(x)输入值.

Now, rather than asking the Stack Overflow community to check your code, you can do much better yourself. You should test the code to prove it is accurate. Test it using a unit testing framework, for example DUnitX. Feed the function values for which you can predict the output. For example, feed it values from y = x2, or y = sin(x).

编写测试的另一个巨大好处是它们可以一次又一次地执行.在开发代码时,您会冒引入错误的风险.该代码在今天可能是正确的,但谁知道您明天进行修改后它是否仍然正确.如果您进行了严格的测试,则可以防御维护期间引入的错误.

The other great benefit of writing tests is that they can be executed again and again. As you develop your code you run the risk of introducing faults. The code might be correct today, but who knows whether it will still be correct after the modification you make tomorrow. If you have a strong test in place, you can defend against faults introduced during maintenance.

关于样式的一种评论是,您永远不要测试= false= true. if语句对布尔表达式进行运算,因此与布尔值进行比较总是毫无意义的.我会这样写你的测试:

One comment on style is that you should not ever test = false or = true. The if statement operates on boolean expressions and so comparing against a boolean value is always rather pointless. I would write your test like this:

if not IsNan(FLastDiffValue) then
  dy := ABuffer[0] - FLastDiffValue
else
  dy := ABuffer[0]

或类似这样:

if IsNan(FLastDiffValue) then
  dy := ABuffer[0]
else
  dy := ABuffer[0] - FLastDiffValue

这篇关于用Delphi区分缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆