All About Circuits Forum  

Go Back   All About Circuits Forum > Software, Microcomputing, and Communications Forums > Programmer's Corner

Notices

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.

Reply   Post New Thread
 
Thread Tools Display Modes
  #11  
Old 04-28-2012, 09:14 PM
WBahn WBahn is offline
Senior Member
 
Join Date: Apr 2012
Location: Larkspur, Colorado
Posts: 7,813
Blog Entries: 9
Default

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.
Reply With Quote
  #12  
Old 04-30-2012, 12:13 PM
THE_RB's Avatar
THE_RB THE_RB is offline
Senior Member
 
Join Date: Feb 2008
Posts: 4,350
Default

Quote:
Originally Posted by WBahn View Post
You need to consider what is important for the application (information which we are missing here).
...
I agree we don't have the OP's exact needs. My first post was in responce to ChrisW's blanket statement "it should have been /1023 anyway" which is common thinking but wrong.

Quote:
Originally Posted by WBahn View Post
...
In almost all cases, the important thing is to minimize the error in our estimates.
...
I agree. To the point of one output unit anyway.

Quote:
Originally Posted by WBahn View Post
...
Now, if you have an application where it is more valuable to always underestimate the true value ...
...
No you are wrong here the math *100 /1024 does not "always underestimate the true value". It does round down the output to the output integer, BUT you have to remember the input is rounded down by the ADC hardware. In a 10 step ADC all values 9.0-9.9 and rounded down by the hardware itself to "ADC 9" which is then faithfully scaled without error by the math *100 /10 to be 90%, ie absolutely correctly scaled.

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 non-linear 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 PIC-sound encoder, Shift1-LCD project, the TalkBotBrain talking PIC controller, LiniStepper open-source microstepping motor driver, the Black Regulator 2-transistor SMPS, and probably some other stuff; www.RomanBlack.com

Last edited by THE_RB; 04-30-2012 at 12:23 PM. Reason: typos, addition
Reply With Quote
  #13  
Old 04-30-2012, 03:59 PM
WBahn WBahn is offline
Senior Member
 
Join Date: Apr 2012
Location: Larkspur, Colorado
Posts: 7,813
Blog Entries: 9
Default

Quote:
Originally Posted by THE_RB View Post
I agree we don't have the OP's exact needs. My first post was in responce to ChrisW's blanket statement "it should have been /1023 anyway" which is common thinking but wrong.
And I'm merely taking issue with your blanket statement that it should always be 1024.

Quote:
No you are wrong here the math *100 /1024 does not "always underestimate the true value". It does round down the output to the output integer, BUT you have to remember the input is rounded down by the ADC hardware.
We aren't trying to estimate the "output integer" of the ADC hardware, we are trying to estimate the true value of the input that was applied to ADC hardware. If any voltage between 1V and 2V applied to the ADC produces the same output and we then estimate the input voltage to be 1V, it most surely does underestimate the true value for all input values (except, strictly speaking, exactly 1V, which is a discrete point and occurs with zero probability).

Quote:
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.
If you add an offset to the estimator, then you have a different estimator and the scaling constant is dependent on the estimator. It's fine to talk about better estimators, but don't say that estimator A should use constant K because estimator B uses K. If you aren't adding an offset, then you should use the best scaling constant for THAT estimator. If you are trying to achieve MMSE with a uniform (or even Gaussian) distribution of input values, then you would use 1023 (in this case). If you are trying to achieve something else, then the constant you use might be something different than either.

Quote:
... but /1023 takes a lot more machine cycles in processing than +512 /1024 anyway.
That's a different argument regarding a different issue. I agree that using 1023 is much more computationally expensive (on most MCU's, anyway) and that using 1024 over 1023 might be warranted on the basis of this point even if 1023 were absolutely, positively, and unequivically the correct value. But whether it is or is not more computationally palatable has no bearing on the issue of whether or not it is the correct value.

Quote:
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;
That may be desirable, but may not be practical or possible. At some point, the measurement sensor is what it is and we simply have to match our estimator to the sensor (because, in reality, the sensor is part of the estimator).
Reply With Quote
The Following User Says Thank You to WBahn For This Useful Post:
ErnieM (05-01-2012)
  #14  
Old 05-01-2012, 12:48 AM
ErnieM's Avatar
ErnieM ErnieM is online now
Senior Member
 
Join Date: Apr 2011
Location: Lon Guyland, Noo Yawk
Posts: 4,726
Default

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.”
Reply With Quote
  #15  
Old 05-02-2012, 12:57 PM
THE_RB's Avatar
THE_RB THE_RB is offline
Senior Member
 
Join Date: Feb 2008
Posts: 4,350
Default

Quote:
Originally Posted by WBahn View Post
...
We aren't trying to estimate the "output integer" of the ADC hardware, we are trying to estimate the true value of the input that was applied to ADC hardware. If any voltage between 1V and 2V applied to the ADC produces the same output and we then estimate the input voltage to be 1V, it most surely does underestimate the true value for all input values (except, strictly speaking, exactly 1V, which is a discrete point and occurs with zero probability).
...
Actually we are, this is not pure mathematics it is a scaling process applied between two imperfect, integer based, hardware systems.

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 (input-1):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 (0-100).

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 non-linear *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 PIC-sound encoder, Shift1-LCD project, the TalkBotBrain talking PIC controller, LiniStepper open-source microstepping motor driver, the Black Regulator 2-transistor SMPS, and probably some other stuff; www.RomanBlack.com

Last edited by THE_RB; 05-02-2012 at 01:14 PM. Reason: typos
Reply With Quote
  #16  
Old 05-04-2012, 12:46 AM
ErnieM's Avatar
ErnieM ErnieM is online now
Senior Member
 
Join Date: Apr 2011
Location: Lon Guyland, Noo Yawk
Posts: 4,726
Default

Quote:
Originally Posted by They Might Be Giants

A woman came up to me and said
"I'd like to poison your mind
With wrong ideas that appeal to you
Though I am not unkind"
She looked at me, I looked at something
Written across her scalp
And these are the words that it faintly said
As I tried to call for help:

There's only one thing that I know how to do well
And I've often been told that you only can do
What you know how to do well
And that's be you,
Be what you're like,
Be like yourself,
And so I'm having a wonderful time
But I'd rather be whistling in the dark
Whistling in the dark
Whistling in the dark
There's only one thing that I like
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.”
Reply With Quote
  #17  
Old 05-05-2012, 01:01 PM
THE_RB's Avatar
THE_RB THE_RB is offline
Senior Member
 
Join Date: Feb 2008
Posts: 4,350
Default

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 PIC-sound encoder, Shift1-LCD project, the TalkBotBrain talking PIC controller, LiniStepper open-source microstepping motor driver, the Black Regulator 2-transistor SMPS, and probably some other stuff; www.RomanBlack.com
Reply With Quote
  #18  
Old 05-07-2012, 06:51 PM
chrisw1990 chrisw1990 is offline
Senior Member
 
Join Date: Oct 2011
Location: UK, Near Brighton
Posts: 543
Default

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.
Reply With Quote
  #19  
Old 05-07-2012, 09:38 PM
ErnieM's Avatar
ErnieM ErnieM is online now
Senior Member
 
Join Date: Apr 2011
Location: Lon Guyland, Noo Yawk
Posts: 4,726
Default

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.”
Reply With Quote
  #20  
Old 05-08-2012, 08:24 AM
WBahn WBahn is offline
Senior Member
 
Join Date: Apr 2012
Location: Larkspur, Colorado
Posts: 7,813
Blog Entries: 9
Default

Quote:
Originally Posted by ErnieM View Post
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.

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.
Reply With Quote
Reply   Post New Thread

Tags
, ,


Related Site Pages
Section Title
Worksheet Analog-to-Digital conversion
Worksheet Voltmeter design
Textbook Practical considerations of ADC circuits : Digital-analog 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 07-10-2011 06:41 PM
can someone help me with my uc project (PIC & temperature sensor & LCD)? voyage Embedded Systems and Microcontrollers 6 05-25-2011 08:36 PM
Project: PIC based Scalextric Lap Counter Crossie The Completed Projects Collection 6 03-11-2011 01:26 PM
PIC compiling problem electronic_noob Embedded Systems and Microcontrollers 13 02-06-2010 11:10 AM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:49 AM.


User-posted content, unless source quoted, is licensed under a Creative Commons Public Domain License.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.