# Special case peak/notch filter for exactly 1/32 sampling rate?

I need a filter at 1/32 my sampling rate. Because it's 1/2n, I was thinking there might be a special case filter for this case. Anything like this?

• If you can live with multiple notches, you could use a comb filter. It has a particularly efficient implementation, but you would get notches at every integer multiple of $\frac{f_s}{32}$ as well. – Jason R Nov 10 '15 at 6:12
• No special case, just another notch filter. – Matt L. Nov 10 '15 at 7:55

The period, half-period, and quarter-period of the peak or notch frequency are integer multiples (32, 16, and 8) of the sampling period. If you want a linear-phase filter then compared to an arbitrary frequency this enables savings.

When using a windowed finite impulse response (FIR) filter, every 16th coefficient will be zero. Small savings.

When using a truncated infinite impulse response (truncated IIR, TIIR) and Goertzel algorithm based filter, if the phase of the frequency is required to be zero at the center of the impulse response then the length of the impulse response can be chosen such that the phase of the frequency will be an integer multiple of $\frac{\pi}{2}$ at the ends of the impulse response. Such phase shifts of a TIIR marginally stable resonator can be implemented without multiplication that would be needed in case of an arbitrary frequency requiring an arbitrary phase shift to obtain a symmetrical impulse response.

The following band-pass composite TIIR filter has an impulse response that is a raised cosine window function of integer length $N+1$ (counting also the zero-valued endpoints) normalized to unit area and multiplied by a cosine at $1/M$ sampling frequency. $N$ must be even and an integer multiple of $M$ (which needs not be integer). The impulse response is symmetrical around time $N/2$.

$$\text{mid}_0[t] = \text{in}[t] - \text{in}[t-2] - \text{in}[t-N] + \text{in}[t-N-2]\\ \text{mid}_1[t] = \text{mid}_0[t] + 2\cos(\frac{2\pi}{M})\text{mid}_1[t-1]-\text{mid}_1[t-2]\\ \text{mid}_2[t] = \text{mid}_0[t] + 2\cos(\frac{2\pi}{M} + \frac{2\pi}{N})\text{mid}_2[t-1]-\text{mid}_2[t-2]\\ \text{mid}_3[t] = \text{mid}_0[t] + 2\cos(\frac{2\pi}{M} - \frac{2\pi}{N})\text{mid}_3[t-1]-\text{mid}_3[t-2]\\ \text{out}_\text{band}[t] = \frac{-1^\frac{N}{M}}{N}\left(\text{mid}_1[t] - \frac{1}{2}(\text{mid}_2[t] + \text{mid}_3[t])\right).$$

The filter takes advantage of that multiplication by a window function is frequency domain convolution and a raised cosine has a 3-term frequency domain representation. The filter can be easily modified to implement other generalized cosine windows. The three middle filter stages are marginally stable and require periodical refreshing for numerical stability. For example for $M = 32$ and $N = 256$:

Figure 1. Impulse response (horizontal axis: time in samples)

Figure 2. Magnitude frequency response (dB, horizontal axis: frequency in radians)

Because the filter is linear-phase, it is easy to convert it to a notch filter or a peaking equalizer filter: $$\text{out}_\text{notch}[t] = \text{in}[t-\frac{N}{2}] - \text{out}_\text{band}[t]\\ \text{out}_\text{peaking}[t] = \text{in}[t-\frac{N}{2}] + a\,\text{out}_\text{band}[t],\ a \ge -1.$$

The following slightly faster reacting version of the filter has no zero as the first or last sample of the impulse response of length $N-1$ and requires that $N$ is an even integer that is an integer-plus-half multiple of $M$. The impulse response is now symmetrical around time $\frac{N}{2}-1$, so conversions to $\text{out}_\text{notch}$ and $\text{out}_\text{peaking}$ must be modified accordingly.

$$\text{mid}_0[t] = \text{in}[t] + \text{in}[t-N]\\ \text{mid}_1[t] = \text{mid}_0[t] + 2\cos(\frac{2\pi}{M})\text{mid}_1[t-1]-\text{mid}_1[t-2]\\ \text{mid}_2[t] = \text{mid}_0[t] + 2\cos(\frac{2\pi}{M} + \frac{2\pi}{N})\text{mid}_2[t-1]-\text{mid}_2[t-2]\\ \text{mid}_3[t] = \text{mid}_0[t] + 2\cos(\frac{2\pi}{M} - \frac{2\pi}{N})\text{mid}_3[t-1]-\text{mid}_3[t-2]\\ \text{out}_\text{band}[t] = -1^{\frac{N}{M}-\frac{1}{2}}\left(\frac{2\sin(\frac{2\pi}{M})}{N}\text{mid}_1[t] - \frac{\sin(\frac{2\pi}{M} + \frac{2\pi}{N})}{N}\text{mid}_2[t] - \frac{\sin(\frac{2\pi}{M} - \frac{2\pi}{N})}{N}\text{mid}_3[t]\right).$$

For example with $M = 32$, $N = 272$, and $\frac{N}{M} = 8.5$:

Figure 3. Impulse response (horizontal axis: time in samples) with a time span from 0 to 270 inclusive.

The frequency response looks pretty much the same as that of the previous filter, except for that the first zero is not at frequency 0. So I did not bother to plot it.

Very similar filters are presented in:

Eric Jacobsen & Richard Lyons. DSP Tips & Tricks: The Sliding DFT. IEEE Signal Processing Magazine, March 2003, pp 74–80.

Eric Jacobsen & Richard Lyons. DSP Tips & Tricks: An Update to the Sliding DFT. IEEE Signal Processing Magazine, January 2004, pp 110–111.