Commit 7d1b1e74d26659865460efab51ce9372e6f9fe9a
1 parent
33baf48955
Exists in
master
Resolve csv reading bug
Showing 1 changed file with 2 additions and 2 deletions Inline Diff
RingDown.py
import matplotlib.pyplot as plt | 1 | 1 | import matplotlib.pyplot as plt | |
import numpy as np | 2 | 2 | import numpy as np | |
import csv, glob | 3 | 3 | import csv, glob | |
from scipy.optimize import curve_fit | 4 | 4 | from scipy.optimize import curve_fit | |
5 | 5 | |||
list_files = (glob.glob('WA*5.CSV')) | 6 | 6 | list_files = (glob.glob('WA*1.CSV')) | |
7 | 7 | |||
data = [] | 8 | 8 | data = [] | |
9 | 9 | |||
for f in list_files: | 10 | 10 | for f in list_files: | |
data_iter = csv.reader(open(f, 'r'), delimiter = ',', quotechar = '"') | 11 | 11 | data_iter = csv.reader(open(f, 'r'), delimiter = ',', quotechar = '"') | |
for i in range(2): | 12 | 12 | for i in range(2): | |
data_iter.next() | 13 | 13 | data_iter.next() | |
temp_data = [value[:-1] for value in data_iter] | 14 | 14 | temp_data = [value for value in data_iter] | |
data.extend(temp_data) | 15 | 15 | data.extend(temp_data) | |
16 | 16 | |||
data = np.asarray(data, dtype = float) | 17 | 17 | data = np.asarray(data, dtype = float) | |
18 | 18 | |||
del(temp_data, list_files, value, f) | 19 | 19 | del(temp_data, list_files, value, f) | |
20 | 20 | |||
plt.subplot(111) | 21 | 21 | plt.subplot(111) | |
plt.clf() | 22 | 22 | plt.clf() | |
23 | 23 | |||
plt.plot(data[data[:,0]>=0,0], data[data[:,0]>=0,1], label ='mes') | 24 | 24 | plt.plot(data[data[:,0]>=0,0], data[data[:,0]>=0,1], label ='mes') | |
25 | 25 | |||
def func(t, tau, A, w , a, b): | 26 | 26 | def func(t, tau, A, w , a, b): | |
return A * np.exp(-t/tau) * np.sin((w+a*t)*t) + b | 27 | 27 | return A * np.exp(-t/tau) * np.sin((w+a*t)*t) + b | |
28 | 28 | |||
popt, pcov = curve_fit(func, data[data[:,0]>=0,0], data[data[:,0]>=0,1], p0 = [1e-5, 1e-2, 1e5, 1e10, 1e-4], maxfev=10000) | 29 | 29 | popt, pcov = curve_fit(func, data[data[:,0]>=0,0], data[data[:,0]>=0,1], p0 = [1e-5, 1e-2, 1e5, 1e10, 1e-4], maxfev=10000) | |
yfit = func(data[data[:,0]>=0,0], *popt) | 30 | 30 | yfit = func(data[data[:,0]>=0,0], *popt) | |
31 | 31 | |||
plt.plot(data[data[:,0]>=0,0], yfit, label ='fit') | 32 | 32 | plt.plot(data[data[:,0]>=0,0], yfit, label ='fit') | |
33 | 33 |