def estimate_signal_to_noise(x, y): raise if 1: x, y = interpolate(x, y) #x, y_tr = fourier_filter(x, y) x, y_tr = savitzky_golay_filter(x, y) noise = y - y_tr else: from scitbx.math import chebyshev_polynome from scitbx.math import chebyshev_lsq_fit x_obs, y_obs = x, y w_obs = flex.double(y_obs.size(), 1) w_obs[0] = 1e16 w_obs[-1] = 1e16 ## determining the number of terms takes much, much longer than the fit n_terms = chebyshev_lsq_fit.cross_validate_to_determine_number_of_terms( x_obs, y_obs, w_obs, min_terms=2, max_terms=30, n_goes=20, n_free=20) #n_terms = 7 print "n_terms:", n_terms fit = chebyshev_lsq_fit.chebyshev_lsq_fit(n_terms, x_obs, y_obs, w_obs) fit_funct = chebyshev_polynome( n_terms, fit.low_limit, fit.high_limit, fit.coefs) y_fitted = fit_funct.f(x) y_tr = y_fitted n = y_tr.size() noise = y - y_tr noise_sq = flex.pow2(noise) from xfel.command_line.view_pixel_histograms import sliding_average #sigma_sq = sliding_average(noise_sq, n=31) sigma_sq = sliding_average(noise_sq, n=15) #sigma_sq = sliding_average(sigma_sq) #signal_to_noise = y/flex.sqrt(sigma_sq) import math signal_to_noise = y/math.sqrt(flex.mean(noise_sq[50:200])) #pyplot.plot(noise) #pyplot.plot(x,y) #pyplot.show() offset = 0.2 * flex.max(y) offset = 0 pyplot.plot(x, y, linewidth=2) pyplot.plot(x, offset+y_tr, linewidth=2) pyplot.show() pyplot.plot(x, noise, linewidth=2) #pyplot.plot(x, flex.sqrt(sigma_sq), linewidth=2) #ax2 = pyplot.twinx() #ax2.plot(x, y) pyplot.show() pyplot.plot(x[:375], signal_to_noise[:375]) #pyplot.xlim( #ax2 = pyplot.twinx() #ax2.plot(x, y) pyplot.show()
def estimate_signal_to_noise(x, y_noisy, y_smoothed, plot=False): """Estimate noise in spectra by subtracting a smoothed spectrum from the original noisy unsmoothed spectrum. See: The extraction of signal to noise values in x-ray absorption spectroscopy A. J. Dent, P. C. Stephenson, and G. N. Greaves Rev. Sci. Instrum. 63, 856 (1992); https://doi.org/10.1063/1.1142627 """ noise = y_noisy - y_smoothed noise_sq = flex.pow2(noise) from xfel.command_line.view_pixel_histograms import sliding_average sigma_sq = sliding_average(noise_sq, n=31) sigma_sq = smoothing.savitzky_golay_filter(x.as_double(), flex.pow2(noise), half_window=20, degree=1)[1] sigma_sq.set_selected(sigma_sq <= 0, flex.mean(sigma_sq)) # or do this instead to use the background region as the source of noise: #signal_to_noise = y_smoothed/math.sqrt(flex.mean(noise_sq[50:190])) signal_to_noise = y_smoothed / flex.sqrt(sigma_sq) #signal_to_noise.set_selected(x < 50, 0) #signal_to_noise.set_selected(x > 375, 0) if plot: from matplotlib import pyplot linewidth = 2 pyplot.plot(x, y_noisy, linewidth=linewidth) pyplot.plot(x, y_smoothed, linewidth=linewidth) pyplot_label_axes() pyplot.show() pyplot.plot(x, noise, linewidth=linewidth, label="noise") pyplot.plot(x, flex.sqrt(sigma_sq), linewidth=linewidth, label="sigma") pyplot_label_axes() pyplot.legend(loc=2, prop={'size': 20}) pyplot.show() pyplot.plot(x, signal_to_noise, linewidth=linewidth) pyplot_label_axes() pyplot.show() return signal_to_noise
def estimate_signal_to_noise(x, y_noisy, y_smoothed, plot=False): """Estimate noise in spectra by subtracting a smoothed spectrum from the original noisy unsmoothed spectrum. See: The extraction of signal to noise values in x-ray absorption spectroscopy A. J. Dent, P. C. Stephenson, and G. N. Greaves Rev. Sci. Instrum. 63, 856 (1992); http://dx.doi.org/10.1063/1.1142627 """ noise = y_noisy - y_smoothed noise_sq = flex.pow2(noise) from xfel.command_line.view_pixel_histograms import sliding_average sigma_sq = sliding_average(noise_sq, n=31) sigma_sq = smoothing.savitzky_golay_filter( x.as_double(), flex.pow2(noise), half_window=20, degree=1)[1] sigma_sq.set_selected(sigma_sq <= 0, flex.mean(sigma_sq)) # or do this instead to use the background region as the source of noise: #signal_to_noise = y_smoothed/math.sqrt(flex.mean(noise_sq[50:190])) signal_to_noise = y_smoothed/flex.sqrt(sigma_sq) #signal_to_noise.set_selected(x < 50, 0) #signal_to_noise.set_selected(x > 375, 0) if plot: from matplotlib import pyplot linewidth=2 pyplot.plot(x, y_noisy, linewidth=linewidth) pyplot.plot(x, y_smoothed, linewidth=linewidth) pyplot_label_axes() pyplot.show() pyplot.plot(x, noise, linewidth=linewidth, label="noise") pyplot.plot(x, flex.sqrt(sigma_sq), linewidth=linewidth, label="sigma") pyplot_label_axes() pyplot.legend(loc=2, prop={'size':20}) pyplot.show() pyplot.plot(x, signal_to_noise, linewidth=linewidth) pyplot_label_axes() pyplot.show() return signal_to_noise
def estimate_signal_to_noise(x, y): raise if 1: x, y = interpolate(x, y) #x, y_tr = fourier_filter(x, y) x, y_tr = savitzky_golay_filter(x, y) noise = y - y_tr else: from scitbx.math import chebyshev_polynome from scitbx.math import chebyshev_lsq_fit x_obs, y_obs = x, y w_obs = flex.double(y_obs.size(), 1) w_obs[0] = 1e16 w_obs[-1] = 1e16 ## determining the number of terms takes much, much longer than the fit n_terms = chebyshev_lsq_fit.cross_validate_to_determine_number_of_terms( x_obs, y_obs, w_obs, min_terms=2, max_terms=30, n_goes=20, n_free=20) #n_terms = 7 print "n_terms:", n_terms fit = chebyshev_lsq_fit.chebyshev_lsq_fit(n_terms, x_obs, y_obs, w_obs) fit_funct = chebyshev_polynome(n_terms, fit.low_limit, fit.high_limit, fit.coefs) y_fitted = fit_funct.f(x) y_tr = y_fitted n = y_tr.size() noise = y - y_tr noise_sq = flex.pow2(noise) from xfel.command_line.view_pixel_histograms import sliding_average #sigma_sq = sliding_average(noise_sq, n=31) sigma_sq = sliding_average(noise_sq, n=15) #sigma_sq = sliding_average(sigma_sq) #signal_to_noise = y/flex.sqrt(sigma_sq) import math signal_to_noise = y / math.sqrt(flex.mean(noise_sq[50:200])) #pyplot.plot(noise) #pyplot.plot(x,y) #pyplot.show() offset = 0.2 * flex.max(y) offset = 0 pyplot.plot(x, y, linewidth=2) pyplot.plot(x, offset + y_tr, linewidth=2) pyplot.show() pyplot.plot(x, noise, linewidth=2) #pyplot.plot(x, flex.sqrt(sigma_sq), linewidth=2) #ax2 = pyplot.twinx() #ax2.plot(x, y) pyplot.show() pyplot.plot(x[:375], signal_to_noise[:375]) #pyplot.xlim( #ax2 = pyplot.twinx() #ax2.plot(x, y) pyplot.show()