def THDns(x, fs): """ We take in rows of data and return a column of THDN measurements. """ # first, zero-mean rowcnt = len(x) meanxr = x.mean(1) meanxr.shape = (rowcnt, 1) xr = x - meanxr out = empty(len(x), Float) thdlist = [] for i, xrow in enumerate(xr): thdn = 0.0 enob = 0.0 (thdnpy, A, B, C, w) = pysinlesq.computeTHDN(xrow, int(fs)) (thdnc, A, B, C, w) = csinlesq.computeTHDN(xrow, int(fs)) if thdnc < thdnpy: out[i] = thdnc else: out[i] = thdnpy return out
def compWave(filename): f = tables.openFile(filename) num = 2 t1 = f.root.A1.gain100.hpf1.sine r1 = [ (row['frequency'], row['data']) for row in t1.where(t1.cols.sourcevpp > 0 )] x1= r1[num][1][:] fs = 32000 N = len(x1) t = r_[0.0:N]/ fs; (thdn, A1, B1, C1, w1) = csinlesq.computeTHDN(x1, fs) x1m = (A1*cos(t*w1) + B1 * sin(t*w1) + C1) print "x1 thdn = ", thdn pylab.figure(1) pylab.plot(x1-x1m, label = "g=1") pylab.legend() pylab.grid() pylab.figure(2) pylab.plot(x1, 'r') pylab.plot(x1m, 'g') pylab.legend() pylab.grid() pylab.grid() test = [] for i in range(100): test.append(mean(x1[(i*1000):((i+1)*1000)]))
t = r_[0:1:(1/fs)] n = array(randn(len(t))/2**14) fc = 300 (b, a) = signal.bessel(1, fc/fs, btype='high') print b, a bits = 16 thdnf = [] thdn = [] wl = r_[20:1000:50] for w in wl: x = sin(w*t * pi * 2) y = x + n q = signal.lfilter(b, a, y) z = quant(q, bits) zu = quant(y, bits) thdn.append(sinlesq.computeTHDN(zu[2**10:], fs)) thdnf.append(sinlesq.computeTHDN(z[2**10:], fs)) pylab.plot(wl, thdn, label = "unfiltered") pylab.plot(wl, thdnf, label = 'filtered') pylab.grid(True) pylab.legend() pylab.show()
def plotmanyTHDns(): f = tables.openFile(sys.argv[1]) h = io.read_array(file('HPF-as-fir.dat')) ns = [2**13, 2**11, 2**9] plots = [2, 10, 19] for pnum, i in enumerate(plots): pylab.subplot( len(plots), 1, pnum+1) cl = list(colorlist) for n in ns: t = f.root.A3.gain10000.hpf0.sine x = array(t[i][0], dtype=Float64) y = (x - mean(x)) /2**15 fs = 32000 #print "before", t[i][1], max(y), min(y), #y = signal.convolve(h, y, mode='same') #print "after", max(y), min(y) N = len(y) / n y.shape = (N, -1) errs = empty_like(y) thdns = [] params = [] for j, yrow in enumerate(y): thdn = 0.0 enob = 0.0 (thdn, A, B, C, w) = csinlesq.computeTHDN(yrow, fs) errs[j] = thdnMeasure.getSineError(yrow, A, B, C, w, fs) thdns.append(thdn) params.append((A, B, C, w)) worst = argmax(array(thdns)) best = argmin(array(thdns)) b = errs[best] w = errs[worst] print n, len(thdns) pylab.plot(arange(N, dtype=float)/(N-1), thdns, color = cl[0], label = "length %d segments " % n) pylab.axhline(mean(thdns), color = cl[0], linestyle = '--', label = "_nolegend_") cl.pop(0) pylab.grid() pylab.title("Freq = %d " % t[i][1]) pylab.legend() leg = pylab.gca().get_legend() ltext = leg.get_texts() # all the text.Text instance the legend pylab.ylabel('THD + n') pylab.setp(ltext, fontsize='xx-small') # the legendfontsize pylab.show()