How do I iterate through a line while skipping a set number of values from the beginning of the line?

1 view (last 30 days)
% Hello, I'm given a data set of EMG signals. I created a running average mean line for the signal defined by variable "y". I want to in a way be able to iterate through each value along the average mean line y, but skipping say the first 50 values at the beginning of the line. This is because what I am trying to do is detect a drop in the average mean line y when it crosses the threshold "j" in this case. But if the starting average mean line y always start at a value of 0, it will always present itself in the elseif case of "concussion".
% MATLAB Code
% EMG = Trial1
function [dMean, dMax, dMin] = EMGFilter6(EMG)
EMG_2 = EMG;
t = EMG_2(:,1); % time
x = EMG_2(:,2); % signal
d = abs(x);
windowSize = 1000;
b = (1/windowSize)*ones(1,windowSize);
a = 1;
dMean = mean(d);
dMax = max(d);
dMin = min(d);
j = 0.01*dMean;
p = 'concussion';
l = 'no concussion';
y = filter(b,a,d);
for k = i:y
plot(t,d,'color','r')
hold on
plot(t,y,'color','b')
legend('Input Data','Filtered Data')
end
for g = i:y
if g>j
disp(l)
elseif g<j
disp(p)
elseif g == j
disp(l)
end
end
end
  1 Comment
Hendrik-Jan Kuijt
Hendrik-Jan Kuijt on 3 Apr 2022
Edited: Hendrik-Jan Kuijt on 3 Apr 2022
I think you can do this with the movmean() function. I think you can use the enpoints option and set it to 'discard' so that you only get results if there is 50 samples of 'y' in the average.
More documentation here: https://www.mathworks.com/help/matlab/ref/movmean.html

Sign in to comment.

Answers (1)

Mathieu NOE
Mathieu NOE on 4 Apr 2022
hello
maybe this code could be of some help (to extract snipsets of data based on threshold comparison with running rms value of the signal)
each snipset of length above min_contiguous_samples and of rms amplitude above threshold is stored in cell array data_store (the colored individual segement in bottom picture) :
code :
%% load data
load data.mat
time = Trial1(:,1);
data = Trial1(:,2);
samples = length(data);
dt = mean(diff(time));
%% parameters
min_contiguous_samples = 1000; % store "red" segments only if they are at least this length
threshold = 0.05; % 1 = max (100%) of rms value
samples_befor = 100; % add some samples before the "start" point detected by code below
samples_after = 100; % add some samples after the "end" point detected by code below
%% main loop %%%%
% running rms (buffered) parameters :
rms_buffer = 500; % nb of samples in one buffer (buffer size)
shift = 1; % = rms_buffer-overlap; % nb of samples between 2 contiguous buffers
for ci=1:fix((samples-rms_buffer)/shift +1)
start_index = 1+(ci-1)*shift;
stop_index = min(start_index+ rms_buffer-1,samples);
time_index(ci) = round((start_index+stop_index)/2); % time index expressed as sample unit (dt = 1 in this simulation)
rms_data(ci) = my_rms(data(start_index:stop_index)); %
end
time_rms = time(time_index);
ind = (rms_data>threshold*max(rms_data));
% now define start en end point of "red" segments
[begin,ends] = find_start_end_group(ind);
length_ind = ends - begin;
ind2= length_ind>min_contiguous_samples; % check if their length is valid (above min_contiguous_samples value)
begin = begin(ind2); % selected points
ends = ends(ind2); % selected points
% define for plot the red rms data
time2 = time_rms(ind);
rms_data2 = rms_data(ind);
% define the begin / ending x, y values of raw data
time2_begin = time_rms(begin);
data_begin = interp1(time,data,time2_begin);
time2_ends = time_rms(ends);
data_ends = interp1(time,data,time2_ends);
figure(1),
plot(time,data,'k',time2,rms_data2,'r.',time2_begin,data_begin,'*c',time2_ends,data_ends,'*m','MarkerSize',12);
legend('signal',['rms above ' num2str(threshold*100) ' %'] ,'begin points','end points');
% store each "red" segment separately (in cell array)
figure(2), hold on
for ci = 1:length(begin)
ind = (time>=time2_begin(ci)-samples_befor*dt & time<=time2_ends(ci)+samples_after*dt);
xx = time(ind);
yy = data(ind);
data_store{ci} = [xx(:) yy(:)]; % 2 columns : time / data
plot(xx,yy);
end
hold off
%%%% end of main file %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [begin,ends] = find_start_end_group(ind)
% This locates the beginning /ending points of data groups
D = diff([0,ind,0]);
begin = find(D == 1);
ends = find(D == -1) - 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function x_rms = my_rms(x)
x_rms = sqrt(mean(x.^2));
end

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!