Extract data between peaks

Hello everyone, I would really appreciate help on solving this problem. I am trying to segment an emg data based on heelstrikes recorded from the accelerometer. I have used the peak to peak to find the location of the heelstrikes. The first peak, third and fifth peak belong to the right foot. The second, fourth,and sixth correspond to the left foot. I would like to extract the data between. Two heelstrikes of the same foot. For example I would like to write a for loop to extract the emg data from peak 1 to peak 3, then peak 3 to peak 5 till the last peak. Here's what I tried to do but it doesn't work. I have showed the extraction portion in the picture attached. Thanks in advance
if true
% clear all
EMG = readmatrix('straightLASER01.xlsx','Sheet',1); %read emg data
ACC = readmatrix('straightLASER01.xlsx','Sheet',2); %read accleration data
[pks,locs] = findpeaks(ACC,'MinPeakDistance',1000); %find peaks
for p = 1:length(locs)
first_strike = floor(locs(p,:));
third_strike = floor(locs((p+2),:));
acc = EMG1(first_strike:third_strike , p);
end
end

 采纳的回答

Adam Danz
Adam Danz 2019-6-10
编辑:Adam Danz 2019-6-10
This segregates the EMG1 data by every 2 right-foot steps.
rightFootIdx = 1 : 2 : numel(locs);
for i = 1:length(rightFootIdx)-1
acc = EMG1(locs(rightFootIdx(i)):locs(rightFootIdx(i+1)), p);
% ...
end

7 个评论

Thank you so much for the quick response. Appreciate it a lot.
I have resolved the problem with your help. I wanted to save each step into a different column. I have used the cell structure and have managed to extract each step into one cell. However each cell has a different length and I would like to do some form of interpolation so that the have equal leghth. Each column contains the data of one gait cycle and I don't think zero padding is the best way to go about it. Could you please help me to write a code that interpolates each column to a common length? Thanks so much.
clear all
EMG = readmatrix('straightLASER01.xlsx','Sheet',1); %read emg data
ACC = readmatrix('straightLASER01.xlsx','Sheet',2); %read accleration data
[pks,locs] = findpeaks(ACC(:,12),'MinPeakDistance',1000); %find peaks
EMG1 = EMG(:,1);
rightFootIdx = 1 : 2 : numel(locs);
acc = {};
for i = 1:length(rightFootIdx)-1
acc1{i} = EMG1(locs(rightFootIdx(i)):locs(rightFootIdx(i+1)),:);
AccSz(i,:) = size(acc1{i}); % Size Of Each Vector
% ...
end
You'll probably have to interpolate after the for-loop so that you know the maximum length within the cell array.
My proposal is to use interp1() to interpolate each element of the cell array so that each element has the same length as the maximum element. Here's what you can do (using fake data)
acc1 = {1:4,1:6,1:2,1:3,1:10,1:15,1:4}; %fake data
maxlen = max(cellfun(@length,acc1)); %maximum length (=15)
% Option 1: for-loop (more intuitive and readable)
acc1Interp = cell(size(acc1));
for j = 1:numel(acc1)
acc1Interp{j} = interp1(1:numel(acc1{j}), acc1{j}, linspace(1,numel(acc1{j}),maxlen));
end
% Option 2: for those who don't like loops
acc1Interp2 = cellfun(@(x)interp1(1:numel(x), x, linspace(1,numel(x),maxlen)),acc1Interp,'UniformOutput',false);
Note that the elements of the cell array will all have the same number of data points but if those data were collected at a fixed interval of time, each element will now have a different sample rate.
Also note that if any of the elements in your array contain only 1 data point, the interp1() function will break. If that happens, write a conditional that either skips that cell array or replicates the single value to the maxlen.
Works perfectly. Thank you so much. Quick question please: the data points were all sampled at 2000Hz. This means that once I use the interp1 function, the sample rate will change for each cell right?
Yes. So if you have 1/2 a second of data, that's 1000 samples. If you interpolate that to 2000 samples, you still have 1/2 second of data but now the sampling rate becomes 4000Hz for the interpolated data.
Perfect. Once again thank you so much for everything. You've saved me hours of struggling.
jay yawson
jay yawson 2019-6-11
编辑:Adam Danz 2019-6-11
Concerning the same work, I've asked a similar question. I would like to extract the data between two regions. However these two regions are not the peaks and I don't know how to automatically detect that. Do you have any ideas? Kindly take a look if you manage to get some free time off you busy schedule. Many thanks.
I gave it a shot.

请先登录,再进行评论。

更多回答(0 个)

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by