Fixed small sinc() calculation bug that caused AA filter attenuation be around -10dB instead of <-50dB.

This commit is contained in:
oparviai 2014-01-05 17:19:19 +00:00
parent 510ac08657
commit 510a94e990

View File

@ -52,6 +52,30 @@ using namespace soundtouch;
#define PI 3.141592655357989 #define PI 3.141592655357989
#define TWOPI (2 * PI) #define TWOPI (2 * PI)
// define this to save AA filter coefficients to a file
// #define _DEBUG_SAVE_AAFILTER_COEFFICIENTS 1
#ifdef _DEBUG_SAVE_AAFILTER_COEFFICIENTS
#include <stdio.h>
static void _DEBUG_SAVE_AAFIR_COEFFS(SAMPLETYPE *coeffs, int len)
{
FILE *fptr = fopen("aa_filter_coeffs.txt", "wt");
if (fptr == NULL) return;
for (int i = 0; i < len; i ++)
{
double temp = coeffs[i];
fprintf(fptr, "%lf\n", temp);
}
fclose(fptr);
}
#else
#define _DEBUG_SAVE_AAFIR_COEFFS(x, y) ()
#endif
/***************************************************************************** /*****************************************************************************
* *
* Implementation of the class 'AAFilter' * Implementation of the class 'AAFilter'
@ -112,8 +136,7 @@ void AAFilter::calculateCoeffs()
work = new double[length]; work = new double[length];
coeffs = new SAMPLETYPE[length]; coeffs = new SAMPLETYPE[length];
fc2 = 2.0 * cutoffFreq; wc = 2.0 * PI * cutoffFreq;
wc = PI * fc2;
tempCoeff = TWOPI / (double)length; tempCoeff = TWOPI / (double)length;
sum = 0; sum = 0;
@ -124,7 +147,7 @@ void AAFilter::calculateCoeffs()
temp = cntTemp * wc; temp = cntTemp * wc;
if (temp != 0) if (temp != 0)
{ {
h = fc2 * sin(temp) / temp; // sinc function h = sin(temp) / temp; // sinc function
} }
else else
{ {
@ -153,17 +176,21 @@ void AAFilter::calculateCoeffs()
for (i = 0; i < length; i ++) for (i = 0; i < length; i ++)
{ {
// scale & round to nearest integer
temp = work[i] * scaleCoeff; temp = work[i] * scaleCoeff;
//#if SOUNDTOUCH_INTEGER_SAMPLES
// scale & round to nearest integer
temp += (temp >= 0) ? 0.5 : -0.5; temp += (temp >= 0) ? 0.5 : -0.5;
// ensure no overfloods // ensure no overfloods
assert(temp >= -32768 && temp <= 32767); assert(temp >= -32768 && temp <= 32767);
//#endif
coeffs[i] = (SAMPLETYPE)temp; coeffs[i] = (SAMPLETYPE)temp;
} }
// Set coefficients. Use divide factor 14 => divide result by 2^14 = 16384 // Set coefficients. Use divide factor 14 => divide result by 2^14 = 16384
pFIR->setCoefficients(coeffs, length, 14); pFIR->setCoefficients(coeffs, length, 14);
_DEBUG_SAVE_AAFIR_COEFFS(coeffs, length);
delete[] work; delete[] work;
delete[] coeffs; delete[] coeffs;
} }