DSPRelated.com
Forums

Accurate Harmonic Analysis

Started by evilzucchini March 1, 2010
I am fairly new (or, more accurately, still very wet behind the ears) to
DSP.  I have very little formal training and got thrust into the duties of
handling the embedded DSP programming for our project.  So please forgive
me if I ask something that is very basic. . .and I believe it is kind of
basic.

At first my duties were pretty simple: sampling some sine waves,
correlating them to determine magnitudes/angles/etc.  I handle this fine
and well within the specs given to us by our customers.

However, now our customers want more (big surprise).  One of the requests
was to do a Total Harmonic Distortion analysis of the currents (we are
working in the power industry).  I have no problem finding and
understanding the mathematics for determining the total harmonic
distortion, however, whenever I do it I get numbers that I believe to be
very high.  I am seeing 11-20% THD on "perfectly" smooth sine waves that I
am generating.  Perfect in the sense that I am adding in no harmonics and
they look very clean on an oscilloscope.  I am using Exocortex 2D FFT
algorithm to generate the power of each harmonic on our sine wave.

I suspect the problem is that I am not sampling perfectly in sync with the
60 Hz sine wave due to imperfections in the clock (like the rate I am
sampling at would not be exactly 128 samples per cycle, but more like
128.5) and that this is making it look like there is more distortion than
there is.  I believe that if you are expecting to see 1 cycle in 128
samples, but you are really only seeing .9 cycles per 128 samples, it may
appear that you have a lot of power in harmonics other than the fundamental
when, in the real world, it is actually a sine wave with very low
distortion.

Would a slightly incorrect sampling rate give significantly incorrect
harmonic data?  If so, how do you correct this?  Is there a good algorithm
for "locking" onto a signal so that your sampling rate becomes more
perfect?  Or am I just totally wrong and it is probably some other
problem?

I did do a search trying to find the solution to this problem or at least
some guidance, but I am not exactly sure what it is called so I have been
unsuccessful trying to find a solution or even if I am barking up the right
tree.

For reference, I am coding in C for a 16 bit Freescale 56f8367 Digital
Signal Controller and doing the FFT calculations in C# for .Net (after
downloading the raw sampled data from the ADC in our unit).

Thank you,
Charlie


On 1 Mar, 20:54, "evilzucchini" <cbar...@digitalgridinc.com> wrote:
> I am fairly new (or, more accurately, still very wet behind the ears) to > DSP. &#4294967295;I have very little formal training and got thrust into the duties of > handling the embedded DSP programming for our project. &#4294967295;So please forgive > me if I ask something that is very basic. . .and I believe it is kind of > basic. > > At first my duties were pretty simple: sampling some sine waves, > correlating them to determine magnitudes/angles/etc. &#4294967295;I handle this fine > and well within the specs given to us by our customers. > > However, now our customers want more (big surprise). &#4294967295;One of the requests > was to do a Total Harmonic Distortion analysis of the currents (we are > working in the power industry). &#4294967295;I have no problem finding and > understanding the mathematics for determining the total harmonic > distortion, however, whenever I do it I get numbers that I believe to be > very high. &#4294967295;I am seeing 11-20% THD on "perfectly" smooth sine waves that I > am generating. &#4294967295;Perfect in the sense that I am adding in no harmonics and > they look very clean on an oscilloscope. &#4294967295;I am using Exocortex 2D FFT > algorithm to generate the power of each harmonic on our sine wave. > > I suspect the problem is that I am not sampling perfectly in sync with the > 60 Hz sine wave due to imperfections in the clock (like the rate I am > sampling at would not be exactly 128 samples per cycle, but more like > 128.5) and that this is making it look like there is more distortion than > there is.
Wrong. Sampling rates have nothing to do with nonlinear distortion. You risk problems with aliasing if your sampling rates are too low, but that's a different problem.
>&#4294967295;I believe that if you are expecting to see 1 cycle in 128 > samples, but you are really only seeing .9 cycles per 128 samples, it may > appear that you have a lot of power in harmonics other than the fundamental > when, in the real world, it is actually a sine wave with very low > distortion.
No.
> Would a slightly incorrect sampling rate give significantly incorrect > harmonic data?
No.
>&#4294967295;If so, how do you correct this? &#4294967295;Is there a good algorithm > for "locking" onto a signal so that your sampling rate becomes more > perfect? &#4294967295;Or am I just totally wrong and it is probably some other > problem?
There are some books out, authored by Bendat either alone or along with Piersol, that address the question of harmonic distortion. Rune
There is 1001 trick how to do harmonic analysis of truncated data. 
However, for power industry purpose, the basic approach would probably 
be good enough:

1. Sample 10 periods or more.
2. Apply Blackman window.
3. Then do the FFT.

Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com


evilzucchini wrote:
> I am fairly new (or, more accurately, still very wet behind the ears) to > DSP. I have very little formal training and got thrust into the duties of > handling the embedded DSP programming for our project. So please forgive > me if I ask something that is very basic. . .and I believe it is kind of > basic. > > At first my duties were pretty simple: sampling some sine waves, > correlating them to determine magnitudes/angles/etc. I handle this fine > and well within the specs given to us by our customers. > > However, now our customers want more (big surprise). One of the requests > was to do a Total Harmonic Distortion analysis of the currents (we are > working in the power industry). I have no problem finding and > understanding the mathematics for determining the total harmonic > distortion, however, whenever I do it I get numbers that I believe to be > very high. I am seeing 11-20% THD on "perfectly" smooth sine waves that I > am generating. Perfect in the sense that I am adding in no harmonics and > they look very clean on an oscilloscope. I am using Exocortex 2D FFT > algorithm to generate the power of each harmonic on our sine wave. > > I suspect the problem is that I am not sampling perfectly in sync with the > 60 Hz sine wave due to imperfections in the clock (like the rate I am > sampling at would not be exactly 128 samples per cycle, but more like > 128.5) and that this is making it look like there is more distortion than > there is. I believe that if you are expecting to see 1 cycle in 128 > samples, but you are really only seeing .9 cycles per 128 samples, it may > appear that you have a lot of power in harmonics other than the fundamental > when, in the real world, it is actually a sine wave with very low > distortion. > > Would a slightly incorrect sampling rate give significantly incorrect > harmonic data? If so, how do you correct this? Is there a good algorithm > for "locking" onto a signal so that your sampling rate becomes more > perfect? Or am I just totally wrong and it is probably some other > problem? > > I did do a search trying to find the solution to this problem or at least > some guidance, but I am not exactly sure what it is called so I have been > unsuccessful trying to find a solution or even if I am barking up the right > tree. > > For reference, I am coding in C for a 16 bit Freescale 56f8367 Digital > Signal Controller and doing the FFT calculations in C# for .Net (after > downloading the raw sampled data from the ADC in our unit). > > Thank you, > Charlie > >
Rune Allnor wrote:
> On 1 Mar, 20:54, "evilzucchini" <cbar...@digitalgridinc.com> wrote: >> I am fairly new (or, more accurately, still very wet behind the ears) to >> DSP. I have very little formal training and got thrust into the duties of >> handling the embedded DSP programming for our project. So please forgive >> me if I ask something that is very basic. . .and I believe it is kind of >> basic. >> >> At first my duties were pretty simple: sampling some sine waves, >> correlating them to determine magnitudes/angles/etc. I handle this fine >> and well within the specs given to us by our customers. >> >> However, now our customers want more (big surprise). One of the requests >> was to do a Total Harmonic Distortion analysis of the currents (we are >> working in the power industry). I have no problem finding and >> understanding the mathematics for determining the total harmonic >> distortion, however, whenever I do it I get numbers that I believe to be >> very high. I am seeing 11-20% THD on "perfectly" smooth sine waves that I >> am generating. Perfect in the sense that I am adding in no harmonics and >> they look very clean on an oscilloscope. I am using Exocortex 2D FFT >> algorithm to generate the power of each harmonic on our sine wave. >> >> I suspect the problem is that I am not sampling perfectly in sync with the >> 60 Hz sine wave due to imperfections in the clock (like the rate I am >> sampling at would not be exactly 128 samples per cycle, but more like >> 128.5) and that this is making it look like there is more distortion than >> there is. > > Wrong. Sampling rates have nothing to do with nonlinear distortion. > You risk problems with aliasing if your sampling rates are too low, > but that's a different problem. > >> I believe that if you are expecting to see 1 cycle in 128 >> samples, but you are really only seeing .9 cycles per 128 samples, it may >> appear that you have a lot of power in harmonics other than the fundamental >> when, in the real world, it is actually a sine wave with very low >> distortion. > > No.
Don't be so quick with that no. If the sample train doesn't represent an integer number of cycles, then it represents all the of harmonics needed to create the abrupt the transition. The FFT "imagines" -- I put it that way to annoy you -- that its input is exactly one cycle of a periodic waveform.
>> Would a slightly incorrect sampling rate give significantly incorrect >> harmonic data?
...
>> If so, how do you correct this? Is there a good algorithm >> for "locking" onto a signal so that your sampling rate becomes more >> perfect? Or am I just totally wrong and it is probably some other >> problem?
There are a few ways. One is sampling for a long time, so that any partial cycle is a small enough part of the whole not to matter. Another, which allows a somewhat shorter sample train, is determining the fundamental power after a jow-pass filter, and subtracting that number from the total power. Properly done, the second method can dispense with an anti-alias filter, capturing the rffect of harmonics higher than half the sample rate.
> There are some books out, authored by Bendat either alone or > along with Piersol, that address the question of harmonic > distortion.
The problem isn't distortion in the signal, but energy that the FFT adds to make the waveform fragment appear to be periodic. Jerry -- "I view the progress of science as ... the slow erosion of the tendency to dichotomize." --Barbara Smuts, U. Mich. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Vladimir Vassilevsky wrote:
> > There is 1001 trick how to do harmonic analysis of truncated data. > However, for power industry purpose, the basic approach would probably > be good enough: > > 1. Sample 10 periods or more. > 2. Apply Blackman window. > 3. Then do the FFT.
Agreed, but for Hi-Fi nuts, the accuracy won't be adequate if one samples 10.5 cycles. Jerry -- "I view the progress of science as ... the slow erosion of the tendency to dichotomize." --Barbara Smuts, U. Mich. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;

Jerry Avins wrote:

> Vladimir Vassilevsky wrote: > >> >> There is 1001 trick how to do harmonic analysis of truncated data. >> However, for power industry purpose, the basic approach would probably >> be good enough: >> >> 1. Sample 10 periods or more. >> 2. Apply Blackman window. >> 3. Then do the FFT. > > > Agreed, but for Hi-Fi nuts, the accuracy won't be adequate if one > samples 10.5 cycles.
BTW, just recently I had to measure ~0.0001% of THD. That's seismic data acqusition specmanship. HiFi nuts, you said ? :-) Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
Vladimir Vassilevsky wrote:
> > > Jerry Avins wrote: > >> Vladimir Vassilevsky wrote: >> >>> >>> There is 1001 trick how to do harmonic analysis of truncated data. >>> However, for power industry purpose, the basic approach would >>> probably be good enough: >>> >>> 1. Sample 10 periods or more. >>> 2. Apply Blackman window. >>> 3. Then do the FFT. >> >> >> Agreed, but for Hi-Fi nuts, the accuracy won't be adequate if one >> samples 10.5 cycles. > > BTW, just recently I had to measure ~0.0001% of THD. That's seismic data > acqusition specmanship. HiFi nuts, you said ? :-)
Can you discuss some of the subtleties involved? What kind of ADC is linear enough to do that? Jerry -- "I view the progress of science as ... the slow erosion of the tendency to dichotomize." --Barbara Smuts, U. Mich. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Thanks for the responses.

I don't think aliasing is a problem because our anti-aliasing filter's
cutoff is less than 2 times our sampling rate.

I am currently looking into window functions and trying to figure out a
good way to try and grab 10 cycles of data and do the analysis on that.

Thanks again,

Charlie
evilzucchini wrote:
> Thanks for the responses. > > I don't think aliasing is a problem because our anti-aliasing filter's > cutoff is less than 2 times our sampling rate.
I don't think aliasing is a problem either. If something I wrote gives the impression that I do, please point it out to me.
> I am currently looking into window functions and trying to figure out a > good way to try and grab 10 cycles of data and do the analysis on that.
A Blackman or Nuttall window ought to work well. Twenty cycles will do better if you can afford the time. Starting and ending the sample train with, say, positive-going zero crossings may work well enough so that ten cycles do nicely. Remember not to include the tempting extra sample. If a digitized cycle begins with a positive-going zero, it ends just before the next positive-going zero. Jerry -- "I view the progress of science as ... the slow erosion of the tendency to dichotomize." --Barbara Smuts, U. Mich. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
evilzucchini <cbarnes@digitalgridinc.com> wrote:
(snip)
 
> However, now our customers want more (big surprise). One of the requests > was to do a Total Harmonic Distortion analysis of the currents (we are > working in the power industry). I have no problem finding and > understanding the mathematics for determining the total harmonic > distortion, however, whenever I do it I get numbers that I believe to be > very high. I am seeing 11-20% THD on "perfectly" smooth sine waves that I > am generating. Perfect in the sense that I am adding in no harmonics and > they look very clean on an oscilloscope. I am using Exocortex 2D FFT > algorithm to generate the power of each harmonic on our sine wave.
There are different ways to say this. I usually say that the FFT has periodic boundary conditions. That is, you get the transform of the signal that is periodic with a period the length of the transform.
> I suspect the problem is that I am not sampling perfectly in sync with the > 60 Hz sine wave due to imperfections in the clock (like the rate I am > sampling at would not be exactly 128 samples per cycle, but more like > 128.5) and that this is making it look like there is more distortion than > there is.
I agree.
> I did do a search trying to find the solution to this problem or at least > some guidance, but I am not exactly sure what it is called so I have been > unsuccessful trying to find a solution or even if I am barking up the right > tree.
One solution is a PLL to lock your clock onto a multiple of the input frequency. Traditionally, that takes hardware. With an FPGA you might be able to do a DLL (digital locked loop). (Well, it is still hardware, but you don't need to add any chips.) The ability to do that, and I believe in general, is that the signal not be do noisy that you can't determine the period. For the PLL, that will be in the properties of the low pass filter, and the time needed for it to lock. While noise might be considered separately from distortion, it isn't always easy to separate them. -- glen