unittests and debug for sinusoidal stimulus

This commit is contained in:
a.ott
2020-03-03 14:27:04 +01:00
parent 04815de85c
commit 861557da0d
2 changed files with 148 additions and 1 deletions

View File

@@ -47,6 +47,40 @@ class SinusAmplitudeModulationStimulus(AbstractStimulus):
# @jit(nopython=True) # makes it slower?
def convert_to_array(carrier_freq, amplitude, modulation_freq, contrast, start_time, duration, time_start, total_time, step_size_s):
full_time = np.arange(time_start, time_start + total_time, step_size_s)
full_carrier = np.sin(2 * np.pi * carrier_freq * full_time)
if start_time > time_start+duration or start_time+duration < time_start:
return full_carrier * amplitude
else:
if start_time >= time_start:
am_start = start_time
else:
am_start = time_start
if time_start + total_time >= start_time + duration:
am_end = start_time + duration
else:
am_end = time_start + total_time
idx_start = (am_start - time_start) / step_size_s
idx_end = (am_end - time_start) / step_size_s
if idx_start != round(idx_start) or idx_end != round(idx_end):
raise ValueError("Didn't calculate integers when searching the start and end index. start:", idx_start, "end:", idx_end)
# print("am_start: {:.0f}, am_end: {:.0f}, length: {:.0f}".format(am_start, am_end, am_end-am_start))
idx_start = int(idx_start)
idx_end = int(idx_end)
am = 1 + contrast * np.sin(2 * np.pi * modulation_freq * full_time[idx_start:idx_end])
values = full_carrier * amplitude
values[idx_start:idx_end] = values[idx_start:idx_end]*am
return values
# if the whole stimulus time has the amplitude modulation just built it at once;
if time_start >= start_time and start_time+duration < time_start+total_time:
carrier = np.sin(2 * np.pi * carrier_freq * np.arange(start_time, total_time - start_time, step_size_s))
@@ -55,8 +89,9 @@ def convert_to_array(carrier_freq, amplitude, modulation_freq, contrast, start_t
return values
# if it is split into parts with and without amplitude modulation built it in parts:
values = np.array([], dtype='float32')
values = np.array([])
# there is some time before the modulation starts:
if time_start < start_time:
carrier_before_am = np.sin(2 * np.pi * carrier_freq * np.arange(time_start, start_time, step_size_s))
values = np.concatenate((values, amplitude * carrier_before_am))