查找循环开始和结束并从阵列计算频率

时间:2016-06-15 14:58:42

标签: c# frequency

这是我的第一篇文章,请原谅我犯错误。

现在我要做的是从给定的双读数数组计算频率。该数组每次读数都有相应的时间。

例如,读数类似于0.010.110.210.180.1-0.03-0.2-0.120等等,每次阅读都有相应的时间,以秒为单位。

我用来启动和找到一个循环的方法是:

double start = read[0];
bool trough = false;
double cycle = 0; // Time for one cycle

for (int j = 1; j < read.Count; j++)
{
    if (read[j] < start)
    {
        trough = true;
    }
    else if (trough)
    {
        cycle = seconds[j];
        break;
    }
}

到目前为止,当循环从正读数开始很好地开始时这是有效的,但是当循环开始处于负读数时失败。 按循环我的意思是一个完整的正弦曲线..所以这个双重数组形成一个波形,如果用excel曲线绘制,有很多正弦曲线,就像你在示波器上看到的那样......我想要找到的是如何有效地获得一个正弦波的开始和结束。实际上,start是数组中的第一个值,但是如何获得第一个正弦波的结束是我迷失的地方。 请帮助,因为我的大脑完全错过了明显的错误/解决方案? 欢呼所有

1 个答案:

答案 0 :(得分:0)

您的正弦波从大约0开始,并且在您的示例中增加到0.21。然后它会降至-0.2并再次升至0

所以你首先需要找到它低于零的点,然后再检测它再次达到零的那一刻。

你可以这样试试:

int index = 0;
while (read[index] >= 0) index++; // skip first half sine wave
while (read[index] <= 0) index++; // skip second half
double cycle = seconds[index-1]; // take seconds of the last reading of the wave

更新:由于您的wave可以完全处于正或负范围,我们需要另一种方法。我们试着找

  • 第一个上升部分(波的前4个,0-π/ 2)
  • 下降部分(波的一半,π/ 2 - 3 /2π)
  • 最后一个上升部分(波的最后4个,3 /2π - 2π)

最后一个是棘手的,因为当我们再次达到起始值而不是下一个最大值时,波结束。

int start = 0; // start of the wave
int index = start + 1; // start at 1 to compare to previous
while (read[index] >= read[index-1]) index++; // skip rising part
index++; // skip the last rising element

while (read[index] =< read[index-1]) index++; // skip falling part
index++;

// now step forward till we reach our start level again
while (read[index] < read[start]) index++; 

// read your time
double cycle = seconds[index-1];

// you can start the next wave here:
start = index; // and do the above in a loop if you need