How to "scale" / map voltage divider output?  - Page 1
Forum section: electronics / beginners

author: doublec4
author_rank: Regular Contributor
timestamp: April 01, 2023, 07:31:52 am
content: Hello,I'm trying to use a simple voltage divider to monitor the battery level on a li-ion battery pack.My controller (ESP32) can accept 3.3V max analog input. I can use a regular divider to easily calculate the resistor values such that the max output remains 3.3V or less for a fully charged battery at ~13.4VHowever the problem is that a "dead" battery is ~9.6V for example. The result is that my voltage divider output range is 2.3 - 3.3V since the ratio of output to input is maintained. How can I scale the output from 0 - 3.3V so that the analog channel on my controller has more resolution to determine the battery percentage? Or am I overthinking this and a range of 1V is enough to get a decent reading?I've been trying to search for a solution but maybe I am not using the right terms.Thank you

author: Kleinstein
author_rank: Super Contributor
timestamp: April 01, 2023, 07:37:59 am
content: One can subtract the offset, but this also adds unvertainties. Modern µCs tend to include a 12 or 10 Bit ADC and to improve an that one would need resistors better than some 0.1 %.When using 1 V of the 3.3 V span one looses less than 2 bits of the theoretical resolution and the remaining 8-10 bits should still be plenty to monitor the charge level. For more one would also look at the temperature, as the voltage also changes with temperature.

author: dietert1
author_rank: Super Contributor
timestamp: April 01, 2023, 08:48:48 am
content: Also one should take advantage of the low required measurement bandwidth and implement some oversampling/noise reduction in the firmware. Often MCU ADCs have considerable noise (more than 1 LSB). Some people halt the CPU while taking ADC samples.Regards, Dieter

author: Ian.M
author_rank: Super Contributor
timestamp: April 01, 2023, 09:02:00 am
content: IIRC the ESP32 internal ADC is notorious for its poor accuracy . . .

author: MrAl
author_rank: Super Contributor
timestamp: April 01, 2023, 07:09:44 pm
content: Quote from: doublec4 on April 01, 2023, 07:31:52 amHello there,For myself i tend to like to go to a higher resolution ADC. You can get an external 15 bit ADC pretty cheap that is already mounted on a small PC board, maybe 3 for $10 USD.If you dont want to do that then the next idea is to subtract a constant voltage from the reading and then adjust the uC code to account for it.This means if you want to measure 10 volts with a range of 8 to 12 volts then you use a voltage reference or divider to subtract 8 volts from the voltage, then measure the resulting voltage.When you subtract 8 from 10 you get just 2 volts. That means that 2 volts now means you really have 10 volts, and 4 volts means you really now have 12 volts. If you then read 0 volts that means your actual voltage is 8 volts. So your range is now from 0 to 4 volts but you are actually measuring from 8 to 12 volts.Let's say you are measuring from 9.6 to 13.6 volts. That's a range of 4 volts. If you multiply 4 by 0.825 you get 3.3 volts.If you subtract 9.6 volts from the reading you get 0v at the low end, and 4 volts at the high end, and if you multiply that (divider) by 0.825 you get 0v at the low end and 3.3v at the high end.You do have to actually subtract that 9.6 volts though and you can use a voltage reference diode set up for 9.6 volts. Alternately you could use an op amp and subtract that way, but you should use a voltage reference diode for the constant voltage reference not a zener.We could draw up a little circuit if you like.I found the subtraction method to be interesting because if your battery goes too low you really don't need to know the voltage, you just need to know the undervoltage threshold was reached and that means something is really wrong. For your example if your battery was down to 3 volts you would not need to know it was at 3.001 volts or 3.000 volts, you already know something is really wrong so it doesn't really help to know the actual voltage.I am using something like this for a solar project but i like a little extra headroom at the top. My battery is 12 volts and it can go up to 14.2 or more but i have my ADC set up to go from 0v to 16.384 volts. I don't really need it that high but dont want to have to bother with losing 2 volts just for better resolution when i intend to move to a 16 bit ADC soon anyway. The resolution i get right now with just a 10 bit ADC isn't that bad either, it's about 0.016 volts from 0 to 16.384 volts, so don't really need much better actually.

author: Siwastaja
author_rank: Super Contributor
timestamp: April 02, 2023, 06:57:54 am
content: Voltage is not a perfect indicator of the battery state-of-charge anyway, so ADC resolution seldom is the dominating problem. Just scale in software.Linearly mapping 3.1V/cell = 0% and 4.15V/cell = 100% produces decent % display, which of course jumps around during heavy load situations etc., and is not linear because cell voltage curve isn't, but never is catastrophically off like more complex fuel gauging algorithms, especially those based on integrating charge (A*s), can be.Basically for a single cell, percentage = (100*(millivolts-3100)) / (4150-3100).Software averaging gets rid of noise. Cumulative moving average (basically a simple IIR filter) is easy to implement:average_cumul = (average_cumul*255 + (latest_sample*256)*1)/256average = average_cumul/256This mixes 1 part of new value to 255 parts of current averaged value. If you work with integers as in this example, you scale the input value by 256x so that you don't lose resolution in calculation, then scale back by /256 when accessing the cumulative average. This filter updates every cycle, smoothly. Another option is just to average samples together and update when you have enough of them:cumul += latest_sample;cumul_n++;if(cumul_n == 256){average = cumul/256;cumul = 0;}

author: MrAl
author_rank: Super Contributor
timestamp: April 02, 2023, 08:07:47 am
content: Quote from: Siwastaja on April 02, 2023, 06:57:54 amHi,Are you sure you did that right?I use this all the time:avg=(avg*255+newsample)/256although i seldom need to average over that many samples, and note that if avg=10 and newsample=10 the result is again avg=10 which is the right value for a perfectly smooth set.It's true that voltage is not the BEST indicator of state of charge, but over time it can be used effectively. The idea is to take a lot of samples over a long period of time. For example, if you charge for 1 hour and you measure 12.0v at 1am and then 11.0v at 2am, that may be standard for that battery, but if you measure 10.0v at 2am you know the charge time or current was not enough.I do like to measure current too though that is good to know also so you can keep an eye on things.

author: Siwastaja
author_rank: Super Contributor
timestamp: April 02, 2023, 08:57:45 am
content: Quote from: MrAl on April 02, 2023, 08:07:47 amThis reduces the resolution of integers to 1/256 of the original, so it only works if you have enormous amount of excess resolution:Try it with original avg=0 and newsample = 100:avg = (0*255 + 100)/256 = 100/256 = 0Next cycle, newsample = 100avg = (0*255 + 100)/256 = 0And so on; it never gets anywhere.Solution is to scale up the input values by 256; then the cumulative average value will be scaled up by 256 too, so need to keep that in mind and divide by 256 when using it.If you have a FPU available, then you can of course simply dodouble average;average = 0.999*average + 0.001*new_sample;and don't care about numerical range or accuracy, asdoublehas plenty.

author: paulca
author_rank: Super Contributor
timestamp: April 02, 2023, 10:52:24 am
content: The digital approach is to use a I2C voltage sensor which supports 0-16V. That puts the finer details of the analouge stuff into the hands of the people who made the voltage sensor. So they can do a lot more complicated stuff and make it a lot more accurate than you or I.INA3221 might be a bit OTT, it has it's 1 channel cousin though.

author: Siwastaja
author_rank: Super Contributor
timestamp: April 02, 2023, 11:10:05 am
content: Quote from: paulca on April 02, 2023, 10:52:24 amOMG, ADC IS a "voltage sensor", and the ADC designers already did think about the "finer details". It's already there, integrated, no need to think about digital communication!If you are adding an external I2C ADC due to the idea of somehow making itsimpler to use, you will have the exact opposite result: adding the complexity of I2C communication of that external chip.Sure, for better accuracy / features, adding another external chip makes sometimes sense.

author: paulca
author_rank: Super Contributor
timestamp: April 02, 2023, 02:52:51 pm
content: So just wire the battery to the ADC them.

author: Siwastaja
author_rank: Super Contributor
timestamp: April 02, 2023, 02:53:59 pm
content: Quote from: paulca on April 02, 2023, 02:52:51 pmWith a simple voltage divider and a filtering capacitor (e.g. 1µF MLCC), yes.

author: paulca
author_rank: Super Contributor
timestamp: April 02, 2023, 04:05:34 pm
content: Does the ESP32 ADC work from 0V to VRef. Vref is around 1.1V. It will accept 3.3 meaning it won't be fried by it, but anything over 1.1V will just read max.

author: Siwastaja
author_rank: Super Contributor
timestamp: April 02, 2023, 06:00:00 pm
content: Read the datasheet. If it has internal Vref=1.1V, then calculate the resistor divider so that maximum input voltage gives 1.1V. If it has an option of using Vcc as reference, and if you have a decent regulator supplying Vcc, you can then use Vcc as reference as well.

author: radiolistener
author_rank: Super Contributor
timestamp: April 02, 2023, 06:51:09 pm
content: 10-12 bit ADC is enough to measure battery voltage, scale on voltage divider doesn't matters. Usually there is multiple of battery cells, each is about 4.2 V. So you can easily use required voltage divider to scale it down to desired Vref. Let's assume you scale 5V max to Vref. Usually battery cell has a working range 2.5...4.2 V, which is 4.2-2.5 = 1.7 V working range and 1.7 / 5 = 0.34 full ADC scale. For 10 bit ADC it will be 2^10 * 0.34 = 348 counts per working range, so the working resolution will be about 1.7 / 348 = 0.004885 V which is good enough for battery measurement.But on the other hand, your voltage divider will consume battery power, which is not good for battery powered device. So, I would suggest to add MOSFET switch to enable voltage divider only during battery voltage measurement.

author: Ian.M
author_rank: Super Contributor
timestamp: April 02, 2023, 07:43:05 pm
content: Rather than high-side switching the battery to potential divider connection, it can be worth adding an ultra-low quiescent current RRIO OPAMP buffer to let you use much much higher value divider resistors so the standing current is comparable to or even significantly less than the battery equivalent self-discharge current, while still maintaining a low source resistance for the ADC input. That also gives you the opportunity to subtract out an offset voltage to better map the battery's usable voltage range to the ADC's range. However it does have the disadvantage of requiring very high resistance values making the circuit sensitive to leakage currents resulting from any surface contamination of the PCB.

author: MrAl
author_rank: Super Contributor
timestamp: April 03, 2023, 06:58:47 am
content: Quote from: Siwastaja on April 02, 2023, 08:57:45 amHello again,I dont think you can do this with integers alone can you?The formula i gave is a digital low pass filter with time constant about 85 percent of the denominator value. So if each sample time was 1 second and the denominator was 10, then the time constant is about 8.5 seconds. Of course you have to use floats or doubles with this kind of calculation.Let me make sure i understand your formula. You suggested:avg2=(255*avg1+256*new)/256then divide that by 256 again:Y=avg2/256with Y being the end result.Is that right?If so, what advantage do you see?There are of course better ways we have not talked about yet either, for example, a digital 2nd order low pass filter. I'll post that at some point also.BTW, for startup speed i never start with 0*255 i always start with the very first actual sample. So if the first sample is 9, i use that even though the average is 10, then after that employ the formula i posted, and of course using floats. Starting with the very first sample as the average speeds up the startup time even though it may be slightly inaccurate. As the samples come in after that, it gets better and better.It's also interesting to let some noise in as long as it is a true random noise source. That would mean no hardware filter capacitor or perhaps a very very small value like 0.001uf or even 100pf.

author: dietert1
author_rank: Super Contributor
timestamp: April 03, 2023, 09:44:31 am
content: People used to do it with integers and the method is called "fixed point". This is what Siwastaja explained without mentioning the term. Nowadays with floating point support in many MCUs you can as well use floating point if you have the FPU.The method to improve DAC and ADC specs by adding simulated or real noise is called "dithering".I think before designing a DSP function engineers should learn some basic terms.Regards, Dieter

author: Siwastaja
author_rank: Super Contributor
timestamp: April 03, 2023, 10:27:19 am
content: Quote from: MrAl on April 03, 2023, 06:58:47 amAdvantages compared to what?* It works,* It's computationally efficient, especially on a 32-bit CPU and 16-bit input values as everything fits in native register size.Compared to your suggestion:* If your suggestion was based on integers, which you did not specify, the advantage of mine is that it works, unlike yours which is broken,* If your suggestion was based on floats, which you again did not specify, then my integer solution has a performance advantage, especially compared to MCUs without floating point units, but even if you have FPU, an integer solution is preferable for example in interrupt service handlers to avoid stacking of FP registers.I have no idea why you think you can't do such calculation with integers alone. Of course you can and that's what most sensible people do with simple microcontrollers without floating point units available.

author: paulca
author_rank: Super Contributor
timestamp: April 03, 2023, 10:39:30 am
content: https://diyi0t.com/ina219-tutorial-for-arduino-and-esp/

author: Zero999
author_rank: Super Contributor
timestamp: April 03, 2023, 10:55:03 am
content: A shunt reference or zener diode can be used to subtract a fixed voltage from the input to the potential divider.

author: iMo
author_rank: Super Contributor
timestamp: April 03, 2023, 11:11:35 am
content: In case you want to enlarge a smaller voltage range to the full scale of your ADC there is only an "active solution" available - you have to use opamps (it means - to amplify 3.3x your 1Volt range into the 3.3Volt range and shift it down from 0V - 3.3V). That is rather difficult exercise. The best way is either to live with the smaller range, or, use an external ADC (ie. I've been using an ADS1110 16bit 6pin I2C ADC for that exact kind of exercises).

author: MrAl
author_rank: Super Contributor
timestamp: April 03, 2023, 02:36:46 pm
content: Quote from: Siwastaja on April 03, 2023, 10:27:19 amApparently we dont understand each other. We could talk about integers, floats, doubles and leprechauns all day and night it wont do either of us any good. I can also see we are not talking about the same register lengths either. I think you might be talking about rolling your own floats (fixed point) also, which i've done and yes it works. That's your choice.Show an example where you are averaging a voltage and whatever count you want to use for that. For example, a 10v input at startup.Also, if mine was 'broken' i would not be able to measure any voltages, which i have been doing for the last 10 years or more with uC chips of different types.

author: MrAl
author_rank: Super Contributor
timestamp: April 03, 2023, 02:41:52 pm
content: Quote from: paulca on April 03, 2023, 10:39:30 amThat's interesting i was just looking at those the other day. I need to add current measurement to my solar panel telemetry.

author: Siwastaja
author_rank: Super Contributor
timestamp: April 03, 2023, 02:42:51 pm
content: In case the OP missed the correct answers in this noise of all weird and bad advice and bot-like troll replies,Quote from: doublec4 on April 01, 2023, 07:31:52 amYou are overthinking it, this range is enough for a decent reading. Neither noise nor any ADC-related such as nonlinearity or lack of resolution are limiting factors in state-of-charge indication accuracy. Therefore, scaling the voltage to fit the full range of ADC input, or using an external more accurate ADC will not help at all, because they reduce errors that are already smaller than the dominating error source.The only way to really make a better battery gauge is to remove the largest error source, and model the battery characteristics and combine current/voltage/possibly temperature measurements, usually using a battery fuel gauge IC, or with a custom solution. This is significantly more complex, more work, and comes with a risk of making things worse compared to your initial suggestion of using a simple voltage divider.TLDR, try with the simple voltage divider to see if you are happy with it.

author: Siwastaja
author_rank: Super Contributor
timestamp: April 03, 2023, 02:48:28 pm
content: Quote from: MrAl on April 03, 2023, 02:36:46 pmI already demonstrated how it outputs average of 0 when fed with constant input of 100. It's an usual bug which I also made as a beginner decades ago which is why I caught it so easily. uC type does not matter, it's C language. C has built-in integer types, you don't have to DIY anything, definitely not floating points. You can call scaling something up and back down "fixed point" if you so wish, that's what it really is. You can go away now, please.

author: paulca
author_rank: Super Contributor
timestamp: April 03, 2023, 03:36:49 pm
content: Quote from: MrAl on April 03, 2023, 02:41:52 pmIn my view if it comes down to need and the budget is available, pick the option with the best bang for buck in time and money, usually offset by complexity.A basic voltage divider with a couple of caps will work. The ESP32 ADC will work. It will work when it's cold and it will work when it's hot. It's accuracy is pretty much up to you. You can finger in the air it or you can characterise it. Personally I tend to eyeball the values before averaging. Looking for MIN/MAX/STDDEV. "Eyeball". If you see an ADC value fluctuating by 10% of it's range, it's accuracy is no better than +-10% I don't care how much averaging you do. You could take the STDDEV but there is no "grace" in that.In the past I have just become fed up with such circuits because they tend to be 100% coupled to the application you put them in. If a single requirement changes you have zero flexibility and (strawman extreme) you need a whole new schematic, PCB and the works.When put in contrast with a £3.99 module from Amazon which does all of the above far, far more accurately and accommodates the 14 pages this thread will become in corner cases and improvements. It supports a wide enough range of both voltage and current to do pretty much any small DC project and has software already written for the platform.If you want to learn how to do it. By all means. If you want to keep your BOM down and can splurge on engineer time, have at it. If you just want to measure some voltage and current accurately and get on with your day, the added complexity of a pre-canned, pre-tested hardware and software solution is pretty hard to beat. There are certainly more limited set of things to go wrong.Note. There is a 3 channel version. Limited only that all 3 share a ground. Be warned though there are two variants of PCB modules around with 2 different wirings of the current sense resistors.

author: Veketti
author_rank: Regular Contributor
timestamp: April 04, 2023, 07:23:13 am
content: Quote from: Siwastaja on April 02, 2023, 06:57:54 amI'm sorry to revive this, but I'm trying to understand this calculation as I don't get any sensible value out of it. Any chance to open it for dummy?Code:average_cumul = (average_cumul*255 + (latest_sample*256)*1)/256average = average_cumul/256Starting point: average_cumul = 0 and latest_sample = 100(0*255 + (100*256)*1)/256 = 100100/256 = 0.390625Next round average_cumul = 100 and latest_sample = 100(100*255 + (100*256)*1)/256 = 199,6094100/256 = 0.7797What did I do wrong?

author: dietert1
author_rank: Super Contributor
timestamp: April 04, 2023, 08:29:05 am
content: You just continue and after 256 averaging cycles you get the desired average. To speed up initialization you can set average_cumul from the first sample. That was mentioned above.If you want less averaging cycles ("a shorter time constant"), you reduce the 255 and 256 scaling factors, e.g. to 31 and 32. The special thing about 256 is that the division can be implemented as a swap bytes operation on a MCU without barrel shifter.Regards, Dieter

author: MrAl
author_rank: Super Contributor
timestamp: April 04, 2023, 09:38:00 am
content: Quote from: Siwastaja on April 03, 2023, 02:48:28 pmHi,I meant demonstrate your method with an example, like say 10 volts, and it's ok if it is a pseudo example like just using the "10" it can be a voltage or just a count.We know that if we have an average of 10 already established and we get a 'new' sample that is also 10, that the average had better be 10 after the calculation. So 10 before, and 10 after, because a sequence of all 10's should lead to an average of 10. That's simple to understand.So your task would be very very simple and quick so it's not asking you to build a brick house from scratch. You would assume an average of 10, then a new sample of 10, then show how your method produces another 10.I would not ask if i was not interested because i am always open to new ideas in this field, and i certainly believe in your "fixed point" idea, but since i dont seem to get the correct results with your method whether i used integers, fixed point, floats, doubles, or gold leprechaun coins, i could be misunderstanding your solution. An example would show me what you had in mind exactly. So you see why i am asking for your example.Just to note, using "fixed point" is fairly common i agree. I actually was FORCED to use this back when i was using an 8 core AMD processor, one of the notorious "FX" processors. I was using a different processor to start with for some image processing and when i went to the 8 core FX CPU i noticed an extreme slowdown with some parts of the program. I discovered that although the CPU was advertised as having 8 cores (and still is i think) it really has 8 integer cores and only 4 floating point units where one float unit is shared with two integer cores. I had to use 64 bit integers to get around the slowdown by turning to 64 bit fixed point calculations. That got the image processing going fast again, and the errors due to using fixed point were very minimal.Some time later i got involved in a class action lawsuit with AMD to recover some of the cost of the processor. They misled the public when they used the wording "8 cores" because at that point in time a single 'core' had become known as an integer core AND a floating point unit together. That's not what was inside the chip package so they had to pay people that bought those cores some money, which amounted to millions of dollars.Since then i stayed away from AMD, since other problems kept popping up too, although i have no experience with their most recent CPU types that came after the FX series. It could be that they are better than the FX CPU's, and i bet they are.Today we have CPU's made up of different types of cores, some faster than others and in the same chip package, but they are advertised as such so they dont deceive anybody.

author: MrAl
author_rank: Super Contributor
timestamp: April 04, 2023, 09:45:26 am
content: Quote from: paulca on April 03, 2023, 03:36:49 pmHi there and thanks for the reply.Are you talking about a ready made ADC dev board? I have one of those i am considering for use. They are pretty amazing. Inside of my unit they have a PGA (programmable gain amplifier) which i can use to sense voltage across a small value resistor and thus determine the current.I think it even has an option to go differential, which i actually need, because my current measurement shares a ground with the power sources yet has to be located a little far away from one of the sources. Ground voltage drops would affect the reading that's why.But i also have to keep the total current draw very very low, hopefully less than around 1.2ma. I may be able to get that anyway though by just taking measurements every so often instead of continuously. I have to think about this more yet though.I have a 16 bit ADC dev board but dont really need that level of precision anyway, so i may just go with a simple op amp differential amplifier to start with to measure that voltage across the small value current sense resistor.Thanks for the ideas.

author: MrAl
author_rank: Super Contributor
timestamp: April 04, 2023, 09:47:22 am
content: Quote from: dietert1 on April 04, 2023, 08:29:05 amHi there,Which method are you talking about here?Maybe you can illustrate the example i talked about previously where we already have an average of 10 and a new sample comes in that is also 10, the output average has to be 10 as well. This can be shown with just one calculation, or whatever you want to do.It's interesting to look at these methods i think so it's worth the time.

author: Veketti
author_rank: Regular Contributor
timestamp: April 04, 2023, 09:53:45 am
content: Quote from: dietert1 on April 04, 2023, 08:29:05 amThanks, I tested in Excel and after 256 rounds I got 6.66 when new value was always 10. Only after 747 readings the value was 10 like the new reading.

author: dietert1
author_rank: Super Contributor
timestamp: April 04, 2023, 10:16:00 am
content: This running average algorithm implements a first order low pass filter. So one time constant after turning on the input you expect 1-1/e = 0.63 of the input value. After two and three time constants you expect 0.86 and 0.95 of the input value. 9.5 gets rounded up to 10.Maybe the time constant isn't exactly 255 units. But with larger numbers (fine integration steps) it will approach the analog case.Regards, Dieter

author: Veketti
author_rank: Regular Contributor
timestamp: April 04, 2023, 11:08:17 am
content: Ok, tried with 820 which could be real life ADC value, but that didn’t get me any closer to correct value. But never mind. I’ve been using method that get eg. 64 readings and average1 them. Then get another average2 for another 64 readings. Then compare whether the average2 is +-2 from average1. If yes (average1+average1+average2)/3. Otherwise use average2. That however is not that good method; I still get some drift on values. I’ve been planning to build something like take three different averages and see if they are all going to same direction than the original value and only then use the latest average or in that case average of the three averages. This way you could filter out the noise which is smaller, bigger, smaller, bigger. I’m not sure, would that work in reality or is that already known filter method with fancy name (if not let's name it Veketti -method). Hope my explanation makes sense..Sorry, if I’m going too offtopic..

author: dietert1
author_rank: Super Contributor
timestamp: April 04, 2023, 11:41:55 am
content: No, this is another step of signal analysis.If you are interested in denoising process data, you can try and read about Kalman tracking. That is a method that takes into acount derivatives and is used all around the world to control the flight path of drones. Don't know whether it makes sense for a battery charger.Regards, Dieter

author: paulca
author_rank: Super Contributor
timestamp: April 04, 2023, 11:51:18 am
content: Quote from: MrAl on April 04, 2023, 09:45:26 amThe INA3321 boards.https://www.amazon.co.uk/Mmjklkm-INA3221-Triple-Channel-Current-Monitor-black/dp/B0BVF5N31H/The black one has 3 separate current sense resistor paths so can be mixed, highside/lowside, the purple one is combined ground, not so useful.On low current draw. I believe, if you disable continuous conversion and just use single shot readings it will go back to sleep at micro amps.You get to set the conversion time and the averaging window which it does in hardware. So you could set a 1ms conversion time for nice accuracy, a 32 sample window and run conversions for a while to get a stabilised value. These are all configurable of course. Then the chip will go to sleep again until you wake it up with I2C signals.Of course you can buy the IC and incorporate it to PCB if/when your project gets there.

author: MrAl
author_rank: Super Contributor
timestamp: April 04, 2023, 02:44:53 pm
content: Quote from: Veketti on April 04, 2023, 07:23:13 amHi,Well i had questions too that's why i asked for an example.In the following image i did some calculations both with the given algorithm and with the simpler one. The given algorithm does not seem to work using integers, although i think i can get it to work using floats.Using integers the max value given a starting cumulative average of 10 and all new samples 10 comes out to just 9, and it takes some time to get to that value. The simpler algorithm starts out at 10 and remains at 10, and that uses floats. Since some applications can do better with fixed point, we can do that too later with the simpler algorithm. Since the implementation of fixed point does not produce results that are that much different from an actual float, we can expect the same results however.Since i can only get a final value of 9 with the given algorithm i have to conclude that either the entire process was not yet laid out properly (such as a typo) or the algorithm simply does not work unless you use floats. See the attached image for results from both algorithms.