2015-01-06 39 views
0

我想在每次更新時以.csv格式記錄多個項目。以前我以兩種不同的方式完成了這項工作:在Unity3D中記錄時間序列數據的有效方法?

  1. 打開並附加文件,然後在每次更新時關閉它。
  2. 存儲多個樣本並定期批量記錄它們。

兩種方法都達到了我想要的效果,但我想知道是否存在效率問題。

我應該完全使用其他方法嗎?

+0

不成熟的優化是萬惡之源:https://shreevatsa.wordpress.com/2008/05/16/premature-optimization-is-the-root-of-all-evil/ – Ich

回答

0

建議如果使用的NLOG使用記錄像NLOG或log4net的

鏈接特定於CSV佈局設置穩定/可靠庫:https://github.com/NLog/NLog/wiki/CsvLayout

提供靈活性進行格式化。效率是您需要根據比較測試運行來衡量的。另外根據您的需要提供歸檔日誌文件的選項。

1

海蘭,

另一種方法可能會使用像 「LabStreamingLayer」 數據AQUISITION框架。你會在我的github account上發現一個很小的實現。

我知道這個問題相當過時,但LSL是一件很酷的事情,值得一試! 這個實現仍在開發中,但應該開箱即用。

[編輯]你絕對是對的。對不起,快速拍攝...所以這裏是更全面的答案:

LabStreamingLayer(LSL)框架是高分辨率時間序列的數據採集框架。主要用於流EEG(Brainsignals ... :))數據通過網絡保存並分析它們。它通過網絡帶來時間同步,因此數據流可以結合起來用於進一步分析。 例如,我正在使用該框架來獲取3D對象的方向和位置數據,該數據稍後將與EEG數據相關聯。要使用該框架,您需要將C#api(實際上只是LSL.cs文件)和liblsl.dll包含到您的c#項目中,並且您將能夠僅將數據推送到LSL幾行代碼。

using LSL; 

public class LSLOutlet : MonoBehaviour 
{ 
private liblsl.StreamOutlet outlet; 
private liblsl.StreamInfo streamInfo; 
private float[] currentSample; 

public string StreamName = "Unity.ExampleStream"; 
public string StreamType = "Unity.Random01f"; 
public int ChannelCount = 4; 

void Start() 
{ 
    currentSample = new float[ChannelCount]; 

    streamInfo = new liblsl.StreamInfo(StreamName, StreamType, ChannelCount, Time.fixedDeltaTime); 

    outlet = new liblsl.StreamOutlet(streamInfo); 
} 

public void FixedUpdate() 
{ 
    currentSample[0] = Random.value; 
    currentSample[1] = Random.value; 
    currentSample[2] = Random.value; 
    currentSample[3] = Random.value; 
    outlet.push_sample(currentSample); 
} 
} 

outlet.push_sample(currentSample)機器會自動時間戳添加到樣品集。關於Unity的固定更新方法,兩個樣本之間的時間應該更多或更少appStartTime + tn -1 + Time.fixedDeltaTime。

在另一邊,你需要它實現了一個應用程序:

public class LSLInlet : MonoBehaviour { 

liblsl.StreamInfo[] results; 
liblsl.StreamInlet inlet; 

int expectedChannels = 0; 

void Start() { 

    // wait until the expected stream shows up 
    results = liblsl.resolve_stream("type", "Unity.Random01f"); 
    // open an inlet and print some interesting info about the stream (meta-data, etc.) 
    inlet = new liblsl.StreamInlet(results[0]); 

    expectedChannels = inlet.info().channel_count(); 

    Debug.Log(inlet.info().as_xml()); 
} 

void FixedUpdate() 
{ 
    // read samples 
    float[] sample = new float[expectedChannels]; 

    while (inlet.samples_available() > 0) 
    { 
     inlet.pull_sample(sample); 

     foreach (float f in sample) 
      Debug.Log(string.Format("\t{0}", f)); 

    } 
} 

} 

所以你會得到一個時間序列。您將可以通過自己保存數據,也可以使用LabRecorder - 將流保存在一個簡單的基於xml的文件中。請參閱自述文件以獲取更多信息。

有關更多示例,請參閱SDK中的示例,鏈接自LSL4Unity存儲庫的自述文件。(對於我的低聲譽,我不能直接鏈接)。

所以,我希望這可能會有所幫助...... :)請考慮LSL的文檔(github wiki等)整個事情都是有據可查的!

+0

請添加更多詳情給你的答案,*你會怎樣使用它?你能提供一個例子嗎?只是一個鏈接通常不是很有幫助。 –

+1

因此,我添加了更多信息。希望,這有助於!? –

相關問題