2013-12-10 26 views
-1

我的圖表中有一系列的線。這與下圖所示的方波類似。我如何計算C#中的脈衝數?檢測圖表系列中的脈衝數

Sample Line Series

對於那些誰希望看到一些代碼可以看看下面。這個想法被設定爲最小值和最大值。例如,如果該點在最小值和最大值之間,並且下一次不是,則我決定存在邊緣。不過,我對這個想法有些懷疑,尤其是對於一些嘈雜的信號。

var K = new Queue<Point>(); // Point is a class that holds DateTime and double value as well as some other properties. 

       foreach (var Point in Source.Data.OrderBy(x => x.Timestamp)) 
       { 
        K.Enqueue(new Point() { Timestamp = Point.Timestamp, Value = Point.Value, InBand = (Point.Value >= Min) && (Point.Value <= Max) }); 
       } 


var Points = new Point[3]; 

       foreach (var Point in K) 
       { 
        if (null == Points[0]) 
        { 
         Points[0] = Point; 
         continue; 
        } 

        if (null == Points[1]) 
        { 
         Points[1] = Point; 
         continue; 
        } 

        if (null == Points[2]) 
        { 
         Points[2] = Point; 
         continue; 
        } 

        if ((Points[0].InBand == false) && (Points[1].InBand == true) && (Points[2].InBand == true)) 
        { 
         this.RunCount++; 

         Points[0] = null; 
         Points[1] = null; 
         Points[2] = null; 

         continue; 
        } 

        if ((Points[0].InBand == true) && (Points[1].InBand == false) && (Points[2].InBand == false)) 
        { 
         this.StopCount++; 

         Points[0] = null; 
         Points[1] = null; 
         Points[2] = null; 

         continue; 
        } 
       } 
+1

這是如何實現的?我們將需要看到一些代碼。 – gleng

+0

@gleng我已經包含了一些代碼。我寧願你指定一個像史蒂夫一樣的算法,而不需要評論一些代碼。 – Demir

回答

0

我會假設您的數據存儲在double[]中。在這種情況下,您可以遍歷這些值,並計算該值從某個閾值以下到該閾值以上的次數。

這是您應該能夠適應的一小段代碼示例。我沒有測試過,但它非常簡單。請注意,如果您的信號非常嘈雜,這可能會給您帶來奇怪的結果,因此可能需要進行過濾。此外,該方法的名稱有點錯誤,因爲這樣計算信號從低於閾值到高於閾值的次數。

public int CountCrossings(double[] waveform, double threshold) { 
    int count = 0; 
    for (int i = 0; i < waveform.Length - 1; i++) { 
     if (waveform[i + 1] >= threshold && waveform[i] < threshold) 
      count++; 
    } 
    return count; 
} 
+0

謝謝@Steve。這給了我一些改進我的解決方案的想法。然而,嘈雜的信號仍然是問題。這時我會忽略噪聲問題,並考慮信號清晰。 – Demir