def main(): """Visually test the addition of Noise using add_noise function.""" org_flux = np.ones(500) for i, snr in enumerate([50, 100, 200, 300, 50000]): # Test that the standard deviation of the noise is close to the snr level print("Applying a snr of {}".format(snr)) noisey_flux = add_noise(org_flux, snr) spec = Spectrum( flux=copy.copy(org_flux)) # Copy becasue org_flux is mutable. spec.add_noise(snr) std = np.std(noisey_flux) print("Standard deviation of signal = {}".format(std)) snr_est = 1 / std snr_spectrum = 1. / np.std(spec.flux) print( "Estimated SNR from stddev = {}".format(snr_est)) print("Estimated SNR from stddev of Spectrum class = {}".format( snr_spectrum)) plt.plot(noisey_flux + 0.1 * i, label="snr={}".format(snr)) plt.plot(spec.flux + 0.1 * i, "--", label="Spectrum snr={}".format(snr)) # Calculate chisqr from one chi2 = np.sum((noisey_flux - 1)**2 / 1**2) print("Chisqr for SNR {} = \t\t{}".format(snr, chi2)) chi2_spectrum = np.sum((spec.flux - 1)**2 / 1**2) print("Chisqr for snr_spectrum {} = \t\t{}".format(snr, chi2_spectrum)) plt.legend() plt.show()
def fake_obs(simnum, snr=200, suffix=None, plot=False, mode="iam"): snr = 200 params1 = [5300, 4.5, 0.0] params2 = [2500, 4.5, 0.0] gamma = 5 rv = -3 normalization_limits = [2100, 2180] mod1_spec = load_starfish_spectrum(params1, limits=normalization_limits, hdr=True, normalize=False, area_scale=True, flux_rescale=True) mod2_spec = load_starfish_spectrum(params2, limits=normalization_limits, hdr=True, normalize=False, area_scale=True, flux_rescale=True) if broadcast: broadcast_result = inherent_alpha_model(mod1_spec.xaxis, mod1_spec.flux, mod2_spec.flux, rvs=rv, gammas=gamma) broadcast_values = broadcast_result(obs_spec.xaxis) result_spectrum = Spectrum(flux=broadcast_values, xaxis=mod1_spec.xaxis) else: # Manually redo the join mod2_spec.doppler_shift(rv) mod_combine = mod1_spec.copy() mod_combine += mod2_spec mod_combine.doppler_shift(gamma) mod_combine.normalize(method="exponential") mod_combine.interpolate(obs_spec.xaxis) result_spectrum = mod_combine # result_spectrum = Spectrum(flux=broadcast_values, xaxis=obs_spec.xaxis) result_spectrum.add_noise(snr) # Save as # Detector limits dect_limits = [(2112, 2123), (2127, 2137), (2141, 2151), (2155, 2165)] for ii, dect in enumerate(dect_limits): spec = result_spectrum.copy() spec.wav_select(*dect) spec.resample(1024) name = "HDsim-{0}_{1}_snr_{2}".format(sim_num, ii, snr)