# Designing appropriate filter by choosing values for $A$ and $B$

Let $$y[n] - ABy[n-1] +A^2y[n-2] = x[n] - Bx[n-1] +x[n-2]$$ Where $$A$$ and $$B$$ are constants. Also there are two sounds which have $$400$$ hertz noise and the sampling rate is $$8000$$ samples/sec. The task is determining $$A$$ and $$B$$ in order to eliminate the noise. I've written a MATLAB program which takes $$A$$ and $$B$$ as inputs and then plots the frequency response of the filter as output(using freqz command). Letting $$A = 0.9$$ and changing $$B$$ from $$1.4$$ to $$1.9$$ leads to shifting the band stop region to the left: Since we want to remove $$400$$ hertz noise choosing $$B = 1.9$$ is good because $$0.1 \times \frac{8000}{2} = 400$$. The main problem is finding appropriate $$A$$. Let $$B = 1.4$$ and increase $$A$$ from $$1.1$$. Part of magnitude plot will move downward: Conversely, if $$B = 1.9$$ increasing $$A$$ from $$0.1$$ to $$0.9$$ causes plot moves upward. If $$A = 1$$ then magnitude plot is a straight line It seems there is some sort of usual trade-off situation here. More elimination of noise is achieved if we select larger values for $$A$$ but this causes more attenuation in other frequencies as well. Is there any mathematical solution for finding the best values for $$A$$ and $$B$$? Also how we can design better filters for removing $$400$$ hertz noise? I mean alternative solutions instead of mentioned IIR filter.

• Isn't your question simply "How we can design better filters for removing 400 hertz noise?" Any reason we need to stay on this A B path? What is the complete details of this noise at 400 Hz, is it really a single frequency or does it have some bandwidth? – Dan Boschen Jul 25 at 1:03
• @DanBoschen No, the main question is determining $A$ and $B$. It's the question that should be answered. I think the noise is a single frequency but I don't know how to check that using given sounds. – S.H.W Jul 25 at 1:09
• @MattL. Would you explain this paragraph, please? "What you actually want is an IIR filter, with poles away from the origin of the complex z-plane. A simple way to design a notch filter is to place the zeros at the desired notch frequency (as you did), and place poles at the same angle but slightly inside the unit circle." I understand we need a zero at the desired notch frequency but why we need poles and place them as you have said? – S.H.W Jul 25 at 13:49
• @S.H.W: The poles make sure that the filter response goes up to 1 close to the notch. Without poles the notch is very broad and a wide frequency band is attenuated. The closer the poles to the unit circle the sharper the notch is and the narrower is the band in which frequencies are attenuated. – Matt L. Jul 25 at 15:23
• @S.H.W: The zero makes sure that there's a notch; a pole inside the circle can't change that, but it can pull up the response close to the notch, so that only frequencies very close to the notch are attenuated. Just plot the filter response for different pole radii and you'll see what's happening. – Matt L. Jul 25 at 16:43

Bottom Line:

$$A <1$$

$$B =2\cos(\omega_n)$$

Where $$\omega_n$$ is the normalized angular frequency of the desired notch location (in this case for the OP with a sampling rate of 8KHz and notch at 400 Hz this would be $$\omega_n = 2\pi400/8000 = \pi/10$$), resulting in $$B \approx 1.902$$ and $$A$$ is the frequency notch bandwidth parameter; the closer $$A$$ is to 1 the tighter the bandwidth of the notch.

Details:

The straight-forward approach to eliminating narrow band noise at 400 Hz would be a classic 2nd order notch filter. This is detailed at this post:

Transfer function of second order notch filter

This approach is done by simply placing a zero at the frequency of interest, and by adding a pole close to the zero, but inside the unit circle of course to be stable, we can adjust the bandwidth of the notch by the proximity of this pole to that zero, meaning $$|p|<1$$, but close to 1.

Using real coefficients, meaning complex conjugate zeros and poles as detailed in the linked post, this results in the transfer function:

$$H(z) = \frac{1+a}{2}\frac{z^2-2z\cos(\omega_n)+1}{(z^2-2az\cos(\omega_n)+a^2)}$$

Which as a difference equation is exactly in the form that the OP has used and therefore given this is the same filter, answers the OP's question. This is is derived from the z-transform as follows (we can ignore the gain scaling parameter $$(1+a)/2$$ which for most cases is close to 1, and express in decreasing powers of z (and assuming I didn't make simple algebra errors!):

$$\frac{Y(z)}{X(z)} = \frac{1-2z^{-1}\cos(\omega_n)+z^{-2}}{(1-2az^{-1}\cos(\omega_n)+a^2z^{-2})}$$

$$Y(z) (1-2az^{-1}\cos(\omega_n)+a^2z^{-2})= X(z)(1-2z^{-1}\cos(\omega_n)+z^{-2})$$

$$Y(z)- Y(z)2a\cos(\omega_n)z^{-1}+Y(z)a^2z^{-2} = X(z)-X(z)2\cos(\omega_n) z^{-1} +X(z)z^{-2}$$

And then with the inverse Z transform we get the desired coefficients that the OP wanted:

$$y[n] - 2a\cos(\omega_n)y[n-1] + a^2y[n-2] = x[n]-2\cos(\omega_n)x[n-1] + x[n-2]$$

With comparison to OP's expression:

$$y[n] - ABy[n-1] +A^2y[n-2] = x[n] - Bx[n-1] +x[n-2]$$

Results in the following:

$$A = a$$

$$B =2\cos(\omega_n)$$

Where $$\omega_n$$ is the normalized angular frequency of the desired notch (for the OP's sampling rate and notch frequency this would be $$\omega_n = 2\pi400/8000 = \pi/10$$) and $$a$$ is the notch bandwidth parameter ($$a<1$$, the closer a is to 1 the tighter the notch, refer to the linked post for complete details).

• Thank you for detailed answer. If we increase $a$ while keeping it less than $1$ doesn't affect attenuation at notch frequency, right? It only affects frequencies close to notch frequency. – S.H.W Jul 25 at 21:28
• What do you mean by "resonance" and "attenuation" regions? Is there any mathematical definition for them? – S.H.W Jul 25 at 23:38
• I’m sorry that answer was misplaced; deleted and let me rephrase It dominantly affects the bandwidth of the attenuation (rejection) region but the depth itself is theoretically infinite at the center frequency of the notch. The magnitude and phase is easily predicted by the equation H(z) by replacing z with $e^{j\omega}$ and sweep $\omega$ from 0 to $\pi$ corresponding to the frequency going from 0 (DC) to half the sampling rate. – Dan Boschen Jul 25 at 23:41
• The bandwidth of the resonance mathematically is typically the region where the magnitude is -3dB less than maximum or more. – Dan Boschen Jul 25 at 23:46
• Thanks. Is there any way to check bandwidth of the noise so that I can choose appropriate value for $A$? I really have no idea how to find the best $A$. – S.H.W Jul 25 at 23:53

This is meant as a stepping stone up to Dan's answer.

The units for frequency at the sample level are radians per sample. You've got:

$$400 \frac{cycles}{second} \cdot 2 \pi \frac{radians}{cycle} / 8000 \frac{samples}{second} = \frac{\pi}{10} \frac{radians}{sample}$$

That is your target $$\omega_t$$.

Sound is a real valued signal, so you can model it like this:

$$s[n] = A \cos( \omega n + \phi ) = A \frac{ e^{i(\omega n + \phi)}+e^{-i(\omega n + \phi)} }{2}$$

Those are your two complex tones you want to zero out. Notice the the units work out as well.

$$\omega \frac{radians}{sample} \cdot n\; samples = \omega n \; radians$$

Starting with:

$$y[n] - ABy[n-1] +A^2y[n-2] = x[n] - Bx[n-1] +x[n-2]$$

Suppose that: \begin{aligned} y[n] &= A_y e^{i(\omega n + \phi_y) } \\ x[n] &= A_x e^{i(\omega n + \phi_x) } \\ \end{aligned}

It follows that: \begin{aligned} y[n+d] &= A_y e^{i(\omega (n + d) + \phi_y) } = A_y e^{i(\omega n + \phi_y }e^{i(\omega d) } = y[n]e^{i\omega d } \\ x[n+d] &= A_x e^{i(\omega (n + d) + \phi_x) } = A_x e^{i(\omega n + \phi_x }e^{i(\omega d) } = x[n]e^{i\omega d } \\ \end{aligned}

\begin{aligned} y[n] - ABy[n-1] +A^2y[n-2] &= x[n] - Bx[n-1] +x[n-2] \\ y[n] \left[ 1 - AB e^{i\omega } + A^2 e^{i2\omega } \right] &= x[n] \left[ 1 - B e^{i\omega} + e^{i2\omega} \right] \\ \end{aligned}

For convenience, make a substitution:

$$z = e^{i\omega}$$

\begin{aligned} ( 1 - AB z + A^2 z^2 ) \cdot y[n] &= ( 1 - B z + z^2 ) \cdot x[n] \end{aligned}

Your $$z_1$$ and $$z_2$$ represent the two tones you want to zero. By putting the polynomial for $$x[n]$$ in factor form, your target values can be plugged right in.

\begin{aligned} 0 = ( 1 - B z + z^2 ) = ( z - z_1 )(z - z_2 ) = z^2 - (z_1 + z_2)z + z_1 z_2 \end{aligned}

\begin{aligned} z_1 &= e^{i\omega_t} \\ z_2 &= e^{-i\omega_t} \end{aligned}

Observe:

$$z_1 z_2 = e^{i\omega_t} e^{-i\omega_t} = e^0 = 1$$

Which fits in the equation without rescaling, therefore

$$B = z_1 + z_2 = e^{i\omega_t} + e^{-i\omega_t} = 2 \cos(\omega_t)$$

• Sorry I couldn't follow your answer. What's the $s[n]$ here? Why you consider $y[n]$ and $x[n]$ in that form? – S.H.W Jul 25 at 21:19