0

我的程序读入多个文件,这些文件包含以不同速率采样的时间/值对。我正在尝试使用具有最高采样率的文件作为所有采样率的时间刻度,并输出一个具有最高采样率文件中唯一时间值的主文件。

每个文件都包含时间/值对,例如:

1,58

1.5,90

2,154

2.5,34

到目前为止,这是我的代码:

public void ReadAndWrite(string[] fileNames)
{
    var stopwatch = Stopwatch.StartNew();
    List<StreamReader> readers = fileNames.Select(f => new StreamReader(f)).ToList();

    try
    {
        using (StreamWriter writer = new StreamWriter(tbxOutputFile.Text))
        {
            string line = null;

            // For each measurement in max measurements
            for (int measNum = 0; measNum < numOfRows; measNum++)
            {                        
                // For each file's reader
                for (int i = 0; i < readers.Count; i++)
                {
                    // If line contains something, then add it to line
                    if ((line = readers[i].ReadLine()) != null)
                    {                           
                        // Process line and then write it to file
                        line = ProcessLine(line);
                        writer.Write(line);
                    }
                    else
                    {
                        writer.Write("");
                    }
                    // If it's not the last column, add delimiter
                    if (i < readers.Count - 1)
                        writer.Write(",");
                }
                writer.WriteLine();

                // Update labels
                int val = ((measNum + 1) * 100) / numOfRows;
                string newText = Convert.ToString("Progress: " + val + "% " + "     " + "Measurement #: " + (measNum + 1)
                    + " out of " + numOfRows); // running on worker thread
                this.Invoke((MethodInvoker)delegate
                {
                    // runs on UI thread
                    lblStatus.Text = newText;                            
                    progressBar1.Value = val;
                });
            }
        }
    }
    catch (Exception)
    {

        throw;
    }
    finally
    {
        foreach (var reader in readers)
        {
            reader.Close();
        }
    }
    MessageBox.Show("File successfully created! " + '\n' + "Elapsed time: " + 
        (stopwatch.ElapsedMilliseconds/1000) + " seconds", "Processing Complete");            
}

我想出了下面的伪代码(currentTime 是每个文件的时间,而 uniqueTime 来自一个数组,该数组每次从最高采样文件中读取):

// if time value from individual file is same as uniqueTime
if currentTime == uniqueTime
{
    valueToWrite = curr_value // write the current value
}
else // currentTime is not same as uniqueTime
{
    valueToWrite = prev_value // write the previous value
}
timeToWrite = uniqueTime // always write the uniqueTime

执行此伪代码以为所有各种采样率创建唯一时间参考的最佳方法是什么?抱歉,如果我的问题令人困惑,如果需要,我可以详细说明。

4

1 回答 1

0

要明确这一点,您不希望它们发生在特定时间的值,而是希望在最高采样源具有的每个时间点为每个源显示一个值?
这应该很简单。在伪代码中:

foreach (ValuePair val in highSampleRateValues) {
    var aggregatedTimePointData;
    aggregatedTimePointData.Add(val.Time, val.Value);
    foreach (ValuePair val2 in lowSampleRateValues) {
        var value = DetermineLatestEntryBackwardFrom(val.Time);
        aggregatedTimePointData.Add(value);
    }
}

这样,较高密度采样信号的采样率用作时钟,但您将有不准确之处,因为来自其他来源的值仅接近,但不完全在其记录的时间点上。如果您希望减少这些不准确性,请选择更高的采样率并执行相同的操作。您可以根据需要尽可能接近实际时间点。

于 2013-07-05T19:10:54.860 回答