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 sample by a factor of 10. In other words, I want to keep 1 point out of every 10. So in general it works like this:
I get a buffer from my library when the low pass filter has completed.
I then collect 10 of these buffers.
When all 10 buffers are collected into one buffer that has 10*16384 doubles I then loop collecting every 10th double from the buffer. The result is one buffer with 16384 doubles. This will sent off for the rest of the data processing.
Here is the code:
double[] rawData = new double[163840];
int bufferCount = 0;
private void genericReal2_ProcessData(object Sender, Mitov.SignalLab.ProcessRealNotifyArgs Args)
{
var realbuffer = Args.InBuffer; //get the buffer of processed doubles
var myData = realbuffer.ToArray(); //must be converted to an array since the data type is not quite an array of doubles.
Array.Copy(myData, 0, rawData, bufferCount * 16384, 16384);
bufferCount++;
if (bufferCount == 10)
{
bufferCount = 0;
int j = 0;
for (int i = 0; i < 163840; i += 10) //this loop takes 20ms
{
realbuffer[j] = rawData[i];
j++;
}
genericReal2.SendData(realbuffer); //send data off for further processing
}
}
The for loop takes about 20ms to run whereas everything else is around 20uS.
So, is there some way I could improve the overall performance of this without using the for loop?
Update************************** I have determined that all the processing time in the loop is taken assigning realbuffer to rawData. So I changed this as follows:
private void genericReal2_ProcessData(object Sender, Mitov.SignalLab.ProcessRealNotifyArgs Args)
{
double[] finalBuffer = new double[16384];
var realbuffer = Args.InBuffer; //get the buffer of processed doubles
var myData = realbuffer.ToArray(); //must be converted to an array since the data type is not quite an array of doubles.
Array.Copy(myData, 0, rawData, bufferCount * 16384, 16384);
bufferCount++;
if (bufferCount == 10)
{
bufferCount = 0;
int j = 0;
for (int i = 0; i < 163840; i += 10)
{
finalBuffer[j] = rawData[i];
j++;
}
var pointer= realbuffer.Read();
//I can get a pointer to the realbuffer.
//It stores 8 bytes for every double value
how can I copy 16384 8 byte values from finalbuffer to realbuffer?
genericReal2.SendData(realbuffer); //send data off for further processing
}