//This is a decmating FIR filter made with my knowledge of sdr so far... don't laugh ;-)
//Reads samples from standard input and writes decimated samples to standard output
//
// to compile: gcc decfir.c -o decfir
//
//rob@m0dts.co.uk
//
//September 2015
#include <stdlib.h>
#include <stdio.h>
#define taps 199
float coeff[taps] ={0.000063, 0.000075, 0.000088, 0.000102, 0.000117,
0.000133, 0.000151, 0.000170, 0.000190, 0.000212, 0.000235, 0.000260
, 0.000286, 0.000314, 0.000343, 0.000374, 0.000407, 0.000442,
0.000478, 0.000517, 0.000557, 0.000599, 0.000643, 0.000689, 0.000736,
0.000786, 0.000838, 0.000891, 0.000947, 0.001004, 0.001064, 0.001125
, 0.001189, 0.001254, 0.001321, 0.001390, 0.001461, 0.001533,
0.001608, 0.001684, 0.001762, 0.001841, 0.001922, 0.002005, 0.002088,
0.002174, 0.002260, 0.002348, 0.002437, 0.002526, 0.002617, 0.002709
, 0.002801, 0.002894, 0.002988, 0.003082, 0.003176, 0.003271,
0.003365, 0.003460, 0.003554, 0.003648, 0.003742, 0.003835, 0.003928,
0.004020, 0.004111, 0.004201, 0.004289, 0.004377, 0.004463, 0.004548
, 0.004630, 0.004712, 0.004791, 0.004868, 0.004943, 0.005016,
0.005086, 0.005154, 0.005219, 0.005282, 0.005342, 0.005399, 0.005453,
0.005504, 0.005552, 0.005596, 0.005638, 0.005676, 0.005710, 0.005741
, 0.005769, 0.005793, 0.005813, 0.005830, 0.005843, 0.005852,
0.005858, 0.005859, 0.005858, 0.005852, 0.005843, 0.005830, 0.005813,
0.005793, 0.005769, 0.005741, 0.005710, 0.005676, 0.005638, 0.005596
, 0.005552, 0.005504, 0.005453, 0.005399, 0.005342, 0.005282,
0.005219, 0.005154, 0.005086, 0.005016, 0.004943, 0.004868, 0.004791,
0.004712, 0.004630, 0.004548, 0.004463, 0.004377, 0.004289, 0.004201
, 0.004111, 0.004020, 0.003928, 0.003835, 0.003742, 0.003648,
0.003554, 0.003460, 0.003365, 0.003271, 0.003176, 0.003082, 0.002988,
0.002894, 0.002801, 0.002709, 0.002617, 0.002526, 0.002437, 0.002348
, 0.002260, 0.002174, 0.002088, 0.002005, 0.001922, 0.001841,
0.001762, 0.001684, 0.001608, 0.001533, 0.001461, 0.001390, 0.001321,
0.001254, 0.001189, 0.001125, 0.001064, 0.001004, 0.000947, 0.000891
, 0.000838, 0.000786, 0.000736, 0.000689, 0.000643, 0.000599,
0.000557, 0.000517, 0.000478, 0.000442, 0.000407, 0.000374, 0.000343,
0.000314, 0.000286, 0.000260, 0.000235, 0.000212, 0.000190, 0.000170
, 0.000151, 0.000133, 0.000117, 0.000102, 0.000088, 0.000075,
0.000063};
void main(void){
int n,i;
int bufptr,bufsize;
float isample,qsample;
float *buf,inbuf[2],outbuf[2];
bufptr=0;
bufsize=1024;
//allocate memory for circular buffer
buf= malloc(bufsize * sizeof(float));
//set buffer to zeroes
for(n=0;n<bufsize;n++){
buf[n] = 0;
}
//main loop, read some samples into buffer and then output some samples every so often
n=0;
while(1){
//Read in 32 pairs of samples
for(n=0;n<32;n++){ //decimation?
fread(&inbuf,sizeof(float),2,stdin);
buf[bufptr] = inbuf[0];
bufptr++;
if (bufptr > bufsize-1){bufptr=0;}
buf[bufptr] = inbuf[1];
bufptr++;
if (bufptr > bufsize-1){bufptr=0;}
}
//reset sum
outbuf[0]=0;
outbuf[1]=0;
//pass on current buffer pointer
n=bufptr;
//apply coefficients to input samples
//filter appears to have 10dB attenuation.
for(i=0;i<taps;i++){
if(n>bufsize-1){n=0;}
outbuf[0] +=buf[n]* coeff[i];
n++;
if(n>bufsize-1){n=0;}
outbuf[1] +=buf[n]* coeff[i];
n++;
}
//output one pair of decimated samples
fwrite(&outbuf,sizeof(float),2,stdout);
}
}