Skip to content

Commit

Permalink
Add test for gen_noise
Browse files Browse the repository at this point in the history
  • Loading branch information
TomDonoghue committed Jan 29, 2020
1 parent 9e2d2c6 commit 7eb93d1
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 26 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
52 changes: 26 additions & 26 deletions fooof/sim/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,32 @@ def gen_peaks(freqs, gaussian_params):
return peak_vals


def gen_noise(freqs, nlv):
"""Generate noise values for a simulated power spectrum.
Parameters
----------
freqs : 1d array
Frequency vector to create noise values from.
nlv : float
Noise level to generate.
Returns
-------
noise_vals : 1d vector
Noise values.
Notes
-----
This approach generates noise as randomly distributed white noise.
The 'level' of noise is controlled as the scale of the normal distribution.
"""

noise_vals = np.random.normal(0, nlv, len(freqs))

return noise_vals


def gen_power_vals(freqs, aperiodic_params, gaussian_params, nlv):
"""Generate power values for a simulated power spectrum.
Expand Down Expand Up @@ -262,32 +288,6 @@ def gen_power_vals(freqs, aperiodic_params, gaussian_params, nlv):
return powers


def gen_noise(freqs, nlv):
"""Generate noise values for a simulated power spectrum.
Parameters
----------
freqs : 1d array
Frequency vector to create noise values from.
nlv : float
Noise level to generate.
Returns
-------
noise_vals : 1d vector
Noise values.
Notes
-----
This approach generates noise as randomly distributed white noise.
The 'level' of noise is controlled as the scale of the normal distribution.
"""

noise_vals = np.random.normal(0, nlv, len(freqs))

return noise_vals


def gen_model(freqs, aperiodic_params, gaussian_params, return_components=False):
"""Generate a power spectrum model for a given parameter definition.
Expand Down
15 changes: 15 additions & 0 deletions fooof/tests/test_sim_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ def test_gen_peaks():
peaks = gen_peaks(xs, gaussian_params)

assert np.all(np.invert(np.isnan(peaks)))
assert xs[np.argmax(peaks)] == 10

def test_gen_noise():

xs = gen_freqs([3, 50], 0.5)

nlv = 0.1
noise = gen_noise(xs, nlv)
assert np.all(np.invert(np.isnan(noise)))
assert np.isclose(np.std(noise), nlv, 0.15)

nlv = 0.5
noise = gen_noise(xs, nlv)
assert np.all(np.invert(np.isnan(noise)))
assert np.isclose(np.std(noise), nlv, 0.15)

def test_gen_power_values():

Expand Down

0 comments on commit 7eb93d1

Please sign in to comment.