From 4db4e5f899d94c81632b1d9c4c3f60e3d42f5225 Mon Sep 17 00:00:00 2001
From: mbergmann <maximilian.bergmann@student.uni-tuebingen.de>
Date: Tue, 22 Oct 2024 15:43:27 +0200
Subject: [PATCH] updated functions for easier power spectrum computation

---
 code/useful_functions.py | 45 +++++++++++++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 10 deletions(-)

diff --git a/code/useful_functions.py b/code/useful_functions.py
index eb750e8..f94e59e 100644
--- a/code/useful_functions.py
+++ b/code/useful_functions.py
@@ -113,18 +113,14 @@ def firing_rate(binary_spikes, dt = 0.000025, box_width = 0.01):
     rate = np.convolve(binary_spikes, box, mode = 'same') 
     return rate
 
-def power_spectrum(spike_times, duration, dt):
+def power_spectrum(stimulus):
     '''
-    Computes a power spectrum based on the spike times
+    Computes a power spectrum based from a stimulus
 
     Parameters
     ----------
-    spike_times : np.array
-        The spike times.
-    duration : float
-        The trial duration:
-    dt : float
-        The temporal resolution.
+    stimulus : Stimulus object or rlxnix.base.repro module
+        The stimulus from which the data is needed.
 
     Returns
     -------
@@ -134,8 +130,9 @@ def power_spectrum(spike_times, duration, dt):
         Power of the frequencies calculated.
 
     '''
+    spikes, duration, dt = spike_times(stimulus)
     # binarizes spikes
-    binary = binary_spikes(spike_times, duration, dt)
+    binary = binary_spikes(spikes, duration, dt)
     # computes firing rates
     rate = firing_rate(binary, dt = dt)
     # creates power spectrum
@@ -225,4 +222,32 @@ def sam_data(sam):
     sam_eodf = np.mean(eodfs)
     sam_nyquist = np.mean(ny_freqs)
     sam_stim = np.mean(stim_freqs)
-    return sam_amp, sam_am,sam_df, sam_eodf, sam_nyquist, sam_stim
\ No newline at end of file
+    return sam_amp, sam_am,sam_df, sam_eodf, sam_nyquist, sam_stim
+
+def spike_times(stim):
+    """
+    Reads out the spike times and other necessary parameters
+
+    Parameters
+    ----------
+    stim : Stimulus object or rlxnix.base.repro module
+        The stimulus from which the spike times should be calculated.
+
+    Returns
+    -------
+    spike_times : np.array
+        The spike times of the stimulus.
+    stim_dur : float
+        The duration of the stimulus.
+    dt : float
+        Time interval between two data points.
+
+    """
+    # reads out the spike times
+    spike_times, _ = stim.trace_data('Spikes-1')
+    # reads out the duration
+    stim_dur = stim.duration
+    # get the stimulus interval
+    ti = stim.trace_info("V-1")
+    dt = ti.sampling_interval  
+    return spike_times, stim_dur, dt
\ No newline at end of file