# FIR filter Gain of 0dB at Passband Frequency

I have designed a FIR pass band filter. The impulse response looks good and on applying this on my input data, the filter works as desired. Now I have a requirement in my system to have exact $0$ dB gain at passband frequency and hence I normalize the FIR coefficients using the following method:

for (n=0; n < N; n++)
Gain += FIRCoefficients[n] * cos(2 * PI_DOUBLE * PassbandFreq * n);

for (n=0; n < N; n++)
FIRCoefficients[n] = FIRCoefficients[n] / Gain;


Even after this normalizing, the Gain at passband frequency is around $23$ dB. What am I doing wrong?

FIR coefficients are $h[n]$, the same as the impulse response. there are $N$ non-zero taps.

$$h[n] = 0 \qquad \text{for } n<0, n \ge N$$

frequency response is

\begin{align} H(e^{j \omega}) &= \sum\limits_{n=-\infty}^{+\infty} h[n] e^{-j \omega n} \\ &= \sum\limits_{n=0}^{N-1} h[n] e^{-j \omega n} \\ &= \sum\limits_{n=0}^{N-1} h[n] \big( \cos(\omega n) - j \sin(\omega n) \big) \\ \end{align}

the magnitude-squared is

\begin{align} \Big| H(e^{j \omega}) \Big|^2 &= \left| \sum\limits_{n=0}^{N-1} h[n] e^{-j \omega n} \right|^2 \\ &= \left| \sum\limits_{n=0}^{N-1} h[n] \big( \cos(\omega n) - j \sin(\omega n) \big) \right|^2 \\ &= \left| \sum\limits_{n=0}^{N-1} h[n] \cos(\omega n) - j \sum\limits_{n=0}^{N-1} h[n] \sin(\omega n) \right|^2 \\ &= \left( \sum\limits_{n=0}^{N-1} h[n] \cos(\omega n) \right)^2 + \left( \sum\limits_{n=0}^{N-1} h[n] \sin(\omega n) \right)^2 \\ \end{align}

you must square root that expression to get magnitude of gain

$$\Big| H(e^{j \omega}) \Big| = \sqrt{\left( \sum\limits_{n=0}^{N-1} h[n] \cos(\omega n) \right)^2 + \left( \sum\limits_{n=0}^{N-1} h[n] \sin(\omega n) \right)^2 }$$

evaluate at at your known PassbandFreq $\omega_0$ (normalized angular frequency) and divide each $h[n]$ by that gain magnitude:

$$h[n] \leftarrow \frac{h[n]}{\ \big| H(e^{j \omega_0}) \big| \ }$$