brian-team / brian2hears

"Brian Hears" auditory modelling toolbox for the brian2 simulator
Other
25 stars 3 forks source link

FIR and IIR filtering in brian2hears #1

Closed victorbenichoux closed 5 years ago

victorbenichoux commented 10 years ago

Hi guys,

I have recently experimented on implementing filtering with brian2 objects. The idea is that all the code here is codegen compatible, which could lead to performance. For now this is not really efficient.

For maximal "wow" factor, you can run the simple_anf.py example in the examples folder

FIR Filtering

The first path I chose was to implement FIR filtering, by using a kind of shift-register structure. Thanks to a lot of help from @mstimberg, I managed to make this happen. The code is in core.linearfilterbank.FIRFIlterbankGroup, and core.linearfilterbank.ShiftRegisterGroup.

The shift register is based on using a reference to the group's variable with rolled indices, and works like a charm. Applying the FIR filter coeficients is then implemented by adding a Synapses object with custom code to do the multiplication, and then a summed variable.

This may be used right now for HRTF filtering, albeit because this relies on a crude implementation of FIR filtering (no fft...) it won't be the most efficient in the long run.

IIR Filtering

My second idea was to implement IIR filtering using the same method as in brian.hears (and scipy). This is achieved in core.linearfilterbank.LinearFilterbankGroup. To implement this, for each channel an array of intermediate value has to be held. To do so, I chose to explicitly construct code with variable names emulating arrays (i.e. x_0, x_1, ...). This is certainly not very efficient, but in general the number of taps in IIR filters (and in turn, the number of intermediate variables) is quite low (3 for the gammatone), so the difference may not be so big. The code in those code objects is sensitive to issues such as brian-team/brian2#260 in brian2 (copy numpy array stuff). The number of neurons is then the number of channels in the filterbank.

Gammatone filtering

I was really happy that I could get to this point. I copied over some code from brian.hears which allowed me to implement the library.gammatone.GammatoneFilterbank object over my LinearFilterbankGroup. Samplerate is set to 1./defaultclock.dt, but I haven't taken special care in clock stuff.

TODO

There are no specific sound objects, or no fancy filterbank chaining and repeating options yet. This is actually very easy to implement, filter chaining is almost like reference variables and repeating/tiling may be implemented with indexed variables. I think the next step is to port as much of this brian hears infrastructure. I should also write documentation!

Tell me what you think!

thesamovar commented 10 years ago

Wow I had no idea you were working on this and you seem to have already achieved a lot! I'm curious to know what the performance is like for C++ standalone for example? Is it good or do we need to think a little harder at some point?

bertrandfontaine commented 10 years ago

Hi Victor,

that sounds promising! I must say I haven't looked at porting Brianhears on Brian 2 for a long time now. So glad you started it! I'll have a closer look on what you've done when I got time.

B.

On Sun, Jun 8, 2014 at 6:03 AM, Dan Goodman notifications@github.com wrote:

Wow I had no idea you were working on this and you seem to have already achieved a lot! I'm curious to know what the performance is like for C++ standalone for example? Is it good or do we need to think a little harder at some point?

— Reply to this email directly or view it on GitHub https://github.com/brian-team/brian2hears/issues/1#issuecomment-45427819 .

victorbenichoux commented 10 years ago

Glad you guys liked it!

@thesamovar The performance is not great, especially with FIR filtering (which currently doesn't use FFT): I will probably open a specific issue on this. As for IIR (as in the gammatone filter), the simple_anf example runs 30% faster in CPP mode in brian2 than in plain python mode in brian.hears. I'll write up speedtests later, but I think it is reasonably fast.

@barnabooze
Maybe you would like to get involved, especially in the porting of library items from brian1... I will keep you updated as to when I get around doing it.

bertrandfontaine commented 10 years ago

I definitly want to be involved but allocating time for this is not really possible at the moment. Keep me posted.

On Tue, Jun 10, 2014 at 1:44 PM, Victor Benichoux notifications@github.com wrote:

Glad you guys liked it!

@thesamovar https://github.com/thesamovar The performance is not great, especially with FIR filtering (which currently doesn't use FFT): I will probably open a specific issue on this. As for IIR (as in the gammatone filter), the simple_anf example runs 30% faster in CPP mode in brian2 than in plain python mode in brian.hears. I'll write up speedtests later, but I think it is reasonably fast.

@barnabooze https://github.com/barnabooze

Maybe you would like to get involved, especially in the porting of library items from brian1... I will keep you updated as to when I get around doing it.

— Reply to this email directly or view it on GitHub https://github.com/brian-team/brian2hears/issues/1#issuecomment-45603161 .

thesamovar commented 10 years ago

OK given that you haven't implemented fft-based filtering I don't find that at all surprising.

By the way, there are a couple of discussions it might be worth looking at for Brian 2:

thesamovar commented 5 years ago

Closing old issues since it looks like we're no longer pursuing this approach to brian2hears.

victorbenichoux commented 5 years ago

😿

thesamovar commented 5 years ago

Well if you have time to work on it I'm still open to it! :)