# Problems with filter

Hello!

I am trying to implement a simple lowpass filter in c++ on the daisy seed for equalizing a guitar signal.

My method is a discretization of an analog RC lowpass filter. I’m using eulers method to discretize the derivative of the current through the capacitor so i end up getting the difference equation for the filter as:

Vout[n] = (alpha/(1+alpha)) * Vin[n] + (1/(1+alpha)) * Vout[n-1] , where alpha = 2pifc/fs

This is taken from Phil’s lab youtube video #1.

Implementing it in my c++ program i have the following functions for setting up the filter and calculating filter coefficients:

void RCfilter_init(RCfilter *filt, float fc_Hz, float fs_Hz)
{
filt->fs_Hz = fs_Hz;
RCfilter_setCutoff(filt, fc_Hz);
filt->out = 0.0f;
filt->out_last = 0.0f;
}

void RCfilter_setCutoff(RCfilter *filt, float fc_Hz)
{
// Check if cutoff freq is within range
if (fc_Hz > (0.5f * filt->fs_Hz)) // upper threshold at nyquist
{
fc_Hz = 0.5f * filt->fs_Hz;
}
else if (fc_Hz < 0.0f) // lower threshold at 0
{
fc_Hz = 0.0f;
}

``````float alpha = 6.28318530718f * (fc_Hz / filt->fs_Hz);

filt->coeff[0] = alpha / (1.0f + alpha);
filt->coeff[1] = 1.0f / (1.0f + alpha);
``````

}

float RCfilter_Update(RCfilter *filt, float input)
{
// Compute new output sample
filt->out = filt->coeff[0] * (input + filt->coeff[1] * filt->out_last);
filt->out_last = filt->out;

``````if (filt->out > 1.0f)
{
filt->out = 1.0f;
}
else if (filt->out < -1.0f)
{
filt->out = -1.0f;
}

return filt->out;
``````

}

I initialize my init function in main setup

My main function where i call the init function

int main(void)
{
hw.Init();
hw.SetAudioBlockSize(4);
float sample_rate = hw.AudioSampleRate(); // 48kHz

``````led1.Init(hw.seed.GetPin(Terrarium::LED_1),false);
led2.Init(hw.seed.GetPin(Terrarium::LED_2),false);
led1.Update();
led2.Update();

ch.Init(sample_rate);

RCfilter_init(&lpRC, 1000.0f, sample_rate);

hw.StartAudio(AudioCallback);

while(1)
{
hw.DelayMs(6);
hw.ClearLeds();
}
``````

My callback function:

void AudioCallback(AudioHandle::InputBuffer in,
AudioHandle::OutputBuffer out,
size_t size)
{
pedalControls();

``````if (pedalAttribute.POT2 != pedalAttribute.previous_POT2)
{
RCfilter_setCutoff(&lpRC, 10000.0f * pedalAttribute.POT2);
pedalAttribute.previous_POT2 = pedalAttribute.POT2;
}

for(size_t i = 0; i < size; i++)
{
buffer.in = in[0][i];

if (hw.switches[Terrarium::FOOTSWITCH_1].RisingEdge() ||
hw.switches[Terrarium::FOOTSWITCH_2].RisingEdge())
{
led1.Set(!pedalAttribute.FS1_bypass ? 1.0f : 0.0f);
led2.Set(!pedalAttribute.FS2_bypass ? 1.0f : 0.0f);
led1.Update();
led2.Update();
}

if (!pedalAttribute.FS1_bypass)
{
buffer.out = RCfilter_Update(&lpRC, buffer.in);
out[0][i] = buffer.out;
}
else // Bypass
{
out[0][i] = buffer.in;
}
}
``````

}

The problem is that, when i turn my potentiometer to change the cutoff frequency, it simply adjusts the volume of the guitar signal and not the ‘tone’. I have been trying to solve this for a while now, but i just cant see what im doing wrong. This implementation should work, as i’ve seen it work implemented like this on Phil’s lab youtube channel, and it is clear on there that the frequency response of the signal changes and not the volume.

If anyone can help out or has any ideas what the issue might be, it would be much appreciated!

Thanks!

Suggestion, post a complete runnable program reduced to demonstrate your problem.

I realized the problem as i was going through the code here. It was a mistake of wrong coefficient calculation, and it works now. So i guess it helped posting it here somehow lol. Thanks.

1 Like