def calcNoiseAndResolution(d, resolutionIndex, voltageRange): """ Takes 128 readings and calculates noise and resolution """ # Make a list to hold our readings readings = [] # The feedback command to send to the device # Analog input reading from channel 15 (GND) with settling factor 4 (200 microseconds) cmd = u6.AIN24AR(15, ResolutionIndex=resolutionIndex, GainIndex=voltageRange, SettlingFactor=4) start = datetime.now() # Collect 128 samples for i in range(128): readings.append(d.getFeedback(cmd)[0]['AIN']) finish = datetime.now() print("%s per sample" % ((finish - start) / 128)) # The Peak-To-Peak Noise is difference between the max and the min. p2pn = max(readings) - min(readings) # Noise-Free resolution in bits follows the formula: nfrbits = 24 - math.log(p2pn, 2) # Noise-Free Resolution (uV) = # <range> / 2 ^ < Noise-Free resolution (bits) > nfrres = (ranges[voltageRange] / (2**nfrbits)) * (10**6) # Get the RMS Noise by calculating the standard deviation. mean = sum(readings) / len(readings) rms = 0 for r in readings: rms += (r - mean)**2 rms = float(rms) / len(readings) rms = math.sqrt(rms) # Effective Resolution is uses a similar formulas as Noise-Free. erbits = 24 - math.log(rms, 2) erres = (ranges[voltageRange] / (2**erbits)) * (10**6) return [p2pn, nfrbits, nfrres, rms, erbits, erres]
def get_ai_value(self, channel, verbose=False, **kwargs): """ """ channel = self._check_ai_channel_input(channel) # retrieve ai channel object from channel number ai_name = list(AI_DCT.keys())[list(AI_DCT.values()).index(channel)] ai_obj = getattr(self, ai_name.lower()) # measure voltage ai_obj.settings.update(kwargs) cmd = u6.AIN24AR(channel, **ai_obj.settings) if verbose: print('cmd: ', cmd) reading = self.d.getFeedback(cmd) volt = self.d.binaryToCalibratedAnalogVoltage( ai_obj.settings['GainIndex'], reading[0]['AIN'] ) return volt
def __init__(self, d, channel, **kwargs): """ """ self.settings = U6Reader.AIN24AR_SETTINGS self.settings.update(kwargs) self.cmd = u6.AIN24AR(channel, **kwargs) self.d = d