我的程序读入包含以不同速率采样的时间/值对的多个文件。我正在尝试使用具有最高采样率的文件作为所有采样率的时间标度,并使用来自最高采样率文件的唯一时间值输出一个主文件。
每个文件都包含时间/值对,如:
1.58
1.5,90
2154
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
执行此伪代码以获取所有各种采样率的唯一时间参考的最佳方法是什么?对不起,如果我的问题很混乱,如果需要,我可以详细说明。
答案 0 :(得分: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);
}
}
这样,较高密度采样信号的采样率可用作时钟,但由于来自其他信号源的值仅接近,但不完全在其记录的时间点上,因此您将具有不准确性。如果您希望减少这些不准确性,请选择更高的采样率并执行相同的操作。您可以根据需要尽可能接近实际时间点。