# A mathematical formula describing Mains Hum

Is there any mathematical formula which gives a better or worse approximation of Mains Hum? The idea is to programatically generate the sound.

• I hope this kind of questions can be asked here. I saw a couple related to programming so I figured it will be okay. – Maurycy Zarzycki Aug 28 '13 at 13:48
• The question is fundamentally about the property of the sound, not the programmatic implementation, so I'd say it's quite alright :) – Warrior Bob Aug 28 '13 at 19:48
• Are you about to invent a new genre? HumStep? – ObscureRobot Aug 28 '13 at 21:08
• @ObscureRobot Tempting, but I only need this for a video game. – Maurycy Zarzycki Aug 29 '13 at 6:28

As heard within amplifiers, the frequency can be 100/120Hz (2nd harmonic of 50 or 60Hz) because of the rectification process when converting AC to DC to power said amplifiers. There can also be a component that is the third harmonic of 50/60Hz as well and this is magnetically "coupled" from the AC transformer inside an amplifier.

There are all sorts of variations too. Due to the rectification process and the magnetics of the transformer the second or third harmonic may well have overtones because of the peaky nature of current flowing into the rectifier when charging reservoir capacitors.

So, it depends on what does the picking up and how close it is to the source. A guitar pickup (non humbucker of course!) may pick up a lot of 50H/60Hz from the current in wiring in the walls of your building.

Why not take a recording of it and use some software to analyse the frequency spectra then generate sinewaves to recreate the components found in the spectra. Or just loop the sound you recorded as your "sound".

• Can you elaborate a bit more on the "analyse the frequency spectra" or point me to some article(s) which can give a n00b some more insight? – Maurycy Zarzycki Aug 29 '13 at 6:29
• @MaurycyZarzycki there are various programs that are quite possibly free that can analyse and manipulate wav files. These programs usually come with fast fourier spectrum analysers that produce a nice clean picture showing the various spectra of the signal. I use wavelab by steinberg but the re are plenty of others. – Andy aka Aug 29 '13 at 7:14

It's a 50 or 60hz sine wave (depending on power frequency). You aren't going to get more accurate than that. The harmonics are going to depend on the particular power generation and will vary based on uncontrolled variations in both the grid and how the hum is actually being introduced in to the signal.

• And by hum, AJ means it is basically a sine wave - the overtones present depend on the specific electrical environment. – Rory Alsop Aug 28 '13 at 18:29
• @DrMayhem - thanks for pointing that out. I thought sine wave and put hum because I'm so used to thinking of it in both those terms and always just call it hum. – AJ Henderson Aug 28 '13 at 18:38

The fastest way to solve this problem is simply sample a mains hum / ground loop and work with that. I would start by recording 5 minutes of ground hum. Then find a section of about a minute long that loops nicely (make sure that the start and end points are aligned to either both a rising or falling zero-crossing). Then progressively shorten the sample until it no longer sounds good. You should be able to do this easily in Audacity. You can even generate a 50Hz or 60Hz sine wave in Audacity and play around with adding various harmonics until you get something that you like.

The slower way would be to still start with the mains hum, but instead of shortening below a 1-minute loop, use a Fourier transformation to determine the first few Cosine waves that make up your signal. Then you just code up an oscillator that is made up of those Cosine waves and their coefficients. You probably need fewer than 10 coefficients, but again you will want to experiment for yourself and pick what you like.

Take the Fourier Transformation of the recorded sound and you can reconstruct it from data.

Wikipedia has a great article on Fourier Transformations. No need to explain it here. http://en.wikipedia.org/wiki/Fourier_transform