Trying to extract data trail by trail, but my data is sampled at different sampling rate.

7 次查看(过去 30 天)
Velocity, position and torque data has sampling frequency of 1000 and EMG's channels at 2000. So I am not getting the right region of intrest. I think I might need two different time vector or is there any other way to solve this problem ?
%% Intialization
fs=960; % sampling frequency of velocity,position and torque
Fs=1960; % sampling frequency of EMG channels
fc=10;
% Low pass Filter of 10 Hz
[b,a]= butter(1,fc/(fs/2),"low");
prewindow=-1000;
postwindow=5000;
% Caliberation
%torque=12.80409731*Torque;
%tosition=34.2465753*position;
%velocity=
%% Assigning all variables
%Position
position=V300degpersec_Ch2.values;
%position=-position;
positionoffset=min(position);
Position=position+abs(positionoffset);%% remove offset
Position=filtfilt(b,a,position);
% Torque
torque=V300degpersec_Ch4.values;
Torque=filtfilt(b,a,torque);
%Velocity
velocity=V300degpersec_Ch3.values;
velocity=-velocity;
Velocity=filtfilt(b,a,velocity);
Time=1/fs:1/fs:length(Velocity)/fs;
% Raw EMG
RFp=V300degpersec_Ch5.values;% Proximal Rectus Femoris
%RFp=downsample(RFp,2);
RFd=V300degpersec_Ch6.values;% Distal Rectus Femoris
%RFd=downsample(RFd,2);
VM=V300degpersec_Ch7.values;% Vastus Medialis
%VM=downsample(VM,2);
BiFem=V300degpersec_Ch8.values;% Biceps Femoris
%BiFem=downsample(BiFem,2);
SM=V300degpersec_Ch9.values;% Semitendinosus
%SM=downsample(SM,2);
time=1/Fs:1/Fs:(length(SM))/Fs;% Time vector for EMG channels
%% Croping ROI
moving=(Velocity>1.5);
%plot(Time,Velocity);
%hold on
%plot(Time, moving*150);
x=diff(moving);
figure(1)
plot(x);
start=find(x>0);
finish=find(x<0);
figure(2)
plot(Velocity);
hold on
plot(Time, moving*150);
%tr=2;
trln=max(finish-start);
trln=trln-10;
ntr=length(start);
%Preloacation of variables
%
% vel=nan(trln,ntr);
% pos=nan(trln,ntr);
% tor=nan(trln,ntr);
% EMG1=nan(trln,ntr);
% EMG2=nan(trln,ntr);
% EMG3=nan(trln,ntr);
% EMG4=nan(trln,ntr);
% EMG5=nan(trln,ntr);
vel={};
pos={};
tor={};
EMG1={};
EMG2={};
EMG3={};
EMG4={};
EMG5={};
for tr=1:ntr
vel{tr}=Velocity(start(tr):finish(tr));
pos{tr}=Position(start(tr):finish(tr));
tor{tr}=Torque(start(tr):finish(tr));
EMG1{tr}=RFd(start(tr):finish(tr));
EMG2{tr}=RFp(start(tr):finish(tr));
EMG3{tr}=VM(start(tr):finish(tr));
EMG4{tr}=BiFem(start(tr):finish(tr));
EMG5{tr}=SM(start(tr):finish(tr));
end
for tr=1:ntr-1
bounds=[start(tr)-prewindow:finish(tr)+postwindow];
pos{tr}=Position(bounds);
vel{tr}=Velocity(bounds);
tor{tr}=Torque(bounds);
EMG1{tr}=RFd(bounds);
EMG2{tr}=RFp(bounds);
EMG3{tr}=VM(bounds);
EMG4{tr}=BiFem(bounds);
EMG5{tr}=SM(bounds);
end
%Time vector position,velocity and torque
for tr=1:ntr-1
lengthtr=finish(tr)-start(tr);
t{tr}=-prewindow:1:lengthtr+postwindow;
end

回答(2 个)

Jan
Jan 2023-1-18
编辑:Jan 2023-1-18
If one signal is measured with 1000 Hz and another with 2000 Hz, it is trivial to determine timepoints of the first one in the time frame of the second one:
time_2000Hz = (time_1000Hz - 1) * 2 + 1
This should allow you to convert the intervals of interest.
  3 个评论
Jan
Jan 2023-1-20
@Kiran Isapure: I do not see, that you have considered my advice to scale the time for the EMG data anywhere in the code.
Your code looks strange:
for tr=1:ntr
vel{tr}=Velocity(start(tr):finish(tr));
pos{tr}=Position(start(tr):finish(tr));
...
end
for tr=1:ntr-1
bounds=[start(tr)-prewindow:finish(tr)+postwindow];
pos{tr}=Position(bounds);
vel{tr}=Velocity(bounds);
...
end
Why do you overwrite vel and pos for the elements 1:ntr-1 ?
By the way, [a:b] is exactly the same as a:b, but the latter is slightly faster. [ and ] is Matlab's operator for concatenation. [a:b] concates the vector a:b with nothing.
Note that
bounds=[start(tr)-prewindow:finish(tr)+postwindow];
pos{tr}=Position(bounds);
is slower than:
a = start(tr)-prewindow;
b = finish(tr)+postwindow;
pos{tr}=Position(a:b);
In this code the index vector a:b is not created explicitly, but Matlab copies the block of memory directly. Aftzer creating the index vector at first, Matlab has to check each element, if it is inside the bounds of the array. This take a lot of processing time.

请先登录,再进行评论。


Star Strider
Star Strider 2023-1-18
I would downsample the EMG signal using the resample function to do the comparisons.
Retain the original signal of course, since the 2kHz sampling frequency has data you do not want to discard.
I would not upsample the 1kHz data, because that creates interpolated data where none previously existed.
  14 个评论
Star Strider
Star Strider 2023-1-20
The error:
Unrecognized function or variable 'time_2000Hz'.
is still unresolved, so I can’t run the code!

请先登录,再进行评论。

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by