我在c#中有一个实时信号存储在List中。我想检查信号是否从订单方向的几个点传过来。事实上,如果我的信号执行高斯钟,我想检测实时。我的想法是检查信号是否以上升顺序和下降顺序从两个阈值传递。我的信号从0到1取值。例如,如果信号从上升顺序的0.25开始,再从上升顺序的0.75开始,之后从0.75和0.25以下降顺序来检测我的钟。如何在c#中实时执行上述算法。我将信号值实时存储在List<double>
被调用的部分中。
double currentValue = parts[parts.Count - 1];
double previousValue = parts[parts.Count - 2];
double diffValue = parts[parts.Count - 1] - parts[parts.Count - 2];
if (currentValue > 0.25 && (currentValue - previousValue) > 0)
{
// the first point is passed
}
if (currentValue < 0.750 && (currentValue - previousValue) > 0)
{
// the second point is passed
}
// the same for the descent order.
}
我想要的是计算我的信号执行高斯钟的次数以及检测返回时间戳的时间窗口(从0.25点到0.25)。
答案 0 :(得分:1)
一个改进的答案,虽然正在进行中。它应该被重构。
void Main()
{
//Assuming a temporally ordered list
List<double> ascOnly = new List<double> { 0, 0.1, 0.2, 0.25, 0.3, 0.5, 0.7, 0.8};
List<double> gBell = new List<double> { 0, 0.1, 0.2, 0.25, 0.3, 0.5, 0.7, 0.8, 0.81, 0.6, 0.45, 0.32, 0.24, 0.1 };
//make sure a first point below .25 exists. find it and its index in the List. (IndexOf also gets the first value so pv1 should always be the index of v1)
var firstValue = gBell.FirstOrDefault(item => item < 0.25);
var positionFirstValue = gBell.IndexOf(firstValue);
Console.WriteLine("Starting below 0.25: " + firstValue + " , Position: " + positionFirstValue);
//find the first point at or above .25 and its index in the List.
var ascendingBottomThreshold = gBell.FirstOrDefault(item => item >= 0.25) ;
var positionAscendingBottomThreshold = gBell.FindIndex(item => item == ascendingBottomThreshold);
//make sure the first threshold is crossed after the initially small threshold (positive slope)
if (positionAscendingBottomThreshold > positionFirstValue) //fyi FindIndex returns -1 if condition is not satisfied
Console.WriteLine("First threshold at/above 0.25: " + ascendingBottomThreshold + " , Position: " + positionAscendingBottomThreshold);
//find the first point at or above .75 and its index in the List
var ascendingTopThreshold = gBell.FirstOrDefault(item => item >= 0.75) ;
var positionAscendingTopThreshold = gBell.IndexOf(ascendingTopThreshold);
//make sure the .75 threshold is crossed after the .25 threshold (positive slope)
if (positionAscendingTopThreshold > positionAscendingBottomThreshold) //fyi FindIndex returns -1 if condition is not satisfied
Console.WriteLine("Second threshold at/above 0.75: " + ascendingTopThreshold + " , Position: " + positionAscendingTopThreshold);
//do i next have to cross .75 while maintaining a positive slope? or just never dip below .25? (assumed the latter)
//make sure the signal that passed .25 never dips below .25 before it reaches .75.
for (int p = positionAscendingBottomThreshold + 1 ; p < positionAscendingTopThreshold ; p++)
{
if (gBell[p] < 0.25)
{
Console.WriteLine("Fell below threshold ...does not qualify(?)");
}
}
//assuming i need at least one more distinct point at or above .75; shows that signal did not just touch and go below, but actually crested.
gBell.RemoveAt(positionAscendingTopThreshold);
var descendingTopThreshold = gBell.FirstOrDefault(item => item >= 0.75) ;
var positionDescendingTopThreshold = gBell.IndexOf(descendingTopThreshold);
//make sure the .75 threshold is hit after the .75 threshold (positive slope)
if (positionDescendingTopThreshold >= positionAscendingTopThreshold) //fyi FindIndex returns -1 if condition is not satisfied
Console.WriteLine("Third threshold at/above 0.75: " + descendingTopThreshold + " , Position: " + positionDescendingTopThreshold);
for (int p = positionAscendingTopThreshold ; p < positionDescendingTopThreshold ; p++) //just positionAscendingTopThreshold (not plus 1 because value removed)
{
if (gBell[p] < 0.75)
{
Console.WriteLine("Fell below threshold ...does not qualify(?)");
}
}
//looking next for .25;
//remove everything that got you 'up the hill'
gBell.RemoveRange(0,positionDescendingTopThreshold);
var descendingBottomThreshold = gBell.FirstOrDefault(item => item <= 0.25) ;
var positionDescendingBottomThreshold = gBell.IndexOf(descendingBottomThreshold);
//make sure the .25 threshold is hit after the second .75 threshold (downhill)
//if (positionDescendingBottomThreshold >= positionDescendingTopThreshold) //fyi FindIndex returns -1 if condition is not satisfied
Console.WriteLine("Fourth threshold at/below 0.25: " + descendingBottomThreshold + " , Position: " + positionDescendingBottomThreshold);
for (int p = 1 ; p < positionDescendingBottomThreshold ; p++)
{
if (gBell[p] > 0.75)
{
Console.WriteLine("Rose above threshold ...does not qualify(?) " + gBell[p]);
}
}
}