Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Incomplete tile warning #245

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions neurodsp/sim/periodic.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ def sim_oscillation(n_seconds, fs, freq, cycle='sine', phase=0, **cycle_params):
sig : 1d array
Simulated oscillation.

Notes
-----
Depending on the requested frequency (`freq`), sampling rate (`fs`), and signal
length (`n_seconds`), the simulated signal may have a non-integer number of cycles.
If so, the frequency-domain representation of the signal will have some power in non-simulated
frequencies. To avoid this, choose `n_seconds` and `fs` to create an integer number of cycles.

Examples
--------
Simulate a continuous sinusoidal oscillation at 5 Hz:
Expand Down
30 changes: 30 additions & 0 deletions neurodsp/sim/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Utility functions for simulations."""

###################################################################################################
###################################################################################################

def check_osc_def(n_seconds, fs, freq):
"""Check whether a requested oscillation definition will have an integer number of cycles.

Parameters
----------
n_seconds : float
Simulation time, in seconds.
fs : float
Signal sampling rate, in Hz.
freq : float
Oscillation frequency.

Returns
-------
bool
Whether the definition will have an integer number of cycles.
"""

# Sampling rate check: check if the number of samples per cycle is an integer
srate_check = (fs/freq).is_integer()

# Time check: check if signal length matches an integer number of cycles
time_check = (n_seconds * freq).is_integer()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is truncation always an issue? This example seems to produce an appropriate spectrum:

n_seconds, fs, freq = 1.99, 1000, 10

print((n_seconds * freq).is_integer())

sig = sim_oscillation(n_seconds, fs, freq)
freqs, powers = compute_spectrum(sig, fs)
plot_power_spectra(freqs, powers)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, great catch!

So yeh, this time check isn't super specific. Whether the number of cycles leads to funky PSD is a bit more complex than this check - it depends on the actual spectral estimation that is done (for example, with Welchs, I think it depends on the segment size (nperseg), and how this relates to the signal, more than the details of the total signal. Because of this, I think we should probably just entirely drop this time check, since I otherwise don't really know how to make it more specific (we can't predict the spectral estimation people will do - and this emphasizes that the details of the resulting PSD are somewhat contextual - not fully determined by the sim'd time series).


return srate_check and time_check
24 changes: 24 additions & 0 deletions neurodsp/tests/sim/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Tests for neurodsp.sim.utils."""

from neurodsp.sim.utils import *

###################################################################################################
###################################################################################################

def test_check_osc_def():

n_seconds = 1.0
fs = 100
freq = 10

# Check definition that should pass
assert check_osc_def(n_seconds, fs, freq)

# Check a definition that should fail the sampling rate check
assert not check_osc_def(1.05, fs, freq)

# Check a definition that should fail the time check
assert not check_osc_def(n_seconds, 1111, freq)

# Check a definition that should fail both checks
assert not check_osc_def(1.05, 1111, freq)