Reputation: 1194
I am interested in finding the signal onset, defined as the point at which the signal deviates from the baseline (i.e. mean of baseline) and never comes back. The following code finds onset in signal F, as the latest negative difference of adjacents elements of F. However, due to some noise in signal F (I do not want to filter it), onset may be detected later than expected (visually).
In the following example of signal F, the "visually right" onset would be located at point 944. However, following the method above described, it is first set at point 1001.
The later part of the code, comprising several if statements, corrects back and search for the latest idx that is comprised in the desired F range (in this case mean(baseline)).
How could these if statements be improved, so that it keeps going back one by one in idx vector until F(onset) <= mean_baseline is TRUE?
The sample signal F.mat can be found here: 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
Upvotes: 0
Views: 55
Reputation: 1264
I can not test the code as you I do not know all the variable but maybe something like this can be done:
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
Alternatively you could apply the function F
to all value of idx
and just pick the last value that satisfies the condition.
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
Upvotes: 1