2017-02-13 69 views
0

我感興趣的是發現信號發作,定義爲信號偏離基線(即基線平均值)並且永不回來的點。下面的代碼發現信號F中的起始點,作爲F的鄰近元素的最近的負差異。然而,由於信號F中的一些噪聲(我不想過濾它),可能比期望的(視覺上)檢測到起始點。如何改進包含多個if語句的以下語法?

在信號F的以下示例中,「視覺上正確的」發病將位於點944。然而,以下描述上述方法中,首先設定在點1001

的後半部分包含若干if語句的代碼糾正回去並搜索包含在所需F範圍(在本例中爲平均值(基線))中的最新idx。

這些if語句如何改進,以便它在idx向量中一直回退,直到F(發生)< = mean_baseline爲TRUE?

採樣信號F.mat可以在這裏找到:https://www.dropbox.com/s/r4t8nzz4s7t9x4e/F.mat?dl=0

Fs = 1926; % sampling frequency 
ff = diff(F); 
idx = find(ff < 0); 
onset = idx(end); % here 

% Take a chunk of F prior to "onset": baseline 
Fs = 1926; % sampling frequency 
baseline = F(1:onset-(Fs*0.15)); % create baseline as a chunk of F, 150ms before the onset found above 
mean_baseline = mean(baseline); % we create this variable to be used later as condition in the if statements 

% we are interested in indexing the latest negative idx (idx < 0) BUT only if the corresponding F value is equeal or lower than the desired value (in this case mean_baseline) 
if F(onset) > mean_baseline 
    idx = find(ff < 0); 
    onset = idx(end-1); 
    if F(onset) > mean_baseline 
     idx = find(ff < 0); 
     onset = idx(end-2); 
     if F(onset) > mean_baseline 
      idx = find(ff < 0); 
      onset = idx(end-3); 
      if F(onset) > mean_baseline 
       idx = find(ff < 0); 
       onset = idx(end-4); 
       if F(onset) > mean_baseline 
        idx = find(ff < 0); 
        onset = idx(end-5); 
        if F(onset) > mean_baseline 
         idx = find(ff < 0); 
         onset = idx(end-6); 
         if F(onset) > mean_baseline 
          idx = find(ff < 0); 
          onset = idx(end-7); 
          if F(onset) > mean_baseline 
           idx = find(ff < 0); 
           onset = idx(end-8); 
          end 
         end 
        end 
       end 
      end 
     end 
    end 
end 
+2

當你說「向量化」你真的是[矢量化(https://mathworks.com/help/matlab/matlab_prog/vectorization.html),或者是你使用的術語的同義詞「語法改進「或」更智能的代碼「? – codeaviator

+1

沒有描述這個代碼應該做什麼,我們甚至不能自己運行它,因爲我們不知道'ini'是什麼。我們所能做的就是猜測。 – beaker

+0

我對此表示歉意。我編輯了這個問題,並建立了一個更清晰的例子,希望能讓你測試和理解我正在努力實現的目標。乾杯。 – AJMA

回答

1

我無法測試的代碼,你,我不知道所有的變量,但也許這樣的事情可以做:

for ii = 1:size(ONSETS,2) 
    baseline = F(ONSETS(ii)-500:ONSETS(ii)-50); 
    max_baseline = max(baseline); 

    idx = find(ini(:,ii) < 0); 
    idxWorking = length(idx); 
    while idxWorking > 0 & F(idx(idxWorking)) > max_baseline 
     idxWorking = idxWorking-1; 
    end 
    ONSETS(1,ii) = idxWorking; 
end 

或者,您可以將函數F應用於所有idx的值,並選取滿足條件的最後一個值。

for ii = 1:size(ONSETS,2) 
    baseline = F(ONSETS(ii)-500:ONSETS(ii)-50); 
    max_baseline = max(baseline); 

    idx = find(ini(:,ii) < 0); 
    Fvalues = F(idx); 
    ONSETS(1,ii) = idx(find(Fvalues<max_baseline,1,'last')); 
end 
+0

我編輯了我的問題,以更好地描述我在找什麼,但你的第二個替代方案正常工作!謝謝! – AJMA