def fit_double_lorentzian(self, flag_plot = False, flag_verbose = False): """ For a selection of points on the w1-axis, take a cut (giving w3 vs z (intensity) plot) and fit it with a double Lorentzian. self.dl_x_i[0] etc are the min/max indices to be fitted """ if flag_verbose: self.verbose("Fit double Lorentzian for " + self.objectname, flag_verbose = flag_verbose) self.verbose(" x_min: " + str(self.dl_x_i[0]) + " " + str(self.mess.s_axis[2][self.dl_x_i[0]]), flag_verbose = flag_verbose) self.verbose(" x_max: " + str(self.dl_x_i[1]) + " " + str(self.mess.s_axis[2][self.dl_x_i[1]]), flag_verbose = flag_verbose) self.verbose(" y_min: " + str(self.dl_y_i[0]) + " " + str(self.mess.s_axis[0][self.dl_y_i[0]]), flag_verbose = flag_verbose) self.verbose(" y_max: " + str(self.dl_y_i[1]) + " " + str(self.mess.s_axis[0][self.dl_y_i[1]]), flag_verbose = flag_verbose) # select the part of the data to be fitted data = self.mess.s[self.dl_y_i[0]:self.dl_y_i[1], self.dl_x_i[0]:self.dl_x_i[1]] x_axis = self.mess.s_axis[2][self.dl_x_i[0]:self.dl_x_i[1]] y_axis = self.mess.s_axis[0][self.dl_y_i[0]:self.dl_y_i[1]] # arrays for the results n_y, n_x = numpy.shape(data) y_max = numpy.zeros(n_y) # index of the maximum y_min = numpy.zeros(n_y) # index of the minimum y_out_array = numpy.zeros((n_y, 8)) # fitting parameters if flag_plot: plt.figure() color_array = ["b", "g", "r", "c", "m", "y", "k"] # calculate the fit for the cut of w1 for i in range(n_y): y = data[i,:] A_out = MATH.fit(x_axis, y, EQ.rb_two_lorentzians, self.dl_A_in) y_out_array[i,:] = A_out x_fit = numpy.arange(x_axis[0], x_axis[-1], 0.1) y_fit = EQ.rb_two_lorentzians(A_out, x_fit) if flag_plot: plt.plot(x_fit, y_fit, c = color_array[i%len(color_array)]) plt.plot(x_axis, y, ":", c = color_array[i%len(color_array)]) y_max[i] = x_fit[numpy.argmax(y_fit)] y_min[i] = x_fit[numpy.argmin(y_fit)] self.dl_ble = y_min self.dl_esa = y_max self.dl_A = y_out_array if flag_plot: plt.show()
def envelope(A,t): """ Wrapper for rb_gaussian INPUT: t: numpy.array A[0]: sigma (sigma^2 = variance) A[1]: mu (mean) A[2]: offset A[3]: scale, before offset CHANGELOG: 20130408/RB: started function """ return EQ.rb_gaussian(A,t)
def make_plot(self, ax = False, normalize = False, fit = False): """ Make a plot of scan spectrum data. INPUT: - ax (plt axis instance, or False): If False, a new figure and axis instance will be made. - normalize (bool, False): If True, the minimum is subtract from the data, then it is divided by the maximum. - fit (Bool, False): If True, a fit will be made and will also be plotted. The fitting parameters are written to the terminal. CHANGELOG: 201604-RB: started function """ if ax == False: fig = plt.figure() ax = fig.add_subplot(111) if normalize: for ds in range(self.r_n[2]): self.r[:,0,ds,0,0,0,0,0] -= numpy.nanmin(self.r[:,0,ds,0,0,0,0,0]) self.r[:,0,ds,0,0,0,0,0] /= numpy.nanmax(self.r[:,0,ds,0,0,0,0,0]) ax.plot(self.r_axes[0], self.r[:,0,0,0,0,0,0,0], color = "g") ax.plot(self.r_axes[0], self.r[:,0,1,0,0,0,0,0], color = "r") if fit: colors = ["lightgreen", "orange"] labels = ["probe", "reference"] sigma = (self.r_axes[0][0] - self.r_axes[0][-1]) / 4 print(" mu sigma offset scale") for ds in range(self.r_n[2]): A = [sigma, self.r_axes[0][numpy.argmax(self.r[:,0,ds,0,0,0,0,0])], 0, 1] # initial guess A_final = M.fit(self.r_axes[0], self.r[:,0,ds,0,0,0,0,0], EQ.rb_gaussian, A) ax.plot(self.r_axes[0], EQ.rb_gaussian(A_final, self.r_axes[0]), color = colors[ds]) print("{label:10} {mu:.5} {sigma:.3} {offset:.3} {scale:.3}".format(label = labels[ds], mu = A_final[1], sigma = A_final[0], offset = A_final[2], scale = A_final[3]))