2016-03-01 107 views
1

我使用National Instrument進行數據採集並在圖表上顯示採集的數據。然而,該曲線具有一個鋒利的尖端,當我在曲線縮放圖像上,如下所示:enter image description here圖表噪聲消除

我使用以下代碼來得到這個圖表:

private void timer9_Tick(object sender, EventArgs e) 
    { 
     Task analogInTask1 = new Task(); 
     AIChannel myAIChannel1; 

     myAIChannel1 = analogInTask1.AIChannels.CreateVoltageChannel(
      "dev1/AI1", 
      "myAIChannel1", 
      AITerminalConfiguration.Rse, 
      0, 
      5, 
      AIVoltageUnits.Volts 
      ); 

     AnalogSingleChannelReader reader1 = new AnalogSingleChannelReader(analogInTask1.Stream); 
     double analogDataIn1 = reader1.ReadSingleSample(); 
     tension1Reading.Text = analogDataIn1.ToString("f2"); 


     DataPoint dp0 = new DataPoint(x, analogDataIn1); 
     chart2.Series[0].Points.RemoveAt(0); 
     chart2.Series[0].Points.Add(dp0); 
     x++; 

     if (checkBox1.Checked == true) 
      chart2.Series["Series1"].Enabled = true; 
     else 
      chart2.Series["Series1"].Enabled = false; 

    } 

我使用的張力傳感器來收集數據,以便我可以在圖表上顯示它們。 x軸的問題與y軸不匹配嗎?由於我使用[x ++;]來計算x軸,而我使用模擬數據輸入來獲取Y軸。我怎樣才能得到一條直線或流暢的線條?

+0

您是否解決了問題? 如果您滿意答案,請考慮[接受](http://stackoverflow.com/help/accepted-answer)它..! – TaW

回答

3

您是否嘗試過移動平均線?這可以根據你的需要量身定做,並且看起來會「平滑」圖表。這是一個非常簡單的示例,說明如何使用它修改代碼。

// get the last 4 points to average out (plus analogDataIn1) 
int pointsToAverage = 4; 
int pointCount = chart2.Series[0].Points.Count(); 
var buffer = chart2.Series[0].Points.Skip(Math.Max(0, pointCount - pointsToAverage)).Select(dp => dp.YValues[0]); 
// calculate the average Y from these points (along with analogDataIn1) 
double avgAnalogDataIn1; 
if (buffer.Count() == 0) 
{ 
    avgAnalogDataIn1 = analogDataIn1; 
} 
else 
{ 
    avgAnalogDataIn1 = (buffer.Sum() + analogDataIn1)/(double)(buffer.Count() + 1); 
} 

DataPoint dp0 = new DataPoint(x, avgAnalogDataIn1); 
chart2.Series[0].Points.RemoveAt(0); 
chart2.Series[0].Points.Add(dp0); 
x++; 

本例使用5點來平均。您可以通過將pointsToAverage增加到10來快速增加平均點數,這將使圖表更加平滑。請注意,這些線條仍然會有'尖端技巧' - 沒有ChartType可以爲您平滑線條。

+0

thz太多了。我試過你的樣品。但我得到1個錯誤,阻止我測試: 'DataPoint'不包含'Y'的定義,並且沒有找到接受類型'DataPoint'的第一個參數的擴展方法'Y'(你是否缺少using指令或者是一個程序集引用?) –

+0

我忘了獲取現有數據點的Y值,它來自'YValues'。我更改了代碼。 – Balah

+0

太過如此了。它現在可以運行。但曲線仍然有那些尖銳的提示 –

1

由於Balah的的答案的變體:

要創建一個移動平均數,你可以簡單地利用強大的built-in statistical functions之一:

chart1.DataManipulator.FinancialFormula(FinancialFormula.MovingAverage, 
             "15", "S1:Y1", "S2:Y"); 

這也就順暢一個Series S1超過15分和將結果繪製成Series S2

enter image description here

請注意,找到正確的範圍來平均完成取決於主要取決於您的數據和您的要求。在我的示例中,當範圍跨越其中一個間隙時會創建工件。

+0

我試着將你的代碼添加到我的函數中,但是當我運行該函數時圖表崩潰了。我仍然在試圖找出錯誤,是否輸入系列名稱錯誤? –

+0

好吧,正如我寫的那樣,它是用「S1系列」進行測試和測試的。如果你的輸入系列是'Series1',而你的輸出系列'Series2'變成'...「15」,「Series1:Y1」,「Series2:Y」'!確保在運行命令之前創建輸出系列! – TaW