Frage

I have the following code for a filter:

// Shift array to the left
memmove(&Fifo[0], &Fifo[1], 12 * 4);

// Add new value
Fifo[12] = NewValue;

int Result = Factor[0] * Fifo[6];

Result += Factor[1] * (Fifo[5] + Fifo[7]);
Result += Factor[2] * (Fifo[4] + Fifo[8]);
Result += Factor[3] * (Fifo[3] + Fifo[9]);
Result += Factor[4] * (Fifo[2] + Fifo[10]);
Result += Factor[5] * (Fifo[1] + Fifo[11]);
Result += Factor[6] * (Fifo[0] + Fifo[12]);

Is there any way I could rewrite this so that I dont have to copy so much memory each call, to increase performance? I thought about circular buffers and linked lists, but it would require much extra code and complexity, that it seems the above is the best option.

War es hilfreich?

Lösung

You could implement a circular buffer by simply keeping an extra variable w/ the index of, say, the start of the buffer, and make all references to Fifo relative to that (modulo 13 -- thanks, Ingo!); that way, shifting is just bumping that index.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top