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] Complete last cycle of bursts #275

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions neurodsp/sim/periodic.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,21 @@ def make_bursts(n_seconds, fs, is_oscillating, cycle):
n_samples_cycle = len(cycle)

burst_sig = np.zeros([n_samples])
for sig_ind, is_osc in zip(range(0, n_samples, n_samples_cycle), is_oscillating):

for ind, sig_ind in enumerate(range(0, n_samples, n_samples_cycle)):

is_osc = is_oscillating[ind]

# If set as an oscillating cycle, add cycle to signal
# The sample check is to check there are enough samples left to add a full cycle
# If there are not, this skipps the add, leaving zeros instead of adding part of a cycle
if is_osc and sig_ind + n_samples_cycle < n_samples:
if is_osc and sig_ind + n_samples_cycle + 1 < n_samples:
burst_sig[sig_ind:sig_ind+n_samples_cycle] = cycle

# Complete the last cycle of a burst
if is_osc and ind < len(is_oscillating) - 1 and not is_oscillating[ind+1]:
burst_sig[sig_ind+n_samples_cycle] = cycle[0]

return burst_sig


Expand Down
5 changes: 4 additions & 1 deletion neurodsp/tests/sim/test_periodic.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,14 @@ def test_sim_damped_oscillation():
def test_make_bursts():

is_osc = np.array([False, False, True, True, False, True, False, True, True, False])
cycle = np.ones([10])
cycle = np.ones([100])

sig = make_bursts(N_SECONDS, FS, is_osc, cycle)
check_sim_output(sig)

# Three is added since each of the three continuous bursts are fully completed
assert sig.sum() == (is_osc.sum() * 100) + 3

# Test make bursts with uneven division of signal and cycle divisions
# In this test, there aren't enough samples in the signal to add last cycle
is_osc = np.array([False, True, True])
Expand Down