Question about this sketch

The below sketch is from Tympan and has been converted to used with the Teensy Audio Shield. I have it running now, which for me is nothing short of a miracle:) I’ve experimented with it and discovered that the section at the bottom is used to set the gain of each frequency. What I don’t understand is how the spectrum is divided up. For example, “gain_L_0.setGain_dB(0.0);” tells me that the gain is set at 0 decibels on the left channel for the range known as 0, but I don’t know what that range is. My goal here is to enter the profile from an audiogram chart. I’ve built a bone conductive system and this is the last piece I need (for now, of course). Thanks!!!

CODE;

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Tympan_Library.h>

//Constants
const int myInput = AUDIO_INPUT_LINEIN;
const float samplerate = 44100.0f ;
const int num_blocks = 128;

//Objects
AudioSettings_F32 audio_settings(samplerate, num_blocks);
AudioControlSGTL5000 sgtl5000_1;
AudioInputI2S i2s_in;
AudioConvert_I16toF32 convertIN_L;
AudioConvert_I16toF32 convertIN_R;
AudioConvert_F32toI16 convertOUT_L;
AudioConvert_F32toI16 convertOUT_R;
AudioFilterbankBiquad_F32 filterbank_L(audio_settings);
AudioFilterbankBiquad_F32 filterbank_R(audio_settings);
AudioEffectGain_F32 gain_L_0;
AudioEffectGain_F32 gain_L_1;
AudioEffectGain_F32 gain_L_2;
AudioEffectGain_F32 gain_L_3;
AudioEffectGain_F32 gain_L_4;
AudioEffectGain_F32 gain_L_5;
AudioEffectGain_F32 gain_L_6;
AudioEffectGain_F32 gain_L_7;
AudioEffectGain_F32 gain_R_0;
AudioEffectGain_F32 gain_R_1;
AudioEffectGain_F32 gain_R_2;
AudioEffectGain_F32 gain_R_3;
AudioEffectGain_F32 gain_R_4;
AudioEffectGain_F32 gain_R_5;
AudioEffectGain_F32 gain_R_6;
AudioEffectGain_F32 gain_R_7;
AudioMixer8_F32 mixer_L(audio_settings);
AudioMixer8_F32 mixer_R(audio_settings);
AudioEffectGain_F32 gainFINAL_L;
AudioEffectGain_F32 gainFINAL_R;
AudioOutputI2S i2s_out;

//Wire Time
AudioConnection patchCord1(i2s_in, 0, convertIN_L, 0);
AudioConnection patchCord2(i2s_in, 1, convertIN_R, 0);

AudioConnection_F32 patchCord3(convertIN_L, 0, filterbank_L, 0);
AudioConnection_F32 patchCord4(convertIN_R, 0, filterbank_R, 0);

AudioConnection_F32 patchCord11(filterbank_L, 0, gain_L_0, 0);
AudioConnection_F32 patchCord12(filterbank_L, 1, gain_L_1, 0);
AudioConnection_F32 patchCord13(filterbank_L, 2, gain_L_2, 0);
AudioConnection_F32 patchCord14(filterbank_L, 3, gain_L_3, 0);
AudioConnection_F32 patchCord15(filterbank_L, 4, gain_L_4, 0);
AudioConnection_F32 patchCord16(filterbank_L, 5, gain_L_5, 0);
AudioConnection_F32 patchCord17(filterbank_L, 6, gain_L_6, 0);
AudioConnection_F32 patchCord18(filterbank_L, 7, gain_L_7, 0);

AudioConnection_F32 patchCord21(filterbank_R, 0, gain_R_0, 0);
AudioConnection_F32 patchCord22(filterbank_R, 1, gain_R_1, 0);
AudioConnection_F32 patchCord23(filterbank_R, 2, gain_R_2, 0);
AudioConnection_F32 patchCord24(filterbank_R, 3, gain_R_3, 0);
AudioConnection_F32 patchCord25(filterbank_R, 4, gain_R_4, 0);
AudioConnection_F32 patchCord26(filterbank_R, 5, gain_R_5, 0);
AudioConnection_F32 patchCord27(filterbank_R, 6, gain_R_6, 0);
AudioConnection_F32 patchCord28(filterbank_R, 7, gain_R_7, 0);

AudioConnection_F32 patchCord31(gain_L_0, 0, mixer_L, 0);
AudioConnection_F32 patchCord32(gain_L_1, 0, mixer_L, 1);
AudioConnection_F32 patchCord33(gain_L_2, 0, mixer_L, 2);
AudioConnection_F32 patchCord34(gain_L_3, 0, mixer_L, 3);
AudioConnection_F32 patchCord35(gain_L_4, 0, mixer_L, 4);
AudioConnection_F32 patchCord36(gain_L_5, 0, mixer_L, 5);
AudioConnection_F32 patchCord37(gain_L_6, 0, mixer_L, 6);
AudioConnection_F32 patchCord38(gain_L_7, 0, mixer_L, 7);

AudioConnection_F32 patchCord41(gain_R_0, 0, mixer_R, 0);
AudioConnection_F32 patchCord42(gain_R_1, 0, mixer_R, 1);
AudioConnection_F32 patchCord43(gain_R_2, 0, mixer_R, 2);
AudioConnection_F32 patchCord44(gain_R_3, 0, mixer_R, 3);
AudioConnection_F32 patchCord45(gain_R_4, 0, mixer_R, 4);
AudioConnection_F32 patchCord46(gain_R_5, 0, mixer_R, 5);
AudioConnection_F32 patchCord47(gain_R_6, 0, mixer_R, 6);
AudioConnection_F32 patchCord48(gain_R_7, 0, mixer_R, 7);

AudioConnection_F32 patchCord51(mixer_L, 0, gainFINAL_L, 0);
AudioConnection_F32 patchCord52(mixer_R, 0, gainFINAL_R, 0);

AudioConnection_F32 patchCord61(gainFINAL_L, 0, convertOUT_L, 0);
AudioConnection_F32 patchCord62(gainFINAL_R, 0, convertOUT_R, 0);

AudioConnection patchCord71(convertOUT_L, 0, i2s_out, 0);
AudioConnection patchCord72(convertOUT_R, 0, i2s_out, 1);

void setup() {
// put your setup code here, to run once:
//AudioMemory(12);
AudioMemory(12);

AudioMemory_F32(32);

sgtl5000_1.enable();
sgtl5000_1.adcHighPassFilterDisable();
sgtl5000_1.inputSelect(myInput);
sgtl5000_1.volume(0.5);

float crossfreq[7]={125.0, 250.0, 500.0, 1000.0, 2000.0, 4000.0, 8000.0};
int test_L = filterbank_L.designFilters(7, 6, samplerate, num_blocks, crossfreq);
int test_R = filterbank_R.designFilters(7, 6, samplerate, num_blocks, crossfreq);

if (test_L < 0 || test_R < 0){
Serial.println(“It broke :(”);
}
else{
Serial.println(“It didn’t break :)”);
}

gain_L_0.setGain_dB(0.0);
gain_L_1.setGain_dB(0.0);
gain_L_2.setGain_dB(0.0);
gain_L_3.setGain_dB(0.0);
gain_L_4.setGain_dB(0.0);
gain_L_5.setGain_dB(0.0);
gain_L_6.setGain_dB(0.0);
gain_L_7.setGain_dB(0.0);
gain_R_0.setGain_dB(0.0);
gain_R_1.setGain_dB(0.0);
gain_R_2.setGain_dB(0.0);
gain_R_3.setGain_dB(0.0);
gain_R_4.setGain_dB(0.0);
gain_R_5.setGain_dB(0.0);
gain_R_6.setGain_dB(0.0);
gain_R_7.setGain_dB(0.0);

Serial.println(“Done!”);
}

void loop() {
// put your main code here, to run repeatedly:
// TO DO: Adjustable gains
}

Hi!

The key is this line:

float crossfreq[7]={125.0, 250.0, 500.0, 1000.0, 2000.0, 4000.0, 8000.0};

This line sets the frequency values separating each filter band. So, your “0” filter covers everything below 125 Hz. Your “1” filter covers 125-250 Hz. Your “2” filter is 250-500Hz, etc…up to your “7” filter, which covers 8000Hz and above.

Note that no filter is a perfect filter, so neighboring filters do bleed into each other. Hence, these frequencies are called the “crossover” frequencies; it’s the frequency where the influence of one filter becomes stronger than the influence of the neighboring frequency. But, if you’ve got a high level of gain set for one filter and a low gain value in a neighboring filter, there will be bleed over and you won’t get as much cut from the low-gain filter as you expect. This is normal for an IIR (aka biquad) filterbank.

Chip

1 Like

Thanks so much! I’m working on a bone conduction hearing aid system – transducers worn on headband – to allow open-ear use and simple fitting. My prototype is working quite well at this point using Teensy 4.0, Teensy Sound Shield, two electrec mics, a 3 watt stereo amp and a 3d printed headband. I’ll open source this once the bugs are all out.

2 Likes

Suer cool! Pictures! Pictures!