High resolution analog to digital conversion of current spikes - Page 1
Forum section: electronics / beginners

author: npelov
author_rank: Frequent Contributor
timestamp: April 22, 2015, 07:41:23 pm
content: 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?

author: T3sl4co1l
author_rank: Super Contributor
timestamp: April 22, 2015, 08:05:31 pm
content: 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

author: f5r5e5d
author_rank: Frequent Contributor
timestamp: April 23, 2015, 03:59:25 am
content: 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 budgetyou 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 uncertaintythe 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 metersif 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...

author: npelov
author_rank: Frequent Contributor
timestamp: April 23, 2015, 08:02:44 am
content: @TimI 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.@f5r5e5dI 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.QuoteOf 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.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: April 23, 2015, 11:31:56 am
content: Quote from: npelov on April 23, 2015, 08:02:44 amIf 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

author: npelov
author_rank: Frequent Contributor
timestamp: April 23, 2015, 12:47:04 pm
content: 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.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: April 23, 2015, 03:48:50 pm
content: 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 theapparent powerin VAs!Tim

author: npelov
author_rank: Frequent Contributor
timestamp: April 23, 2015, 04:47:47 pm
content: 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 ofanylow 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.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: April 23, 2015, 10:18:54 pm
content: Quote from: npelov on April 23, 2015, 04:47:47 pmYou 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!QuoteYes, 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.htmQuoteCorrect! 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

author: npelov
author_rank: Frequent Contributor
timestamp: April 24, 2015, 08:24:58 am
content: 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 signalexactlyand 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.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: April 24, 2015, 12:48:57 pm
content: Quote from: npelov on April 24, 2015, 08:24:58 amYes, 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.QuoteIndeed. 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.QuoteIf 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

author: npelov
author_rank: Frequent Contributor
timestamp: April 24, 2015, 05:17:45 pm
content: 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.15uFC2 = 0.068uFC3 = 0.01uFThe 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).

author: jahonen
author_rank: Super Contributor
timestamp: April 24, 2015, 08:36:14 pm
content: 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

author: npelov
author_rank: Frequent Contributor
timestamp: April 24, 2015, 10:40:24 pm
content: 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 readmicrochip'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.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: April 24, 2015, 11:12:01 pm
content: 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

author: npelov
author_rank: Frequent Contributor
timestamp: April 25, 2015, 10:49:47 am
content: 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.QuoteI 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.QuoteWhat is the shape of the impulse? Square or sine?QuoteI 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"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 byHanning function?So we sample, correct, normalize and then apply perform the convolution on the data from the ADC.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: April 25, 2015, 01:08:25 pm
content: Quote from: npelov on April 25, 2015, 10:49:47 amThe 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 likehttp://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.)QuoteThis 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.QuoteYes!Tim

author: jahonen
author_rank: Super Contributor
timestamp: April 25, 2015, 01:22:45 pm
content: 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+b1*x[n-1]+b2*x[n-2]-a1*y[n-1]-a2*y[n-2]. x 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:» [b,a] = butter(2, 50*2/3900)b =0.00153406027143 0.00306812054286 0.00153406027143a =1.00000000000000 -1.88619313823365 0.89232937931937So we now know that coefficients are:a0 = 1 (implicitily assumed in above calculation)a1 = -1.88619313823365a2 = 0.89232937931937b0 = 0.00153406027143b1 = 0.00306812054286b2 = 0.00153406027143You 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 precisionFor 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

author: npelov
author_rank: Frequent Contributor
timestamp: April 25, 2015, 01:45:24 pm
content: 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?

author: npelov
author_rank: Frequent Contributor
timestamp: April 25, 2015, 02:06:01 pm
content: @Janne Thanks for the tips. Especially the reason to use FIR over IIR.So y 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.htmlButterworth IIR filter had only two b coefficients. Is it some kind of optimisation?

author: npelov
author_rank: Frequent Contributor
timestamp: April 25, 2015, 03:02:57 pm
content: 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.gzI'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.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: April 25, 2015, 04:05:49 pm
content: 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

author: npelov
author_rank: Frequent Contributor
timestamp: April 25, 2015, 06:59:07 pm
content: Quote from: T3sl4co1l on April 25, 2015, 04:05:49 pmI 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.Quote from: T3sl4co1l on April 25, 2015, 04:05:49 pmOk. I got that. But if we have to be precise about the amount of error - should I take attenuation at Fs or Fs/2?

author: jahonen
author_rank: Super Contributor
timestamp: April 25, 2015, 07:17:26 pm
content: Quote from: npelov on April 25, 2015, 02:06:01 pmThat 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

author: T3sl4co1l
author_rank: Super Contributor
timestamp: April 26, 2015, 01:41:43 am
content: Quote from: npelov on April 25, 2015, 06:59:07 pmAh, great. I'll file that in my brain.QuoteTo 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

author: npelov
author_rank: Frequent Contributor
timestamp: April 26, 2015, 10:39:16 am
content: I talked about digital filters in general because I want to implement them in my projects. I did't say I'll implement them in this project, although I probably will. But let's sayI don't have any digital filtering- is the highest possible error the attenuated frequency at Fs/2 or Fs? Let's say there is very strong 0 db tone just above Fs/2 - at (Fs/2)*1.1. If the filter has -40dB at Fs will the error be 1% it'll be the attenuation level at Fs/2.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: April 26, 2015, 03:51:49 pm
content: If there's no digital filtering, then it's Fs/2.Tim

author: npelov
author_rank: Frequent Contributor
timestamp: May 02, 2015, 04:42:06 pm
content: I realize that digital filters of 24 bit data are not really an option with 8 bit MCUs. Floating point is too slow to achieve anything > 900sps (leaving no CPU time for anything else) and compilers for 8 bit mcus do not support more than 32 bit integers, so there is no space of multiplying 24 bit data by reaasonable resolution coefficients.So I should either switch to more powerful MCUs (not going to happen soon) or I should stick to analog filters. The problem with MCP3901 is that it has digital noise +-2000 LS digits in 24 bit and about 4-5 digits in 16 bit mode. Probably my circuit is adding even more.So I'm going to forget about 16 bit resolution and maybe round it to 10 000 counts (14 bits, non full scale). I think I can get almost stable reading at 10 000 counts.Digital filters are quite interesting, but I wasn't able to find good calculator that scales the coefficients to integer with certain number of bits. That would be useful for MCUs.Is scalling the coefficients as simple as multiplying them by certain number and then the result should be divided to that number?

author: T3sl4co1l
author_rank: Super Contributor
timestamp: May 02, 2015, 06:11:55 pm
content: Hmm, I had written a... was it a 16x16-->32 bit MAC? That takes 29 cycles on AVR (has 8 bit, 2 cycle hardware mult).Do some reading on fixed point. The best premise is to consider the ADC reading a dimensionless fraction of its range (0 to 2^N - 1 == 0 to 0.999... == 0 to 5V or whatever), and then it becomes apparent that: every addition potentially produces a carry, which is overflow and nothing else (can be detected and used to saturate the result, if needed); every multiply produces additional bits, which should be carried for the duration of the immediate calculation but must be discarded at the end, because they are simply such tiny fractions (in the 2^(-2N) range) that they don't matter to the result.That is, when you perform the multiplicationAB * XY = A*X + ((B*X + A*Y) >> N) + (B*Y >> 2N) = CDEF(where A/X and B/Y are the high and low words, respectively, and C is high, D is low, E is low-low and F is low-low-low),the last product is superfluous because it lands in words EF (except for possible carry into D), and therefore can be truncated at the end of the calculation.At the very last possible, final, step, you might apply scaling (calibrations?) and units (perhaps even resorting to floating point), only for the last step of, say, writing it to the display as a human readable number. At such low update rates, this is a tolerable use of such slow operations.FIR coefficients are scaled linearly, because the distributive property. IIR coefficients you'd have to work out.If nothing else, you can simulate a series of N samples on a spreadsheet, or write a script for it in any language you prefer (C, Java, MATLAB/Octave, etc.). You can achieve perfect numerical accuracy (i.e., bitwise perfect agreement with the integer code that will eventually end up on the MCU) by making judicious use of scaling and rounding functions.The MAC I mentioned earlier, was a function I wrote as part of proving out a temperature correction function. Here's the heading comment,Code:;; countToTemp;; Converts a 12 bit ratiometric ADC count into a; temperature (in 12.4 fixed point format, °C),; using a 7th order polynomial approximation.;; 74 words, approximately 310 cycles.;; Input:; r17:r16: 12 bit ADC count to convert; Returns:; r1:r0: 12.4 fixed point, temperature in °C;I had written two of these, the other based on a seemingly elegant sum-of-reciprocals method: however, because division is such a painful operation to compute (without hardware... heck, even with!), it was way larger and slower.Tim

author: npelov
author_rank: Frequent Contributor
timestamp: May 03, 2015, 05:42:33 am
content: Well, 16x16 is not a big deal. I don't think that it's worth trying to optimize it because of the results of the Microchip App. Note:http://ww1.microchip.com/downloads/en/AppNotes/00852a.pdfTheir test results with 16 bit IIR and 8 bit FIR coefficients show that at 8kHz of 10 bit ADC uses ~ 2.5MIPS of the 10 MIPS available - which is 1/4. I'm assuming that they spent some effort optimizing it, even if it's not the best optimisation. So 10 bits by 16 bits will rarely spread to more than 24 bits. But what if I have to multiply 24 bits ADC data by 16 bits coefficients. If I use fixed point that would give me 48 bit result which I can later round to 24 bits again. Also the MCU time for doing that would be a lot more. Also you said that IIR is a lot sensitive to rounding.I could spent a lot of efforts of doing optimized assembly code for the filters, but that'll probably not stabilize the ADC input. With second order IIR I was able to get stable 500.000 mV reading (LS digit playing +-2) at cut off frequency <1Hz (not sure about the exact numbers). The display wasn't updating fast enough. It took seconds to get into 1% reading.I agree that it could be done (probably), but I'm not sure if it's worth the time spent for optimizing it.Maybe some day I'll try to optimize it for dsPIC which has 16x16 bit signed hardware multiplier. With it 24 x 16 multiplication won't be that time consuming. And even then it might be less expensive to replace it with PIC32 and just use non-optimized floating pointing in plain C ... unless it's something that will be produced in 1000s.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: May 03, 2015, 08:08:48 am
content: Quote from: npelov on May 03, 2015, 05:42:33 amThat's probably fair for that platform. Their inner loop looks awfully long. I don't know if that's a PIC thing. Possibly; it's an ancient instruction set, and pretty sparse, most ponderously so on registers.The pile of registers on the AVR lets it plow through DSP stuff like this -- set up all your coefficients and operands, and just chug chug chug away. If register values need to be protected, push/pop goes outside the loop, around the function, so they only cost constant time. The fewer memory sources you have to read/write during the loop, the better; and if they can be accessed with sequentially indexed addressing, all the more (e.g., reading samples or coefficients from a linear buffer).Most any modern CPU should perform as well or better. At a glance, dsPIC and MSP430 (assuming hardware multiply is included) are probably a few times better than AVR at this task, and PIC32 (MIPS core) and ARM anything (Cortex M0 on up) even better still (wider operands, especially handy for higher precision; more addressing modes; higher clock rates, and in the even faster ones, pipelines and cache).Or a DSP architecture, but that would be serious overkill for such simple filtering. Setting up the data flows might well be more overhead for such a simple problem on those.QuoteTotal number of (possibly required) bits in the product is always the sum of bits in the operands, but 10b x 16b with real signal data probably won't miss the two bit truncation (but you will still need to carry them through, otherwise that would be equivalent to truncating one or the other operand directly, which wouldn't be nice).For a fixed size hardware multiplier (i.e., 8x8 or 16x16 or whatever), more bits takes O(N^2) operations, so if you're doing 24 or 48 bit MACs, it can get very attractive to use a more powerful instruction set.IIR is very sensitive, but that's one place where simulation can help. Occasionally, you can play around with the bits, and round it to a convenient number, that happens to give a stable result, but at a slightly different cutoff frequency, or Q factor, or gain. This can be solved before hand, and the compensating gain term can be calculated.Much the same as selecting rounded component values for the analog filter, but those are much more stable, so it's a lot easier (since the poles lie on the complex plane -- instead of poles inside the unit circle, a much tighter margin).Point being, if you calculate a filter that requires, say, 18 bit coefficients for stability, but you get lucky and find a set of coefficients that end in two zeroes, you can truncate those to 16 bits, and save a whole bunch of calculations on an 8/16 bit platform!QuoteWell... yeah, that's exactly what a filter should do. If you want an aggravatingly slow update, you can filter the piss out of it, and get one really stable number. It might not even be as accurate as it is stable (i.e., you're stretching it out to way more bits than the ADC is even worth), but that's certainly something that can be done.If you want something nice and responsive, an IIR with rapid settling, or an FIR, or a combination of both, would do well. The IIR, of course, must be calculated in much the same way that you'd calculate an analog filter with rapid settling -- i.e., minimal overshoot and phase delay: a Bessel filter. (The FIR can simply be a "hump" of any suitable shape -- a windowed piece of sin(x), Gaussian, actually sampled Bessel impulse response, etc. But to get comparable performance, it will have to be long.)There are still other possibilities. If you want a periodic update, where each frame evaluates exactly the data in the proceeding frame duration, then you can just accumulate for a long time, then when it comes time to spit that out to the display, divide by the number of samples and correct for gain and whatnot. The output sample rate is therefore the frame rate, which is less than the input sample rate -- a decimation process. An impulse read during a frame will only show up after that same frame, so the impulse response is finite (= FIR). This is what almost all DMMs perform, whether through analog (multi-slope integration) or digital means.Tim