如何使用 for 循环创建滑动 window?

How do I create a sliding window using a for loop?

我收集了 16 个不同渠道的神经数据。此数据是在 30 秒内记录的。

在 10 秒的时间内(从 20 到 30 秒),我想记录大于或等于指定阈值的神经数据点的数量。我想根据 0.001s 的 bins 来做这个。

我正在使用 MATLAB 2019b。

到目前为止我的代码如下所示:

t1 = 20;
t2 = 30;

ind1 = find(tim_trl>=t1, 1);
ind2 = find(tim_trl>=t2, 1);

time1 = tim_trl(ind1:ind2); %10s window

sampRate = 24414; %sampling freq (Hz), samples per sec
muaWindow = 0.001; %1ms window

binWidth = round(muaWindow*sampRate); %samples per 1ms window
threshold = 0.018;
    
    for jj = 1:16 %ch
        
        data = AbData(ind1:ind2, jj); %10 sec of data
   
        for kk = 1:10000 
            
            abDataBin = data(1:binWidth,jj); %data in 1 bin
            dataThreshold = find(abDataBin >= threshold); %find data points >= threshold
            mua(kk,jj) = sum(dataThreshold); %number of data pts over threshold per ch
           
        end
        
    end

到目前为止,我在这一点上遇到了一些麻烦:

abDataBin = data(1:binWidth,jj); %data in 1 bin

当我 运行 循环时,bin 1 中的数据被覆盖,而不是转移到 bin 2、3...10000。对于解决此问题的任何反馈,我将不胜感激。

非常感谢。

您忘记使用 运行 变量作为索引来访问您的数据。尝试

% create data with 16 channels
AbData = rand(10000,16);

binWidth = 24;
threshold = 0.001;

for channel=1:16
    
    data = AbData(2001:3000,channel);   
    counter = 1;  % needed for mua indexing

    % looping over the bin starting indeces
    for window=1:binWidth:length(data)-(binWidth)
        % access data in one bin
        bindata = data(window:window+binWidth);
        % calculate ms above threshold
        mua(counter, channel) = sum(bindata >= threshold);
         
        counter = counter+1;  
    end
end

编辑: 您的 data 变量的维度为 nx1,因此不需要使用 jj

进行列索引