fix a calcuation bug in FLAC__lpc_compute_best_order()

This commit is contained in:
Josh Coalson
2006-04-28 00:11:31 +00:00
parent 96d8cd2390
commit 6e2b5659c1
2 changed files with 21 additions and 13 deletions

View File

@@ -200,10 +200,11 @@ FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scal
* IN lpc_error[0,max_order-1] >= 0.0 error returned from calculating LP coefficients * IN lpc_error[0,max_order-1] >= 0.0 error returned from calculating LP coefficients
* IN max_order > 0 max LP order * IN max_order > 0 max LP order
* IN total_samples > 0 # of samples in residual signal * IN total_samples > 0 # of samples in residual signal
* IN bits_per_signal_sample # of bits per sample in the original signal * IN overhead_bits_per_order # of bits overhead for each increased LP order
* (includes warmup sample size and quantized LP coefficient)
* RETURN [1,max_order] best order * RETURN [1,max_order] best order
*/ */
unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample); unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order);
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */

View File

@@ -62,6 +62,13 @@ void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_le
FLAC__ASSERT(lag > 0); FLAC__ASSERT(lag > 0);
FLAC__ASSERT(lag <= data_len); FLAC__ASSERT(lag <= data_len);
/*
* Technically we should subtract the mean first like so:
* for(i = 0; i < data_len; i++)
* data[i] -= mean;
* but it appears not to make enough of a difference to matter, and
* most signals are already closely centered around zero
*/
while(lag--) { while(lag--) {
for(i = lag, d = 0.0; i < data_len; i++) for(i = lag, d = 0.0; i < data_len; i++)
d += data[i] * data[i - lag]; d += data[i] * data[i - lag];
@@ -410,28 +417,28 @@ FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scal
} }
} }
unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample) unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order)
{ {
unsigned order, best_order; unsigned order, index, best_index; /* 'index' the index into lpc_error; index==order-1 since lpc_error[0] is for order==1, lpc_error[1] is for order==2, etc */
FLAC__double best_bits, tmp_bits, error_scale; FLAC__double bits, best_bits, error_scale;
FLAC__ASSERT(max_order > 0); FLAC__ASSERT(max_order > 0);
FLAC__ASSERT(total_samples > 0); FLAC__ASSERT(total_samples > 0);
error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples; error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
best_order = 0; best_index = 0;
best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (FLAC__double)total_samples; best_bits = (unsigned)(-1);
for(order = 1; order < max_order; order++) { for(index = 0, order = 1; index < max_order; index++, order++) {
tmp_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[order], error_scale) * (FLAC__double)(total_samples - order) + (FLAC__double)(order * bits_per_signal_sample); bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[index], error_scale) * (FLAC__double)(total_samples - order) + (FLAC__double)(order * overhead_bits_per_order);
if(tmp_bits < best_bits) { if(bits < best_bits) {
best_order = order; best_index = index;
best_bits = tmp_bits; best_bits = bits;
} }
} }
return best_order+1; /* +1 since index of lpc_error[] is order-1 */ return best_index+1; /* +1 since index of lpc_error[] is order-1 */
} }
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */