Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gain calculation for EQ 2nd order low pass filters. #20

Merged
merged 1 commit into from
Aug 2, 2021

Conversation

bw3
Copy link
Contributor

@bw3 bw3 commented Jul 26, 2021

Without this fix, a gain other than 0db will cause significant distortion.

@MCUdude
Copy link
Owner

MCUdude commented Jul 27, 2021

Thanks for the PR! This algorithm was borrowed from the AidaDSP library, which this library is based on. May I ask how you figured this out, or where you found the algorithm that confirms your conclusion?

@bw3
Copy link
Contributor Author

bw3 commented Jul 30, 2021

I tried to set up a low pass filter with a gain of -20db, and the volume was not reduced as expected, when I added the parenthesis, the volume was reduced, and the audio sounded low-passed as expected.

I used this page to figure out the correct formula:
http://shepazu.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html

  • Equations 2 and 3 show that b0 b1 and b2 should be multiplied by the overall gain value.

  • A bit further down, we see the formula for a low pass filter (LPF) are as follows:

a0 = 1 + alpha
a1 = -2 * cos(w0)
a2 = 1 - alpha
b0 = (1 - cos(w0)) / 2
b1 = 1 - cos(w0)
b2 = (1 - cos(w0)) / 2
  • Combined, this gives us:
a0 = 1 + alpha
a1 = -2 * cos(w0)
a2 = 1 - alpha
b0 = (1 - cos(w0)) / 2 * gainLinear
b1 = (1 - cos(w0)) * gainLinear
b2 = (1 - cos(w0)) / 2 * gainLinear
  • If the overall gain is not being used, it would be 0 db, giving a gainLinear of 1. So if an overall gain is not being applied, b1 is still calculated correctly without the fix.

@MCUdude
Copy link
Owner

MCUdude commented Aug 2, 2021

Awesome, thanks for the explaination!

@MCUdude MCUdude merged commit edfac6c into MCUdude:master Aug 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants