C Progrma Elliptic IIR filter, direct form II implementation. Uses MATLAB generated coefficients specified in include txt file to calculate filter. Circular buffer used, initialized using calloc().
/*** Elliptic IIR filter, direct form II implementation. Uses MATLAB generated
* coefficients specified in include txt file to calculate filter. Circular
* buffer used, initialized using calloc().
* @param sample sample_in, xin on diagram
* @return 64bit output, yout on diagram
*/
double Dir_form_2(double sample) {
static int write_index = N; //updatable write index for write_data
double in_data = 0.0; //for multiplying by a1, a2, a3...
double out_data = 0.0; //for multiplying by b1, b2, b3...
double write_data = 0.0; //summation of delayed coefficients, multiplied by a1, a2, a3, with input value
int read_index, loop_index; //declaration of iterators
for (loop_index = 1; loop_index <= N; loop_index++) {
read_index = ((write_index + loop_index)>N) ? write_index + loop_index - N - 1 : write_index + loop_index; //circular buffer wraparound logic
out_data += buffer[read_index] * b[loop_index]; //output acculumator
in_data -= buffer[read_index] * a[loop_index]; //input acculumator
}
write_data = sample + in_data; //summation of incoming sample and input side of direct form
buffer[write_index] = write_data; //write to buffer
out_data += write_data*b[0]; //multiplication of summed write_data, aka top line of direct form structure
if (--write_index == -1)write_index = N; //handle circular buffer wraparound
return out_data;
}