I've a problem with my energy monitor. Possibly it's due to my use of the software high pass filter.
My energy monitor is based on the openenergymonitor design but uses a PIC. It samples 4 channels simultaneously - grid voltage/grid current/PV current/generated current diverted to the immersion heater. The sampling rate is 5kHz. I've been getting better than 1% accuracy.
However I decided to try sampling at a 20kHz rate. When I do this, the measured grid voltage is too low. As I increase the sampling rate from 5kHz to 20kHz, the grid voltage decreases. It's about 7V too low at 20kHz.
I'm fairly sure the ADC is working OK at the higher sampling rate. Also there seems to be plenty of processing time between sampling interrupts. I'm wondering whether the problem may be the high pass digital filter. I'm using the software filter from the openenergymonitor design.
The filter works well at a 5kHz sampling rate. Should it be OK to use it unchanged with 20kHz sampling?
Re: Digital high pass filter problem?
The time constant is a direct function of the sampling rate, so to restore the performance you almost certainly need to adjust the coefficients. I think what you've inadvertently done is you've pushed the corner frequency up by a factor of four and the attenuation at the mains frequency is no longer negligible.
We changed from using the result of the high pass filter directly, to using a low pass filter to extract the offset and then subtract it. It uses slightly less maths, but it's capable of being initialised and a consequence of that is it can have a longer time constant and therefore less ripple.
Wikipedia has a complete explanation of both filters.
Re: Digital high pass filter problem?
Thanks. That makes sense. I will take a look at the low pass filter.
Re: Digital high pass filter problem?
Assuming you were using α = 0.996 and a sampling interval of 400 μs (which are about right for the old high pass filter in the emonTx sampling at 2.5 kHz), the corner frequency works out at 1.59 Hz. The attenuation at 50 Hz would therefore be 0.05%.
Running the same filter at 5 kHz would double the corner frequency, and the attenuation changes to 0.2%. This is what you probably had.
However, taking the frequency up to 20 kHz, the corner frequency is now 8 times the original design value at nearly 13 Hz, and the attenuation at 50 Hz is 3.12%. So you saw a difference of 2.9%, pretty close to your 7 V.
You probably want an α of about 2-16 (about 0.000015) in the low-pass filter, giving a corner frequency of 0.05 Hz. Remember to initialise it to the half-rail input so that it settles within a reasonable time.
Re: Digital high pass filter problem?
Robert,
Your comments were very interesting.
I looked at a low pass filter, but decided to change the high pass filter because it was so easy to do. I changed α to 0.999. This gave me pretty much the same time constant sampling at 20kHz as I'd had at 5kHz with α = 0.996. The change to the software filter was trivial, because .999 is close to 1023/1024, instead of 255/256 for .996. So the only change needed in the software filter was to change the 8 bit shifts to 10 bit shifts.
When I did this, the voltage when sampling at 20kHz was a sensible value. As you noted, there would have been a little attenuation at 5kHz with α = 0.996. Sure enough when I sampled at 5kHz the voltage increased a little when I changed α from 0.996 to 0.999.
I've not been checking all this very carefully so far, just recompiling to try the different scenarios, and in the meantime the mains voltage was changing a little anyway, but everything looks consistent with your comments. But maybe I'll find that changing the filter produces other problems when I test all this properly - I don't really know enough about filters.
I have a question. As you said there would have been a small amount of attenuation with the original filter and 5kHz sampling. I should be getting a similar small amount of attenuation sampling at 20kHz with α = 0.999. If I were to change the filter routine to shift 11 bits instead of 10, would this improve the measurement accuracy (without causing other problems)?
Re: Digital high pass filter problem?
"would this improve the measurement accuracy (without causing other problems)?"
Probably yes and no. Yes, it will reduce the attenuation of the 50 Hz wanted signal (or if you've got that right with calibration, reduce the errors that come from giving that bit more weight to the harmonics), and no, you probably won't get away without more problems because the settling time grows, so you need longer after starting or resetting before you can trust the readings.
That's the main point of changing to the low pass filter - it can be preloaded so it's almost settled with the first sample.
The high pass and the low pass approaches both achieve the same result in the end, but do it in very different ways. The high pass filter attempts to take out the d.c. offset directly, but as I mentioned, it can't be pre-loaded at startup so the lower the corner frequency, hence the less error you have at 50 Hz, the longer the settling time ('settling time' being measured in filter iterations, so the time it's been filtering samples - pausing as in the 'discrete sample' sketches doesn't count). Therefore at startup you need to throw away measurements until it's done enough to have settled.
The low pass filter removes all the mains frequency and harmonics to leave only the d.c. component, which you then subtract from the original signal. Therefore it gets the value of the offset and removes it. But you can guess to a first approximation the value of the offset, so starting from there, the filter is very nearly settled at switch-on. You can take advantage of that and reduce the corner frequency to a very low value, thus removing even more of the a.c. ripple, and still have the filter settle within a few hundred samples.
There are pictures of simulations in the Building Blocks article.
Re: Digital high pass filter problem?
The way I use the monitor is to start it running, then leave it. It streams data to my PC which saves the information in a database. I can then analyse the data offline. I've had the monitor running for several months without resetting it. Once I stop playing with it, I hope to leave it running for a year or more before resetting. It has a backup battery supply.
So the settle time for the high pass filter doesn't bother me in the least. My concern with the low pass filter is that it first finds the offset then uses it from then on. As in the building blocks article, my monitor supplies a buffered common mid point voltage for all the ADC measurements. This voltage should be stable, but if it does drift at all over the course of a year or more, it will cause problems. As I understand it, I wouldn't have this problem with the high pass digital filter.
Actually, I've just thought of a problem. If there's a power outage, the mcu will continue running, but the AC voltage will go. Neither the low pass nor the high pass filters will be accurate when the power returns. So I probably ought to detect the loss of AC voltage and call the settling routine when it comes back. The shorter settle time for the low pass filter would be an advantage in this situation.
Re: Digital high pass filter problem?
"My concern with the low pass filter is that it first finds the offset then uses it from then on."
Not true, and I don't think I wrote that anywhere either. What I wrote above is the offset is set at startup to what we hope is the correct value. After that it continually updates, which you can easily see if you study the sketch.
"Neither the low pass nor the high pass filters will be accurate when the power returns."
I don't understand your thinking there. Looking only at the low-pass filter, the time constant is huge and the contribution from each sample will be only one part in 65536 or thereabouts, so if the mains supply goes away, the offset will immediately start to settle to the exact midpoint. Then when the power returns, it will immediately start to follow the input again, though attenuated as designed. If you re-initialise when power returns, it's likely to be more inaccurate, not less. Whatever happens, any disturbance can never be larger than the residual ripple unless a large spike gets through.
If you're really worried, you can write a simulation in a spreadsheet and test the effects of starting at pre-determined points on the cycle.
Re: Digital high pass filter problem?
Thank you for the explanation. I will try the low pass filter.