Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 23 of 23

Thread: FIR filter with Teensy 4 + audio adapter

  1. #1
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44

    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://t-filter.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)

  2. #2
    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).

  3. #3
    Senior Member
    Join Date
    Jul 2020
    Posts
    728
    Quote Originally Posted by fred_france View Post
    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 low-pass and band-pass 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://t-filter.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)

  4. #4
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    840
    I agree with tomas. Is there a tool for predicting how many bits you need for how much distortion?

  5. #5
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    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 ...

  6. #6
    Quote Originally Posted by fred_france View Post
    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/Fixed-point_arithmetic

  7. #7
    Quote Originally Posted by jonr View Post
    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...r-filters.html

  8. #8
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    Quote Originally Posted by tomas View Post
    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/Fixed-point_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

  9. #9
    Senior Member
    Join Date
    Jul 2020
    Posts
    728
    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).

  10. #10
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    Quote Originally Posted by MarkT View Post
    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?

  11. #11
    Senior Member
    Join Date
    Jul 2020
    Posts
    728
    The documentation for the arm primitives notes a restriction on scaling to prevent overflow - this might be why the 200 limit is there?

  12. #12
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    thanks for your help
    i finally found the convolution filter with more coefficient and steeper slopes
    https://github.com/bmillier/Teensy-F...olution-Filter

  13. #13
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    840
    While it's only necessary for high resolution at low frequencies, I am able to run 12288 taps, stereo.

  14. #14
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    Quote Originally Posted by jonr View Post
    While it's only necessary for high resolution at low frequencies, I am able to run 12288 taps, stereo.
    With convolution ?

  15. #15
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    840
    Yes, specifically "fast, partitioned convolution".

  16. #16
    Senior Member
    Join Date
    Jul 2020
    Posts
    728
    BTW why use FIR for simple audio LP/BP/HP filtering? Do you actually need linear-phase?

  17. #17
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    my code allows only 513 taps, can you show me yours ?
    thanks

  18. #18
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    Quote Originally Posted by MarkT View Post
    BTW why use FIR for simple audio LP/BP/HP filtering? Do you actually need linear-phase?
    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

  19. #19
    Senior Member
    Join Date
    Jul 2020
    Posts
    728
    But do you need phase information?

  20. #20
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    I donít think I need phase information (Iím a newbie, iím not shure of what I can do with phase information)

  21. #21
    Senior Member
    Join Date
    Jul 2020
    Posts
    728
    Well if you don't need well defined phase response IIR filters are probably more straight-forward, 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.

  22. #22
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    840
    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://drc-fir.sourceforge.net/

  23. #23
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    Quote Originally Posted by jonr View Post
    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://drc-fir.sourceforge.net/
    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
  •