Is it possible to calculate the output level change (dB) filter does?

Lets say you have a peaking EQ filter with +12dB gain boost and Q = 1/sqrt(2). I've tried by just summing the magnitude responses (dB) at every sample and then divided result by number of samples in calculation (fs/2):

pkg load signal
pkg load control

clear all;

fs = 44100;
fc = 1000;
G = 12;  % dB
Q = 1/sqrt(2);

fs2 = fs/2;
nf = logspace(0, 5, fs2);

    % peak filter coefficients
    a0 = 10^(G/40);

    w0 = 2*pi*fc/fs;
    cw0 = cos(w0);
    sw0 = sin(w0);

    alpha = sw0/(2*Q);

    b(1) = 1 + alpha * a0;
    b(2) = -2 * cw0;
    b(3) = 1 - alpha * a0;
    a(1) = 1 + alpha / a0;
    a(2) = -2 * cw0;
    a(3) = 1 - alpha / a0;

    % peak filter
    flt = tf(b,a,1/fs);

    % total gain after filter
    [mag, pha] = bode(flt,2*pi*nf);
    Gdl = sum(abs(mag))/fs2

    GdB = 20*log10(abs(Gdl))  % dB

    semilogx(nf, 20*log10(abs(mag)), 'color', 'g', 'linewidth', 1, 'linestyle', '-');
    hold on;

and it results +2.5321 dB level change after filter.

Tried with few other Q values and it looks like level change decreases when Q value increases and increases when Q value decreases:

0.5*Q   3.8485 dB
2*Q     1.5236 dB
3*Q     1.0933 dB
4*Q     0.8533 dB
5*Q     0.6999 dB

Are these results OK? Is this a proper way to solve the level change filter does (when no audio present)? If it is then, does this hold when you filter say (white) noise signal? If not then how to do it?

EDIT: Here's Desmos sheet demonstrating the effect of Q for level after filter (no signal present). Polynomial there is calculated in LibreOffice Calc (trend line tool) using Q values and their corresponding dB values below code gives.

EDIT2: Here's example code which shows better what I'm trying to solve (add this code right after the above source code).

    hold on;

    % decrease the filter produced extra gain to 0dB by modifying the peak filter gain coefficients
    g = -10^(GdB/20)  
    a = a*g               
    flt = tf(b,a,1/fs);

    % total gain filter produces should be now close to 0 dB
    [mag, pha] = bode(flt,2*pi*nf);
    semilogx(nf, 20*log10(abs(mag)), 'color', 'r', 'linewidth', 1, 'linestyle', '--');

    Gdl2 = sum(abs(mag))/fs2
    GdB2 = 20*log10(abs(Gdl2))  % dB

Now the final peaking filter should not increase output level from its side.

enter image description here


1 Answer 1


Are these results OK?

its not wrong but not particularly useful either.

Is this a proper way to solve the level change filter does (when no audio present)?

Sorry, this doesn't make any sense. If there is no audio present there is no level change. Both output and input power are zero.

If it is then, does this hold when you filter say (white) noise signal?

Yes, for white noise this will work. But ONLY for signals that have a full range white spectrum, which is almost never the case for audio signals

If not then how to do it?

It's sort of an ill-posed question. The amount of level difference between the output and the input depends heavily on the input signal. Simple example: if you feed a 50 Hz sine wave into your filter, the level change is essentially zero. If you feed it 1 kHz sine wave the level gain is 12 dB.

For an arbitrary signal the output energy can be calculated by weighing the transfer function with the input spectrum and integrating (or averaging). Honestly, the easiest way to do this, is just to run the signal through the filter and measure the output power.

For fixed point processing, you often need to know what's the maximum gain a filter can produce. The two relevant metrics are

  1. Max gain in the frequency domain (12dB in your case)
  2. Max gain in the time domain which is given as the absolute sum of the impulse response, i.e. $G_{max} = \sum |h[n]|$. (13.7dB in your case)
  • $\begingroup$ For 2nd quote, I meant measuring just the filter response(s) as done in example code not filtering some signal. $\endgroup$
    – Juha P
    Apr 5, 2023 at 16:00
  • $\begingroup$ I understand that. But you are trying to measure something that's undefined. The "filter gain" depends on the input signal and if there is no input signal, you can't define the gain. I assume you define the gain as $g = \frac{\sum y^2[n]}{\sum x^2[n]}$, i.e. output energy divided by input energy. If $x[n] = 0$ this is undefined. $\endgroup$
    – Hilmar
    Apr 5, 2023 at 16:19
  • $\begingroup$ "its not wrong but not particularly useful either" --> If results are about OK (and it it holds with white noise signals) then my final aim is to use the results for to calculate interpolating polynomial so I can get close enough result for every Q value in selected range (slot 12 in Desmos sheet : desmos.com/calculator/hlbi342kgd $\endgroup$
    – Juha P
    Apr 6, 2023 at 7:24
  • 1
    $\begingroup$ You certainly can do this, but it's only useful for white noise signals which are fairly uncommon in audio. Most dominant noise sources are more pink, brown, or narrow-band (hum) and normal audio signals (speech, music, movies, etc.) are not white at all. $\endgroup$
    – Hilmar
    Apr 6, 2023 at 15:22
  • 1
    $\begingroup$ @JuhaP: I see. if you need gain compensation to avoid clipping or overflow, you probably should use worst case gain. If it's about power compensation I would use pink or ICE noise instead. If it's about perceived loudness compensation, I would go with pink noise and then apply an A-weighting. It all depends on what you want do, but I can't think of an audio application where white noise is the correct answer (other than maybe amplifier noise). Pink weighting is easy enough to do analytically. $\endgroup$
    – Hilmar
    Apr 7, 2023 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.