common code: Remove DC level before using Goertzel filter

A DC level degrades the quality of the filtered result
This commit is contained in:
Andreas Eversberg 2016-05-01 10:17:21 +02:00
parent 0c99848120
commit 28e7f1e9d1
1 changed files with 11 additions and 3 deletions

View File

@ -32,17 +32,18 @@
/* return average value (rectified value), that can be 0..1 */ /* return average value (rectified value), that can be 0..1 */
double audio_level(int16_t *samples, int length) double audio_level(int16_t *samples, int length)
{ {
int bias; int32_t bias;
double level; double level;
int sk; int sk;
int n; int n;
/* level calculation */ /* calculate bias */
bias = 0; bias = 0;
for (n = 0; n < length; n++) for (n = 0; n < length; n++)
bias += samples[n]; bias += samples[n];
bias = bias / length; bias = bias / length;
/* level calculation */
level = 0; level = 0;
for (n = 0; n < length; n++) { for (n = 0; n < length; n++) {
sk = samples[n] - bias; sk = samples[n] - bias;
@ -71,10 +72,17 @@ double audio_level(int16_t *samples, int length)
*/ */
void audio_goertzel(int16_t *samples, int length, int offset, int *coeff, double *result, int k) void audio_goertzel(int16_t *samples, int length, int offset, int *coeff, double *result, int k)
{ {
int32_t bias;
int32_t sk, sk1, sk2; int32_t sk, sk1, sk2;
int64_t cos2pik; int64_t cos2pik;
int i, n; int i, n;
/* calculate bias to remove DC */
bias = 0;
for (n = 0; n < length; n++)
bias += samples[n];
bias = bias / length;
/* we do goertzel */ /* we do goertzel */
for (i = 0; i < k; i++) { for (i = 0; i < k; i++) {
sk = 0; sk = 0;
@ -83,7 +91,7 @@ void audio_goertzel(int16_t *samples, int length, int offset, int *coeff, double
cos2pik = coeff[i]; cos2pik = coeff[i];
/* note: after 'length' cycles, offset is restored to its initial value */ /* note: after 'length' cycles, offset is restored to its initial value */
for (n = 0; n < length; n++) { for (n = 0; n < length; n++) {
sk = ((cos2pik * sk1) >> 15) - sk2 + samples[offset++]; sk = ((cos2pik * sk1) >> 15) - sk2 + samples[offset++] - bias;
sk2 = sk1; sk2 = sk1;
sk1 = sk; sk1 = sk;
if (offset == length) if (offset == length)