Forum Rule: Always post complete source code & details to reproduce any issue!

FIR filter with Teensy 4 + audio adapter
Hello,
I am a beginner in the world of Teensy, and I bought a version 4.0 + the audio adapter.
I want to experiment audio filters (bandpass, high pass, low pass).
For this, I want to use an FIR filter, but I can't seem to get any valid response curves.
To start, I tried the example provided in Audio / Effects / Filter_FIR, and I measured the frequency response of the low pass filter: the shape is correct, but I only have 10dB of attenuation at 2Khz instead of 60 dB as advertised in the demo sketch.
Moreover, I tried to generate my own tables of coefficients with a tool (http://tfilter.engineerjs.com/), by setting: 44117hz, 200 taps, and changing "static int" to "const short ”, the results curves are catastrophic.
I noticed that the examples found on the internet are for a Teensy v3.x and not for v4, maybe some settings need to be adapted for my version 4?
thanks a lot for your help
(sorry for my bad english)

Datatype short is 16 bit  it is too low resolution for coefficients of a good filter. Also if you just change the data type from int to short, you will likely overflow the range, as short has only +/2^15 range (32768...+32767) while int spans much higher range (2^31...+2^31) so all coefficients get wrong. To avoid overflowing the range, when changing from int to short you would need to divide all coefficients by 65536 (2^16). For good quality filters you need either int or float. Super sharp filter may even need double (note that the tool you pointed out only supports int and double).

Originally Posted by
fred_france
Hello,
I am a beginner in the world of Teensy, and I bought a version 4.0 + the audio adapter.
I want to experiment audio filters (bandpass, high pass, low pass).
For this, I want to use an FIR filter, but I can't seem to get any valid response curves.
To start, I tried the example provided in Audio / Effects / Filter_FIR, and I measured the frequency response of the low pass filter: the shape is correct, but I only have 10dB of attenuation at 2Khz instead of 60 dB as advertised in the demo sketch.
Both the lowpass and bandpass work perfectly for me on T4.0 + audio adapter. You are using the left channel for the low pass?
Moreover, I tried to generate my own tables of coefficients with a tool (
http://tfilter.engineerjs.com/), by setting: 44117hz, 200 taps, and changing "static int" to "const short ”, the results curves are catastrophic.
I noticed that the examples found on the internet are for a Teensy v3.x and not for v4, maybe some settings need to be adapted for my version 4?
Shouldn't make any difference
thanks a lot for your help
(sorry for my bad english)

I agree with tomas. Is there a tool for predicting how many bits you need for how much distortion?

Thank you for your answers.
I tried differently with another software "rePhase" more complete and easier to use
This software gives me coefficients in float.
To process these floats on the Teensy side, I found an example on the internet which converts them into shorts like this:
(short) (32768 * 0.00000122889570184005)
It works well, but I don't really understand why ...

Originally Posted by
fred_france
This software gives me coefficients in float.
To process these floats on the Teensy side, I found an example on the internet which converts them into shorts like this:
(short) (32768 * 0.00000122889570184005)
It works well, but I don't really understand why ...
It works because you have converted (rescaled) floating point coefficients to fixed point. If you use short data type (16 bit), coefficients are supposed to be in the range of 32768 upto 32767 and that values represent fractions of 1.0..+1.0. See: https://en.wikipedia.org/wiki/Fixedpoint_arithmetic

Originally Posted by
jonr
I agree with tomas. Is there a tool for predicting how many bits you need for how much distortion?
Yes see:
https://www.mathworks.com/help/dsp/u...rfilters.html

Originally Posted by
tomas
It works because you have converted (rescaled) floating point coefficients to fixed point. If you use short data type (16 bit), coefficients are supposed to be in the range of 32768 upto 32767 and that values represent fractions of 1.0..+1.0. See:
https://en.wikipedia.org/wiki/Fixedpoint_arithmetic
ok, i understand, thank you.
is it possible to increase the number of taps ? (more than 200), i've tried in filter_fir.h to set 400 instead 200, but it doesn't work with arrays with more than 200 coefs

You'd have to edit filter_fir.h to change the #define FIR_MAX_COEFFS to be more than 200.
Note the number of taps needs to be even and 4 or more to work with the ARM fir primitive used in filter_fir.cpp (for T4 at least).

Originally Posted by
MarkT
You'd have to edit filter_fir.h to change the #define FIR_MAX_COEFFS to be more than 200.
Note the number of taps needs to be even and 4 or more to work with the ARM fir primitive used in filter_fir.cpp (for T4 at least).
yes this is the first thing i tried but after i tried a 250 array that didn't work .. has anyone tried?

The documentation for the arm primitives notes a restriction on scaling to prevent overflow  this might be why the 200 limit is there?

thanks for your help
i finally found the convolution filter with more coefficient and steeper slopes
https://github.com/bmillier/TeensyF...olutionFilter

While it's only necessary for high resolution at low frequencies, I am able to run 12288 taps, stereo.

Originally Posted by
jonr
While it's only necessary for high resolution at low frequencies, I am able to run 12288 taps, stereo.
With convolution ?

Yes, specifically "fast, partitioned convolution".

BTW why use FIR for simple audio LP/BP/HP filtering? Do you actually need linearphase?

my code allows only 513 taps, can you show me yours ?
thanks

Originally Posted by
MarkT
BTW why use FIR for simple audio LP/BP/HP filtering? Do you actually need linearphase?
I am building a speaker frequency response analyzer, so I generate sounds (20hz to 20khz) in the loudspeaker to be tested, and I measure the signal level with the microphone.
So I need to filter the signal from the microphone to only have the one produced by the sound generator

But do you need phase information?

I don’t think I need phase information (I’m a newbie, i’m not shure of what I can do with phase information)

Well if you don't need well defined phase response IIR filters are probably more straightforward, and if you need several their coefficient
arrays are _much_ more compact.
For simple frequency analysis an FFT is normally used direct, you can do any filtering directly in the frequency domain once you have
the spectrum.

For speaker analysis and correction, this is great.
https://www.roomeqwizard.com/
If you want to do convolution based correction, then just use the impulse response from REW and also use http://drcfir.sourceforge.net/

Originally Posted by
jonr
thanks for these links!
in fact I also want to also measure the sound insulation of windows, doors, discover the world of acoustics and Teensy.
It's more about discovery and learning than a real need
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules