def period(vector, time=[], ts=-1, mode=slab.tmodeRise): m = slab.halfRange(vector) tlist = tcross(vector, m, mode, time, ts) n = len(tlist) if n < 2: raise slab.SlabEx("Not enough edges for period") sum = 0 for i in range(0, n - 1): sum = sum + tlist[i + 1] - tlist[i] return sum / (n - 1)
def logRange(start, end=0, ndec=0, ppd=10): # Check if SciPy is loaded slab.checkSciPy() stlog = np.log10(start) # We don't provide end if end == 0: if ndec == 0: raise slab.SlabEx('Need to provide end or decades') return 10**np.arange(stlog, stlog + ndec, 1.0 / ppd) # We provide end endlog = np.log10(end) return 10**np.arange(stlog, endlog, 1.0 / ppd)
def distortion(v1, v2, freq, show=True): points = int(slab.maxSFfresponse / freq) if points > 100: points = 100 if points < 50: raise slab.SlabEx("Frequency too high") cycles = 10 slab.waveCosine(v1, v2, points) slab.setWaveFrequency(freq) slab.tranStore(cycles * points) t, s = slab.singleWaveResponse() if show: slab.plot11(t, s, "Time plot", "time(s)", "ADC1(V)") c, f = ftransform(s, t) if show: ac.plotFreq(f, c) # THD base = np.abs(c[cycles]) tot = 0 for i in range(2, 7): tot = tot + np.abs(c[i * cycles]) * np.abs(c[i * cycles]) tot = np.sqrt(tot) print("tot: " + str(tot)) thd = 100.0 * tot / base # THD+N rms_total = std(s) rms_signal = base / np.sqrt(2.0) rms_no_signal = np.sqrt(rms_total * rms_total - rms_signal * rms_signal) thdn = 100.0 * rms_no_signal / rms_signal # Harmonic Distortion 2nd h2 = dB(np.abs(c[2 * cycles]) / base) # Harmonic Distortion 3rd h3 = dB(np.abs(c[3 * cycles]) / base) if show: print() print("THD : " + str(thd) + " %") print("THD+N : " + str(thdn) + " %") print("Harmonic distortion 2nd : " + str(h2) + " dBc") print("Harmonic distortion 3rd : " + str(h3) + " dBc") print() return thd, thdn, h2, h3
print() ''' Commands without connection to the board ''' print("Test of commands that don't require the board to be connected") print() # Commands: save, load print('Checking file commands') data1=[1,2,3] slab.save('slabTest.dat',data1) data2=slab.load('slabTest.dat') if data1 != data2: raise slab.SlabEx("Read data don't match saved data") print('pass') print() # Commands: highPeak, lowPeak, peak2peak, halfRange, mean, rms, std data1=np.arange(0,6,0.01) data2=0.5+np.sin(data1) data3=np.arange(0,3,0.01) data4=0.2+np.cos(data3) print('Vector utility commands') if not compare(slab.highPeak(data2),1.5): raise slab.SlabEx("highPeak fails") print(' highPeak pass') if not compare(slab.lowPeak(data2),-0.5): raise slab.SlabEx("lowPeak fails") print(' lowPeak pass')
def sineGainAll(v1, v2, freq, npre=5, maxfs=-1): #global adc_delay # Check if SciPy is loaded slab.checkSciPy() # No sat warning yet satWarn = False # Load defaults if maxfs == -1: maxfs = slab.maxSFfresponse # Checks if not slab.opened: raise slab.SlabEx("Not connected to board") if v1 > v2: raise slab.SlabEx("Minimum value must be below maximum value") if maxfs > 1 / slab.min_sample: raise slab.SlabEx("Too high max sample frequency") if freq > maxfs / 4.0: raise slab.SlabEx("Frequency too high") # This command is silent prev_verbose = slab.setVerbose(0) # Create wave if maxfs > 200 * freq: npoints = 200 nsamples = 200 else: npoints = int(maxfs / freq) nsamples = int(200 / npoints) * npoints npre = int(npre * npoints / nsamples) # Create test wave amplitude = (v2 - v1) / 2.0 slab.waveSine(v1, v2, npoints) st = slab.setWaveFrequency(freq) # Setup measurement slab.setTransientStorage(nsamples, 1) # Measure all channels list = [] for channel in range(1, nadcs + 1): time, out = slab.singleWaveResponse(channel, npre, tinit=0.0) # Check peak values vmax = slab.highPeak(out) vmin = slab.lowPeak(out) if (vmax / slab.vref) > SAT_HIGH or (vmin / slab.vref) < SAT_LOW: satWarn = True # Find best fit angles = np.array(range(0, nsamples)) * 2.0 * np.pi / npoints # Initial guess mean0 = np.mean(out) amp0 = (vmax - vmin) / 2.0 phase0 = 0 # Function to optimize optimize_func = lambda x: x[0] * np.sin(angles + x[1]) + x[2] - out # Perform optimization amp, phase, mean = leastsq(optimize_func, [amp0, phase0, mean0])[0] # Warn if needed if satWarn: slab.warn("Saturated reading at ADC " + str(channel)) # Gain to reported gain = amp * np.cos(phase) / amplitude + 1j * amp * np.sin( phase) / amplitude # Add to list list.append(gain) # Restore verbose level slab.setVerbose(prev_verbose) # Return the list return list