Skip to content

Commit

Permalink
Ability to add noise to spectrum with a given snr value.
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-neal committed Mar 24, 2017
1 parent 64da77e commit 537b754
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions spectrum_overload/Spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ def wav_select(self, wav_min, wav_max):
self.xaxis = x_org
raise

def add_noise(self, snr):
"""Add noise level of snr to the flux of the spectrum."""
sigma = self.flux / snr
# Add normal distributed noise at the SNR level.
self.flux += np.random.normal(0, sigma)

def doppler_shift(self, RV):
"""Function to compute a wavelength shift due to radial velocity.
Expand Down
9 changes: 9 additions & 0 deletions spectrum_overload/test/test_Spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,12 @@ def test_spline_interpolation_when_given_a_ndarray():


# test_doppler_shift_with_hypothesis()
@pytest.mark.parametrize('snr', [50, 100, 200, 1000])
def test_add_noise(snr):
"""Test addition of noise."""
x = np.linspace(2000, 2200, 10000000)
y = np.ones_like(x)
spec = Spectrum(xaxis=x, flux=y)
spec.add_noise(snr)

assert np.isclose(np.std(spec.flux), 1. / snr, atol=1e-5)

0 comments on commit 537b754

Please sign in to comment.