def sineBridgeResponse(vmax=3.0, freq=100.0): base = 3.1 - vmax slab.waveSine(base, base + vmax, 100) slab.setWaveFrequency(freq) slab.waveSine(base + vmax, base, 100, second=True) slab.tranStore(500, 4) t, a1, a2, a3, a4 = slab.waveResponse(dual=True) vi = a1 - a2 vo = a3 - a4 slab.plot1n(t, [vi, vo], "Sine Bridge Response", "time (s)", "Vi,Vo (V)", ["Vi", "Vo"])
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
#House arbTriangle(wavey1, 1000, 5000, 2.0, 3.0) arbConst(wavey1, 1300, 2000, 2.7) #Car arbSectA(wavey1, 6000, 7000, 1.0, 1.2) arbSectB(wavey1, 8500, 9000, 1.0, 1.2) arbConst(wavey1, 7000, 8500, 1.4) wavex2, wavey2 = arbNew(10000, 1) arbConst(wavey2, 2000, 4000, 1.5) #Car arbEllipse(wavey2, 6500, 7000, 1.0, 0.9) arbEllipse(wavey2, 8000, 8500, 1.0, 0.9) # Show wave slab.plot1n(wavex1, [wavey1, wavey2]) # Connect to board slab.connect() # Upload wave slab.loadWavetable(wavey1) slab.loadWavetable(wavey2, second=True) slab.setWaveFrequency(2.0) # Show wave slab.wavePlay(40, dual=True) # Disconnect slab.disconnect()
#Step response test print('Step response check') v=slab.stepResponse(1.0,2.0) region1 = v[1][0:15] region2 = v[1][25:100] if not isVectorConstant(region1,1.0): raise slab.SlabEx("stepResponse fails") if not isVectorConstant(region2,2.0): raise slab.SlabEx("stepResponse fails") print(' stepResponse pass') print() # Single wave commands print('Single wave test') w1Values = slab.waveSine(1.0,2.0,100,returnList=True) slab.setWaveFrequency(100) slab.tranStore(100,1) v = slab.waveResponse() dif1 = v[1]-w1Values if not isVectorZero(dif1,0.05): raise slab.SlabEx('waveResponse fails') print(' waveResponse pass') print() # Dual wave commands print('Dual wave test') w2Values = slab.waveCosine(1.2,2.2,100,returnList=True,second=True) slab.tranStore(100,2) v = slab.waveResponse(dual=True) dif1 = v[1]-w1Values dif2 = v[2]-w2Values
fc = 72 Hz ''' # Locate slab in the parent folder import sys sys.path.append('..') sys.path.append('.') import slab # Set prefix to locate calibrations slab.setFilePrefix("../") # Open serial communication slab.connect() # Set storage requirements slab.setTransientStorage(100, 2) # Set wave slab.waveSine(1.0, 2.0, 100) # Set frequency slab.setWaveFrequency(72.3) # Measure slab.wavePlot(10) # Close serial communication slab.disconnect()
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
def squareResponse(v1=1, v2=2, freq=100): slab.waveSquare(v1, v2, 100) slab.setWaveFrequency(freq) slab.tranStore(500, 4) slab.wavePlot()
def triangleResponse(vmin=1, vmax=2, freq=100): slab.waveTriangle(vmin, vmax, 100) slab.setWaveFrequency(freq) slab.tranStore(500, 4) slab.wavePlot()