Implementing a "moving window" on an array and selecting values preceding each window

16 次查看(过去 30 天)
Hey all! I have an array which I will call as "spiketimes''. It's essentially an array consisitng of time points, from ~0.1 secs to ~4.5 secs. Now I have another array called "eyetimes", also consisting of time points. What I ultimately want to do is to select values of the "eyetimes" array based on the values of the "spiketimes" array and how I want to go about this is to implement a "moving window" of sorts.
For eg, the first window will take a window of 0.2 secs in the "spiketime" array from the start (0.0 secs) till about 0.2 secs. Based on this selection, I need to select the value of the "eyetimes" array right before this window.
The second window will now offset the starting point by 0.05 secs such that the starting point will now be at 0.05 secs and the end point will be at 0.25 secs, while keeping the same interval of 0.2 secs. Again I need to select the value of "eyetimes" before the start of the second window.
Likewise, the third window will start at 0.1 secs and end at 0.3 secs and so on...
This continues till the end of the array contents, offsetting the start of the window by 0.05 secs, while still maintaining the interval of 0.2 secs. What might be the best way to go about this? Thanks in advance!
Edited: attached .mat files for both "spiketimes" and "eyetime"
  7 个评论
Kevin Akash Rajasekaran
A spike event happens at a particular time point and is stored in the spike times array. Preceding this particular time point is an eye time event stored in the eye times array. The catch is that not all eye time points leads to a spike event. Hence the reason for selecting eye times based on spike times. Hope this helps!

请先登录,再进行评论。

回答(1 个)

VINAYAK LUHA
VINAYAK LUHA 2023-10-12
Hi Akash,
As per my understading, the use-cases you are trying to achieve are as follows: -
  • You have two sorted arrays of time values, “spiketimes” and “eyetimes”.
  • Let the parameters for your “sliding window-based algorithm be defined as-
d =>window size.
[s,t] =>start and end of the window.
r =>smallest time value in “eyetimes” array closest to “s” in “spiketimes” array.
m =>sliding offset for the window.
  • You want to find all “r”s as the window of size “d”, defined by time [s,t] slides over the “spiketimes” array with sliding offset “m”.
Implementing the sliding window algorithm for this purpose is suboptimal as you don’t require to do any computation for the window elements.
Here is an alternate more efficient approach to achieve your use-case using binary search algorithm-
  • Define “s” as 0 initially and a vector “sol” to store the solution.
  • Loop over and keep incrementing s by “d” each step till s” <spiketimes(end)”.
  • In each iteration –
Define variables a” and “b” as follows:
a =upper bound of “s” in the “spiketimes array (using a binary search variant).
b =lower bound of a” in the “eyetimes” array (using another binary search variant).
Store the values of “b” in the “sol” array.
For your better understanding, I am attaching here, the code snippet that demonstrates this approach:
load eyetime.mat;
load spiketimes.mat;
s=0;
d=0.05;
sol=zeros(1,100)
while s<=spiketimes(end)
a=upperbound(s,spiketimes);
b=lowerbound(a,eyetime);
disp(num2str(s)+" "+num2str(a)+" "+num2str(b))
s=s+d;
end
%finds a number greater than equal to “k” in the array arr.
function a=upperbound(k,arr)
startIndex = 1;
endIndex = length(arr);
while startIndex < endIndex
mid = floor((startIndex + endIndex) / 2);
if k>=arr(mid)
startIndex = mid + 1;
else
endIndex = mid;
end
end
if (startIndex<length(arr)&&arr(startIndex)<=k)
startIndex=startIndex+1;
end
a = arr(startIndex);
end
%finds a number less than “k” in array “arr”
function b=lowerbound(k,arr)
startIndex = 1;
endIndex = length(arr);
while startIndex < endIndex
mid = floor((startIndex + endIndex) / 2);
if k<=arr(mid)
endIndex = mid;
else
startIndex = mid+1;
end
end
if (startIndex<length(arr)&&arr(startIndex)<k)
startIndex=startIndex+1;
end
b = arr(startIndex-1);
end
Further, you can refer to the following articles to understand the algorithm in detail:
  • Binary search:
  • Finding lower and upper bound of an element using binary search:
I hope you find this solution useful, and this helps you find the correct set of values from theeyetimes” array, as desired.
Regards,
Vinayak Luha

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by