在我的超采样模拟中导致负偏差的原因是什么?

时间:2011-05-03 17:28:23

标签: python algorithm statistics

我正在开发一个小测试程序,看看是否可以向ADC添加噪声以获得过采样位。在我们开始之前,有点理论。奈奎斯特的采样定理表明,一位分辨率的增加需要四个额外的样本,并且通常,n个以上的位需要2 ^(n + 1)个样本。我正在模拟一个完美的10位ADC,它从0..1023单调返回一个值,对于0-2V的输入没有噪声。

要获得更多位,有必要添加随机分布的噪声(它不一定是实际随机的,但它必须随机出现随机,如白噪声。)问题我虽然分辨率在增加,但实际读数会被一些小的负数量所抵消。这是输入1伏的一个示例输出(参考电压为2伏,因此对于单调ADC,计数应该恰好是一半):

10 bits:  512         volts:  1.0
11 bits:  1024        volts:  1.0
12 bits:  2046        volts:  0.9990234375
13 bits:  4093        volts:  0.999267578125
14 bits:  8189        volts:  0.999633789062
15 bits:  16375       volts:  0.999450683594
16 bits:  32753       volts:  0.999542236328
17 bits:  65509       volts:  0.999588012695
18 bits:  131013      volts:  0.999549865723
24 bits:  8384565     volts:  0.999518036842
28 bits:  134152551   volts:  0.999514393508

事实上,无论我运行模拟多少次,我总是以〜0.9995左右结束,而不是1;并且最后一个值应该是134,217,728,而不是134,152,551,大约是65,771 - 或大约是额外18位分辨率的1/4(巧合?我潜水4.)我怀疑我的PRNG在某种程度上有偏见,但是我正在使用Python附带的默认Mersenne Twister。

#!/usr/bin/python
# 
#  Demonstrates how oversampling/supersampling with noise can be used
#  to improve the resolution of an ADC reading.
#
#  Public domain.
#

import random, sys

volts = 1.000
reference = 2.000
noise = 0.01
adc_res = 10

def get_rand_bit():
    return random.choice([-1, 1])

def volts_with_noise():
    if get_rand_bit() == 1:
        return volts + (noise * random.random() * get_rand_bit())
    else:
        return volts

def sample_adc(v):
    # Sample ADC with adc_res bits on given voltage.
    frac = v / reference
    frac = max(min(frac, 1.0), 0.0) # clip voltage
    return int(frac * (2 ** adc_res))

def adc_do_no_noise_sample():
    return sample_adc(volts)

def adc_do_noise_sample(extra_bits_wanted):
    # The number of extra samples required to gain n bits (according to 
    # Nyquist) is 2^(n+1). So for 1 extra bit, we need to sample 4 times.
    samples = 2 ** (extra_bits_wanted + 1)
    print "Sampling ", samples, " times for ", extra_bits_wanted, " extra bits."
    # Sample the number of times and add the totals.
    total = 0
    for i in range(samples):
        if i % 100000 == 99999:
            print float(i * 100) / samples
            sys.stdout.flush()
        total += sample_adc(volts_with_noise())
    # Divide by two (to cancel out the +1 in 2^(n+1)) and return the integer part.
    return int(total / 2)

def convert_integer_to_volts(val, num_bits):
    # Get a fraction.
    frac = float(val) / (2 ** num_bits)
    # Multiply by the reference.
    return frac * reference

if __name__ == '__main__':
    # First test: we want a 10 bit sample.
    _10_bits = adc_do_no_noise_sample()
    # Next, create additional samples.
    _11_bits = adc_do_noise_sample(1)
    _12_bits = adc_do_noise_sample(2)
    _13_bits = adc_do_noise_sample(3)
    _14_bits = adc_do_noise_sample(4)
    _15_bits = adc_do_noise_sample(5)
    _16_bits = adc_do_noise_sample(6)
    _17_bits = adc_do_noise_sample(7)
    _18_bits = adc_do_noise_sample(8)
    _24_bits = adc_do_noise_sample(14)
    _28_bits = adc_do_noise_sample(18)
    # Print results both as integers and voltages.
    print "10 bits: ", _10_bits, "  volts: ", convert_integer_to_volts(_10_bits, 10)
    print "11 bits: ", _11_bits, "  volts: ", convert_integer_to_volts(_11_bits, 11)
    print "12 bits: ", _12_bits, "  volts: ", convert_integer_to_volts(_12_bits, 12)
    print "13 bits: ", _13_bits, "  volts: ", convert_integer_to_volts(_13_bits, 13)
    print "14 bits: ", _14_bits, "  volts: ", convert_integer_to_volts(_14_bits, 14)
    print "15 bits: ", _15_bits, "  volts: ", convert_integer_to_volts(_15_bits, 15)
    print "16 bits: ", _16_bits, "  volts: ", convert_integer_to_volts(_16_bits, 16)
    print "17 bits: ", _17_bits, "  volts: ", convert_integer_to_volts(_17_bits, 17)
    print "18 bits: ", _18_bits, "  volts: ", convert_integer_to_volts(_18_bits, 18)
    print "24 bits: ", _24_bits, "  volts: ", convert_integer_to_volts(_24_bits, 24)
    print "28 bits: ", _28_bits, "  volts: ", convert_integer_to_volts(_28_bits, 28)

我对此提出任何建议表示感谢。我的计划最终是将其用于低成本微控制器以实现高分辨率ADC。速度是相当重要的,所以我可能会使用LFSR来生成PRNG位,这不会是Mersenne twister的一半,但对于大多数用途应该足够好,并且希望它足够好。

3 个答案:

答案 0 :(得分:4)

sample_adc(..)中你可能想要舍入而不是截断(系统地向负无穷大舍入),即:

return int(frac * (2 ** adc_res) + 0.5)

而不是

return int(frac * (2 ** adc_res))

然后与一个人的偏差并不总是在同一边:

10 bits:  512   volts:  1.0
11 bits:  1025   volts:  1.0009765625
12 bits:  2046   volts:  0.9990234375
13 bits:  4100   volts:  1.0009765625
14 bits:  8196   volts:  1.00048828125
15 bits:  16391   volts:  1.00042724609
16 bits:  32784   volts:  1.00048828125
17 bits:  65528   volts:  0.999877929688
18 bits:  131111   volts:  1.00029754639
24 bits:  8388594   volts:  0.99999833107
28 bits:  134216558   volts:  0.999991282821

虽然要检查偏见,例如致电adc_do_noise_sample(..),例如10,000次(对于每种分辨率)并计算平均偏差和该均值的不确定性(并检查它与零的兼容性)。

答案 1 :(得分:0)

听起来你的噪音源有偏见。我不熟悉Mersenne Twister,但我确实知道LFSR伪随机噪声发生器总是有轻微的偏差。您可以通过延长LFSR的长度来任意缩小偏差,但它总是在那里。

答案 2 :(得分:0)

我不熟悉这里的域名,但是如果你使用的是python2,你可能会遇到意想不到的整数除法。有一些方法可以解决这个问题。

>>> 10 / 3
3
>>> 10 * 1.0 / 3
3.3333333333333335
>>> from __future__ import division
>>> 10 / 3
3.3333333333333335
>>> 10 // 3
3