Don't reuse that int

Designer's notes #3 - Home - Prev - Next
Řyvind Teig,  Trondheim,  Norway (

100% accurate with an int then - why not now? 

The curve was fluctuating back and forth a tiny little, so little that we  had to zoom in on it to see the plus/minus 1/400 of a period error. We soon learned that reusing software has a downside. Not that we didn't know before, but this time it was interesting. The hardware we had used in the previous product had a maximum sampling rate of 83.3 kHz, and the new had a maximum rate of 39.0625 kHz. Yes, it sometimes happens that a new design has lower performance than the old. Positioning the A/D samples was done relative to time stamps collected when the unit delivering input to the A/D converter was at special positions.

I was certain that all calculations were done with floating point numbers whenever this was needed. The interpolated x-values were float values, and the samples  were converted to float, and all internal calculations within the interpolation function were float. I printed out all kind of intermediate values to track down the error. Some days with analysing values, and trying to switch on technological intuition, a much underestimated trait, I looked at the sampling rates again. The new sampling times could be102 and 307, where the old would show values like 48 and 144. Hard to spot the difference in texture of those numbers.

Then I realised that the old times were multiples of 12 ms sharp since the frequency was 83.33333 kHz.  The new were multiples of 25 point 6 ms. There was the error - the sample times should have been 102 point 4 ms and 307 point 2 ms. The function which calculated the time of a sample had always, correctly, returned an integer. With the new hardware this was plainly wrong, and having it return the float value instead fixed the error. I still don't understand why I hadn't returned a float in the original code, we used a floating point processor then too. Now the curve stands firm as a rock. Reuse also costs.


Other publications at