In our many experiments one thing that we have often run across is the presence of mains-related hum from the spillover and "glow" of city lights that can invade the audio. In the tests that in which I have personally been involved this has not necessarily been a huge issue as our location (in the relatively sparsely western U.S.) is not completely saturated with such illumination. Even when we have run tests across town we have been able to avoid paths that are "terribly" affected by such energy.
The prototype comb filter.
Click on the image for a larger version.
Not so for some of the folks doing similar experiments in the U.K., one of the most densely-populated countries in Europe. There, it can be difficult - particularly near populated areas - to find a location that does not have a "glow" of city lights on the horizon.
To be sure, there are at least two energy components within such glows that can cause issues:
- The modulation of the lights themselves, typically at twice the AC mains frequency. Because of the non-sinusoidal nature of the waveforms this "hum" contains many harmonics, and because the lighting overall may be from all three phases of the electrical grid, some of these harmonics can be quite strong!
- A "hiss" caused by the thermal noise of such lighting.
Fortunately, the thermal noise generally follows the "1/F" profile - which is to say that it is more intense at lower frequencies and the energy rapidly drops off, being much less of a problem at the frequencies that we use for voice (300-3000 Hz) than at lower frequencies. Because of the nature of human speech, our brains can typically do some "mental DSP" to reduce the deleterious effects of the noise as background noise is a fact of modern life, but the raucous noise from the buzz and hum of the harmonics is far more difficult to deal with!
The Comb filter:
As it turns out what we need to filter the hum/buzz and its harmonics is a "comb filter" which, as its name implies, filters out a base frequency and the harmonics. What is also fortuitous is the fact that such filters are very easy to implement in a simple processor using fixed-point arithmetic.
The type of filter that is implemented in this case is an "IIR" (Infinite Impulse Response). To "construct" a notch filter in software we need only do the following steps:
- Set up a buffer that will delay the "audio" data put into it by the period of the base frequency of the comb filter. If you wanted to filter out a signal that consisted of 100 Hz and its harmonics, this delay would be 1/100th of a second, or 10 milliseconds.
- Take the output of the delay and multiply it by a factor of n, where "n" <1. If "n" is 0.75 for "75% feedback", you would reduce it to 75% of its original amplitude.
- Take a copy of the original signal and reduce it by "1-n". Taking the 75% example above, this original signal would be reduce to 25% of its original amplitude.
- Invert either signal (it does not matter which) and sum the two together and put this result into the delay.
- The output is taken from the delayed signal.
Practically speaking the useful values for the above range from comb filters that have as little as 50% feedback (fairly wide notches - which impart a somewhat "hollow" sound on the audio and fast response) to as much as 93.75% feedback which has quite narrow notches but is quite slow to respond to changes: Values outside this range have been empirically tested and found to be less useful in this particular application - particularly higher values of feedback which tend to excessively slow the response. (Note that the relative response time is proportional to the "base" frequency of the filter, so a 1000 Hz comb filter would respond more quickly than a 100 Hz comb filter with the same amount of feedback.)
This filtering is all done in "C" using the CCS compiler. While not as streamlined as straight assembly, I've worked with this compiler for many years and have learned how to tweak it to produce "reasonably efficient" code that isn't terribly less efficient in execution speed than assembly - plus it takes far less time for me to write and debug C than assembly and the additional bonus is that the "meat" of the algorithms are portable!
For this sort of filtering all that is required in the maths are shifts, adds and subtracts: You may note that the ratios, above (50%, 75%, 93.75%) all consist of discrete inverse powers-of-two (e.g. 93.75% = 100 - (50% + 25+ 12.5% + 6.25%) - all being numbers that can be derived by doing simple right shifts of the data!
Figure 3, below, depicts the schematic diagram of the prototype comb filter:
The schematic of the comb filter board.
Click on the image for a larger version.
The heart of the circuit is U102, a PIC16F1847 processor internally clocked at 32 MHz which yields a sample rate of approximately 32 kHz with full 10 bit resolution of the PWM generator, used for D/A conversion. The processed audio output, in PWM form, is then integrated and low-pass filtered back to "baseband" audio by U101B which is also configured as a low-pass filter. This filtering is not as "strong" as the input filtering since, in most applications, it is less important that the generated energy above the Nyquist frequency be removed to the same degree as the input filtering.
As can be seen in the diagram the input pins are used to select the various filter modes as follows:
- 50/60 Hz - This selects the mains frequency to be filtered. 60 Hz is used in the U.S. and its possessions, parts of North and South America and a few other countries while 50 Hz is used in the rest of the world.
- 1x/2x - This selects whether the "base" frequency of the comb filter is 50/60 Hz ("1x") or twice the frequency (100/120 Hz for "2x").
- Bypass - When left open (high) the filter is bypassed, but pulling this line low will enable the filter.
- Sel1, Sel2 - This selects the various types of IIR filters, each with different amounts of "feedback" as noted in Figure 3.
So there you go: Using a low-end PIC for DSP!