DC Offset Removal in input_adc.cpp, input_adcs.cpp

Status
Not open for further replies.
[EDIT] Minimizing redundancy. Keeping code below for anybody wanting to look at the algorithm on a PC.

Here's my test-code based on the Teensy DC remover:
Code:
//
// Teensy DC Removal (chunky moving-average)
//
#define AUDIO_BLOCK_SAMPLES 128
int dc_average_hist[16];
int current_dc_average_index;

void teensy_dc_remover(int *data)
{

	// Find and subtract DC offset... We use an average of the
	// last 16 * AUDIO_BLOCK_SAMPLES samples.
	int dc = 0;
    int tmp = 0;
    int i = 0;
	for (i = 0; i < 16; i++) {
		dc += dc_average_hist[i];
	}
	dc /= 16 * AUDIO_BLOCK_SAMPLES;
	dc_average_hist[current_dc_average_index] = 0;
	int *p = data;
	int* end = p + AUDIO_BLOCK_SAMPLES;
	do {
		dc_average_hist[current_dc_average_index] += (*p);
		tmp = (*p) - dc;
		*p++ = dc;
	} while (p < end);
    current_dc_average_index = (current_dc_average_index + 1) % 16;

}

//
// Test routine to validate performance as expected.
//
#define FS 44100
int main()
{
    float T = 1.0/FS;
    float t = 0.0;

    int i = 0;
    unsigned short int xn = 28000;
    int y = 0;
    int sv[2];
    sv[0] = 0;
    sv[1] = 0;

    // for (i = 0; i < FS; i++)
    // {
    //     y = dcr_filter(xn, sv);
    //     //apply 0 to 1 step response
    //     if( (i % (FS/50)) == 0)
    //     {
    //         if(xn == 10000)
    //             xn = 28000;
    //         else
    //             xn = 10000;
    //     }
    //
    //     if(i%10 == 0)
    //         printf("%f\t%d\t%d\n", t, xn, y);
    //
    //     t += T;
    // }

    //Run Teensy remover
    float x = 0.0;
    float tt = 0.0;
    float dt = 1.0/FS;

    for(i=0; i<16; i++)
        dc_average_hist[i] = 0;
    current_dc_average_index = 0;

    int audbuf[AUDIO_BLOCK_SAMPLES];
    for(i = 0; i < AUDIO_BLOCK_SAMPLES; i++)
        audbuf[i] = 0.0;
    int j = 0;

    int count = 0;
    for(j=0; j < FS/AUDIO_BLOCK_SAMPLES; j++)
    {
        for(i=0; i<AUDIO_BLOCK_SAMPLES; i++)
        {
            x = 20000.0 + 15000.0*cosf(2.0*M_PI*tt*AUDIO_BLOCK_SAMPLES*3.3);
            tt += dt;
            if(tt >= 2.0*M_PI) //wrap phase
                tt -= 2.0*M_PI;
            audbuf[i] = (int) x;
        }
        teensy_dc_remover(audbuf);
        for(i=0; i<AUDIO_BLOCK_SAMPLES; i++)
        {
            printf("%d\t%d\n", count, audbuf[i]);
            count++;
        }

    }
    return 0;
}
 
Last edited:
Status
Not open for further replies.
Back
Top