#include <stdio.h>
#define BUFFER_SIZE 64
#define SAMPLE short
void decimator(short *buffer) {
unsigned int n;
short inp1, inp2;
short D;
static short d11 = 0, d21 = 0, d22 = 0;
static short d31 = 0, d32 = 0, d33 = 0;
for (n = 0; n < BUFFER_SIZE>>1; ++n) {
inp1 = buffer[(n<<1)+1]>>1;
D = inp1 - d11;
D = (D>>1)-(D>>3)-(D>>5)-(D>>8);
buffer[n] = D - d11 + (buffer[(n<<1)]>>1);
d11 = D - inp1;
}
for (n = 0; n < BUFFER_SIZE>>2; ++n) {
D = d22 - (buffer[(n<<1)]>>1);
d22 = (D>>1)-(D>>4) - d22;
buffer[n] = d22 - D;
inp1 = buffer[(n<<1)+1]>>1;
D = inp1 - d21;
D = D>>3;
buffer[n] += D - d21;
d21 = D - inp1;
}
for (n = 0; n < BUFFER_SIZE>>3; ++n) {
inp1 = buffer[(n<<1)]>>1;
D = inp1 - d33;
D = (D>>2)+(D>>4);
buffer[n] = D - d33;
d33 = D - inp1;
inp1 = buffer[(n<<1)+1]>>1;
D = inp1 - d31;
D = (D>>4)+(D>>6);
inp2 = D - d31;
d31 = D - inp1;
D = d32 - inp2;
d32 = (D>>2)+(D>>5)+(D>>7) - d32;
buffer[n] += d32 - D;
}
}
int main(){
unsigned int counter = 0;
char *inputFileName = "data/sines.bin";
char *outputFileName = "data/output.bin";
FILE *inputFile = fopen(inputFileName, "rb");
FILE *outputFile = fopen(outputFileName, "wb");
short buffer[BUFFER_SIZE];
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(short), BUFFER_SIZE, inputFile) > 0) {
decimator(buffer);
fwrite(buffer, sizeof(short), BUFFER_SIZE>>3, outputFile);
counter++;
}
printf("Processed %i samples\n", counter*BUFFER_SIZE);
fclose(inputFile);
fclose(outputFile);
return 0;
}