
Programmer's Corner Discussion forum for all aspects of programming and software engineering. Any software programming language welcome: C, C++, C#, Fortran, Java, Matlab, etc. 

Thread Tools  Display Modes 
#11




You need to consider what is important for the application (information which we are missing here). In almost all cases, the important thing is to minimize the error in our estimates. Now, if you have an application where it is more valuable to always underestimate the true value rather than minimize the magnitude of the error, you would use a different error metric than if it is the other way around.
If you are going to add an offset to correct for rounding down, notice that I dealt with that case and showed that you want to use the number of states. It is when you are NOT doing that as part of your estimation that you want to use a diminished number of states. 
#12




Quote:
Quote:
Quote:
If the goal is to spread the error so there is the least error in any sample then the scaling math should not be corrupted to produce a nonlinear error (as I said previously with *100 /1023 the lower values have a high  error, the high values have a high + error and the middle values have +/ errors about half the error of the extreme values. So *100 /1023 is a poor solution. If it is important to reduce the max error in any one sample (and generally it's not that critical to reduce errors less than one output unit) but if it is, then adding an offset allows the scaling to remain correct and linear and gives every output sample an equal error distribution of exactly +/ half a unit. So the scaling math would be (*output + half input) / input or (*100 + 512) /1024 *100 /1023 is just a poor solution. Not only are the scaling linearity and error distribution wrong but /1023 takes a lot more machine cycles in processing than +512 /1024 anyway. And if you wanted to be a stickler for errors <1 unit, then really the ADC hardware rounding should be fixed before the scaling by adding half an ADC count first; (((ADC+0.5 counts) *100) + 512) /1024 and as micros need to use integer math that could be; ((((ADC*2)+1) *100) + 512) /2048
__________________
Roman Black  PICs and electronics. Author of BTc PICsound encoder, Shift1LCD project, the TalkBotBrain talking PIC controller, LiniStepper opensource microstepping motor driver, the Black Regulator 2transistor SMPS, and probably some other stuff; www.RomanBlack.com Last edited by THE_RB; 04302012 at 12:23 PM. Reason: typos, addition 
#13




Quote:
Quote:
Quote:
Quote:
Quote:

The Following User Says Thank You to WBahn For This Useful Post:  
ErnieM (05012012) 
#14




WBahn: Very good point. I hadn't considered a "minimum error" algorithm approach, although when possible I do use two points to calibrate a conversion so I can get a "b" coefficient out of the readings.
__________________
“Freedom is never more than one generation away from extinction. We didn’t pass it to our children in the bloodstream. It must be fought for, protected, and handed on for them to do the same, or one day we will spend our sunset years telling our children and our children’s children what it was once like in the United States where men were free.” 
#15




Quote:
If the rounding down error needs to be removed from the input hardware then applying (ADC + 0.5) removes the rounding down error from the first system and means the data applied to the scaling process is correct. It's an extra process but means that all values have equally distributed error, ie; any analog voltage will be correctly rounded to the nearest ADC integer, apart from discrete points of zero probability of course. The scaling process is *100 /1024 as this is the only process that produces identical error distribution per integer unit. And as I said we are forced to use integer units. The ratio scaling is proven correct as if the ADC data is found to be of a larger system (ie 10v 10 ADC steps not 5v 5 ADC steps) there is only ONE correct ratio of input:output between both linear ratio units of input and output. The ratio input:output is known. If there was a linear voltage going in and a linear voltage coming out (like a resistor divider) the scaling ratio MUST be input:output. Any other scaling ratio is wrong. Using the correct ratio ensures that for all input ADC steps there is an equal error bias. All steps are rounded down to the integer. If you use an incorrect scaling ratio ie (input1):output you have the very serious linearity problem I mentioned (that you have glossed over) where all low input values have an additional  error and all high input values have an additional + error. I think this is one of those cases where trying to be too clever and trying to handle this as a pure math issue has led you to the wrong solution. There are two imperfect hardware integer systems and the best real world solution is the (((ADC+0.5 counts) *100) + 512) /1024 solution if you require a properly functioning *linear* ADC result that includes 101 output values (0100). I have enjoyed reading your input and seeing your argument about the MMSE and gaussian distribution etc, but I believe linear scaling is a much better solution even if it means the two values 0 and 100 suffer a little in the process. This is not pure math, this is electronics, so if the input waveform varies 4v peak between 0.5v to 4.5v it is important the output values linearly reflects that differential, and not add  error to the low value and + error to the high value which gives output values NOT linearly reflecting the proper ADC voltage differential. And in many real world cases where we don't care too much about the integer rounding of the final output to the display, then the simplified *100 /1024 still gives the correct linear ADC conversion to output. Many of the sensors we use with ADC give a voltage linear to temperature or pressure etc, and if the sensor goes up 100'C the display needs to go up linearly 100'C. The nonlinear *100 /1023 or *101 /1024 may give "better gaussian distribution" but for a real world ADC device are a poor solution when the correct ratio scaling can be used instead.
__________________
Roman Black  PICs and electronics. Author of BTc PICsound encoder, Shift1LCD project, the TalkBotBrain talking PIC controller, LiniStepper opensource microstepping motor driver, the Black Regulator 2transistor SMPS, and probably some other stuff; www.RomanBlack.com Last edited by THE_RB; 05022012 at 01:14 PM. Reason: typos 
#16




Quote:
And that is whistling in the dark.
__________________
“Freedom is never more than one generation away from extinction. We didn’t pass it to our children in the bloodstream. It must be fought for, protected, and handed on for them to do the same, or one day we will spend our sunset years telling our children and our children’s children what it was once like in the United States where men were free.” 
#17




I always cheer up immensely if an attack is particularly wounding because I think, well, if they attack one personally, it means they have not a single political argument left.
Margaret Thatcher He who strikes the first blow admits he's lost the argument. Chinese Proverb
__________________
Roman Black  PICs and electronics. Author of BTc PICsound encoder, Shift1LCD project, the TalkBotBrain talking PIC controller, LiniStepper opensource microstepping motor driver, the Black Regulator 2transistor SMPS, and probably some other stuff; www.RomanBlack.com 
#18




wow, this got HORRIBLY in depth:P i think i do kinda get whats been discussed.. damn complicated though, but the basic version seems to have done the job.. interesting on error compensation though but not needed anything that specific as yet thank goodness
__________________
if in doubt.. use a hammer. 
#19




It's really not complicated at all. If you want to cut a loaf of bread into two pieces you cut it once. If you wish to divide a range into 1024 intervals you divide it by 1023.
There's no higher math then that to this, no matter how much you stammer and wave your hands.
__________________
“Freedom is never more than one generation away from extinction. We didn’t pass it to our children in the bloodstream. It must be fought for, protected, and handed on for them to do the same, or one day we will spend our sunset years telling our children and our children’s children what it was once like in the United States where men were free.” 
#20




Quote:
So if I have a load of bread that is one foot long and I want to divide it into 2 intervals, you are saying I divide it by 1. So now I have two pieces of bread that are each 1 foot long? Sounds somewhat biblical. ;P It's the classic fencepost problem: The correct number to use depends on whether you want to know how many sections or how many posts. 
Tags 
calculate, maths, pic 
Related Site Pages  
Section  Title  
Worksheet  AnalogtoDigital conversion  
Worksheet  Voltmeter design  
Textbook  Practical considerations of ADC circuits : Digitalanalog Conversion 
Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
How to calculate a watt in power meter system using pic microcontroller  arunagp  Embedded Systems and Microcontrollers  1  07102011 06:41 PM 
can someone help me with my uc project (PIC & temperature sensor & LCD)?  voyage  Embedded Systems and Microcontrollers  6  05252011 08:36 PM 
Project: PIC based Scalextric Lap Counter  Crossie  The Completed Projects Collection  6  03112011 01:26 PM 
PIC compiling problem  electronic_noob  Embedded Systems and Microcontrollers  13  02062010 11:10 AM 
Thread Tools  
Display Modes  

