# Can someone help me with codes or function combination to evaluate the Average force from tdms file with four channels? Pls, see codes I applied "manually" to attempt it.

2 views (last 30 days)
Commented: Mathieu NOE on 19 Jan 2023
#1 codes used to extract the tdms file
ChannelGroupName = "Force", ...
ChannelNames = ["Feed force" "Main force" "Passive force" "AERMS"], ...
RowTimes = "ArrTime");
#2 codes used to separate each forces from the channels eg for "Main force" - Cutting force
>> plot(data{1, 1}.(2))
>> plot(data1{1, 1}.(2))
>> main_force1 = data1{1, 1}.(2);
# codes used to compute the average Forces after picking the points Ranges for Fc1, Fc2 and Fc3 as shown the Main force plot figure at the bottom of codes;
>> sep_m1b=main_force1(950000:1592000);
>> sep_Fc1=main_force1(950000:1592000);
>> mean(sep_Fc1)
ans =
-75.6126 % the negative values shows the direction of the force due to position of dynamometer
>> sep_Fc1=main_force1(1741000:1971000);
>> sep_Fc2=main_force1(1741000:1971000);
>> mean(sep_Fc2)
ans =
-63.5206
>> sep_Fc3=main_force1(2051000:2221000);
>> mean(sep_Fc3)
ans =
-64.0661 75.6,
#Question : Can someone assist or suggest codes/functions that will COMBINE codes #1, #2 and #3, such that it Reads the 20221128131019RUN1force.tdms file, Extract any/each of the forces (Feed, Main, Passive or AERMS), Evaluate the Average values from the range and give the values 75.6N, 63.5N and 64.1N respectively skipping the ROUGH JUMPS and NON-cutting signal zones. The link to the tdms file, I have provided in the link. https://drive.google.com/file/d/1as21ZQWwY5RsKSNISNI8m761oG84i89I/view?usp=sharing Edited: Ugifada on 11 Jan 2023
Oh my error. However, I tried to upload the mat file but is very large (39MB) even as a zip or .rar file exceeding the max of 5MB. So I shared with the link from google drive. Can this be helpful.? https://drive.google.com/file/d/1_ooEOQGzYIj_rO88AdiqWKGeV9t77XsQ/view?usp=sharing

Mathieu NOE on 11 Jan 2023
Ok so this is what I can offer you today
1/ I noticed your sampling rate could be much lower (at least factor 1000) without any loss of information !! so this could reduce your file size and further processing time by large amount
2/ the rest is quite simple : we look for the positive and negative transitions and use that time stamps to extract the "flat" sections of data , and take the mean value of them . a bit of smoothing is applied before derivation
last , I prefer peakseek over the built in findpeaks but it's everyone taste so pick either one or the other....
results :
force_mean = -75.6448 -63.6153 -64.4441
plot code
ForceData = table2array(ForceData{1,1}); % ["Feed force" "Main force" "Passive force" "AERMS"], ...
mainforce = ForceData(:,2);
samples = numel(mainforce);
t = (1:samples);
%% task 1 : decimate (because we don't need that much data !!)
decim = 1000;
mainforce2 = decimate(mainforce,decim);
samples2 = numel(mainforce2);
t2 = (decim/2:decim:samples);
% figure(1),
% plot(t,mainforce,t2,mainforce2);
%% task 2 : find start / stop indexes for flat signal areas
clear ForceData mainforce samples t % we don't need the hude initial data anymore so let's clear some memory
% smooth the signal
mainforce2s = smoothdata(mainforce2,'gaussian',50);
% derivation for finding transients
% positive peaks
minpeakdist = 100;
minpeakh = max(d)/3;
[locspos, pkspos]=peakseek(d,minpeakdist,minpeakh);
t2pos = t2(locspos);
% negative peaks
minpeakdist = 100;
minpeakh = -min(d)/3;
[locsneg, pksneg]=peakseek(-d,minpeakdist,minpeakh);
t2neg = t2(locsneg);
pksneg = -pksneg;
figure(2),
subplot(2,1,1),plot(t2,mainforce2,t2,mainforce2s);
hold on
for k = 1:numel(pksneg)
ind_start = locsneg(k);
ind_stop = locspos(k);
length=(ind_stop-ind_start);
samples2remove = round(0.15*length); % remove this amount of samples at the beginning and end of this data buffer
ind_start = ind_start+samples2remove;
ind_stop = ind_stop-samples2remove;
%duration(k)=(ind_stop-ind_start);
time = t2(ind_start:ind_stop);
data = mainforce2s(ind_start:ind_stop);
subplot(2,1,1),plot(time,data,'*-');
time_mean(k) = mean(time);
force_mean(k) = mean(data);
end
subplot(2,1,1),plot(time_mean,force_mean,'dr','Markersize',15);
legend('signal decimated','signal decimated and smoothed','data segment 1','data segment 2','data segment 3','force mean')
subplot(2,1,2),plot(t2,d,t2pos,pkspos,'dg',t2neg,pksneg,'dk','Markersize',10);
legend('signal derivative','positive peaks','negative peaks')
hold off
force_mean
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [locs, pks]=peakseek(x,minpeakdist,minpeakh)
% x is a vector input (generally a timecourse)
% minpeakdist is the minimum desired distance between peaks (optional, defaults to 1)
% minpeakh is the minimum height of a peak (optional)
%
% (c) 2010
% Peter O'Connor
% peter<dot>ed<dot>oconnor .AT. gmail<dot>com
if size(x,2)==1, x=x'; end
% Find all maxima and ties
locs=find(x(2:end-1)>=x(1:end-2) & x(2:end-1)>=x(3:end))+1;
if nargin<2, minpeakdist=1; end % If no minpeakdist specified, default to 1.
if nargin>2 % If there's a minpeakheight
locs(x(locs)<=minpeakh)=[];
end
if minpeakdist>1
while 1
del=diff(locs)<minpeakdist;
if ~any(del), break; end
pks=x(locs);
[garb, mins]=min([pks(del) ; pks([false del])]); %#ok<ASGLU>
deln=find(del);
deln=[deln(mins==1) deln(mins==2)+1];
locs(deln)=[];
end
end
if nargout>1
pks=x(locs);
end
end
Mathieu NOE on 19 Jan 2023
hello again
hope you have succeeded your exams !
I was just wondering if this new approach was better , more robust than the previous one
yes for sure, you may have to tweak some parameters according to your data
as soon as the main code structure is fine, that's a good point and i am glad I could help you on that
good luck for the future !