【问题标题】:How do I create a sliding window using a for loop?如何使用 for 循环创建滑动窗口?
【发布时间】:2020-11-21 06:54:50
【问题描述】:

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

在 10 秒期间(从 20 到 30 秒),我想记录大于或等于指定阈值的神经数据点的数量。我想按照 0.001s 的 bin 来做。

我正在使用 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。如有任何有关解决此问题的反馈,我将不胜感激。

非常感谢。

【问题讨论】:

  • 欢迎来到本站!要获得帮助,请包含一个可以运行的代码的最小示例(定义所有变量),并定义预期输出

标签: matlab for-loop sliding-window


【解决方案1】:

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

% 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 进行列索引

【讨论】:

  • 谢谢,我已经尝试过了,但我收到错误消息:位置 2 的索引超出数组边界(不得超过 1)。
  • 我基本上希望我的数据每 24 个样本或 1 个 bin(即 1:24、25:48、48:72....9976:10000)移动一次,并将数据元素的数量相加每个 >= 阈值的 bin。这就是我正在尝试做的事情,但我在做这件事时遇到了麻烦。
  • 谢谢!对此,我真的非常感激。它做我想做的事,除了我最终得到一个 10172 x 16 的 mua 矩阵,当我在我的数据上运行代码时它应该是 10000(或 10001,因为总和行)x 16。我正在查看 0.001 秒的箱(本质上是 24 个数据元素),鉴于我专注于 10 秒,我最终应该得到一个 10000 x 16 矩阵。我拥有的数据矩阵(长 10 秒)是 244141 x 16。
  • 那么 24 个元素是 1 毫秒?
  • 我的采样率为 24414 Hz,我正在根据 0.001 秒的窗口计算 mua,所以从技术上讲,每 0.001 秒有 24.414 个元素,但我不得不四舍五入这个数字,导致每 0.001 有 24 个元素s。对于我的数据,我将 24 个元素视为 1 毫秒。
猜你喜欢
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
  • 2020-07-26
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
  • 2014-05-04
  • 1970-01-01
相关资源
最近更新 更多