Hi
I was wondering if anyone knows of an efficient way to calculate the THD of a 60Hz sine wave using a basic microcontroller. I was hoping there is a way to do this without using a full FFT. Is this possible?
Scott
Hi
I was wondering if anyone knows of an efficient way to calculate the THD of a 60Hz sine wave using a basic microcontroller. I was hoping there is a way to do this without using a full FFT. Is this possible?
Scott
Think of how an analog THD meter does the "computation." Null the fundamental and measure the noise. This is not really THD but SINAD. However, if the THD is bad, the results will be close.
You can use a LMS scheme to create a signal that represents the fundamental, then subtract it out to get the THD and noise.
Or a DFT, which isn't too much crunching if you only want to dig out the fundamantal and subtract it from everything else.
John
Yes, it is.
Calculate the total energy in the signal.
Then find the best-fit 60Hz sine wave in the signal (take John Larkin's suggestion and do a DFT, or use the overly-hyped Goertzel algorithm). Find its energy.
Subtract the two -- that's your harmonic energy + noise.
-- Tim Wescott Control systems and communications consulting http://www.wescottdesign.com Need to learn how to apply control theory in your embedded system? "Applied Control Theory for Embedded Systems" by Tim Wescott Elsevier/Newnes, http://www.wescottdesign.com/actfes/actfes.html
I see what you are doing, and it sounds simpler than my suggestion, but if the THD isn't really crappy, this kind of subtraction could mix rounding noise due to the finite bitsize, i.e. add quantization noise that really is not part of the THD.
What I was thinking about was to unwrap the signal with an arcsin, which produces a straight line. [Essentially phase accumulation.] Then LMS that data. The intercept and slope should allow an optimal sine wave to be created for the subtraction purpose.
However, the more I think about this, some sort of sampling scheme phase locked to the 60Hz signal could be more productive. Once the sampling is synchronous, you can notch the fundamental.
I did a 4046 PLL driving a MF10 (?)filter to notch out the fundamental on mains about 20 years ago.ISTR that with a high Q filter any spikes would detune the notch for quite some time, while it re settled. I never got around to putting a BPF infront of the PLL, as something else turned up to work on
martin
The first step is to remove the 60Hz part. This is harder than it sounds. You need to match the signal frequency exactly. There are two ways to go at this. One is to phase lock the sample rate of the ADC to the input so that it does an integer number of samples per cycle. The other is to work out exactly how many samples (with fractional part) are in the cycle.
I will assume you do the former. If you do the latter, there is a bit more work to this.
If you write a bit of code that does this sort of thing:
FOR J = 1 to SAMPLES_PER_CYCLE Y(J)=0 NEXT J
FOR I = 1 TO CYCLES_PER_SAMPLE FOR J = 1 TO SAMPLES_PER_CYCLE Y(J) = Y(J) + ADC DELAY AS NEEDED NEXT J NEXT I
When you get done you have an array that holds the shape of the waveform with the signal to noise improved by sqrt(CYCLES_PER_SAMPLE). If you now remove the 60Hz, you have the distortion with the noise reduced.
I real life, it is likely that you will want to remove the 60Hz as you total up the distortion. This takes less memory than leaving the 60Hz in place until you are ready to find the distortion because you can use shorter numbers.
Of course. Pretty much any microcontroller would do. There are two very simple ways:
Vladimir Vassilevsky DSP and Mixed Signal Consultant
If you're not trying to measure very low distortion levels, it might be feasible to pull out the 60 Hz fundamental with a digital filter algorithm, either a highpass or a notch, possibly a combination, ie a highpass elliptical filter. The good thing about AC line frequency is that it doesn't usually change much.
So compute the trms value of the unfiltered and the filtered samples and do the math... fairly simple.
Or use an *analog* filter and a 2-channel adc to sample the raw and filtered signals. Some of the switched-capacitor filter chips are spiffy, with a bit of anti-aliasing help.
I did an ac power meter that computed harmonics. It had a sine lookup table and did DFTs on a line-by-line basis, on successive harmonics of
60 Hz, up to the 16th or something like that, brute force. It ran on an MC6803 (min instruction time 2 usec) and wasn't terribly slow. The assumption was that both the 60 Hz line and the sample rate were precisely known, which is fine for things connected to the grid. I might be able to find the code.John
-- Many thanks, Don Lancaster voice phone: (928)428-4073 Synergetics 3860 West First Street Box 809 Thatcher, AZ 85552 rss: http://www.tinaja.com/whtnu.xml email: don@tinaja.com Please visit my GURU\'s LAIR web site at http://www.tinaja.com
_I_ suspect that anything either of us could cook up would be subject to quantization noise, so a designer would want to carefully check for that, and cope with it.
-- Tim Wescott Control systems and communications consulting http://www.wescottdesign.com Need to learn how to apply control theory in your embedded system? "Applied Control Theory for Embedded Systems" by Tim Wescott Elsevier/Newnes, http://www.wescottdesign.com/actfes/actfes.html
I've used FFT based distortion analyzers, though not any late model gear. They tend not to be as good as the traditional notch filter scheme UNLESS the THD is really bad.
The FFT method can perform as well as the notch type but to do so, they end up costing more than the notch type. They have to maintain a very low distortion all the way to the digitization. This needs about as much design work as making the notch type.
My mind was back in the day when all you had were 16 bit converters. However, Crystal makes 24 bit converters that look super on paper. Most sound card implementation of these mash chips don't live up to the datasheets.
Very mediocre audio cards have the input with the THD at the order of
0.01%. This is more then enough for the measurement of the harmonics of the power frequency.Vladimir Vassilevsky DSP and Mixed Signal Design Consultant
ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.