 # Getting complete sinusoidal signal for calculation of total harmonic distortion

Hello all. hope you are tuned and updated. I got head pain since last 2 weeks for calculation of total harmonic distortion (THD) with arduino uno. I believe, efficient THD calculation is possible when some one is applying FFT on complete sinusoidal signal. but limitation of Arduino is that it does not give provision of negative half cycle to be read. Solution which is clicking in my mind, that is outside of the arduino programming (i.e taking +2.5 DC offset of AC signal to make it complete positive making it readable) but i don't want to take much risk of over distortion due large list of components because already transformer is a non linear device that introduce lot of distortion in actual signal be stepping down, if doing 220/12v by transformer--then-->voltage divider circuit--then--> opamp circuit for offsetting--then-->arduino input pin..is a series of enemy. Bunch of components will introduce extra large distortion, so after measuring there will be lot of difference between real THD and measured THD. that will be fake THD measured by my meter. Due to this i don't want to use amplifier circuit just limiting at voltage divider circuit. After story: Question is that, is there a possible way of generating negative half from positive half cycle exactly at the same time of real negative half or in at exactrly same time duration of zero line (zero due to negative half cycle). I think in this way i can generate better results of THD. Bear it please. Thank you :)

A level-shifting plastic film capacitor will not intruduce distortion anything like the transformer, and opamps and resistors ditto. You simply need to DC offset the signal with a capacitor and resistor divider. Use high value resistors like 100k.

With a 10 bit ADC you are limited by quantization noise to -50dB or thereabouts anyway, most passive circuit components are orders of magnitude more linear. Your transformer is the limited factor already I think.

There is no perfect sine wave in the digital domain* because the information is quantized in both the time and amplitude directions. And, the sample rate limits high frequencies so there’s a limit to the harmonics you can read. …I don’t know how all the math works-out but the distortion may have to be severe before you can measure it. (FFT isn’t perfect either.)

If you are measuring the 220V power line, I’d assume the distortion is usually very low. If you are measuring a DC-to-AC converter, the distortion may be high enough to measure.

Solution which is clicking in my mind, that is outside of the arduino programming (i.e taking +2.5 DC offset of AC signal to make it complete positive making it readable) but i don’t want to take much risk of over distortion due large list of components because already transformer is a non linear device that introduce lot of distortion in actual signal be stepping down, if doing 220/12v by transformer–then–>voltage divider circuit–then–> opamp circuit for offsetting–then–>arduino input pin.

If you have enough signal you don’t need an op-amp. All you need is [u]two equal-value resistors and a capacitor[/u].

Question is that, is there a possible way of generating negative half from positive half cycle exactly at the same time of real negative half or in at exactrly same time duration of zero line (zero due to negative half cycle).

If it’s a constant frequency & amplitude (like the power line) then all you have to do is invert the data for the duration of 1/2 cycle. But, you’d have to assume that the negative half-cycle distortion is the same as the positive half-cycle distortion. If there’s no distortion, the inversion would be 100% accurate, but we don’t know the nature of the distortion.

* P.S.
Of course, there are no perfect sine waves in the analog domain either… They only exist in math…

What resolution do you want?

I worked in pro audio for a while, and measured down to 0.002% regularly with analog test gear - eg the Radford LDO / distortion analyser ( that was a long time ago!)

that's about 100dB down, and requires an accurate signal source and at least 17 bits of resolution in an ADC.

Arduinos go home

regards

Allan

manzar_laghari: Hello all. hope you are tuned and updated. I got head pain since last 2 weeks for calculation of total harmonic distortion (THD) with arduino uno. I believe, efficient THD calculation is possible when some one is applying FFT on complete sinusoidal signal. but limitation of Arduino is that it does not give provision of negative half cycle to be read. Solution which is clicking in my mind, that is outside of the arduino programming (i.e taking +2.5 DC offset of AC signal to make it complete positive making it readable) but i don't want to take much risk of over distortion due large list of components because already transformer is a non linear device that introduce lot of distortion in actual signal be stepping down, if doing 220/12v by transformer--then-->voltage divider circuit--then--> opamp circuit for offsetting--then-->arduino input pin..is a series of enemy. Bunch of components will introduce extra large distortion, so after measuring there will be lot of difference between real THD and measured THD. that will be fake THD measured by my meter. Due to this i don't want to use amplifier circuit just limiting at voltage divider circuit. After story: Question is that, is there a possible way of generating negative half from positive half cycle exactly at the same time of real negative half or in at exactrly same time duration of zero line (zero due to negative half cycle). I think in this way i can generate better results of THD. Bear it please. Thank you :)

Hello,

You are saying a lot there.

First, yes the transformer would introduce harmonics but it would also change the frequency response so you'd have to do a profile on the transformer and try to work that into the response equation. If the transformer isnt too bad this would be possible. But yes a voltage divider should work better if your project can stand direct contact to the 220vac input line. That's a safety concern that you'd have to deal with.

Also, i dont know what your max distortion frequency is but you probably have to sample at twice that rate. If the Arduino you use can sample 10k per second that that limits your high end frequency to about 5kHz and you would probably have to include an anti aliasing filter on the front end.

The idea of an offset of +2.5v would work, in the calculation you'd just end up with an a0 term (the DC component) of 2.5 instead of zero. If it is a smooth DC then there should not be any other harmonics introduced.

Since it seems you are working with 220vac, it may be possible to full wave rectify the waveform to make it 'pulsating' DC where the peaks are all positive but they are still of sinusoidal shape. The difference should only be the introduction of a second harmonic, which you can easily ignore in the calculations as well as an increased DC component which you could also ignore since you originally intended to use a transformer, and also some of the the frequencies might be doubled, but since you know the line frequency and can detect the zero crossing, you may be able to negate every other set of samples so that you get the negative half cycle in software rather than hardware. I've never had to do this but it might work so you can look into that if you wanted too. That way you retain all the amplitude resolution as well, for what it is worth. The diodes will introduce a little more distortion but because the line peak is so high relative to the diode drops it should be minimal. Of course some calculations would tell you if it would be acceptable or not and if you could compensate for those drops if you wanted to do that.

Just some ideas.

If you want a true figure you have to sample LOT faster than 2x the frequency of interest..

Power people look up to the 7th harmonic - 350Hz

and you want at least 10x that for sampling.... say 3500/sec

Yes , the arduino can do that, but only to 10 bits . this allows you a resolution of about -60dB ie about 0.1% followed by with a 1024-point FFT.

Whether it can do the sums that fast I don't know . that's a lot of sums in 3mS. Or whether it has enough RAM.... just about

Depends on how accurate you want your numbers to be....

regards

Allan

Wise words by allanhurst. Plus 1.

Hi, This might help.

http://apcmag.com/arduino-project-audio-spectrum-analyser.htm/ It is for stereo, s you only need to use one of the inputs.

Tom.... :)

Thank you all for your quick and kind grant. Actually I want to design a complete package of measurement for one home at least now. My meter should give real time data to me through app using wifi module. It should give real time data i.e voltage, current, power, frequency, energy (here EEPROM will be used for retaining consumed energy), power quality, power factor, phase angle of voltage and current, and may be any other information which is not in my mind at this time. so your suggestions are needed to improve system in terms of other necessary features or upgrading in these limited parameters. Note: This will be only one way communication right now means it will just send the data, further two way communication can be enabled to switch power source (lets say solar to wapda or reverse) or turning on and off loads, just because of that i am using wifi because it gives facility of two way also there is no any limitation of range. Cheers :)

allanhurst: If you want a true figure you have to sample LOT faster than 2x the frequency of interest..

Power people look up to the 7th harmonic - 350Hz

and you want at least 10x that for sampling.... say 3500/sec

Yes , the arduino can do that, but only to 10 bits . this allows you a resolution of about -60dB ie about 0.1% followed by with a 1024-point FFT.

Whether it can do the sums that fast I don't know . that's a lot of sums in 3mS. Or whether it has enough RAM.... just about

Depends on how accurate you want your numbers to be....

regards

Allan

Hi,

Is that in response to my previous reply? Because i did not say 2 times the highest frequency of interest, i said 2 times the highest harmonic (of interest). That means if we sample a 50Hz line and we want up to the 10th harmonic, we have to sample at least at 2*10*50=1000Hz. I was going by the Nyquist rate which i think applies here although it's been a long time since i had to do this. Since it is stated as an inequality rather than an equality, that would really mean something just over 1000Hz but 1000Hz will probably work too. So it is not that we would sample at 50*2=100Hz, we'd actually have to use 1000Hz or above (10th harmonic that is). To get up to the 20th harmonic we'd need to go to 2000Hz. Of course an anti aliasing filter would be almost mandatory.

I hope this is more clear now.

As to the actual calculations, they probably have to be done after the sampling some time if this does not have to be done in real time. If in real time, then some careful algorithms or even a different processor.