Understanding FIR Filtering

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Understanding FIR Filtering
by on (#117934)
This question comes from a guy with little DSP background. So, when I googled for information, I get lost in the sea of equations and I generally don't get what the websites are talking about.

I really want to learn with respect to the SNES, which provides 8 FIR filter coefficients. I want to understand how the coefficients work, how certain sounds get created. I can experiment with the filter coefficient values, and it was fun to a point, however I have no idea what I am doing.

If I am right, the filter coefficients can be used somehow like an EQ? I understand how EQ's are used to boost or lessen certain frequencies to add/subtract Low/Mid/High frequencies. Is that essentially what these Filter Coefficients will do? Are there differences from EQ that I don't know about??

I'm really looking for the knowledge and application.
Re: Understanding FIR Filtering
by on (#117936)
While I'm not familiar with the SNES inner working, I do have some knowledge in DSP. Of course, DSP is lots of math that is not trivial to understand, but if I try to keep it simple, yes, a 8 coefficient FIR will do some EQ to a sound.

The relation between your 8 coefficient FIR and how it affects sound in the frequency domain is found using the FFT (Fast Fourier Transform). If you know the spectra of the filter you want to achieve, then you can determine easily the FIR coefficients needed to implement this filter, using the inverse fast Fourier transform. Of course, 8 coefficients don't allow for very fine EQ adjustment, but you can still achieve something interesting. With 8 coefficients, you can affect the following "frequencies": DC (0 Hz), ⅛ Fs, ¼ Fs, ⅜ Fs and ½ Fs (Fs → Sampling rate, so ½ Fs is the Nyquist rate). The frequencies in-between are affected too, but it's not by something simple like linear interpolation—it's sinc interpolation.

Well, if you're basically trying to achieve some filter function I can easily give you the coefficients to implement it (without considering the effect of overflow and rounding error) but to you to achieve arbitrary filters it requires some knowledge of DSP, specifically FFT/IFFT, so the best, if you want to keep away from the reading, is to get someone (maybe me) to make a script to do the job for you. If you do want to do some reading, then you should learn what are complex numbers, what is a FIR, what is a complex frequency spectra and what FFT/IFFT does (at the high level, not how it works), basically.

EDIT: I've found some interesting toy for you: http://www.falstad.com/dfilter/. The sad part is that I've found too that you don't do much with a 8 coefficient FIR. If you want to try: use "Filter=custom FIR", "Window=rectangular" and put the order slide all the way down, then use the right arrow to increment order up to 8. Then, you can draw a spectrum in the frequency response graph. If you feel that the spectrum doesn't change much when drawing a spectrum, don't worry: the 8 coefficient FIR can't do much physically.
Re: Understanding FIR Filtering
by on (#117938)
This all combines into the SNES Tracker program, whose prototype design is not complete. I want to design a system that will allow a user to orchestrate music on the SNES, leveraging the full capabilities of the SNES SoundCPU. Part of this is utilizing these Coefficient Filters for the Echo sound tone.

I believe a famous implementation will be several preset Filter settings, such as (low pass, high pass filter, some things in between), with the final setting being to implement your own 8 coefficient values.

While building this program itself will not require the DSP knowledge. I long for it so that I can truly understand how to create cool-sounding FIR filters. Also to create a filter that affects the sound as I intend (if I have direction, like a certain EQ setting I want to make the echo effect sound as). I'm not yet sure how to do that.


Getting back to your reply, I really don't understand the fractional sample rates.
You might be right, that I'll have to learn that stuff to figure it out. Do you have any useful sources that can teach this material to me?
Re: Understanding FIR Filtering
by on (#117939)
The FIR is like taking the input signal, then adding several versions of it together, each offset by one sample, and with each one's gain adjusted separately. So you have the input signal, the input delayed by one sample, delayed by two samples, up to delayed by 7 samples.

If you add just the first, you get the original. If you add the first and second at equal gains, you get a lowpass, since a waveform of say +0.5, -0.5, +0.5, -0.5... would get nullified when added to a shifted version of itself. But if you add the original at 1.0 gain, and the delayed-by-one-sample version at -1.0 gain, you intensify the highest frequencies (and do the opposite to low frequencies, since they only slow change).

Jarhmander, are you sure you don't get more resolution from an 8-point FIR? Remember that it doesn't have to be symmetrical since the phase response can be non-linear.
Re: Understanding FIR Filtering
by on (#117942)
blargg wrote:
Jarhmander, are you sure you don't get more resolution from an 8-point FIR? Remember that it doesn't have to be symmetrical since the phase response can be non-linear.

Well, I don't know precisely with the toy, but the maths won't lie here: from a 8-point FIR you won't control more than 8 discrete frequencies directly, regardless of its phase properties. I don't feel like doing mathematical proofs here (I'm not good at that anyway), but I know this for sure: if all a's below are real (any real digital filter have real FIR/IIR coefficients, all real Z transforms have real valued coefficients, and that holds too in the analog world with the Laplace transform), then the following holds true:
Code:
dft([a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7)]) = [A(0), A(1), A(2), A(3), A(4), A(3)*, A(2)*, A(1)*]

Where dft stands for discrete Fourier transform (FFT is after all only a fast algorithm for doing a DFT), and the notation X* mean "complex conjugate of X". The last 3 values of the RHS are confined to be somewhat the mirror of the values before the Nyquist frequency, but phase reversed. That's because in the first equation here, the e^(-2*i*x*k*n/N) term, when k > N/2, actually alias—the rotating exponential will appear to turn clockwise­—so for X(k) when k > N/2, the function will measure the amplitude of the aliases which, for real signals, are the same as the non-alias ones, but with phase reversion.
Re: Understanding FIR Filtering
by on (#117943)
How come sometimes based on my FIR filter settings, the sound turns into distortion which turns into trash, playing seemingly endlessly until i change some Filter Coefficient(s) to 0 or some mysterious value that 'works'
Re: Understanding FIR Filtering
by on (#117944)
It's as if somehow the coefficients can seem to extend the amount of feedback, and through some settings can make that feedback endless.
Re: Understanding FIR Filtering
by on (#117945)
blargg wrote:
The FIR is like taking the input signal, then adding several versions of it together, each offset by one sample, and with each one's gain adjusted separately. So you have the input signal, the input delayed by one sample, delayed by two samples, up to delayed by 7 samples.

If you add just the first, you get the original. If you add the first and second at equal gains, you get a lowpass, since a waveform of say +0.5, -0.5, +0.5, -0.5... would get nullified when added to a shifted version of itself. But if you add the original at 1.0 gain, and the delayed-by-one-sample version at -1.0 gain, you intensify the highest frequencies (and do the opposite to low frequencies, since they only slow change).

Jarhmander, are you sure you don't get more resolution from an 8-point FIR? Remember that it doesn't have to be symmetrical since the phase response can be non-linear.


Thats a neat trick! :) For non-pow 2 frequencies you can do it weighted as well I think.
Re: Understanding FIR Filtering
by on (#117946)
bazz wrote:
How come sometimes based on my FIR filter settings, the sound turns into distortion which turns into trash, playing seemingly endlessly until i change some Filter Coefficient(s) to 0 or some mysterious value that 'works'

If you use coefficients of say $7F $7F $7F, you'll obviously overflow the integer range and get nasty numeric overflow distortion (not analog saturation distortion). Use smaller values like 0x1F when you're experimenting.
Re: Understanding FIR Filtering
by on (#117952)
If you want to learn about FIR filters, it might be worth getting away from the SNES and playing with bigger ones. 8 taps is a pretty limited filter, so I think it might be difficult to get a good understanding playing with just the SNES.

As Jahrmander said, the key to building an FIR filter is usually a fourier transform. It's hard to understand what's going on by reading just the FIR coefficients, but when you transform them into a frequency graph you can know what it should sound like just by looking at it.
Re: Understanding FIR Filtering
by on (#117961)
I have some fun parameters that I've done before. I've used these all usually with -128 feedback:

64, 32, 16, 8, 4, 2, 1, 0

64, 0, 0, 0, 0, 0, 0, -63

Of course the user can also define them. I just simply find out which ones work well for me just by directly fooling around with the values.