# Convolution Code Example

People who use hrtflib often don’t have a background in audio programming, and so they ask me what to do with the output of that library. The answer is convolution. There is tons of information on the net about it, and there is lots of cover on it but I wanted to put up a simple code sample in C, to show how convolution works. Here it is:

```void Convolve(const float* const input, unsigned int inputLength, const float* const filter, unsigned int filterLength, float* output) { unsigned int i; unsigned int j; unsigned int lengthOfOutput = inputLength + filterLength - 1;   for(i = 0; i < lengthOfOutput; ++i) { output[i] = 0.f; }   for(i = 0; i < inputLength; ++i) { for(j = 0; j < filterLength; ++j) { output[i + j] += input[i] * filter[j]; } } }```
This entry was posted in Audio, DSP, Programming. Bookmark the permalink.

### 6 Responses to Convolution Code Example

1. Masaaki says:

I recently became somewhat obsessed with binaural audio in games. I’m trying to implement a sort of prototype in Unity as an audio filter.
So I’ve got a set of HRIR data from a database, these were provided as a big dump of WAV files. The question is, what does each WAV file actually mean? And how do I use them to implement a FIR filter?
Any answers would be appreciated. I get the feeling I’m in way over my head – I’m no audio engineer by any means ðŸ™‚

• aristotel says:

Hi,

Without knowing what your background is (coder/designer/audio engineer) I can’t say if you are “in way over your head”, but it sounds like you might be. That isn’t a problem if you are committed enough to learn all the relevant bits. Without knowing your background I would suggest starting off with learning about FIR filters and how they are applied to a signal. Once you know that , the most “basic” form to use the HRTFs is to pick the HRTF of a direction you want to simulate, filter your sound source with that HRTF for each ear. That is basic but if you get that far then you can get to things like being able to change direction (on the fly without interpolating between HRTF sets).

Aristotel

• Dan says:

Hi, is there any chance of you re-uploading your Bformat2Binaural plug-in? I’ve scoured many websites trying to find a copy but it seems to be completely gone. I’d like to know if it can be used with a UHJ recording. Thanks

• Dan says:

Ok, I see your bidules now and I’ve played with them a bit. I have two questions still though:

1. UHJ format – How do I go about recovering the ambisonic information from a stereo UHJ released album?

2. The binauralizer has options ‘normal’ and ‘diffused.’ Could you explain what the differences are and under what circumstances diffused would be utilized?

• aristotel says:

Hi Dan,

You will need to put your UHJ source through a UHj-to-BFormat decoder first. I never implemented one, but if Google doesn’t turn out useful, then your best bet is to join the surround (https://mail.music.vt.edu/mailman/listinfo/sursound) mailing list and ask the guys there.