Tho*_*s O 6 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
Run Code Online (Sandbox Code Playgroud)
事实上,无论我运行模拟多少次,我总是以〜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)
Run Code Online (Sandbox Code Playgroud)
我对此有任何建议表示感谢.我的计划最终是将其用于低成本微控制器以实现高分辨率ADC.速度将是相当重要的,所以我可能会使用LFSR来生成PRNG位,这不会是梅森捻线机的一半,但对于大多数用途应该足够好,并且希望它足够好.
您sample_adc(..)可能想要舍入而不是截断(系统地舍入到负无穷大),即:
return int(frac * (2 ** adc_res) + 0.5)
Run Code Online (Sandbox Code Playgroud)
代替
return int(frac * (2 ** adc_res))
Run Code Online (Sandbox Code Playgroud)
那么与一个的偏差并不总是在同一侧:
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
Run Code Online (Sandbox Code Playgroud)
尽管要检查偏差,例如可以调用adc_do_noise_sample(..)10'000 次(对于每个分辨率)并计算平均偏差和该平均值的不确定性(并检查它是否与零兼容)。