Rich (BB code):
float AccelPerc=0, BrakePerc=0;
AccelPerc=(AccelPedV/1024)*100; //where accelpedv is 10-bit ADC Sample.
float AccelPerc=0, BrakePerc=0;
AccelPerc=(AccelPedV/1024)*100; //where accelpedv is 10-bit ADC Sample.
Why? A 10bit ADC output is in binary 2^10 which is scaled at 1024....
... //should have been 1023 anyway
1023 is right. A 10 bit number can range from 0..1023 (1024 distinct possibilities). If you divide by 1024, then the maximum output won't be 100%.Why? A 10bit ADC output is in binary 2^10 which is scaled at 1024.
In the same way a percentage is scaled in decimal at 100 (10^2) and math uses the constant 100.
Those are 2 facts. However the number system and the correct math is /1024.1023 is right. A 10 bit number can range from 0..1023 (1024 distinct possibilities). If you divide by 1024, then the maximum output won't be 100%.
Let's look at a real world analog to digital converter transfer function from the PIC12F615, a typical device who's sheet I have handy:(Oh, go back and read it for yourself)
Whether 5V is in the range or not is irrelevant. The thing that is most relevant is missing. Namely, given a value from the ADC -- 0 to 9 in this case -- what estimator (the algorithm for estimating the input voltage) results in the minimum error? Now, "minimum error" depends on a number of things, but in the absence of more detailed knowledge, it is generally taken to mean the minimum mean-squared-error (MMSE) assuming a random input that is uniformly distributed over the input range.Those are 2 facts. However the number system and the correct math is /1024.
Consider if the ADC module had only 10 steps, and was measuring voltages between 0v and 5v. The result looks like this;
step0 = 0v to 0.4999v
step1 = 0.5v to 0.9999v
step2 = 1v to 1.4999v
step3 = 1.5v to 1.9999v
step4 = 2v to 2.4999v
step5 = 2.5v to 2.9999v
step6 = 3v to 3.4999v
step7 = 3.5v to 3.9999v
step8 = 4v to 4.4999v
step9 = 4.5v to 4.9999v
Note there is no 5v. 5v is an illegal value (that would actually equal step10 as this is all a base 10 system!) so it would just default to step9 since the ADC is a 10 step system.
VOLTS ADCout *100 /4 *100 /5
0.0v 0 0 0
0.5v 0 0 0
1.0v 1 25 20
1.5v 1 25 20
2.0v 2 50 40
2.5v 2 50 40
3.0v 3 75 60
3.5v 3 75 60
4.0v 4 100 80
4.5v 4 100 80
(5.0v) 5 120? 100
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.You need to consider what is important for the application (information which we are missing here).
...
I agree. To the point of one output unit anyway....
In almost all cases, the important thing is to minimize the error in our estimates.
...
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....
Now, if you have an application where it is more valuable to always underestimate the true value ...
...
And I'm merely taking issue with your blanket statement that it should always be 1024.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.
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).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.
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.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.
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.... but /1023 takes a lot more machine cycles in processing than +512 /1024 anyway.
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).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;
Actually we are, this is not pure mathematics it is a scaling process applied between two imperfect, integer based, hardware systems....
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).
...
There's only one thing that I likeThey Might Be Giants said: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
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.
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
D | new maths discovery | General Science, Physics & Math | 12 | |
M | Ohms law, battery capacity, what’s the mistake in my maths | General Electronics Chat | 7 | |
Decibels Maths, Query. | General Science, Physics & Math | 6 | ||
D | 'quick maths' solver website | General Science, Physics & Math | 11 | |
What Maths i need to get good at | Homework Help | 7 |