+ Post New Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22
  1. #1
    Member level 4
    Points: 1,272, Level: 8

    Join Date
    Sep 2014
    Location
    Prague
    Posts
    74
    Helped
    2 / 2
    Points
    1,272
    Level
    8

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

    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.

  2. #2
    Super Moderator
    Points: 29,643, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,863
    Helped
    1115 / 1115
    Points
    29,643
    Level
    41
    Blog Entries
    9

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

    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  3. #3
    Super Moderator
    Points: 77,827, Level: 68
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,771
    Helped
    3590 / 3590
    Points
    77,827
    Level
    68

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

    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



  4. #4
    Advanced Member level 5
    Points: 14,971, Level: 29
    schmitt trigger's Avatar
    Join Date
    Apr 2013
    Posts
    2,488
    Helped
    805 / 805
    Points
    14,971
    Level
    29

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

    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.
    My batteries are recharged by "Helpful Post" ratings.
    If you feel that I've helped you, please indicate it as a Helpful Post



  5. #5
    Member level 4
    Points: 1,272, Level: 8

    Join Date
    Sep 2014
    Location
    Prague
    Posts
    74
    Helped
    2 / 2
    Points
    1,272
    Level
    8

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

    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?



    •   AltAdvertisement

        
       

  6. #6
    Super Moderator
    Points: 29,643, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,863
    Helped
    1115 / 1115
    Points
    29,643
    Level
    41
    Blog Entries
    9

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

    Quote Originally Posted by KlausST View Post
    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  7. #7
    Super Moderator
    Points: 77,827, Level: 68
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,771
    Helped
    3590 / 3590
    Points
    77,827
    Level
    68

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

    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



  8. #8
    Member level 4
    Points: 1,272, Level: 8

    Join Date
    Sep 2014
    Location
    Prague
    Posts
    74
    Helped
    2 / 2
    Points
    1,272
    Level
    8

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

    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?



  9. #9
    Advanced Member level 5
    Points: 13,005, Level: 27

    Join Date
    May 2015
    Location
    Melbourne, Australia
    Posts
    2,213
    Helped
    754 / 754
    Points
    13,005
    Level
    27

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

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

    http://sim.okawa-denshi.jp/en/TwinTCRkeisan.htm

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



  10. #10
    Super Moderator
    Points: 29,643, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,863
    Helped
    1115 / 1115
    Points
    29,643
    Level
    41
    Blog Entries
    9

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

    Quote Originally Posted by harvie View Post
    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  11. #11
    Member level 4
    Points: 1,272, Level: 8

    Join Date
    Sep 2014
    Location
    Prague
    Posts
    74
    Helped
    2 / 2
    Points
    1,272
    Level
    8

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

    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.



  12. #12
    Super Moderator
    Points: 29,643, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,863
    Helped
    1115 / 1115
    Points
    29,643
    Level
    41
    Blog Entries
    9

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

    Quote Originally Posted by harvie View Post
    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  13. #13
    Member level 4
    Points: 1,272, Level: 8

    Join Date
    Sep 2014
    Location
    Prague
    Posts
    74
    Helped
    2 / 2
    Points
    1,272
    Level
    8

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

    Quote Originally Posted by andre_teprom View Post
    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.



  14. #14
    Super Moderator
    Points: 29,643, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,863
    Helped
    1115 / 1115
    Points
    29,643
    Level
    41
    Blog Entries
    9

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

    Quote Originally Posted by harvie View Post
    Quote Originally Posted by andre_teprom View Post
    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:

    Click image for larger version. 

Name:	0f6e74b4493.jpg 
Views:	2 
Size:	47.0 KB 
ID:	123568





    Source:
    http://www.ni.com/white-paper/3016/pt/
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  15. #15
    Member level 4
    Points: 1,272, Level: 8

    Join Date
    Sep 2014
    Location
    Prague
    Posts
    74
    Helped
    2 / 2
    Points
    1,272
    Level
    8

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

    Quote Originally Posted by andre_teprom View Post
    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.



  16. #16
    Super Moderator
    Points: 29,643, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,863
    Helped
    1115 / 1115
    Points
    29,643
    Level
    41
    Blog Entries
    9

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

    Quote Originally Posted by harvie View Post
    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  17. #17
    Member level 4
    Points: 1,272, Level: 8

    Join Date
    Sep 2014
    Location
    Prague
    Posts
    74
    Helped
    2 / 2
    Points
    1,272
    Level
    8

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

    Quote Originally Posted by andre_teprom View Post
    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:

    Click image for larger version. 

Name:	Mains_hum_spectrum.png 
Views:	1 
Size:	5.6 KB 
ID:	126776

    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.



  18. #18
    Super Moderator
    Points: 29,643, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,863
    Helped
    1115 / 1115
    Points
    29,643
    Level
    41
    Blog Entries
    9

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

    Quote Originally Posted by harvie View Post
    I am not sure about this
    It is not clear the distortion level that the above FFT represents. Unless it was taken from an industrial environment dealing with spark devices, the supposed distortion due to linear and even switched power suppliers should not be too expressive.

    I would expect something more similar to that:

    Click image for larger version. 

Name:	agilent.png 
Views:	3 
Size:	88.1 KB 
ID:	126778
    Source:
    http://archive.siliconchip.com.au/cm...9/article.html
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  19. #19
    Super Moderator
    Points: 260,240, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,443
    Helped
    13827 / 13827
    Points
    260,240
    Level
    100

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

    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.



  20. #20
    Advanced Member level 5
    Points: 13,005, Level: 27

    Join Date
    May 2015
    Location
    Melbourne, Australia
    Posts
    2,213
    Helped
    754 / 754
    Points
    13,005
    Level
    27

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

    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.
    Cheers, Tony.


    1 members found this post helpful.

--[[ ]]--