Implementing a "moving window" on an array and selecting values preceding each window
16 views (last 30 days)
Show older comments
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 Comments
Catalytic
on 12 Jul 2021
Please do not make us download multiple .mat files. Put all your variables in one .mat file.
Answers (1)
VINAYAK LUHA
on 12 Oct 2023
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 the “eyetimes” array, as desired.
Regards,
Vinayak Luha
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!