Segment data into overlapping windows

7 次查看(过去 30 天)
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!

采纳的回答

William Rose
William Rose 2023-12-19
@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)
N=300, nseg=100, noffset=10, number of segments=21.
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 个评论
Panos Kerezoudis
Panos Kerezoudis 2023-12-19
awesome thank you so much for all your help! Will let you know if I run into any issues.
Panos

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Time Series Events 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by