I need to measure voltage (RMS) from incoming PDM (Pulse Density Modulation) data.
The voltage is actually 50Hz line voltage distorted sine.
For test I do in multiple steps.
I send PDM data to the low pass IIR filter so I got reconstructed waveform.
After that I accumulate squared samples and finally do the sqrt with samples for one sine wave (20ms)
This works but take too long time. I think there should be better solution but I don't know for that.
Not sure what your sample rate and bandwidth are, but the long hanging fruit on this would be to filter and decimate to reduce the amount of data.
The sampling is at 1MHz. And I wish to average 5 periods of 50Hz. So I should got result every 100ms.
What bandwidth are you looking across for the RMS measurements? If this is AC (mains) power then unless it's some exceptional condition you're chasing probably going out the 5th or 7th harmonic would be close enough? Though if you're trying to do a "standard" power line harmonic analysis I seem to recall there's some industry standards that define the details?
If going out to the 5th or 7th is true you could filter and decimate to drop the sample rate to around 1 or 2 kHz.
It's not voltage analyzer. I only capture data from other circuit and need to measure voltage to do some callibartion process.
I do have old board with just RC filter and ADC and work fine but I have issue as ADC on microcintroller does some drift with temperature/supply. So I think if I capture data digitally as is already used in system I should skip all drifting problem.
But the micro is to weak to do filtering and measuring RMS with alghoritms I have/know.
I think I made an oops from reading your original question. It's not the filter output that's at 1 MHz, that's the PDM rate? What is the rate out of the filter/downsampler that you've tried?
I'm not sure what you mean by "not a voltage analyzer" as in the original post you wrote "I need to measure voltage (RMS)"
Is it correct you just need RMS value averaged across 100 msec? What bandwidth do you need to calculate the RMS value across? Is this signal noisy (from the measurement system and/or the source?)
I try to clarify a little bit.
I have 50Hz (distorted sine) signal that are converted to delta sigma signal at 1MHz bit clock. The digital data is passed to other part of machine near 50m away. On this side the signal is low pass filtered and used in analog circuit by machine itself. Here I need to measure RMS value of original 50Hz signal. I need just RMS value I don't do harmonics analyse. Doing that with microcontroller and ADC after analog filter works, but the ADC is not stable enought (AVR microcontroller). So I came on idea to capture digital signal and measure the voltage encoded within it. For test I capture digital signal with logic analyser and do some math on PC.
The test program is like this:
*sample 0.1s of data
*feed each sample to low pass filter (single pole IIR)
*make accumulate sum of all samples multiplied by itself (x=x^2)
*divide accumumulator by number of samples
*do square root of divide result
*multiply result with 300 (the max input voltage range before digital encoding)
That's work way better than ADC coupled to analog filter.
So I need to implement that in microcontroller.
The voltage output must be at least 1% repeatable. (The absolute value is not so important)
There is operator that read that value and adjust the knob to react to changes. The automatic change is not allowed for security reasons.
So as in micro is not enought memory to store 0.1s of data I need to somehow process that in realtime (I need to do that for 1st 3 steps above). The voltage change in sistem is slow. In normal operation usualy change for less than 1V/10minutes.
Thanks for the detailed explanation. Did you look at this older post?
in addition to the ST app note referenced there, take a look at ST AN5027, it seems like a pretty detailed explanation.
The only difference is that for your system I think you can get away with a sample rate of 1 KHz or maybe even 500 Hz (I wouldn't go much below 10x the frequency of interest here).
I didn't see a discussion of microcontroller family in the ST app notes, but would lean towards M4 or M7 as I suspect once you have some DSP capability there will be other things that will be desired, and picking a part with a MAC will leave you plenty of CPU cycles for other things.
I take 1MHz sampling rate as the modulator running in that frequency, and I can got the clock line too so I do synchronous sampling. (the data and clock are transmitted from one end to the other end in twisted pair like RS485 line. So I can use SPI interface to capture data and got x8 less overhead.
I still wonder if it's possible to measure RMS without so much of conversion. PDM -> Reconstructed signal -> RMS measurement. I wonder is it's possible to skip "analog" part.
You can't manipulate PDM (or PWM) data mathematically (AFAIK...maybe someone else knows). In the end it's nothing more than a "value" of some signal and it's the value you need, not the representation of it. There are of course times where a different representation can make things easier - like using complex numbers or switching in to a different domain (frequency, wavelets, whatever).
But in the end it's all numerical values that are manipulated, and you need to get the numeric value that represents the signal. (as an aside this is why DSD for audio is such a crock, the number is either the value of the signal or it isn't; there's no magic involved)
The analog filter in front of the ADC is essentially doing that same operation(s) as you would do digitally. Given your 1% accuracy requirement, you could use a slightly better ADC and analog front end and achieve the exact same results. That being said since the incoming data is digital you can eliminate the pesky analog stuff and it will be lower cost and more reliable/accurate - assuming of course the sensor is accurate.
Really then the question is minimizing the calculations to get to the answer you want. Getting the sample rate down is the simplest and most direct.
If you know more about your signal you could not do a brute force RMS calc but instead just find the peaks, and knowing that the waveform is a sine wave calculate the RMS from the peak. But you mention distorted waveform so maybe that wouldn't be accurate enough.
As you said I want to eliminate analog stuff as I can capture data digitally. As I say I can receive data synchronous and that part work's well. I was tried to do lookup table simulation on pc. So I have 16 bit lookup table and 8 bit samples (try to do lowpass on 1MHz data and downsample 8 times) Seems to works but the table itself is 1Mbyte. So it's not best solution for micro. So now find a way to squeze that on 256kb micro.
..and data is "distorted sine" ant that "distortion" is used in analog part for machine itself. Mean that in one condition the sine is perfect, in other condition the sine is distorted (seems like AM modulated with 200Hz at aprox 12%) But I need "just" RMS of signal. I just hoped that someone else have some simplification in mind. I tryed with Goertzl, but got just amplitude of 50Hz and not Rms.
Not to drag this out, but why are you using a 16 bit table? 8 bits drops the rate to to a very manageable value and you could take it down to 500 Hz or so and do the RMS calc.
And if 12% of the energy is not in the fundamental and you want 1% accuracy then I don't see how you can beat a standard RMS calc on the 500 Hz data in terms of loading (plus the filter/decimator load, and with such a huge decimation factor there's gotta be some clever compute savings in that)
Though in all honesty when I read the table idea I didn't convince myself that was totally accurate - no doubt some gap in my understanding and something to come back to on a rainy day to figure out what I missed. But if you simulated it and got the right results that's good.
The table does low pass filtering and decimation! and for precission required I need at least 14 bits if I'm correct.
Y(14bit)= lookup( Y(14bit):X(8bit) )
So I take 8 digital samples and put it into byte.
then take old Y and append this 8 samples as LSB.
take value from lookup. So I do Sampling at 1MHz and lowpass filtering at that + decimation (/8) so I got 125KHz PCM signal. Feeding that to RMS calculator give me perfect result. But it's to hungry on memory.
Ah, got it. Barring some clever way to fold that table due to some symmetries of some sort (just a WAG that there might be some) really then it's a tradeoff between memory and CPU, i.e. using less bits for the lookup <--> higher rate out.
As nobody else has chimed in (there's a lot of people on this list way smarter than me on this stuff) then I'm going to go with answer no, there's no trick (beyond lowering the sample rate to 500 Hz) that will save you here.