Improving performance of for loop when decimating a buffer

后端 未结 3 1616
无人共我
无人共我 2021-01-28 07:13

I am collecting 16384 double values from a hardware device using a signal processing library. The 16384 values I receive are the output of a low pass filter. I want to down sa

相关标签:
3条回答
  • 2021-01-28 07:19

    This is solved. The libraries I use have a function which will assign all values from a double[] to it's internal real buffer

     realbuffer.Equals(finalBuffer);
    

    This takes 50us... Thanks for all the help.

    0 讨论(0)
  • 2021-01-28 07:24

    You could try putting each buffer into a dictionary and processing them in a for each. Theoretically this should be faster because the arrays will be processed in parallel. 20ms is pretty fast, why the need for such speed?

    var buffers = new Dictionary<int, double[]>();
    

    Then process something like this:

    var myData = realbuffer.ToArray();
    
    buffers.Add(bufferCount, myData);
    
    if (bufferCount == 10)
    {
       Parallel.ForEach(buffers, (buffer) =>
       {
          //process buffer independently
       });
    }
    
    0 讨论(0)
  • 2021-01-28 07:27

    You are actually getting everything and then drop the unnecessary ones. How about dropping those without copying at first step. I mean rather than using Array.Copy just copy the needed ones like;

    int j = bufferCount * 16384;
    for (int i = 0; i < 16384; i += 10)
    {
        realbuffer[j++] = rawData[i];
    }
    

    By doing so you will not need to run the loop inside if statement.

    If you cannot change the overall structure of you code due to any condition, you can increase the loop performance by a technique that is used in sorting algorithms . Define your array with the size 163840 + 1. Assign some value to the last position of your array such as -1 which will not be inside the received data. And change the loop as follows which decreases the number of comparison in the execution of the loop;

    for (int i = 0; i < rawData[i+=10 ] !=-1; ) 
    {
        realbuffer[j] = rawData[i];
        j++;
    }
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题