sliding window algorithm for time-series data

I have sample data and sampling frequency . Sample data points are 27900 and sampling frequency is 600 hz . I want to apply slidding window concept for my data. I want divide all data into set of 5 sec each with overlap of 4 sec. (i.e. 0-5, 1-6, 2-7, etc.). Please help me to apply slidding window concept.

 Accepted Answer

HELLO
see demo below - second section implement a overlap method
all the best
clc
clearvars
% dummy data
data = rand(320,3); % data must be column oriented (number of rows = number of samples)
buffer = 25; % nb of samples for averaging
%% zero overlap averaging (unweighted)
[m,n] = size(data);
for ci=1:fix(m/buffer)
start_index = 1+(ci-1)*buffer;
stop_index = min(start_index+ buffer-1,m);
time_index(ci) = round((start_index+stop_index)/2); % time index expressed as sample unit (dt = 1 in this simulation)
avg_data(ci,:) =mean(data(start_index:stop_index,:)); %
end
figure(1),
plot(time_index,avg_data,'+-');
% return
%% averaging with overlap
clearvars
% dummy data
data = rand(320,3);
buffer = 25; % nb of samples in one buffer (buffer size)
overlap = 10; % overlap expressed in samples
%%%% main loop %%%%
[m,n] = size(data);
shift = buffer-overlap; % nb of samples between 2 contiguous buffers
for ci=1:fix((m-buffer)/shift +1)
start_index = 1+(ci-1)*shift;
stop_index = min(start_index+ buffer-1,m);
time_index(ci) = round((start_index+stop_index)/2); % time index expressed as sample unit (dt = 1 in this simulation)
avg_data(ci,:) = mean(data(start_index:stop_index,:)); %
end
figure(2),
plot(time_index,avg_data,'+-');

8 Comments

@Mathieu NOE Thanks sir. Can you please explain me (time_index(ci)) code line. Because I have frequency and number of samples from which I can create time step coloun. So can I use that Time step coloum in this code.
Thank you
hello Sameer
you are speaking about the time vector which is associated with your raw data (sampled at 600 Hz)
when I do buffer averaging , the output data have a different time spacing (increment) which depends on the buffer and overlap :
in my code, each buffer of data is associated with the time index around the middle of the buffer (for example , if my buffer has 11 samples , then I associate this buffer with the 6th sample (center position of the buffer) , then I do the buffer averaging and this value is associated with the 6th sample time index
hope this answer your question
That looks nice , have you another question ?
% clear window
clc;
clear all;
% % import xls or csv file
axial_Depth_1 = xlsread('E:\SIT\Datasets\TOOL WEAR DATASET OF NUAA_IDEAHOUSE\+-¦½-²+¦\Data collection of variable cutting parameters\Variable axial cutting depth\1');
%fz: feed per tooth= 0.03 ; n: spindle speed= 1200; ap: axial cutting depth=1; ae: radial cutting depth=3.0
%extract data in respective sensors sensors
Axial_cutting_force= axial_Depth_1 (:,1:1);
[m,n] = size(Axial_cutting_force);
buffer = 600;
for ci=1:fix(m/buffer)
start_index = 1+(ci-1)*buffer;
stop_index = min(start_index+ buffer-1,m);
time_index(ci) = round((start_index+stop_index)/(2*buffer)); % time index expressed as sample unit
% 1. mean (Average)
Mean_Axial_cutting_force(ci,:) = mean(Axial_cutting_force(start_index:stop_index,:)); %
% 2. rms (Root mean square), Value that generally tends to get bigger as the degree of fault increases
RMS_Axial_cutting_force(ci,:) = rms(Axial_cutting_force(start_index:stop_index,:));
% 3. std(x)(Standard Deviation)
std_Axial_cutting_force(ci,:) = std(Axial_cutting_force(start_index:stop_index,:));
%4. Peak (Maximum value of signal absolute value)
Peak_Axial_cutting_force(ci,:) = max(Axial_cutting_force(start_index:stop_index,:));
%5. Skewness (The asymmetry of the probability density function of the signal)
Skewness_Axial_cutting_force(ci,:) = skewness(Axial_cutting_force(start_index:stop_index,:));
%6. Kurtosis (The sharpness of the probability distribution of the signal)
kurtosis_Axial_cutting_force(ci,:) = kurtosis(Axial_cutting_force(start_index:stop_index,:));
%7. Crest factor (The ratio of peak values to the RMS of a signal)
CF_Axial_cutting_force(ci,:) = Peak_Axial_cutting_force(ci,:)/RMS_Axial_cutting_force(ci,:);
%8. Clearance factor (Peak value divided by the square of root mean)
CL_Axial_cutting_force(ci,:)= (Peak_Axial_cutting_force(ci,:)/(RMS_Axial_cutting_force(ci,:))^2);
%9.Shape factor (RMS divided by mean)
SF_Axial_cutting_force(ci,:) = (RMS_Axial_cutting_force(ci,:)/ Peak_Axial_cutting_force(ci,:));
%10. Impulse factor (The ratio of peak values to the mean of a signal)
IF_Axial_cutting_force(ci,:)= (Peak_Axial_cutting_force(ci,:)/ Mean_Axial_cutting_force(ci,:));
%11. Peak to peak (The difference between maximum and minimum values of the signal)
Max_Axial_cutting_force (ci,:)= max(Axial_cutting_force(start_index:stop_index,:));
Min_Axial_cutting_force (ci,:)= min(Axial_cutting_force(start_index:stop_index,:));
P2P_Axial_cutting_force(ci,:)= (Max_Axial_cutting_force (ci,:)- Min_Axial_cutting_force (ci,:));
end
figure(1),
subplot (3,4,1)
plot(time_index,Mean_Axial_cutting_force,'+-', 'color','r');
title('Mean Axial Cutting Force'); grid on
xlabel('Time (Sec)')
ylabel('Mean Axial force in N')
subplot (3,4,2)
plot(time_index,RMS_Axial_cutting_force,'+-','color','b');
title('RMS Axial cutting force'); grid on
xlabel('Time(Sec)')
ylabel('RMS Axial cutting force in N')
subplot (3,4,3)
plot(time_index,std_Axial_cutting_force,'+-','color','g');
title('Std. Dev. Axial cutting force'); grid on
xlabel('Time(Sec)')
ylabel('std axial cutting force in N')
subplot (3,4,4)
plot(time_index,Peak_Axial_cutting_force,'+-','color','m');
title('Peak Axial cutting force'); grid on
xlabel('Time(Sec)')
ylabel('Peak Axial cutting force in N')
subplot (3,4,5)
plot(time_index,Skewness_Axial_cutting_force,'+-','color','r');
title('Skewness Axial cutting force'); grid on
xlabel('Time(Sec)')
ylabel('Skewness Axial cutting force in N')
subplot (3,4,6)
plot(time_index,kurtosis_Axial_cutting_force,'+-','color','b');
title('kurtosis Axial cutting force'); grid on
xlabel('Time(Sec)')
ylabel('kurtosis Axial cutting force in N')
subplot (3,4,7)
plot(time_index,CF_Axial_cutting_force,'+-','color','g');
title('Crest factor of Axial cutting force'); grid on
xlabel('Time(Sec)')
ylabel('CF of Axial cutting force in N')
subplot (3,4,8)
plot(time_index,CL_Axial_cutting_force,'+-','color','m');
title('Clearance factor of Axial cutting force'); grid on
xlabel('Time(Sec)')
ylabel('CL of Axial cutting force in N')
subplot (3,4,9)
plot(time_index,SF_Axial_cutting_force,'+-','color','r');
title('Shape factor of Axial cutting force'); grid on
xlabel('Time(Sec)')
ylabel('SF of Axial cutting force in N')
subplot (3,4,10)
plot(time_index,IF_Axial_cutting_force,'+-','color','b');
title('Impulse factor of Axial cutting force'); grid on
xlabel('Time(Sec)')
ylabel('IF of Axial cutting force in N')
subplot (3,4,11)
plot(time_index,P2P_Axial_cutting_force,'+-','color','g');
title('Peak to Peak value of Axial cutting force'); grid on
xlabel('Time(Sec)')
ylabel('P2P of Axial cutting force in N')
% return
Is it correct?
time_index(ci) = round((start_index+stop_index)/(2*buffer));
yes
maybe I should emphasize that this is an index , means it about samples , not time is seconds
if you want to have it in seconds divide time_index by your sampling rate
Okay. Thank you so much sir.
This was very helpfull to me also. Thanks alot.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!