Saturday, March 31, 2012

Measure the frequency (hertz) of the AC mains power with MATLAB.


Measure the Frequency of the utility / mains power supply using MATLAB.


why not try to find out what sort of waveform my UPS is generating, whether it’s really a true sine or maybe a step sine. So the result is the following experiment. It really worked as in my case. Apologies to Electronics aficionados, I couldn’t afford a CRO for my home.

To get thing going let’s start with the items required for the setup

1)      A good microphone (I used the one which shipped with my Creative SBLive! Sound card)
2)      A sound card preferably with the Mic. Boost option enabled in the record volume control panel.
3)      A power block which contains a transformer (e.g. the black box with a long wire which came along with your modem, scanner, etc.  Otherwise any other transformer connected to the mains should do!)

On to the procedure part

1)      Connect the microphone to the microphone socket of your sound card. Put the recording volume level to the maximum and turn on microphone boost.
2)      Place the microphone on top of the power block, so that it starts picking up the vibrations due the transformer inside the block. (The transformer is generally the step – down type i.e. it converts 220V AC or 110 V AC to something like 12 V AC.)
3)      Ensure that no other sound is audible in the room such as through the speakers, fans, blowers, etc. (Please turn them off for accurate results, they are sources of noise.)
4)      Now boot up MATLAB (I have Release 6.1, guess Release 5 would do too.)
5)      Next run the following command either in a script file or manually
a=wavrecord(11025,11025);  %record a 1 second sample @ 11025 KiloSamples/sec.
plot(a);
grid on;
6)      That’s it. MATLAB would plot out what the sound card acquired and digitized for you (the AC waveform supplied by your utility company, or the one being generated by your UPS if the mains is down or you have an Online UPS.)
7)      To calculate the frequency, just find out the number of samples between two consecutive crests / troughs of the waveform. (Let it be ‘N’) This can be found by zooming into the graph and finding out the exact sample wherein the crests lie.
8)      Now the frequency is simply (11025/N). Plop up your windows calculator and calculate the frequency. If it’s like 50Hz to 60Hz, then your appliances are safe, it’s high time you hear a loud bang! from your computer’s Power supply (J don’t worry  there might be some mistake somewhere, repeat the experiment)

Figure: A typical sample
           

Figure: Zoomed In at two crests, one at sample number 2038, other one at 2270.
N=2270-2038 = 232. Therefore frequency = 11025/232 = 47.52 Hz.


9)      You can develop own algorithm to calculate the frequency automatically from the data. I used one which considers two peaks which are at a distance of at least 100 samples.





No comments: