Band pass filter?

Does anyone know where I might find a function to do band pass filtering in software? I've been researching it, and a lot of the math is beyond me.

If I can't find something I'll probably try to figure out my own solution using what I know of how to implement low and high pass filters on images, but I'm nor sure how to directly apply that to sounds. Ie, I know how to low pass filter an image with a 64 pixel radius, but I don't know what frequency a low pass filter with a radius of 64 samples would be equivalent to.

Though my best guess is that based on nyquist's sampling theorem if I were playing a sound at 48000hz and I reduced the resolution by half by averaging with a moving window and a radius of 2, that would be equivalent to a low pass filter that filters out everything above 24000hz.

I guess based on that I should be able to calculate precisely how large my window would need to be to low pass filter to any specific frequency. And I guess I would need to do some kind of weighted average to calculate a window of only 1.8 samples for example.

And highpass = original - lowpass. So that's easy enough to calculate I guess...

Then for bandpass, I need to take the original, and pass it through a low pass filter, then a highpass filter.

But the highpass filter in this case would be set to a different frequency than the lowpass filter. So band pass would be something like:

Have a function lowpass() which does weighted windowed average on data based on input data and desired frequency, and returns new data.

Then do this:

tmp = lowpass(original, lowpassfrequency) ' Do lowpass filter on original data.
bandpass = tmp - lowpass(tmp, highpassfrequency) ' Do highpass filter on lowpassed data.

Maybe that would work. My research indicates there's lots of different kinds of lowpass filters though. I have no idea what this sort would be called, or if it will even sound right.

Is it your plan to try and perform the sampling and calculations on an Arduino? I'm not sure it's a powerful enough processor for that with it's limited memory resources and 8 bit operations.

Lefty

Oh, I think it can handle it. :slight_smile:

http://www.geek.com/articles/games/cubase64-11-real-time-audio-effects-on-a-commodore-64-2010105/

The Arduino may be an 8bit, 16mhz processor, but so was the 386 I owned back in the day and it could mix eight channels of samples and do all sorts of effects at the same time. If what I said above will work to do a band pass filter, then it shouldn't be too processor intensive.

Of course the real question is: Is the Arduino powerful enough to do it in realtime if I don't code it in assembly language? And I don't have an answer for that. But doing it in realtime isn't my only option. I could calculate the sample at the start of my code, or I could simply store the sample in program memory. So, if doing in realtime doesn't work, I'm not totally screwed.

The Arduino may be an 8bit, 16mhz processor, but so was the 386 I owned back in the day and it could mix eight channels of samples and do all sorts of effects at the same time. If what I said above will work to do a band pass filter, then it shouldn't be too processor intensive.

I always thought 386 were 32 bit processors and running much higher speed? Anyway I think you will find the most limiting resource on a AVR chip is the small SRAM used for variables and arrays, just 2k on a 328 chip. That really limits things like storing sample arrays.

However I'm quite new to software development and I know that expert programmers can at time do magic and get pretty incredible results with even limited hardware. Perhaps you know of what you speak.

Good luck

Lefty

The 386 was a 32 bit processor - it played a serious part in the birth of Linux for that reason, the earliest ones ran at 66Mhz on the PC. The nearest the PC got to 8 bit was the 8088 (original IBM PC) which was a 16 bit with 8 bit traits. It was a cut down 8086 which was a true 16 bit. The PC (and most early 8 bit home computers) had much more memory than the Arduino does.

Whoops, you're right about it being 32 bit. But they did run at 16mhz. Some of them anyway.

There's also another factor in the speed though besides the speed that just occured to me. And that's how long it takes to execute each instruction. The 386 for example took many clocks to execute certain instructions. The Arduino, I don't know how many clocks it takes to execute anything. But if it takes fewer clocks then it might be the equivalent of a 32mhz 386.

Except for that 32bits thing. I dunno how that would affect the speed. I suppose it depends on what you're doing. I'll be modifying 8bit samples in my code though.

As for the 2K of memory... I keep forgetting it's got so little. :confused:

I'd be using the 32K of program memory to store the sample if I precalculate it. Then I'd adjust the pitch of it in realtime.

But is 2K enough to modify my sound sample in realtime? I don't know. If I'm playing sound back at 30khz, that 2K is enough for 1/15th of a second.

And more importantly is 2K enough to precalculate a sample and store it when my prop first boots? Well, it is a very short sample, played repeatedly and rapidly. It may fit. But then I don't have any ram left for a sound buffer.

Hm. Being spoiled with having virtually unlimited ram on a PC I keep forgetting how little ram I actually have to work with on the Arduino. The ram and not the processor speed might be the limiting factor here.

Well... I don't need a big ram buffer to play samples back at different speeds.

And while I don't know how fast it is to read the flash memory on the chip, I have to assume its fast enough given there is example code that utilizes it to play sound.

I guess what I may be forced to do is store maybe three samples in flash ram and then read and mix them as I play them back with a small or nonexistent sound buffer. The effect I'm trying to create is two chords and some noise from what I can tell from the spectral analysis, and they're very short. If I've got 32K of flash ram and they're only like 1/10th of a second each, then they ought to fit.

In my defense, it has been like 16 years since I touched a 386. :slight_smile:

Btw, you guys should read that C64 guy's paper on how his music player works. Guy's a genius:

[edit]

Well crap! I thought by reading that I'd learn something I could use to implement the band pass filter in realtime on the Arduino, but it turns out the majority of the effects are actually handled by the sid chip, and all the real genius stuff was in how he compressed the sound and how that enabled him to do effects like vocoder and auto-tune.

:cry:

I was wrong, the earliest 386s did run at 16 mHz, I'm getting confused with the earliest Pentium. It was 20 years ago (15 for the Pentium) and I've slept a few times since then :slight_smile:

It was definitely 32 bit though........

It definitely wasn't as slow as 16mHz though ! :wink:

Finger trouble..... ::slight_smile: