FIRフィルタの実装4 Double Size Circular Buffer



//コア部分

// N : number of coefficients
// h(n) : filter coefficients, n = 0,…N-1
// x(n) : stored input data, n = 0,…2N-1
// input sample : variable that contains the newest input sample
// index1 : the first place in x[n] where the new sample is to be stored
// index2 : the second place in x[n] where the new sample is to be stored

// variable are initialized to the following values
int index1 = 0;
int index2 = N;

// As a new sample is received put it in two places
x[index1] = input_sample;
x[index2] = input_sample;

ynew = 0;
for (i=0;i<N;i++){
    ynew = ynew + h[i]*x[index2-i];
{
index1 = (index1+1)%N;
index2 = index1 + N;



// 関数実装
double fir_filter (double input, double fir_coeff[], double fir_buffer[], unsigned *index, unsigned length){

    double output;
    unsigned idx;

    fir_buffer[*index]        = input;
    fir_buffer[*index+length] = input;

    *index=*index+1;
    if(*index==length) *index=0;

    output = 0;
    for (idx=0;idx<length;idx++){
        output = output + fir_coeff[idx]*fir_buffer[*index+idx];
    }

    return output;
}




---------------------------
# define FIR_LEN 1000
double x,y;
double fir_buffer[2*FIR_LEN];
double fir_coeff[FIR_LEN];
unsigned fir_ix;
unsigned temp;

double fir_filter(double, double[], double[], unsigned*, unsigned);

void main (void){
    // initialize FIR Filter
    for (temp=0;temp<FIR_LEN;temp++){
        fir_coeff[temp]=0.0;
        fir_buffer[temp]=0.0;
        fir_buffer[temp+FIR_LEN]=0.0;
    }
    // impulse response of echo
    fir_coeff[490]=1;
    fir_coeff[440]=0.6;
    fir_coeff[200]=0.3;
    fir_coeff[130]=0.6;
    fir_coeff[0]=0.4;
    fir_ix=0;

    //main part
    y = fir_filter(x,fir_coeff,fir_buffer,&fir_ix,FIR_LEN);
}