Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Audio detector that ignores 50Hz mains hum using arduino/attiny

Status
Not open for further replies.

harvie

Full Member level 1
Joined
Sep 10, 2014
Messages
98
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Location
Prague
Activity points
2,485
Hello, i want to design circuit that will automatically switch my amplifier on and off according to signal at the input. I'd like it to be quite sensitive even for quiet music across wide frequency spectre. But in some cases where audio cable is too long there is 50Hz (EU) mains hum, it's not very loud, but it's close to treshold of sensitivity that i want to use. Also when cable is disconnected and left loose without ground on other end it gets even louder, but obviously in such case i want my amp to shut down too.

I've been thinking about some simple analog circuit, but then i realized that this will be much more simple if done using arduino (and attiny once proved to work later). MCU seems to be easier for tuning timeouts and hysteresis than analogue circuits. I Also tought that i can use simple RLC bandstop filter to get rid of mains hum. But then i realized that i can possibly implement it on MCU itself. This is good idea, because such filter can be easily modified to filter 60Hz mains in US or any other site-specific noise that can be present in various places.

So what i need: If there is signal (anything in audible range except for 49-51 Hz) on at least one of two ADCs (i need stereo) then arduino should set output pin HIGH. If there is no such signal for some time (eg.: 5-60 seconds) then arduino should set output pin LOW again. It should react even on relatively quiet signals (so there is place left to decrease sensitivity to needed level using pot).

Maybe there is even no need to use filter and i can use FFT, Goertzel or something like that. I know that i can easily use goertzel algorithm to detect presence of 50Hz signal, so i wonder if there is some similar algorithm that can detect presence of any non-50Hz signal.

I've never done DSP on arduino/attiny, i know that bit depth and samplerate is crappy for music, but i guess it's reasonably good for task like this. It would be great if you can show me your code implementing such filters/algos on arduino. Sometimes it's hard for me to figure how to make theoretical principles into working code.
 

I guess you could do that by an IIR bandpass filter, taking the average amplitude, and then passing this signal through a kind of integrator which would act as an rectifier.
 

Hi,

kill all frequencies below 300Hz with at least second order filter. Then only about 3% of 50Hz remains (-30dB).

Then use a limiting opamp amplifier. high gain using a high ohmic feedback resistor (select for sensitivity) and a zener in parallel to this resistor.
Feed the output of the OPAMP to a low pass filter. tau some seconds. And use a comparator to detect audo presence.

How it works:
50 Hz suppression is clear.
The zener in the opamp circuit gives unsymmetric feedback. As soon as the opamp limits, this unsymmetry causes a DC component at the output.
This is filtered and with a comparator detected.. maybe add some hysteresis to the comparator.
Maybe delay the "switch OFF" 10...20s

Not very sophisticated, but it works.

Klaus
 

Just to make things clear;
the hum you can listen to, is actually at TWICE the powerline frequency, 100 or 120 Hz. Check it out with a scope if you don't believe me.

Nevertheless, Klaus recommendation is very valid. A second order low pass analog filter is very simple to achieve. Whatever noise remains can be filtered out with simple algorithms.
 

But i am looking for simplest DSP solution. Not analog.

I've figured that i can use some arduino lib to do FFT, then add up all FFT bins except for the ones containing hum to get single value and use it to trigger output. What do you think?
 

50 Hz suppression is clear.
The zener in the opamp circuit gives unsymmetric feedback. As soon as the opamp limits, this unsymmetry causes a DC component at the output.

Another way to make a 50Hz digital notch filter is sampling the input audible signal at (1/2*n*50) sampling intervals. It means that the 50Hz presence will be seen invisible because each sample will be took at the same point of the sinewave every time, like a constant level.

- - - Updated - - -

I just saw that Schmitt mentioned the exact same thing.
Important remark that n parameter above should ensure double of the maximum frequency of the audible signal.
 

Hi,

But i am looking for simplest DSP solution
.. DSP ... simple...

DSP:
IIR lowpass to filter out below 50Hz. May be higher order....
--> DC is attenuated also.
Then simply rectify and averag using an new very low frequency IIR.
Threshold. Turn OFF dely.

Almost the same as the analog solution, but here the rectifier is easier and way more precise.

Klaus
 

And what do you think about my idea of simply doing FFT and then trigger output based on sum of all bins except those containing hum?
 

How about a twin T notch filter tuned for 50 Hz ?

**broken link removed**

Just enter 50Hz and press "calculate"
Produces about a -50dB notch.
Nice values of R and C too...
 

And what do you think about my idea of simply doing FFT and then trigger output based on sum of all bins except those containing hum?

The FFT algorithm performs an analysis of the waveform that need to be splitted into long intervals in order to obtain an acceptable resolution, and although the 50Hz mains hum has a typical a steady behavior along repetitive cycles, the non-continuous characteristic of the FFT processing could makes it unsuited for this application. Furthermore the original question asks for not only the detection, but also the ellimination of the 50Hz component.

The solution proposed on post #4 still seems the simplest one.
 

In fact i don't need the filtering for any other purposes than preventing false positive detecting of the audio signal. It will be used only for switching amp on and off, but amplified signal will be unfiltered as i suppose that filtering will possibly add more distortion than the hum itself.
 

i suppose that filtering will possibly add more distortion than the hum itself.

This filtering in particular do not affect output, and actually should not be called properly as a filter. It is just a trick to elliminate determined frequency at the sampling stage itself, making it blind, due considering that all samples will bee took at the same phase of the 50Hz noise, it will add to output a constant value, such as a DC offset component of the waveform.
 

This filtering in particular do not affect output.

That's true, it will not affect output, because there will be no filtering nor sampling before amplifier. Amplifier gets unprocessed analogue input. DSP is just to turn this analogue amp on and off. I am not sure i made this completely clear. I don't care about quality of digital signal. I just don't want to turn the amp off when there will be actual music playing or turn it on when there will be just hum and no music.

I will just tap before amplifier input with this high impedance ADC input to detect signal and react accordingly in terms of turning amplifier on and off with some delay. But signal going to amplifier will not be modified or preprocessed in any way.
 

This filtering in particular do not affect output
That's true, it will not affect output, because there will be no filtering nor sampling before amplifier. Amplifier gets unprocessed analogue input. DSP is just to turn this analogue amp on and off. I am not sure i made this completely clear. I don't care about quality of digital signal. I just don't want to turn the amp off when there will be actual music playing or turn it on when there will be just hum and no music.

I guess I'm the one who was not clear. When I said that didn´t impact on output I meant in terms of the phase shift and distortion that you mentioned right above. No other frequencies than 50Hz will be affected. You can see this sampling like an hiper selective noch filter having infinite Q at the 50Hz frequency.

Have a look at Figure bellow, which somehow illustrates the concept behind this technique
The waveforms at left are before samping, and the corresponging waveforms at right are after sampling:

0f6e74b4493.jpg





Source:
 

Have a look at Figure bellow, which somehow illustrates the concept behind this technique

Yeah, this concept is very clear for me and the image confirms that we are both thinking about the same thing. However it can filter just single frequency. And somebody in this thread said that mains hum is composed of multiple frequencies, or at least 50Hz and it's harmonics. So i supposed that zeroing out single frequency may not be enough.
 

somebody in this thread said that mains hum is composed of multiple frequencies

In real world, the waveform of the mains is not exactly sinusoidal as shown at the pictures above, but softened at the peak, and this actually add some multiple odd harmonics of the fundamental frequency, but considering their much smaller amplitude, the humming effect should be negligible.
 

In real world, the waveform of the mains is not exactly sinusoidal as shown at the pictures above, but softened at the peak, and this actually add some multiple odd harmonics of the fundamental frequency, but considering their much smaller amplitude, the humming effect should be negligible.

I am not sure about this. I've made simple tunable hi-pass RC filter. Generated 50Hz sine from computer and used the filter to cut it on way to speakers. It was completely blocked by filter. Then i tried this setup with 50Hz hum sample from wikipedia:

Mains_hum_spectrum.png

https://en.wikipedia.org/wiki/Mains_hum
https://en.wikipedia.org/wiki/File:MainsBrum50Hz.ogg

And the filter was not able to filter it at all. Even after tuning to higher cutoff... Most of the hum get it to the amplifier and was clearly heard. So i am not really sure that the harmonics are negligible.
 


This is mostly wide band noise that can hardly be filtered. The dominant 50 Hz component is in the same level range as the mid frequencies after applying an A-weight curve representing ear sensitivity.

Your original hum signal may be less wide band.

But in some cases where audio cable is too long there is 50Hz (EU) mains hum
It's actually not the long cable as such rather than an unsuitable single-ended signal connection. Using twisted pair cable with separate ground and differential amplifiers or audio transformers on the receiver side can eliminate the problem.
 

While its true that the mains contains a great deal of harmonic distortion and wide band hash (typically from arching) most of the energy is still at the fundamental 50 Hz.

And we do not even need to get rid of all of that, just a decent attempt at a 50 Hz notch filter should be more than sufficient for the purpose here.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top