Writing a loop to compute Indices
22 次查看(过去 30 天)
显示 更早的评论
Please help me to write a loop to compute the indices (starting and stopping) – . Assume that your time history has 5,000 points. Use a record length of 2^10 = 1,024 points. List both sets of indices, preferably in a matrix
a. For the case of 0% overlap
b. For the case of 50% overlap
0 个评论
回答(2 个)
Mathieu NOE
2025-4-7,10:19
hello
see basic demo below
select which option you want
overlap = 0*buffer_size; % overlap expressed in samples (a. For the case of 0% overlap)
% overlap = 0.5*buffer_size; % overlap expressed in samples (b. For the case of 50% overlap)
% dummy data (a noisy sinusoid)
% NB : data must be column oriented
n=5000;
t= (1:n)';
y = sin(2*pi*t./max(t))+0.1*rand(size(t));
% choose the amount of overlap
buffer_size = 1024;
overlap = 0*buffer_size; % overlap expressed in samples (a. For the case of 0% overlap)
% overlap = 0.5*buffer_size; % overlap expressed in samples (b. For the case of 50% overlap)
%%%% main loop %%%%
[tt,yy,start_index,stop_index] = my_movmean(t,y,buffer_size,overlap);
plot(t,y,tt,yy,'*-r');
title('data');
legend('raw data','1024 samples mean');
xlabel('Time(s)');
% display start_index,stop_index in command window
start_index
stop_index
%%%%%%%%%% my functions %%%%%%%%%%%%%%
function [new_time,data_out,start_index,stop_index] = my_movmean(t,data_in,buffer_size,overlap)
% NB : buffer size and overlap are integer numbers (samples)
% data (in , out) are 1D arrays (vectors)
shift = buffer_size-overlap; % nb of samples between 2 contiguous buffers
[samples,~] = size(data_in);
nb_of_loops = fix((samples-buffer_size)/shift +1);
for k=1:nb_of_loops
start_index(k) = 1+(k-1)*shift;
stop_index(k) = min(start_index(k)+ buffer_size-1,samples);
x_index(k) = round((start_index(k)+stop_index(k))/2);
data_out(k,:) = mean(data_in(start_index(k):stop_index(k),:),1,'omitnan'); %
end
new_time = t(x_index); % time values are computed at the center of the buffer
end
0 个评论
Maneet Kaur Bagga
2025-4-9,4:10
Hi,
As per my understanding, you have given a signal of length 5000 and a window length of 1024. You want to compute the start and stop indices of segments for 0% and 50% overlap.
You can refer to the following MathWorks documentation to segment time series using "pwelch" function.
The segmenting data involves defining a hop size for which you can refer to the following code snippet:
% Parameters
N = 5000; % Total number of points
L = 1024; % Record length
overlap_0 = 0; % Overlap for case (a)
overlap_50 = 512; % 50% overlap for case (b)
% Function to calculate indices
function idx_matrix = compute_indices(N, L, overlap)
hop = L - overlap;
start_idx = 1:hop:(N - L + 1); % Start indices of each segment
stop_idx = start_idx + L - 1; % End indices of each segment
idx_matrix = [start_idx.', stop_idx.']; % Nx2 matrix
end
Hope this helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!