0

I have a csv file in the following structure:

time (milliseconds), "x", "y", "z"

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

........

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

I have used an ArrayList to save each data set (time,x,y,z) of the csv file as an object. Now I want to compute the average of x, y, z seperately within a sliding time window of two seconds with one second overlapping.

I hope my question straightforward, if not please let me know, in order to rephrase my question.

Im absolutely new to Java and I dont have any idea how the realize such a idea. I would be very grateful for any ideas.

Thanks in advance!!!

best regards, michael

Community
  • 1
  • 1
  • Voted to close. You need to show what you've tried so far so that we can provide help, as it stands you're just asking for someone to do the work for you – tddmonkey Dec 22 '14 at 12:43
  • Hello! Thanks for your answer. My intention is not that someone is doing the work for me. I would be even grateful for some clues how to approach such a problem e.g. is are any algorithm for sliding time window. – Benaissa Tahiti Dec 22 '14 at 13:01
  • Find the matching records within your window, then for each of x, y and z compute the average. You really need to show more work than this to get useful help. – tddmonkey Dec 22 '14 at 14:14
  • @BenaissaTahiti: So did you try to follow my idea? – striving_coder Dec 23 '14 at 20:58

1 Answers1

2

OK, let me write it in "improvised pseudocode" since you said you only need an idea rather than ready-made solution and I think it'll be more fun for you to implement it yourself:

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
}
striving_coder
  • 798
  • 1
  • 5
  • 7