【问题标题】:Implementation sliding time window in JavaJava中实现滑动时间窗口
【发布时间】:2015-02-20 13:05:25
【问题描述】:

我有一个结构如下的 csv 文件:

时间(毫秒)、“x”、“y”、“z”

1389776970139,"-0.042138","0.0531513","8.28537"

........

1389776970833,"-0.0124942","-0.00338816","0.704891"

我使用 ArrayList 将 csv 文件的每个数据集(时间、x、y、z)保存为对象。现在我想在两秒的滑动时间窗口内分别计算 x、y、z 的平均值,一秒重叠。

我希望我的问题直截了当,如果不是,请告诉我,以便重新表述我的问题。

我对 Java 完全陌生,我不知道如何实现这样的想法。如果有任何想法,我将不胜感激。

提前致谢!!!

最好的问候, 迈克尔

【问题讨论】:

  • 投票结束。您需要展示您迄今为止所做的尝试,以便我们可以提供帮助,因为您只是要求有人为您完成工作
  • 您好!感谢您的回答。我的意图不是有人在为我做这项工作。我什至会感谢一些线索如何解决这样的问题,例如是任何滑动时间窗口的算法。
  • 在你的窗口中找到匹配的记录,然后为每个 x、y 和 z 计算平均值。您确实需要展示比这更多的工作才能获得有用的帮助。
  • @BenaissaTahiti:那你有没有尝试过我的想法?

标签: java csv sliding-window


【解决方案1】:

好吧,让我用“即兴伪代码”写它,因为你说你只需要一个想法而不是现成的解决方案,我认为你自己实现它会更有趣:

strings_array[][4];   // dynamic array of arrays; outer array's elements are strings of your CSV file
                      // and inner array has 4 elements corresponding for time, x, y and z
results_array[][4];   // same type of array to store results: final timestamp of each window
                      // and respective averages for x, y and z
start_time = strings_array[0][0]; // saving starting time
sum_coord[3] = {0,0,0};            // array to store sum of each of the x, y and z
points_num = 0;                    // variable to store number of time points in the current window
line_num = 0;                      // variable to store number of lines processed
while (line_num < strings_array.length) {  // iterating over outer part of strings_array - i.e. over
                                           // lines of original CSV file
    string_arr = strings_array[line_num];  // storing current line of CSV file
    line_num++;                            // incrementing total number of lines processed
    if (string_arr[0] < start_time+2000) { // if end of the window is not reached yet
        for (i=0; i<3; i++)
            sum_coord[i] += string_arr[i]; // adding each of x, y and z to the sum of the window
        points_num++;                      // incrementing number of time points in the window
        continue;                          // go to the next line of CSV file
    }
    // if, on the other hand, we have exceeded the window
    // storing averages for the window
    if (points_num == 0)
        results_array.append({start_time, 0, 0, 0});
    else
        results_array.append({start_time, sum_coord[0]/points_num, sum_coord[1]/points_num, sum_coord[2]/points_num});
    sum_coord = {0, 0, 0};                 // resetting each of x, y and z sums of the window
    points_num = 0;                        // resetting number of time points in the window
    while (strings_array[line_num-1][0] >= start_time+1000)  // going back 1 sec until we find start of the next window
        line_num--;
    start_time+=1000;  // resetting beginning of the window (make sure it's what you mean!)
                       // otherwise, can do: start_time = strings_array[line_num-1][0]
                       // to have next window based on the last timepoint within 1 sec
}

【讨论】:

  • 你好 strriving_coder!非常感谢您的方法,它有很大帮助。感谢您的努力。
猜你喜欢
  • 2015-06-16
  • 2014-03-28
  • 2017-09-04
  • 2022-11-27
  • 2021-08-10
  • 2018-07-23
  • 2015-07-13
  • 2017-09-23
  • 2013-01-17
相关资源
最近更新 更多