Segment data into overlapping windows
12 views (last 30 days)
Show older comments
Panos Kerezoudis
on 19 Dec 2023
Commented: William Rose
on 20 Dec 2023
Hi!
I have a vector of time series data (1x300), that I would like to segment into 20 segments that are 100 ms in duration and have 90% overlap. I tried the buffer function, but the indices in the first segments start with zeros, which is not desirable. I would like the first epoch to start from the first time point and go from there. Here is a screenshot from the figure in the paper where the authors applied the method (Foster, Neuron, 2015). thank you in advance!
0 Comments
Accepted Answer
William Rose
on 19 Dec 2023
@Panos Kerezoudis, you did not specify the sampling rate. You said you have time series data (1x300).
I assume 300 is the number of points.
You said you want 20 segments that are 100 ms long with 90% overlap. This means the segments overlap by 90 ms. This means you can fit 21 segments onto 300 ms of data.
Let us assume the sampling rate is 1 kHz.
%% Generate simulated data
fs=1000; % sampling rate (Hz
N=300; % signal length (points)
t=(0:N-1)/fs; % time vector (s)
y=cos(2*pi*20*t); % 20 Hz sinusoidal signal, for demonstration purposes
%% Prepare to segment the data
tseg=0.100; % segment duration (s)
nseg=tseg*fs; % segment length (points)
noverlap=0.9*nseg; %overlap (point)
noffset=nseg-noverlap; % offset (points)
K=floor(1+(N-nseg)/noffset); % number of segments
fprintf('N=%d, nseg=%d, noffset=%d, number of segments=%d.\n',N,nseg,noffset,K)
yseg=zeros(K,nseg); % allocate arrays for time and y for each segment
tseg=zeros(K,nseg);
%% Segment the data
for i=1:K
tseg(i,:)=t(1+(i-1)*noffset:(i-1)*noffset+nseg);
yseg(i,:)=y(1+(i-1)*noffset:(i-1)*noffset+nseg);
end
%% Plot all segments, with vertical offsets
figure;
for i=1:K
plot(tseg(i,:),yseg(i,:)+(i-1)/5,'-r')
hold on
end
grid on; xlabel('Time (s)')
Looks decent.
Good luck with your research.
3 Comments
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!