Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: What's the "best" way to do a reconstruction filter? (Read 6007 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

What's the "best" way to do a reconstruction filter?

I need to 8x upsample/downsample some WAVs for offline analysis. For various reasons, I am sensitive to any deviations in the downsampled output file, not just those that are audible. Ideally, I'd like it to be 100% lossless, but clearly that's not possible. However, I see no reason why it couldn't be possible in a more limited case - if the input signal is bandlimited to below the transition bands of the sampling filters.

Yet I can't really come close to that. I strongly expect that what I'm trying to do is still fairly impossible, but I haven't yet found any literature or source that implies that, nor can I find the literature that gives a filter design process that optimizes towards this.

This is the evaluation method I'm using:
  • Create 10 seconds of white noise with peak amplitude -6dbFS
  • Use some sort of brickwall filter on the signal with a transition band of about 17-19khz. Right now I'm just repeatedly applying the FIR filter in Audacity.
  • Run the signal through the upsampling/downsampling algorithm of my choice (assuming that its transition band starts above 19khz)
  • Correct the signal for sample delay against the original signal, then subtract the new signal from the original one.
  • Record the peak amplitude.
Ideally, I'd like the peak amplitude to be around -90dbFS (meaning the last 1-2 bits are different) but I've only been able to get it down to -71dbFS. That's with a filter design program that spits out Kaiser filters. The input parameters are pass=0.95*fs, stop=1.05263*fs, and rejection ratio = 240dB. It spits out a Kaiser-windowed FIR lowpass with 3823 taps and beta=25.49. Changing the input parameters affects the performance in very non-obvious ways. In particular, increasing the rejection ratio tends to increase the peak amplitude most of the time, and either expanding or shrinking the transition band tends to also reduce performance.

In comparison, upsampling/downsampling with foo_dsp_pphs in Ultra mode gives a peak amplitude of -16db, when using a single-precision intermediate WAV. Not completely sure if I'm tweaking it correctly, but it makes me think I'm on the right track.

Any suggestions or references I can look at to improve this?

What's the "best" way to do a reconstruction filter?

Reply #1
An infinitely long sinc filter should be lossless.  If you have the CPU time to waste, you might try using a 2n-1 tap sinc filter for your n length waveform.  Although you can get "might as well be lossless" performance out of, say, a 64000 tap windowed sinc filter.

[edit: typo]

What's the "best" way to do a reconstruction filter?

Reply #2
Close, but no cigar. I just tried a 65537-tap (you meant 2N+1, right?) sinc filter with a rectangular window and a lowpass of 0.5fs exactly.. the peak amplitude is now down to -78db, so I'm down to the last 3 bits.

I'll now try a 131073-tap lowpass at 0.45fs and see what happens. I'll let you know when it finishes.

What's the "best" way to do a reconstruction filter?

Reply #3
try it with blackman window

What's the "best" way to do a reconstruction filter?

Reply #4
Or a Kaiser window with a fairly high beta. With a filter that long, your SNR should be much better than 78dB - libsamplerate does better than that with a much shorter filter. Are you sure you aren't seeing round-off effects? What bit depth are you processing in? What order are you adding your coefficients in?

What's the "best" way to do a reconstruction filter?

Reply #5
Ok, sinc filter with lowpass=0.475fs, 131073 taps, Blackman window. Peak amplitude went up to -55dbFS. 

That took a few hours to run so I'll need to look into optimizing it. I'm pretty sure the convolution code path is full 64-bit, and it's using a naive summation scheme, but I'll need to check to be sure.

EDIT: After looking at spectrum plots of the difference signal, I'm not completely satisfied in the bandlimited nature of the white noise source. I'll prepare a new input signal and then get back to tweaking.

What's the "best" way to do a reconstruction filter?

Reply #6
Yeah, I'm a complete idiot - the test signal I was using was crap and was not filtered properly. Sadly, this is not the first time I've forgotten that Audacity is not trustworthy for the creation of test signals.

I made another one, this time with 220db of stopband rejection, and saved it as a 32-bit floating point WAV. Now, with a 1001-tap filter (Kaiser beta=10.06 low=0.475fs high=0.526316fs) I'm getting a peak amplitude of -117dbFS, when saving back to 32-bit. Cranking the parameters any further  causes the peak amplitude to neatly flatline at -144db.

That's
more like it.

What's the "best" way to do a reconstruction filter?

Reply #7
I made another one, this time with 220db of stopband rejection, and saved it as a 32-bit floating point WAV. Now, with a 1001-tap filter (Kaiser beta=10.06 low=0.475fs high=0.526316fs) I'm getting a peak amplitude of -117dbFS, when saving back to 32-bit. Cranking the parameters any further  causes the peak amplitude to neatly flatline at -144db.

That's
more like it.
Yeah, that does sound more like what can be expected. I like to prototype stuff in Scientific Python or MATLAB before I write code in other languages - makes tweaking a lot easier.

What's the "best" way to do a reconstruction filter?

Reply #8
Remember that if you want to upsample by 8, then downsample by 8, and get the original waveform back, all you need to do is repeat each sample 8 times for the upsample, and throw away every 7 out of 8 samples for the downsample.

However, the intermediate result may not be optimal.


In my memory, I have a vague recollection of an upsampling filter where individual taps are chosen to preserve the exact values of the original samples, with correct interpolation between. That sounds more useful.

Cheers,
David.

 

What's the "best" way to do a reconstruction filter?

Reply #9
When you do stuff like this, use Matlab or octave, and you'll be sure what you did in the first place.

I've tried quite a few "filters" and such in various editing programs, and I haven't yet been very satisfied.
-----
J. D. (jj) Johnston