Author Topic: High resolution analog to digital conversion of current spikes  (Read 10692 times)

0 Members and 1 Guest are viewing this topic.

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: bg
    • Microlab.info
Hi,

I'm designing a power meter. It'll read voltage and current through and calculate power and energy. I want to be able to measure with relatively high resolution. My goal is 15-16 bits stable reading (noise free).
The tricky part is when measuring microcontrollers that go to low power mode, then wake for short period of time. These wakeups produce short bursts of 10 to 100 times (or even more) higher current than the current in sleep mode. In order to catch these current bursts I have to sample at high rate. Let's say that there is 1 ms burst every 1 second and the current rises 100 times. If I sample at rate 10 sps I might miss it and that's 1% error. RMS (in ideal situation) is 1.099 (or ~1.1mA). So if I miss the burst it's 1% error which defeats the high resolution. So these are the 3 ways that came up with for measuring current bursts:
1. True RMS converter - These are not really good for DC (or <0-1Hz)
2. High speed, high resolution ADC - that should be pretty expensive, right?
3. Integrator opamp circuit - the problem comes from the time of between adc conversion and resetting the integrator. That will probably a lot of time to make a test setup and I'm not sure if I can get any good results from it.

I didn't say requirements for bandwidth, because I'm not sure what's possible. Currrent bursts produce a lot of high frequency content. 100kHz sounds like a nice round digit to start with. That's the bandwidth of the analog signal. It doesn't mean that I have to sample that with 1 MHz to get accurate power reading (unless I find ADC that can sample 1MHz and produce 16 noise free bits for reasonable price).

Currently I'm playing with MCP3901. It's pretty noisy. I barely get 15 noise free bits (RMS noise) at (I think) 3.9ksps .That's with analog circuit powered by separate supply - battery + 78L05. I haven't tried boost option yet. 3.9ksps is not enough to register small bursts. Also I get a lot of p-p noise that occurs pretty rarely, but very large value - it reaches 33000 adc counts at 24 bits.

So what should I use to measure fast changes in current when doing energy measurements?
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21606
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: High resolution analog to digital conversion of current spikes
« Reply #1 on: April 22, 2015, 08:05:31 pm »
If your analog channel is filtered to meet the Nyquist sampling theorem, then the total charge (amp-seconds) or flux (volt-seconds) will be conserved.  Only the average matters, so you can sample extremely slowly, given that your filters are sized accordingly and do not contribute undue noise and offset.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline f5r5e5d

  • Frequent Contributor
  • **
  • Posts: 349
Re: High resolution analog to digital conversion of current spikes
« Reply #2 on: April 23, 2015, 03:59:25 am »
the "goal" may be beyond "beginners forum" - as a decades experienced designer of industrial instrumentation I wouldn't try to do "15-16 bit stable (noise free)...short bursts of 10 to 100 times...good for DC (or <0-1Hz)" at home, certainly not without 4-5 figure US$ lab equipment and man-year engineer and tech budget

you should perhaps review what the end goal is - an energy budget for a portable or upower device doesn't need to be measured to much greater accuracy than the battery leakage rate or energy source uncertainty

the integrator approach could be good - use 2 time interleaved if you really can't miss any event - but unless you allow Volts of burden V for the current sensing then noise, offset and drift will be issues - some can be dealt with textbook techniques from integrating /slope converter meters

if do you allow Volts of burden V for the I measurement then you have tolerance issue - 0.1% starts getting pricey, then there's the cascode or regulator circuit adding to your error budget too...
« Last Edit: April 23, 2015, 04:08:48 am by f5r5e5d »
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: bg
    • Microlab.info
Re: High resolution analog to digital conversion of current spikes
« Reply #3 on: April 23, 2015, 08:02:44 am »
@Tim I think that "filtering" will remove the high frequency content instead of account for it. Filtering is the first thing that popped in my head, but filtering is not like RMS to DC conversion. Could you give an example of what filtering you are talking about. Maybe I can make quick tests.

@f5r5e5d I understand my goal is set a bit too high, but it's in this forum because I'm trying to learn about RMS measurements, integrators and the "traps" of high resolution. I spent a lot of time reading datasheets for all kind of errors - temperature drift, long term stability, noise and other stuff that can affect the total error. I realise that 16 bits and 0.1% is not that easily achievable. But I want to try to make the best setup I can and then I will try to find a lab where I can test it and specify it.

Quote
an energy budget for a portable or upower device doesn't need to be measured to much greater accuracy
Of course I don't aim for 1 lsb accuracy. Well, I do aim for <1% accuracy - somewhere between 0.5 and 0.1%. The resolution won't be covered by accuracy, but that doesn't mean that it won't be helpful. If you have the resolution you can see changes in voltage/current/power. For example slight rises of power usage while the battery is dropping with SMPS. Or you can monitor battery voltage getting up as the device goes into standby mode.

Burden V will be minimised, but as long as you know about it you can take it out of the equation. It's not that important as long as it's not too big to cause problems. Almost always you would test either power source or load and never both at the same time. So if you measure voltage on the right side of the shunt resistor and cable drops (4 wire measurement) you don't care about burden voltage.

Noise is a problem, but it's not something that will ruin the 0.1% attempt. Offset can be nulled. Drift  (temperature, time) is the big deal. I can just start with datasheet values and try to measure it and monitor it.  Also you need at least 5-10 times better equipment to guarantee 0.1%.  0.01-0.02% is not that expensive. Current will be a problem because most precision multimeters have 10 times less accuracy on current than on voltage. But a good current sense resistor with the proper technique can be a nice transfer standard I can use for measuring current as long as I find a lab to measure it.

I want to know more about the integrator circuit - practical schematic, how you reset it, what capacitor to use to minimise the error, what time constant to choose, how to calculate the error from time needed for resetting the integrator, how do I synchronize ADC with it.
« Last Edit: April 23, 2015, 08:55:44 am by npelov »
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21606
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: High resolution analog to digital conversion of current spikes
« Reply #4 on: April 23, 2015, 11:31:56 am »
@Tim I think that "filtering" will remove the high frequency content instead of account for it. Filtering is the first thing that popped in my head, but filtering is not like RMS to DC conversion. Could you give an example of what filtering you are talking about. Maybe I can make quick tests.

If you don't know the basic math, you're going to have a hard time...

If you do, you should be able to follow along:

The question is, do you have to sample at a high frequency in order to resolve high-frequency content?  If you want the wave shape, yes, absolutely, of course.  If all you want is the average, then the question becomes,

Is the charge, over a given sample period, invariant under bandwidth limiting, given that BW is less than the sample rate?

Charge is defined as the time integral of current.  So if we integrate current for every sample period, sample and reset the integrator, and repeat, we should get the correct answer.

If we integrate the current signal, convolved with the impulse response of the filter, and determine if that result is different or not, then we will have the answer.

Convolution has the property:
Int (f(x) Conv g(x)) dx = (Int f(x) dx) * (Int g(x) dx)
so we can fully factor out the impulse response g(x).  In particular, so long as Int g(x) dx = 1 (equivalent to saying the low-pass filter has unity gain at DC), the result is indeed invariant over filtering.

More practically, if the integrals are over a fixed range, say 0 to Ts, then as long as g(t) remains bounded near 0, for t >= Ts (meaning, the impulse response of the filter is asymptotic within a certain time frame, which for a typical RLC filter, will be within a few time constants), then the same result will also be true.

We also see that, if the filter does not reach unity at Ts, then the gain factor will be different, and we may be missing information.  This is effectively equivalent to failing the Nyquist signal reconstruction theorem's conditions.

Tim
« Last Edit: April 23, 2015, 11:34:07 am by T3sl4co1l »
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: bg
    • Microlab.info
Re: High resolution analog to digital conversion of current spikes
« Reply #5 on: April 23, 2015, 12:47:04 pm »
First I have to point that I already mentioned that fast sampling is one way to measure the energy over a period of time. That's why I was looking at integrator circuit.

I have to do have troubles with math. I feel like your explanation is like a punishment. Are you a math teacher? ... But I do understand the meaning of integral to some point. I read somewhere that a filter acts as integrator in frequencies close to DC. A perfect integrator would mean that it calculates the area of the high frequency pulse and converts it to DC offset which adds to the DC offset of input signal. A perfect low pass filter would cut the high frequency and leaves only the DC offset of the input signal. Feel free to correct me when I'm wrong.

Would you make up for my missing time in school and help me solve the correct values.

Well let's take for example this circuit:


How can I calculate how much high frequency information I'm loosing for certain values of R2 and C? And also how accurate the high frequency is integrated? Because rising the time constant is loss of high frequency content, lowering it - the integral will not add full area of the high frequency pulse and we'll have an error of measuring high frequency content.

A high frequency pulse of short wake up of a MCU is close to square wave. As in school I'll try hard to avoid working with integrals (I don't say it's a good thing) and I can find first n harmonics and calculate transfer function of the filter with:
Vout = (-Vin*(R2/R1))/(1+2*Pi*f*C*R2)
Then calculate the RMS of the harmonics - Vout/sqrt(2), sum all results and compare them to the RMS of the input signal - which is Vinsleep+ Vinwaken/sqrt(duty) to find the error of the integral.

Am I at least close to the truth?

P.S. Just got the idea to split the signal to DC with very low frequency low pass filter and AC with high pass filter. Then pass DC to the ADC and the AC through RMS chip-> ADC. If the RMS to DC converter has accuracy 0.1% and the duty cycle is 10 then it'll add 0.01% to the measurement error. But for duty cycle <10 I'll still need math to find the perfect corner frequency for the filters.
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21606
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: High resolution analog to digital conversion of current spikes
« Reply #6 on: April 23, 2015, 03:48:50 pm »
Real power is defined as:
average(v(t) * i(t))

If you are only concerned with DC power, then the average distributes, so that you can use:
average(v(t)) * average(i(t))
which is likely much easier to calculate.

Ideally, the average is for all time (t = -infty to +infty), but a practical meter needs to have some response time; so maybe instead of an average over all time, an effective average over a known period of time would be useful.  Which is another way to say: a lowpass filter.

Afraid I don't have any more succinct way to express the truths at work here.  We can work through them and show, in a wave-of-the-hands, visualizable way, that they are probably correct.  Until then, I realize my "proof" is but an opinion to you, and carries little more weight than someone just telling you what to do.  The fact is, if you want to design instruments, these are things you'll need to know, or take for granted.

As for the filter, when you're talking pulses, you might be better off working with the time domain response.  A sufficiently short pulse effectively delivers a fixed charge to the capacitor, so that before the pulse, the integrator was at some voltage, and an arbitrary time after the pulse, it's at V + dV.  So all you need to do is sample fast enough to find the "start" and "end" values resulting from that pulse.  Which, for an ideal integrator, is any sample rate.

In the frequency domain, that's equivalent to sampling at a frequency such that the attenuation of the filter is way down, like -40dB or whatever (to get a corresponding error level, i.e., an error signal that's -40dB is 1% of the signal).

You can use a steeper filter (not just an RC integrator) to achieve quicker attenuation in the stopband, without compromising the integration behavior.  Ultimately, the sampling theorem holds, so that an ideally band-limited signal can be sampled at Fs = 2*BW, and reconstructed perfectly (or, in this case, averaged perfectly).  Or for some finite attenuation above Fs/2 (say -40 or -60dB), then a worst-case expected error (due to aliasing) corresponding to that amount of attenuation.

If you are concerned with more than DC power, then you must do the full AC calculation as well, however DC systems generally do not transmit or consume AC (it's usually reflected due to stray reactance, or trapped within local bypass caps and filters), so that measuring the AC component will always be very near zero.  Furthermore, definitely do not make the mistake of measuring Vrms * Irms, which is the apparent power in VAs!

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: bg
    • Microlab.info
Re: High resolution analog to digital conversion of current spikes
« Reply #7 on: April 23, 2015, 04:47:47 pm »
Well you can see why I posted this in beginners' forum... And yes, I didn't quite understand your proof. Did you prove that if the sampling speed is far (higher) enough  from the edge of any low pass filter then you have good enough integrator?

So, let me see if I understood you correctly. If I make the filter so that at it's response at 3.9kHz is -60dB (or Vout = Vin/1000) and I sample at 3.9kHz I'll get an integration error of 0.1% (not accounting for all other errors for simplification).

And to make it better I can put an inductance in series with R1.

About AC power - for now I want to take it out of the equation. But now that you said that Vrms*Irms is apparent power I got curious. Is it enough to measure Vrms and Irms and phase between V and I to calculate the power in Watts - i.e. active power - Vrms*Irms*cos(?). Maybe not if you don't have perfect sine wave of both voltage and current.
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21606
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: High resolution analog to digital conversion of current spikes
« Reply #8 on: April 23, 2015, 10:18:54 pm »
Well you can see why I posted this in beginners' forum... And yes, I didn't quite understand your proof. Did you prove that if the sampling speed is far (higher) enough  from the edge of any low pass filter then you have good enough integrator?

You can do some "toy" models with concepts like infinity, or fixed periods, and actually implement those (e.g., using a combination of analog switches to make an integrator that samples hunks of time), but you can pull the analysis a level higher and look at what that sampling window is doing: it's a sampling process, so it exhibits a predictable frequency response (the sinc(f) function), and so on.

And yes: you can generalize that to any type of filter, whether an ideal brick wall (as in the Nyquist theorem; which, being a frequency-sampled process -- has a sinc(t) time response!), or a regular RC, or a higher-order one which offers a sort of compromise between those extremes.

Furthermore, you can establish an upper bound (worst case) on what the cost of violating the Nyquist sampling theorem is: if your filter has X attenuation at frequencies that will be aliased (i.e., > Fs/2), then you certainly won't get more than X error in the reading.  You have no clue what frequency that signal might appear as -- if it's right at N * Fs, it will alias near DC, and your DC measurement will be off.  It might alias near Fs/2, where it will effectively be filtered by your digital signal processing.  It's *not* the predicted, average case -- because the input signal is unknown.  If the source signal contains no frequencies above Fs/2, then you can reconstruct it perfectly, with or without the filter (your work is already done, the filter isn't doing anything).  If it contains frequencies very high above Fc (cutoff), they will be attenuated so strongly that they might as well not exist (about -100dB for a 16 bit ADC).  In the inbetween range (from Fs/2 to -100dB attenuation), you may get some error, but the error will never be more than a known amount.

Or to put it in terms of pulsed inputs, you might simply happen to sample during one of those blips on the input -- but if the filter is smoothing over the bumps really well, your measurement won't see the bump, just a very gradual swell.

And you can see the value in this filter, because if you have unlimited bandwidth at the input, you *must* sample as fast as the input could change, otherwise you might miss one of those blips.  Moreover,  which is ridiculous.  You'd be building an oscilloscope for a purpose vastly inferior to the hardware!

Quote
So, let me see if I understood you correctly. If I make the filter so that at it's response at 3.9kHz is -60dB (or Vout = Vin/1000) and I sample at 3.9kHz I'll get an integration error of 0.1% (not accounting for all other errors for simplification).

And to make it better I can put an inductance in series with R1.

Yes, worst case error.

Don't go putting in inductors willy-nilly, though.  For this frequency range, I would suggest a 3rd or 5th order active (op-amp) filter.  There are calculators on the web, e.g.
http://sim.okawa-denshi.jp/en/Sallenkey3Lowkeisan.htm

Quote
About AC power - for now I want to take it out of the equation. But now that you said that Vrms*Irms is apparent power I got curious. Is it enough to measure Vrms and Irms and phase between V and I to calculate the power in Watts - i.e. active power - Vrms*Irms*cos(?). Maybe not if you don't have perfect sine wave of both voltage and current.

Correct!  RMS works for *any* arbitrary waveform, no matter how many frequency components it might contain.  There waveform need not be periodic (in which case, there is no good definition of phase, either!).

In the single frequency case, the apparent power is the hypotenuse between real and imaginary (reactive) power phasors, and the angle is the phase shift.

This remains true for any number of frequency *components* in the signal, but you must treat each frequency individually, and add them up with the RMS method each time to get the total.  This is Parseval's theorem.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: bg
    • Microlab.info
Re: High resolution analog to digital conversion of current spikes
« Reply #9 on: April 24, 2015, 08:24:58 am »
Well thanks for the comprehensive explanation. I really appreciate it. I have a lot of misses in signal sampling. I'm watching "The signal path" and I have hard time understanding some of the things he says. I'll read more about Nyquist theorem. If you know any good source on the internet I'll be glad if you share it.

A bit of brainstorming to see if I understood. So in theory it's enough to have 20 Msps to make 10 MHz oscilloscope ... if you have an ideal brick low pass filter at 10 MHz. So that's why they usually sample at 10 times higher frequency to avoid alias of the attenuated signals over the scope bandwidth. But in if that's true and if you have 10MHz ideal filter and sample with 20Msps then you'll have to do some post-processing to reconstruct the signal. The post processing is actually a low pass filter (let me guess - at 10 MHz?) - the sinc function or sin(x)/x. But let's say that you have 20 MHz sampling of 10 MHz sine signal exactly and you happen to sample at zero crossing - you'll get straight line at 0V. So you need >20MHz sampling. Let's say 20.001MHz (depending on how good your post processing is)

And  about AC power. I realized what you said - "there is no clearly defined phase if there are multiple frequencies". So if you want to measure power of unknown signal with multiple (unknown+changing) frequency components how do you do it? If you have known stable frequencies you can filter them and measure phase. If not - what? Look at frequency domain to get the frequencies and then filter them digitally. It sounds too complex. Maybe that's why most AC power measuring instruments do not promise great accuracy. Slightly distorted waveform gives you enough error to start with.
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21606
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: High resolution analog to digital conversion of current spikes
« Reply #10 on: April 24, 2015, 12:48:57 pm »
A bit of brainstorming to see if I understood. So in theory it's enough to have 20 Msps to make 10 MHz oscilloscope ... if you have an ideal brick low pass filter at 10 MHz. So that's why they usually sample at 10 times higher frequency to avoid alias of the attenuated signals over the scope bandwidth. But in if that's true and if you have 10MHz ideal filter and sample with 20Msps then you'll have to do some post-processing to reconstruct the signal. The post processing is actually a low pass filter (let me guess - at 10 MHz?) - the sinc function or sin(x)/x.

Yes, although an oscilloscope isn't a good example -- if the cutoff were chosen as 20kHz, an audio converter would be a better example.  Phase shift doesn't matter (usually) to audio, so the filters can be very steep.  Whereas, an oscilloscope requires a flat phase response (you don't want sinc style ringing on every damn pulse!), and because the oscilloscope is generally triggered (phase locked to the signal), it can be very useful to have extended bandwidth.  When this is controlled in the right way (the time delay relative to the trigger event is incremented for every pass over the waveform, and the samples reassembled in the correct time sequence relative to the trigger event), you achieve equivalent time sampling (ETS), which is effectively harnessing aliasing to down-convert high frequency content to the pass band.

Note that ETS breaks the sampling theorem, because the sampled signal will no longer be reconstructed perfectly from the samples.  For an oscilloscope, that's fine, because you can only ever use a very slow display anyway (i.e., a graphics display read by human eyes with a response time well over 10ms), so long as you show it as being the correct equivalent time/div.

Quote
But let's say that you have 20 MHz sampling of 10 MHz sine signal exactly and you happen to sample at zero crossing - you'll get straight line at 0V. So you need >20MHz sampling. Let's say 20.001MHz (depending on how good your post processing is)

Indeed.  So between that, and the added expense / complexity of having to realize a very tight filter, a practical (Nyquist theorem-compliant) DAQ / playback / etc. system will have a sample rate at least 10% higher than 2*BW (e.g., 20kHz audio sampled at 44.2kHz), up to perhaps 4x ("real time" oscilloscopes which do sample in real time, without taking advantage of EQ triggering).  The higher limits being for more gradual filters (usually a Bessel (maximally flat phase) type), which have better pulse response but usually need 5th or 7th order to reach suitable attenuation at 2*Fc.

Quote
And  about AC power. I realized what you said - "there is no clearly defined phase if there are multiple frequencies". So if you want to measure power of unknown signal with multiple (unknown+changing) frequency components how do you do it? If you have known stable frequencies you can filter them and measure phase. If not - what? Look at frequency domain to get the frequencies and then filter them digitally. It sounds too complex. Maybe that's why most AC power measuring instruments do not promise great accuracy. Slightly distorted waveform gives you enough error to start with.

If you're doing a complete frequency acquisition sort of thing (like a spectrum analyzer), you could do it that way -- take the two spectra (hopefully, they are phase matched -- and if they aren't, you're in the frequency domain already, so a mere correction table could simply be multiplied through!), multiply them element-wise, and there are your answers for each frequency.  Sum them up (RMS wise) and that's your total power figure.

If you're doing sampling but not FFTs, you're much better off doing the element-wise multiplication p(t) = v(t) * i(t) and averaging that (average(p(t)) = real power flow).

Note that multiplication in the time domain is convolution in the frequency domain, and vice versa.  (When you're working with data sets, convolution is basically sliding one array past the other, and taking the total sum of products.  The position variable is the index for the resulting 2N-1 long output.)  So you could convolve the two spectra, average, and have the same result.  It's convenient that the summation in the average can also be carried out as an RMS over element-wise multiplication, according to the theorems I used earlier, so in either domain, you don't have to use convolution for this exact operation.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: bg
    • Microlab.info
Re: High resolution analog to digital conversion of current spikes
« Reply #11 on: April 24, 2015, 05:17:45 pm »
Now that I saw it in real world I kinda realize what you meant by "average". My setup:
3d order filter from the link Tim gave me with Butterworth filter" setting at 100Hz:
R1 = 11k?
R2 = 110k?
R3 = 33k?
C1 = 0.15uF
C2 = 0.068uF
C3 = 0.01uF

The ideally thinking calculator promised about -90db at 4kHz.
I used MCP601. The signal ground is offset by about 500-600 mV from negative rail by a 1N4148 + 1k resistor (that's what the ADC wants).
The network analyser said about -75db at 4 kHz. It's a bit down in the noise limits of the network analyser, but it looks to be close.
 I used 500 mV offset and 100 mV amplitude in wavegen. That's 400mV DC + 200 mV pulses (square wave). Since I can calculate average of square wave from duty cycle it'll be easy to check the measurement.

Apart from the wavegen offset it looks pretty close. It's just a quick experiment. I didn't bother too much with calculating the error accurately. Also the "Analog Discovery" does not allow better calculation. It's just did quick proof of concept.
The idea is to sample with 3.9 ksps. If I aim for 0.1% total error I might want to minimise this error by bringing the corner frequency down a bit and lower the attenuation to -80dB or more. Also it'll make the display readings better if it shows more stable value (in case of AC components).
« Last Edit: April 24, 2015, 05:22:05 pm by npelov »
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1052
  • Country: fi
Re: High resolution analog to digital conversion of current spikes
« Reply #12 on: April 24, 2015, 08:36:14 pm »
You could also add some (IIR) digital filtering afterwards in software to lower the bandwidth further still, might be easier to tweak according to measurement requirements.

Regards,
Janne
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: bg
    • Microlab.info
Re: High resolution analog to digital conversion of current spikes
« Reply #13 on: April 24, 2015, 10:40:24 pm »
I never tried to implement anything more complex than averaging. Now that you mention that I got curious and googled it. I had hard time understanding any of the IIR/FIR methods. Too much math for me.
I got the Tim's description of "convolution". For what I read on the internet FIR filter is convolution of a special data (pulse response...) with the signal data. The pulse response is (guessing) when you pass a pulse through the filter and you record the output of it.  But what technique is used to actually create this data - I'm not sure. I tried to read microchip's app note about IIR and FIR filters. I didn't get that much of it. It looks like it's made to be used and not to explain the theory in details. I can probably try use the code as is, but I don't like to use things I don't understand.
About IIR - I didn't find explanation that's in a form I can understand. I'll just keep searching. Shouldn't be that hard.
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21606
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: High resolution analog to digital conversion of current spikes
« Reply #14 on: April 24, 2015, 11:12:01 pm »
IIR is closer to a digital equivalent of an RLC circuit; you can directly transform an RLC network into a system of IIR coefficients (nasty polynomial stuff) and from that, calculate the coefficients needed for the feedback chain.

Structurally, IIR is the same as FIR, except: instead of the signal coming in, going through a series of MAC blocks (which perform the convolution), and going straight out; the signal is fed back to the beginning, so that rather than the output being only in terms of the input, it's in terms of input AND past output samples, weighted accordingly.

So you might guess, quite correctly, that the feedback coefficients are quite sensitive (in terms of stability, gain and frequency response), and non-obvious to calculate (they require polynomial solutions, which are relatively difficult to compute or represent accurately).

A sliding average is simply a FIR filter with equal taps; or you can use an accumulator and record the deltas (+1 at the start, -1 at the end), which makes it very easy to realize in software, using a circular buffer and addition only.

It takes quite many taps to realize a FIR filter with a desired frequency response, if that response has a long time constant (in terms of the number of samples): a very low cutoff frequency, or a very narrow bandpass or notch.  For these cases, an IIR is better.

For deep low-pass purposes, it may be worthwhile to combine filtering and decimation stages, to reduce the number of samples in the final filter, without sacrificing any of the actual input data.

The data in a FIR filter is simply the impulse response, so they can easily be synthesized by generating the impulse response of the desired filter (e.g., a bandpass has a decaying sinusoid "ringing" response), sampling that, and perhaps applying some suitable corrections (since the sampling process naturally perturbs the sampled response, just as it perturbs real signals), the most common being a windowing function (which acts to smooth the rigid rectangular window of samples into a more gradual response with more desirable frequency or step response).

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: bg
    • Microlab.info
Re: High resolution analog to digital conversion of current spikes
« Reply #15 on: April 25, 2015, 10:49:47 am »
So IIR requires a lot of nasty mathematics. With risk of looking stupid ... isn't it possible to calculate these coefficients from a signal and it's filtered waveform by reversing the calculations in an already known algorithm. Like you would find the gain of an amplifier by processing an amplified signal and dividing it to the input signal, then maybe get the average gain value. Or varying the parameters of IIR (like changing the cut off frequency) would change the whole algorithm.

Quote
A sliding average is simply a FIR filter with equal taps; or you can use an accumulator and record the deltas (+1 at the start, -1 at the end), which makes it very easy to realize in software, using a circular buffer and addition only.
I didn't understand that.  A FIR filter with equal taps means that all the values in the sliding array are the same - rectangular window function. But you multiply the taps to the input signal and then sum it. How do you do that with addition only. When you say sliding average here is what pops in my head: a buffer (circular) with initially zeros in it. Data comes into the start of the buffer is summed and divided to number of samples. In case of MCU you get power of 2 buffer and use shift instead of divide. I like number 256 because then I can just ignore the least significant Byte. So I didn't get the "detltas" part and +1 at the start, -1 at the end.

Quote
The data in a FIR filter is simply the impulse response
What is the shape of the impulse? Square or sine?

Quote
For deep low-pass purposes, it may be worthwhile to combine filtering and decimation stages, to reduce the number of samples in the final filter, without sacrificing any of the actual input data.
I didn't get that too.
Edit: I think I got that now. Scaling the taps so you get the correct value as you want to show it. For example if I have 10 bit ADC and 5V input range I'd calculate value like this: X*5/1024 (without the optimisation for clarity). Instead I can multiply all the taps by 5/1024 to reduce the number of operations.

Quote
The data in a FIR filter is simply the impulse response, so they can easily be synthesized by generating the impulse response of the desired filter (e.g., a bandpass has a decaying sinusoid "ringing" response),  sampling that, and perhaps applying some suitable corrections
"generating the impulse response" - like passing very short impulse through the filter, sampling the output and using the samples as our window function?
"suitable corrections" - like for example multiplying the taps by Hanning function?

So we sample, correct, normalize and then apply perform the convolution on the data from the ADC.
« Last Edit: April 25, 2015, 01:10:54 pm by npelov »
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21606
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: High resolution analog to digital conversion of current spikes
« Reply #16 on: April 25, 2015, 01:08:25 pm »
So IIR requires a lot of nasty mathematics. With risk of looking stupid ... isn't it possible to calculate these coefficients from a signal and it's filtered waveform by reversing the calculations in an already known algorithm. Like you would find the gain of an amplifier by processing an amplified signal and dividing it to the input signal, then maybe get the average gain value. Or varying the parameters of IIR (like changing the cut off frequency) would change the whole algorithm.

The algorithm doesn't change, but the algorithm required to calculate them is, as you say, mathy.  Better to have a program generate the coefficients for you.  It may be a problem, but it's a solved problem, at least!

Changing the cutoff frequency or gain, to a certain extent, might be as simple as multiplying all the coefficients by a proportion.   But, I don't remember offhand if that is the case,

The first order IIR is the easiest to analyze: it's simply an RC filter, and is guaranteed stable (bounded) as long as the coefficient is negative.  The smaller the coefficient is, the slower the accumulation decays, and the larger it grows due to having the input summed into it.  Thus, the coefficient is proportional to the cutoff frequency, and inversely proportional to gain.  (You usually have a final gain term to compensate for this, or choose a "lucky" gain like 256, as you say elsewhere.)

A single tap, you can think of as a maximum 90 degree phase shift for any frequency the filter can handle.  So the feedback won't go unstable (180 degrees).

For any higher order, there is real delay (up to or beyond 180 degrees, at whichever frequencies) in the feedback loop, and the coefficients are very particular with regards to stability.

The usual design process is to follow a standard filter calculator like http://www-users.cs.york.ac.uk/~fisher/mkfilter/ but there are ways to calculate an arbitrary filter response as well.  (The FIR response is easier to measure, so is probably more appropriate where arbitrary frequency response is desired, even if it takes a pretty generous number of taps to achieve.)

Quote
I didn't understand that.  A FIR filter with equal taps means that all the values in the sliding array are the same - rectangular window function. But you multiply the taps to the input signal and then sum it. How do you do that with addition only. When you say sliding average here is what pops in my head: a buffer (circular) with initially zeros in it. Data comes into the start of the buffer is summed and divided to number of samples. In case of MCU you get power of 2 buffer and use shift instead of divide. I like number 256 because then I can just ignore the least significant Byte. So I didn't get the "detltas" part and +1 at the start, -1 at the end.

This is what's lucky about the rectangular function: every output sample consists of equally weighted input samples n-1 to n-N (i.e., N previous inputs).  Instead of performing N additions (the multiplications are for free if our filter is bit-aligned as you suggest), you take the previous output (we're cheating slightly by technically using one IIR feedback tap!), add input n-1 and subtract n-N.

The 0th order feedback term looks like an integrator, so instead of a rectangular window, you need the derivative, which is a delta at the start and a negative delta at the end.  Which, of course, is simply adding the next sample and subtracting the last one out of the chain.

(Delta is simply the smallest pulse possible; in DSP, it's a single sample having a value of 1, the rest zero.  delta(t) in real analysis is an imaginary function such that delta(0) is infinite, zero elsewhere, and Integral(delta(t) dt) = 1, so the pulse is infinitely tall, thin, but has a fixed area.  The impulse response is the output when the input is a delta.)

Another convenient FIR is 1-3-3-1, which has reasonable frequency response, enough to "take the edge off", and can also be performed with additions and/or shifts.  Notice it looks vaguely like a window function.

I don't think there are really any higher order windows with convenient numbers.  Perhaps more results drawn from Pascal's triangle would be promising, but pretty quickly, even though the coefficients are integers, using multiplication starts to look so much easier.

Quote
"generating the impulse response" - like passing very short impulse through the filter, sampling the output and using the samples as our window function?
"suitable corrections" - like for example multiplying the taps by Hanning function?

So we sample, correct, normalize and then apply perform the convolution on the data from the ADC.

Yes!

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1052
  • Country: fi
Re: High resolution analog to digital conversion of current spikes
« Reply #17 on: April 25, 2015, 01:22:45 pm »
IIR is not very much more complicated than FIR regarding to the filtering itself.

For example, second order direct form I filter is calculated by y = b0*x[n]+b1*x[n-1]+b2*x[n-2]-a1*y[n-1]-a2*y[n-2]. x[n] is current input sample, x[n-1] is previous input sample, and x[n-2] is the input sample before that. Same logic goes to y[..] notation. Bigger question is how to get the filtering coefficients an and bn. This is best done by using some software, like Matlab or Octave. Or if you want an algebraic exercise, then the way it is done is called bilinear transformation, starting with analog filter prototype. But for quick example, we can calculate 2nd order Butterworth 50 Hz filter using 3.9 kHz sampling frequency using Matlab as follows:

Code: [Select]
ยป [b,a] = butter(2, 50*2/3900)

b =

   0.00153406027143   0.00306812054286   0.00153406027143


a =

   1.00000000000000  -1.88619313823365   0.89232937931937

So we now know that coefficients are:

a0 = 1 (implicitily assumed in above calculation)
a1 = -1.88619313823365
a2 = 0.89232937931937   
b0 = 0.00153406027143
b1 = 0.00306812054286
b2 = 0.00153406027143

You can (I suggest you really do, you will understand this IIR stuff much better then!) try these out using a spreadsheet. Start with all previous y's and x's as 0 and start inputting 1's as x, this gives so called step response. If all is correct, output value should settle to 1 eventually after enough iterations.

If you are satisfied by standard responses, like Butterworth or Chebyshev, then they usually contain a function to calculate the coefficients by just specifying the cutoff frequency and filter order. I can give you a pro tip and suggest that you don't try to implement anything bigger than 2nd order directly. Reason for that is that IIR is very sensitive to numerical round-off errors and could be unstable. Two second order sections in cascade is much more robust in that respect than 4th order direct form filter.

There is one practical issue still to be noticed. As these coefficients are decimal numbers, you usually must convert them to scaled integers and that is usually the hard part determining how many bits you need. It takes some trial and error to do that. Of course if you can do floating point calculations fast enough, then go ahead and use them, you'll avoid many headaches regarding the scaling input and output values, but that doesn't solve all the problems with IIR as they still have finite precision :)

For average filter by just summing Tim means that all impulse response values are implicitly assumed to be ones, thus just summing the input values is enough. FIR filtering seems a bit overkill for your purpose and computationally too intensive. The main reason why people use FIR is that because it is possible to determine the coefficients so that all frequencies are delayed by the filter by equal amount of time, i.e. it has linear phase, and pulse shape is not distorted. If one is just interested about magnitude response, then IIR is perfectly fine.

Regards,
Janne
« Last Edit: April 25, 2015, 01:41:32 pm by jahonen »
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: bg
    • Microlab.info
Re: High resolution analog to digital conversion of current spikes
« Reply #18 on: April 25, 2015, 01:45:24 pm »
Tim, you said I should use analog filter with 60 db down at sampling frequency if I want 0.1%. But now that I played a bit with the calculator you gave me it looks like a FIR filer is a band stop centered at the nyquist frequency. Shouldn't I aim to get 60 db down at nyquist frequency instead?
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: bg
    • Microlab.info
Re: High resolution analog to digital conversion of current spikes
« Reply #19 on: April 25, 2015, 02:06:01 pm »
@Janne Thanks for the tips. Especially the reason to use FIR over IIR.

So y[n] is the last result, y[n-1] - the result before that.

I don't have matlab and I didn't really pay attention when we study it at class. Is it complex to calculate these coefficients? I tried this calculator:
http://www-users.cs.york.ac.uk/~fisher/mkfilter/trad.html
Butterworth IIR filter had only two b coefficients. Is it some kind of optimisation?
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: bg
    • Microlab.info
Re: High resolution analog to digital conversion of current spikes
« Reply #20 on: April 25, 2015, 03:02:57 pm »
I was able to compile the mkfilter command line tool under linux:
http://www-users.cs.york.ac.uk/~fisher/mkfilter/

It's pretty old and it includes some old deprecated C++ functions. I created proper linux make file and I fixed some C standard problems.
I did it because I don't like to rely on a website for something although this one has been around for last 16 years.
I converted the documentation to pdf for convenience. It probably could be compiled under cygwin for windows.
Here it is if someone is interested:
http://download.nicksoft.info/linux/mkfilter.tar.gz

I'm not sure about copyright, but it's quite old and I don't think the author would mind. I included the original readme with the author website url.
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21606
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: High resolution analog to digital conversion of current spikes
« Reply #21 on: April 25, 2015, 04:05:49 pm »
FYI, the author of that website I believe passed away, so that it's still online at all is remarkable; I'd love to have an archive of it (of course, that doesn't work with cgi..), or a mirror or something.  Calculators like that are handy to have around!

Digital filters typically have nulls and phase reversals (poles and zeros in the transfer function), which analog filters usually don't have (all-pole types, versus Elliptical types that do have zeros).  The response is of course symmetrical above Fs/2, because those frequencies alias to the same apparent frequency below Fs/2.  This is why you want the analog filter to be well attenuated above Fs/2, because you don't want analog signals presenting in the high gain aliased region (near Fs).

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: bg
    • Microlab.info
Re: High resolution analog to digital conversion of current spikes
« Reply #22 on: April 25, 2015, 06:59:07 pm »
FYI, the author of that website I believe passed away, so that it's still online at all is remarkable; I'd love to have an archive of it (of course, that doesn't work with cgi..), or a mirror or something.  Calculators like that are handy to have around!
Tim

I can make the web frontend for it, but currently I don't have any free time. Meantime if something happens with the site - you can contact me and I'll do something about it.


Digital filters typically have nulls and phase reversals (poles and zeros in the transfer function), which analog filters usually don't have (all-pole types, versus Elliptical types that do have zeros).  The response is of course symmetrical above Fs/2, because those frequencies alias to the same apparent frequency below Fs/2.  This is why you want the analog filter to be well attenuated above Fs/2, because you don't want analog signals presenting in the high gain aliased region (near Fs).

Ok. I got that. But if we have to be precise about the amount of error - should I take attenuation at Fs or Fs/2?
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1052
  • Country: fi
Re: High resolution analog to digital conversion of current spikes
« Reply #23 on: April 25, 2015, 07:17:26 pm »
Butterworth IIR filter had only two b coefficients. Is it some kind of optimisation?

That web page seems to produce the same coefficients but b-coefficients packed slightly differently, gain is factored in separately. So you can call it an optimization.

Regards,
Janne
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21606
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: High resolution analog to digital conversion of current spikes
« Reply #24 on: April 26, 2015, 01:41:43 am »
I can make the web frontend for it, but currently I don't have any free time. Meantime if something happens with the site - you can contact me and I'll do something about it.
Ah, great.  I'll file that in my brain. :)

Quote
Digital filters typically have nulls and phase reversals (poles and zeros in the transfer function), which analog filters usually don't have (all-pole types, versus Elliptical types that do have zeros).  The response is of course symmetrical above Fs/2, because those frequencies alias to the same apparent frequency below Fs/2.  This is why you want the analog filter to be well attenuated above Fs/2, because you don't want analog signals presenting in the high gain aliased region (near Fs).

Ok. I got that. But if we have to be precise about the amount of error - should I take attenuation at Fs or Fs/2?

To be perfectly precise, neither; the interesting point is where the product of both filters reaches the error limit.  If the digital filter cutoff is set pretty low, then the rise will be pretty sharp near Fs.  If there's no digital filter, it can't be above Fs/2, otherwise your error floor gets (possibly) blown by stuff up there.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf