return a*x+b def logaritmico(x, a, b): f = a*np.log(b*x) # f[f<0]=0 return f def dlogaritmico(x, a, b): return a/x #2c-1 Vin, tmin, tmax = np.genfromtxt("dati/1c.txt", unpack=True) #tmin, tmax = tmin*10**-6, tmax*10**-6 dVin, dtmax = mz.dVosc(Vin), mz.dtosc(tmax) tmax, dtmax = tmin, mz.dtosc(tmin) popt, pcov, dpopt, chi2, pvalue = mz.curve_fitdx(logaritmico, Vin, tmax, dVin, dtmax, dlogaritmico) print(popt,dpopt, chi2/2) t = np.linspace(0.2, 10, 2000) pl.errorbar(Vin, tmax, dtmax, dVin, fmt='.', label="Misure") pl.plot(t, logaritmico(t, *popt), label="Fit") pl.xlabel("Vin [V]") pl.ylabel("Durata segnale in uscita [us]") pl.legend() pl.savefig("dati/1c.png") pl.close() #2c-2 Vdiscr, Vpot = np.genfromtxt("dati/1c-2.txt", unpack=True)
import menzalib as l3 import numpy as np Vout, f = np.loadtxt("2a.txt", unpack=True) Vin = 1.14 dVin = l3.dVosc(Vin) dVout = l3.dVosc(Vout) Av = Vout / Vin dAv = l3.drapp(Vout, dVout, Vin, dVin) out = open("output.txt", "w") for i in range(len(Vout)): s = "$%0.1f \pm %0.1f$& $ %0.2f \pm %0.3f$& $%0.3f\pm %0.4f$ \\\ \n" % ( f[i], f[i] * 0.01, Vout[i], dVout[i], Av[i], dAv[i]) out.write(s) out.close()
def costante(x, a, b): return a #Punto 1.a R1 = 1.187e3 dR1 = mz.dRdig(R1) R2 = 12.16e3 dR2 = mz.dRdig(R2) Aexp = R2 / R1 dAexp = mz.drapp(R2, dR2, R1, dR1) #punto 1.c Vin, Vout = np.genfromtxt('dati/1c.txt', unpack='True') dVin = mz.dVosc(Vin) dVout = mz.dVosc(Vout) A = Vout / Vin dA = mz.drapp(Vout, dVout, Vin, dVin) tab1 = [mz.ne_tex(Vin, dVin), mz.ne_tex(Vout, dVout), mz.ne_tex(A, dA)] mz.stampa_matrice_latex(tab1) popt, pcov = mz.curve_fitdx(lineare, Vin, Vout, dx=dVin, dy=dVout) chi2, pval = mz.chi2_pval(lineare, Vin, Vout, dVout, popt, dx=dVin, df=costante)
dR1 = mz.dRdig(R1) dC1 = mz.dCdig(C1) #errore frequenza di taglio (punto 1a) Ft1, dFt1 = frequenza_di_taglio(R1, C1, dR1, dC1) #Attenzuazione e errore (punto 1c e 1d) A1 = ([ attenuazione_passa_basso(2e3, Ft1, dFt1), attenuazione_passa_basso(2e4, Ft1, dFt1) ]) #qua faccio il plot del diagramma di bode Vout, freq = np.genfromtxt('dati/2.txt', unpack=True) Vin = 12.5 dVin = mz.dVosc(12.5) A = Vout / Vin dA = errore_rapporto(Vout, mz.dVosc(Vout), Vin, dVin) pl.errorbar(freq, 20 * np.log10(A), yerr=dA, fmt='.', markersize=3) #qua faccio il punto 2.b.ii #la retta obliqua popt1, pcov1 = curve_fit(lineare, np.log10(freq[7:]), np.log10(A[7:]), (1, 1), dA[7:] / (A[7:] * np.log(10))) x = np.linspace(3.5, 6.5, 10) pl.plot(10**x, 20 * (lineare(x, *popt1))) #la retta parallela popt2, pcov2 = curve_fit(lineare, np.log10(freq[:4]), np.log10(A[:4]), (1, 1), dA[:4] / (A[:4] * np.log(10))) x = np.linspace(1, 4, 10) pl.plot(10**x, 20 * (lineare(x, *popt2))) #l'intersezione delle due rette
from numpy.random import normal as nm def ordina_2_vett(v1, v2): if len(v1) != len(v2): return 0 s1 = np.sort(v1) s2 = np.zeros(len(s1)) for i in range(0, len(v1)): for j in range(0, len(v1)): if (s1[i] == v1[j]): s2[i] = v2[j] break return s1, s2 Vt, dVt = [0.260, mz.dVosc(0.260)] #voltaggio in ingresso f, df, deltaf, ddeltaf, V = np.genfromtxt( 'dati/1.txt', unpack='True') #aggiusta l'errore sullo sfasamento ddeltaf = np.sqrt((0.02 * deltaf)**2 + ddeltaf**2) #calcolo la fase fase = 360 * f / deltaf dfase = 360 * mz.drapp(f, df, deltaf, ddeltaf) #metto i dati alla frequenza di taglio f = np.append(f, 1598.34) #frequenza di tagli df = np.append(df, 0.01) V = np.append(V, 182) #ampiezza di taglio fase = np.append(fase, 0) dfase = np.append(dfase, 0.08) #aggiusta l'errore sullo sfasamento
s2 = np.zeros(len(s1)) for i in range(0, len(v1)): for j in range(0, len(v1)): if (s1[i] == v1[j]): s2[i] = v2[j] break return s1, s2 f, Vout = ordina_2_vett(f, Vout) t1 = 3 t2 = 6 t3 = -5 t4 = -3 dVout = mz.dVosc(Vout) Av = Vout / Vin dAv = mz.drapp(Vout, dVout, Vin, mz.dVosc(Vin)) popt1, pcov1 = curve_fit(lineare, np.log10(f[:t1]), 20 * np.log10(Av[:t1]), sigma=20 * mz.dlog10(Av[:t1], dAv[:t1])) popt2, pcov2 = curve_fit(lineare, np.log10(f[t2:t3]), 20 * np.log10(Av[t2:t3]), sigma=20 * mz.dlog10(Av[t2:t3], dAv[t2:t3])) popt3, pcov3 = curve_fit(lineare, np.log10(f[t4:]), 20 * np.log10(Av[t4:]), sigma=20 * mz.dlog10(Av[t4:], dAv[t4:]))