Some advice of DSP here...

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

User avatar
James
Posts: 429
Joined: Sat Jan 22, 2005 8:51 am
Location: Chicago, IL
Contact:

Re: Some advice of DSP here...

Post by James » Thu Jul 02, 2015 9:17 pm

I spent some time analyzing test signals tonight (square, triangle, and noise). Decimating before filtering does cause aliasing that is a) measurable in a spectrum analyzer and b) audible, particularly at higher frequencies. Decimating by 3 is especially bad.
get nemulator
http://nemulator.com

lidnariq
Posts: 9655
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Some advice of DSP here...

Post by lidnariq » Thu Jul 02, 2015 10:04 pm

I assume the aliasing is audible with tonal noise... is it audible when the LFSR's in full white noise mode?

User avatar
zeroone
Posts: 934
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Some advice of DSP here...

Post by zeroone » Fri Jul 03, 2015 9:14 am

James wrote:I spent some time analyzing test signals tonight (square, triangle, and noise). Decimating before filtering does cause aliasing that is a) measurable in a spectrum analyzer and b) audible, particularly at higher frequencies. Decimating by 3 is especially bad.
Are you suggesting not to generate an audio sample every 3 CPU cycles?

tepples
Posts: 22044
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Some advice of DSP here...

Post by tepples » Fri Jul 03, 2015 9:23 am

I'm suggesting to generate a sample every 1 or 2 cycles. With 2 cycles, all aliases of square, even-period triangle, noise, and DMC will fold exactly on top of the actual frequencies. Aliasing from odd-period triangle and $4011 writes should be negligible and can be made even more so by using a 2-sample box filter before feeding those particular channels to your big brick wall.
Unfortunately, if you try a higher rate, such as every other CPU cycle or every CPU cycle, at a filter order of 16, the Butterworth filter becomes unstable.
If every 2 cycles causes stability problems in the roll-off after your brick wall, use decimation between the brick wall and the roll-off.

User avatar
zeroone
Posts: 934
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Some advice of DSP here...

Post by zeroone » Fri Jul 03, 2015 9:53 am

zeroone wrote:If every 2 cycles causes stability problems in the roll-off after your brick wall, use decimation between the brick wall and the roll-off.
The stability problems I referred to are a consequence of numerical accuracy. Discrete, high-order Butterworth filters require maintaining a lot of decimal places for certain cutoff frequencies. It's like a bifurcation event in chaos theory; the inaccuracies are rapidly magnified until you just get garbage out. I think that is why James only used a 4th order Butterworth filter and combined it with an Elliptic filter; he said he was able to do all the computations using only 32-bit precision, where as the 16th order Butterworth requires 64-bit.

Prior to experimenting with this filter, I was using a simple box filter and for the most part, it sounded just fine. For practically reasons, I may have to do what you suggested: putting a box filter or some other easy to compute filter in front of the Butterworth filter to try to get something out of all the available samples.

Edit: Another alternative is to use a lower order Butterworth and just apply it multiple times.

Edit 2: A 64-bit, 8th-order Butterworth filter remains stable for input sampling rates of 19687500.0 / 11.0 (NTSC) and 53203425.0 / 32.0 (PAL) and output sampling rates of 44100.0 or 48000.0. The filtering characteristics seem reasonable, but I do not have access to Matlab or any other special filter generating software; I wrote a test program that simply feeds the decimator with a slowly increasing frequency signal and it prints out the ratio of output-to-input gain. Timing still suggests that it should not eat up that much CPU, but I won't really know until I plug it into my emulator.

User avatar
James
Posts: 429
Joined: Sat Jan 22, 2005 8:51 am
Location: Chicago, IL
Contact:

Re: Some advice of DSP here...

Post by James » Fri Jul 03, 2015 3:50 pm

lidnariq wrote:I assume the aliasing is audible with tonal noise... is it audible when the LFSR's in full white noise mode?
mode = 0? No. mode = 1, yes. Here's an example. Noise mode 1, period 5.

Not decimated
2x decimated
zeroone wrote:I think that is why James only used a 4th order Butterworth filter and combined it with an Elliptic filter; he said he was able to do all the computations using only 32-bit precision, where as the 16th order Butterworth requires 64-bit.
You're correct in saying that a 16th order Butterworth would require double precision, but my decisions have nothing to do with stability, because...
zeroone wrote:Edit: Another alternative is to use a lower order Butterworth and just apply it multiple times.
This is what I'm doing. A series of biquad filters. Matlab can convert higher order filters to second order sections. Looks like you can do it with Octave as well, but I've never tried.

Decimating by 2 is probably ok if you're short on CPU time. The only case where it's really audible -- with the signals I've tested -- is with high frequency mode 1 noise.
get nemulator
http://nemulator.com

User avatar
zeroone
Posts: 934
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Some advice of DSP here...

Post by zeroone » Fri Jul 03, 2015 4:52 pm

I plugged in my 8th-order Butterworth decimator and it does not eat up a lot of CPU. In fact, it should be possible to cascade a few of them together for a sharper drop-off.

But, at least to my ear, it does not sound significantly different from what I was doing originally, which was simply averaging together every ~37.287 samples together.

I am having issues with Namco 163 Audio. I can hear a sort of crackling noise in the introduction to Erika to Satoru no Yume Bouken. I assumed it was aliasing, a consequence of my simple sound sample averaging technique. But, nothing changed after plugging in the Butterworth decimator. My Namco 163 implementation averages together the enabled channels per the Wiki suggestion. But, I can still hear a crackling sound. I guess I must be doing something wrong there.

lidnariq
Posts: 9655
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Some advice of DSP here...

Post by lidnariq » Fri Jul 03, 2015 5:35 pm

zeroone wrote:But, at least to my ear, it does not sound significantly different from what I was doing originally, which was simply averaging together every ~37.287 samples together.
A 37-sample boxcar FIR has awful frequency rejection:
37-sample-boxcar-FIR-transfer-function.png
37-sample-boxcar-FIR-transfer-function.png (9.63 KiB) Viewed 1756 times
As a demonstration, try listening to a square wave at 9.4kHz with that implementation; there should be a pretty audible alias (-43dBFS) at 1kHz (from the 5th overtone).

Alternatively, try the tonal noise, which randomly (depending on the exact value of the LFSR when you switch from periodic to tonal) has either a VERY LOUD or completely absent 31st harmonic: [$400E] with $80-$82 should produce aliases from this overtone (i.e. above 24kHz, for 48kHz sample rate output)

User avatar
zeroone
Posts: 934
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Some advice of DSP here...

Post by zeroone » Fri Jul 03, 2015 5:47 pm

lidnariq wrote:As a demonstration, try listening to a square wave at 9.4kHz with that implementation; there should be a pretty audible alias (-43dBFS) at 1kHz (from the 5th overtone).

Alternatively, try the tonal noise, which randomly (depending on the exact value of the LFSR when you switch from periodic to tonal) has either a VERY LOUD or completely absent 31st harmonic: [$400E] with $80-$82 should produce aliases from this overtone (i.e. above 24kHz, for 48kHz sample rate output)
I'm sure you are right, but is there is a game I can try that will sound significantly different? It is possible that my hearing abilities are limited or that my cheap USB Logitech speakers are making these things indistinguishable. Or, my Butterworth filter may not be removing the right frequencies.

User avatar
zeroone
Posts: 934
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Some advice of DSP here...

Post by zeroone » Fri Jul 03, 2015 6:29 pm

James wrote:This is what I'm doing. A series of biquad filters.
And, this is what happens if you cascade 100 8th-order Butterworth filters with a cutoff frequency of 24000 Hz (red is the filter, blue is the cascade). Some gain is lost, a ringing appeared on the left and the drop-off slope did not increase all that much.

tepples
Posts: 22044
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Some advice of DSP here...

Post by tepples » Fri Jul 03, 2015 6:41 pm

James wrote:Here's an example. Noise mode 1, period 5.

Not decimated
2x decimated
It doesn't have a period 5. The shortest periods are 4, 8, 16, and 32. All periods in the LUT are even, in fact. Can you hear a difference between CPU and CPU/2 for any even period?

User avatar
James
Posts: 429
Joined: Sat Jan 22, 2005 8:51 am
Location: Chicago, IL
Contact:

Re: Some advice of DSP here...

Post by James » Fri Jul 03, 2015 7:00 pm

tepples wrote:
James wrote:Here's an example. Noise mode 1, period 5.

Not decimated
2x decimated
It doesn't have a period 5. The shortest periods are 4, 8, 16, and 32. All periods in the LUT are even, in fact. Can you hear a difference between CPU and CPU/2 for any even period?
Good catch; my little test generator script wasn't using the lookup table :oops: Any even cases are fine.

So for the decimating by 2 case, that only leaves the triangle channel, and I can't hear any difference there. Any differences in the spectrum analysis are outside of audible range.
Last edited by James on Fri Jul 03, 2015 8:16 pm, edited 1 time in total.
get nemulator
http://nemulator.com

User avatar
James
Posts: 429
Joined: Sat Jan 22, 2005 8:51 am
Location: Chicago, IL
Contact:

Re: Some advice of DSP here...

Post by James » Fri Jul 03, 2015 7:12 pm

zeroone wrote:
James wrote:This is what I'm doing. A series of biquad filters.
And, this is what happens if you cascade 100 8th-order Butterworth filters with a cutoff frequency of 24000 Hz (red is the filter, blue is the cascade). Some gain is lost, a ringing appeared on the left and the drop-off slope did not increase all that much.
You don't want to use the same filter for each section, though.

Here's the filter I (currently) use in nemulator. It's comprised of 8 biquad filters:
Image

And here's each individual biquad/section:
Image
get nemulator
http://nemulator.com

User avatar
zeroone
Posts: 934
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Some advice of DSP here...

Post by zeroone » Fri Jul 03, 2015 9:03 pm

Well, if you worked out all the filter constants, maybe I should just borrow them :) I thought you mentioned you used a Butterworth filter in there somewhere.

tepples
Posts: 22044
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Some advice of DSP here...

Post by tepples » Sat Jul 04, 2015 5:50 am

It looks like an elliptic brick wall followed by a Butterworth to hide some of the passband ripple. You can identify the Butterworth part in the overall gain plot as the gradual decrease 12 and 20 kHz. In the individual biquads plot, the two Butterworth biquads are those that don't cross down to -inf dB: dark green and lavender.

Popular IIR filter families from least sharp and most linear phase to most sharp and least linear phase:
  1. Bessel: as close to linear phase as possible, an IIR approximation of a Gaussian filter like that used in the S-DSP
  2. Butterworth: sharpest possible without ripple
  3. Chebyshev: ripple in either passband or stopband
  4. Elliptic: ripple in both passband and stopband
TIP: To simulate the speaker of a Game Boy, use a third-order (18 dB/octave) Butterworth highpass with a corner at 800 Hz. However, some design tools may not support odd orders, as Stephen Butterworth's paper introducing the concept of a maximally flat filter described only even orders.

Post Reply