#include <stdio.h>
#define BUFFER_SIZE 128
void filter(long *buffer) {
unsigned int n, k;
long inp, out, D;
static long d1[4] = {0};
static long d2[4] = {0};
static long d3[4] = {0};
for (k = 0; k < 2; k++)
for (n = 0; n < BUFFER_SIZE; n++) {
inp = buffer[n]>>1;
D = inp - d1[k];
d1[k] = d1[k] + (D>>2)+(D>>3);
buffer[n] = d1[k] - D;
D = d2[k] - d3[k];
d3[k] = d3[k] + (D>>3)-(D>>5);
out = d3[k] - D;
D = out - inp;
d2[k] = (D>>2)-(D>>5) - out;
buffer[n] += d2[k] - D;
}
for (k = 2, n = 0; n < BUFFER_SIZE; n++) {
inp = buffer[n]>>1;
D = inp - d1[k];
d1[k] = d1[k] + (D>>1)-(D>>3)-(D>>5);
buffer[n] = d1[k] - D;
D = d2[k] - d3[k];
d3[k] = d3[k] + (D>>4)+(D>>5);
out = d3[k] - D;
D = out - inp;
d2[k] = (D>>2) - out;
buffer[n] += d2[k] - D;
}
for (k = 3, n = 0; n < BUFFER_SIZE; n++) {
inp = buffer[n]>>1;
D = inp - d1[k];
d1[k] = d1[k] + (D>>2)-(D>>5);
buffer[n] = d1[k] - D;
D = d2[k] - d3[k];
d3[k] = d3[k] + (D>>4);
out = d3[k] - D;
D = out - inp;
d2[k] = (D>>2)-(D>>4) - out;
buffer[n] += d2[k] - D;
}
}
int main() {
unsigned int counter = 0;
char *inputFileName = "data/sines_long.bin";
char *outputFileName = "data/output.bin";
FILE *inputFile = fopen(inputFileName, "rb");
FILE *outputFile = fopen(outputFileName, "wb");;
long buffer[BUFFER_SIZE];
if (BUFFER_SIZE%2 == 1) {
fprintf(stderr, "BUFFER_SIZE has to be an even integer\n");
return 1;
}
if (!inputFile) {
fprintf(stderr, "Unable to open file %s\n", inputFileName);
return 1;
}
if (!outputFile) {
fprintf(stderr, "Unable to open file %s\n", outputFileName);
return 1;
}
while (fread(buffer, sizeof(long), BUFFER_SIZE, inputFile) > 0) {
filter(buffer);
fwrite(buffer, sizeof(long), BUFFER_SIZE, outputFile);
counter++;
}
printf("Processed %i samples\n", counter*BUFFER_SIZE);
fclose(inputFile);
fclose(outputFile);
return 0;
}