In [2]:
from simulators.iam_module import prepare_iam_model_spectra, continuum_alpha, continuum, inherent_alpha_model
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [38]:
mod1_spec, mod2_spec = prepare_iam_model_spectra([5000,4.5, 0.0], [2400, 4.5, 0.0], limits=[2110, 2130],
area_scale=True, wav_scale=True)
mod1_spec.plot()
In [54]:
# Use Beta sigma method to access the noise in synthetic spectrum.
from PyAstronomy import pyasl
ti = mod1_spec.xaxis
t2 = np.linspace(ti[0],ti[-1], 1000)
gi = mod1_spec.flux / np.median(mod1_spec.flux)
noise = 0.000
yi = mod1_spec.flux / np.median(mod1_spec.flux)
y2 = np.interp(t2, ti, yi) + np.random.normal(0, noise*np.ones(1000))
nd=len(y2)
# Create class instance for equidistant sampling
bseq = pyasl.BSEqSamp()
# Specify jump parameter (j) for construction of beta sample
j = 2
# Order of approximation to use
Ns = [0,1,2,3, 4, 5, 6, 7]
# Use to store noise estimates
smads, dsmads = [], []
# Loop over orders of approximation between 0 and 3
for N in Ns:
# Get estimates of standard deviation based on robust (MAD-based) estimator
smad, dsmad = bseq.betaSigma(y2, N, j, returnMAD=True)
print("Order of approximation (N): ", N)
print(" Size of beta sample: ", len(bseq.betaSample))
print(" Robust estimate of noise std: %6.7f +/- %6.7f" % (smad, dsmad))
# Save result
smads.append(smad)
dsmads.append(dsmad)
# Plot g(t) and the synthetic data
plt.subplot(2,1,1)
plt.title("Data (top) and noise estimates (bottom)")
plt.plot(ti, gi, 'b.-', label="$g(t_i)$")
plt.errorbar(t2, y2, yerr=np.ones(nd)*noise, fmt='r+', label="$y_i$")
plt.legend()
plt.subplot(2,1,2)
plt.title("N=0 is insufficient")
plt.errorbar(Ns, smads, yerr=dsmads, fmt='k+', label="Noise estimates")
plt.plot([min(Ns)-0.5, max(Ns)+0.5], [noise]*2, 'k--', label="Input value")
plt.legend()
plt.xlabel("Order of approximation (N)")
plt.ylabel("Noise STD")
plt.tight_layout()
plt.show()
In [46]:
In [ ]: