# 1/f^2 weighting for least-squares FIR

I am currently modifying the firls.m from Octave to allow all types of FIRs, differentiators, and Hilbert transformers, while taking a peak at Matlab's. I see there that, for differentiators, the default weighting is 1/f^2. In Selesnick's paper there's no mention of this. Does anyone know how to achieve it?

My thoughts go to $\int{K(\omega)\cos(n\omega)d\omega}$ (and the rest). Does it derive from $\int{K(\omega)^2\cos(n\omega)d\omega}$ ? Or from $\int{\left[\int{K(\omega)d\omega}\right]\cos(n\omega)d\omega}$ ? Does anyone have a hint?

I just thought of it this way: if it uses a $1/f^2$ weighting, doesn't that mean that the weighting $K(\omega)$ becomes $1/\omega^2$ ? So, the integral would become:

$$\frac{1}{\pi}\int_0^{\pi}{K(\omega)\cos(n\omega)d\omega}=\frac{1}{\pi}\int_0^{\pi}{\frac{\cos(n\omega)}{\omega^2}d\omega}$$

which is similar to the exponential cosine integral, without the log part. Further on there's:

$$\frac{in}{2}\left[\Gamma(-1,in\omega)-\Gamma(-1,-in\omega)\right]$$

The nice thing is that there is no imaginary part in the result. Maybe I'm hoping too much?

Well the page you link to says:

'differentiator' for type III and type IV filters, using a special weighting technique. For nonzero amplitude bands, the integrated squared error has a weight of (1/f)2 so that the error at low frequencies is much smaller than at high frequencies. For FIR differentiators, which have an amplitude characteristic proportional to frequency, the filters minimize the relative integrated squared error (the integral of the square of the ratio of the error to the desired amplitude).

Bolding mine.

So, following the paper: $$\epsilon_2 = \int_0^\pi W(\omega)(A(\omega)−D(\omega))^2d\omega$$ for the differentiator, we want: $$W(\omega) = \frac{1}{\omega^2}\\ D(\omega) = \omega$$ and $A(\omega)$ has the same form as before.

So $$\epsilon_2 = \int_0^\pi \frac{1}{\omega^2}(A(\omega) - \omega)^2 d\omega\\ = \int_0^\pi \left[\frac{A^2(\omega)}{\omega^2} - 2\frac{A(\omega)}{\omega} + 1 \right] d\omega$$ Just substitute the expression for $A(\omega)$ and you're done.

• Isn't that a least-squares FIR's purpose? After all (quote from the same page): "firls designs a linear-phase FIR filter that minimizes the weighted, integrated squared error", which sounds very similar. This is simply something I'd rather not just throw darts. I could try various formulas, whichever seems about right, but I fear the results might not come close to what a true 1/f^2 weighting is. :-) I suppose I just want to be sure about this. – a concerned citizen May 17 '17 at 19:37
• Sorry, I'm not sure I understand. The weighting in firls can be chosen virtually arbitrarily. For differentiators, because their magnitude response is linear with frequency, they've chosen a relative error. It's not clear to me what your question is.... – Peter K. May 17 '17 at 19:39
• The weights are constant over each band, e.g. for a band of [0 pi/3] the weight can only be a number, fixed, like 10, but this 1/f^2 weighting means the weights change with frequency, so it's variable. For constant weights, there's the derivation in Selesnick's paper, but for 1/f^2, I don't know, not even where to start from, hence my few guesses in the question. – a concerned citizen May 17 '17 at 19:44
• @aconcernedcitizen : See my update. Does that help? I'm missing what you're missing. – Peter K. May 17 '17 at 20:01
• I edited my question, but now, looking at both, I realize I forgot it's $K(\omega)D(\omega)$, which means, for $K(\omega)=1/\omega^2$ and $D(\omega)=\omega$ that the result is $1/\omega$, so then the integral becomes $\int{\frac{\cos(n\omega)}{\omega}d\omega}$, which is the cosine integral. This looks promising so I'll go on with this, see where it leads. Two brains are better than one, my grandpa always said. :-) – a concerned citizen May 17 '17 at 20:21