NES APU in Matlab

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
NES APU in Matlab
by on (#182458)
Hi I'm fairly new to this forum and NES emulation but I'm looking to make a project for a DSP class I'm taking and thought this would be a good community to look for help. I've spent a couple days reading over different threads involving the DSP involved with the emulation and am looking to implement a fairly basic NES APU entirely in matlab demonstrating topics I've learned in class. I'm well versed in FIR/IIR filters, sampling, nyquist rates and aliasing from a conceptual standpoint but not as much a practical one. Are there any general resources I should take a look at before starting up this project? (Specifically related to implementing this in matlab). Has anyone done this in matlab that can offer some advice on what problems to tackle first? Thanks
Re: NES APU in Matlab
by on (#182459)
If you are primarily interested in just exercising your DSP knowledge, we could just provide a recording of raw APU output and you could filter, down-sample, decimate, etc. that data set.
Re: NES APU in Matlab
by on (#182464)
That would be great. Sorry if this was a relatively simple thing I could have searched for.
Re: NES APU in Matlab
by on (#182466)
What are your requirements?
Re: NES APU in Matlab
by on (#182497)
The project requires the manipulation of any 1D signal. So raw APU output like song files for each channel would work or anything similar that I can mess around with.
Re: NES APU in Matlab
by on (#182650)
Anything from 3-5 .wav or .mp3 files of Super Mario Bros. or similarly recognizable songs from an NES game. Raw output that I can manipulate (pre-filtered, unsampled, possibly seperate channels) would be great
Re: NES APU in Matlab
by on (#182662)
Depending on who you ask, "Raw" might mean a sampling rate of 1,789,773Hz, rather than something sane that you would normally think of for audio.
Re: NES APU in Matlab
by on (#182670)
Correct. On the NES, "raw" means either 315/176 = 1.790 MHz, the fastest rate in the APU, or 315/352 = 0.895 MHz, the "sufficient" rate that is perfect for everything but the triangle channel and $4011 writes and near perfect even for those.
Re: NES APU in Matlab
by on (#182681)
...huh. Just tried telling my NSF player to generate a WAV file at the NES CPU rate, and that is apparently a thing it will do out of the box. Sent Audacious into cardiac arrest when I tried to play it, but the data's there.

Still has all the high-/low-pass filtering on it, but I can hack that out easily enough, if you really want it...?
Re: NES APU in Matlab
by on (#182683)
Rahsennor wrote:

Still has all the high-/low-pass filtering on it, but I can hack that out easily enough, if you really want it...?


If it's not too much that'd work great. Even both so I can mess around with each. I'm just looking to exercise some knowledge to crudely show what the NES would do.
Re: NES APU in Matlab
by on (#182691)
Here's the first 30 seconds of the SMB1 main theme at 1789773 Hz, unfiltered. It's quiet and has a large DC offset because that's what I do to avoid clipping.

I can do more if you want, just let me know what to render.
Re: NES APU in Matlab
by on (#182727)
I though the APU would generate samples at odd CPU clocks only.
Re: NES APU in Matlab
by on (#182728)
Pulse, noise, and DMC autoplayback do operate only every other CPU cycle. Triangle and sta $4011 can change on any cycle, but every other cycle should be enough for those in practice.
Re: NES APU in Matlab
by on (#182931)
So I've started working on systems to filter/downsample all of this data which is giving me a good amount of flexing my knowledge but is there anything the NES does internally before outputting the audio? Also any more data sets/raw song snippets would be great for testing what I already have. Thanks is advance.
Re: NES APU in Matlab
by on (#182945)
kyguy37536 wrote:
So I've started working on systems to filter/downsample all of this data which is giving me a good amount of flexing my knowledge but is there anything the NES does internally before outputting the audio?

There is some nonlinearity in the mixing. The two pulse channels interfere with each other, and the triangle, noise and DMC channels interefere with each other. These two sets are then mixed normally - this is where my emulator output is sampled in the file above.

It then applies a series of high-pass and low-pass filters. You can find all the details on the wiki.

kyguy37536 wrote:
Also any more data sets/raw song snippets would be great for testing what I already have. Thanks is advance.

Anything in particular you're looking for, or should I just grab tracks at random?
Re: NES APU in Matlab
by on (#183086)
Would it be at all possible to get some tracks before they've been mixed together? If not then a couple of random tracks would work.
Re: NES APU in Matlab
by on (#183277)
Actually, if they could all be tracks from super mario bros for consistency that would work. Thanks is advance. I'm looking to see if I can also implement the mixer as part of the project.
Re: NES APU in Matlab
by on (#183289)
Four more tracks, in 7z format because zip broke the attatchment size limit.
Re: NES APU in Matlab
by on (#183412)
Does the way you sample the output let you just take the pulse, triangle and noise channels separately before they've passed through the DACs?
Re: NES APU in Matlab
by on (#183422)
kyguy37536 wrote:
Does the way you sample the output let you just take the pulse, triangle and noise channels separately before they've passed through the DACs?

Not without significant modifications.
Re: NES APU in Matlab
by on (#183440)
Okay no problem. Thanks for all the help/data. Are there are any ideas off the top of your head that would maybe allow me to improve on the way that the NES processed the audio so I can go a little more in depth on the project?
Re: NES APU in Matlab
by on (#183450)
kyguy37536 wrote:
Okay no problem. Thanks for all the help/data. Are there are any ideas off the top of your head that would maybe allow me to improve on the way that the NES processed the audio so I can go a little more in depth on the project?

My goal as an emulator author is accuracy, so I honestly have no idea.
Re: NES APU in Matlab
by on (#183603)
Do you have the samples of the songs after the filters have been applied?
Re: NES APU in Matlab
by on (#183607)
NES generates those pretty square waves and triangle waves and stuff at sampling rates in the MHz, then everything after that is analog. No more emulation at that point, just simulating the effects of the circuits. Heck, you'd probably need an oscilloscope to see how close the NES's generated waves are to their ideal forms before any other analog stuff happens.
Re: NES APU in Matlab
by on (#183610)
The triangle is certainly going to look a lot less than ideal :lol:

Speaking of which, does anybody know the +/- voltage of the channels before and after mixing? Would be good to know when adding in ext. audio from a cart.
Re: NES APU in Matlab
by on (#183617)
kyguy37536 wrote:
Do you have the samples of the songs after the filters have been applied?

No, and I couldn't post them anyway, because it destroys the compression ratio. And my filters aren't hardware-accurate to begin with.