what changes would you recommend

1 view (last 30 days)
Manav Divekar
Manav Divekar on 29 Nov 2021
Answered: Abhinaya Kennedy on 4 Jun 2024
I am trying to do muscle fatigue analysis this is how far i have made. What changes i should make so that my code works.
for the acquired data set.
% 2 sec Time Trials
clear
clc
close all
files = dir('*.txt'); % Returns all the files and folders in the directory
fileTable = struct2table(files);
numOfFiles = height(fileTable);
for k = 1:numOfFiles % check for time trial files and excludes MVIC
if strfind(fileTable.name{k}, 'sec')
secfiles{k} = fileTable.name{k};
end
end
allsecfiles = secfiles(:,~cellfun(@isempty,secfiles(1,:)));
masterData = struct;
for j = 1:length(allsecfiles) % compile file name information
file_info(:,j) = split(allsecfiles(j), "_");
masterData(j).group = file_info(1,j);
masterData(j).time = file_info(2,j);
masterData(j).trial = extractBefore(file_info(4,j),'.');
end
timetrials = {'2sec';'5sec';'10sec'}; % define iterations/categories of files
newfig = 1; % beginning figure
r=1;
for t = 1:length(timetrials) % iterate for each time duration
timetrialfiles = find(strcmp(file_info(2,:),timetrials{t})); % index files that correspond to selected time duraiton
gettimefiles = allsecfiles(timetrialfiles); % files that correspond to time duration
for s = 1:length(gettimefiles) % iterate for each trial (2 or 3)
for c = 1:2 % iterate for columns for bicep and tricep
if c==1
musc_c = 'Bicep'; % Bicep data located in first 8 columns
timecol = 1;
emgcol = 2;
else
musc_c = 'Tricep'; % Tricep data located in second 8 columns
timecol = 9;
emgcol = 10;
end
imptimetrials = readtable(char(gettimefiles(s)));
time = table2array(imptimetrials(:,timecol));
emg = table2array(imptimetrials(:,emgcol));
figure(newfig)
plot(time,emg,'b') % plot raw EMG data in blue
hold on
% determine where to split concentric and eccentric data based on file name
idc = strfind(timetrials,'2sec');
idx = ~cellfun('isempty',idc);
findidx = find(idx==1);
if findidx == t
cut = 1;
endtime = 2;
else
idc = strfind(timetrials,'5sec');
idx = ~cellfun('isempty',idc);
findidx = find(idx==1);
if findidx == t
cut = 2.5;
endtime = 5;
else
cut = 5;
endtime = 10;
end
end
con = find(time < cut & time > 0); % first half of time trial
ecc = find(time > cut & time < endtime);
contime = time(con);
conemg = emg(con);
ecctime = time(ecc);
eccemg = emg(ecc);
figure(newfig)
plot(contime,conemg, 'Color','#D95319')
hold on
plot(ecctime,eccemg, 'Color','#EDB120') % plot second half in yellow
xlabel('Time (s)')
ylabel('EMG Response (mV)')
legend(sprintf('%s EMG',musc_c), 'Concentric', 'Eccentric')
title(sprintf('%s: %s EMG vs Time [Trial %d]',timetrials{t},musc_c,s))
hold off
newfig = newfig+1;
figure(newfig)
% Plot Concentric Rectified
subplot(1,2,1)
plot(contime,conemg,'b')
hold on
smooth_con = smooth(abs(conemg - mean(conemg)));
plot(contime,smooth_con,'k')
hold on
RMScon = sqrt(movmean(smooth_con.^2,500));
plot(contime,RMScon,'r')
legend('RawEMG','Rectified EMG','Moving Average')
title(sprintf('%s %s EMG: Concentric Contraction vs Time',timetrials{t}, musc_c))
xlabel('Time (s)')
ylabel('EMG Response (mV)')
hold off
% Plot Eccentric Rectified
subplot(1,2,2)
plot(ecctime, eccemg, 'b')
hold on
smooth_ecc = smooth(abs(eccemg-mean(eccemg)));
plot(ecctime, smooth_ecc,'k')
hold on
RMSecc = sqrt(movmean(smooth_ecc.^2,500));
plot(ecctime,RMSecc,'r')
legend('RawEMG','Rectified EMG','Moving Average')
title(sprintf('%s %s EMG: Eccentric Contraction vs Time',timetrials{t}, musc_c))
xlabel('Time (s)')
ylabel('EMG Response (mV)')
hold off
newfig = newfig+1;
%% Rectified and Integrated EMG
% Concentric
figure(newfig)
subplot(1,2,1)
plot(contime,smooth_con,'k')
hold on
intCon = cumtrapz(RMScon);
yyaxis right
plot(contime,intCon,'g')
legend('Rectified EMG','Integrated EMG')
title(sprintf('%s %s EMG: Concentric Contraction vs Time',timetrials{t},musc_c))
xlabel('Time (s)')
ylabel('EMG Response (mV-s)')
yyaxis left
ylabel('EMG Response (mV)')
hold off
% Eccentric
subplot(1,2,2)
plot(ecctime,smooth_ecc,'k')
hold on
intEcc = cumtrapz(RMSecc);
yyaxis right
plot(ecctime, intEcc,'g')
legend('Rectified EMG','Integrated EMG')
title(sprintf('%s %s EMG: Eccentric Contraction vs Time',timetrials{t},musc_c))
xlabel('Time (s)')
ylabel('EMG Response (mV-s)')
yyaxis left
ylabel('EMG Response (mV)')
hold off
newfig = newfig+1;
maxRMScon(r,c) = max(RMScon);
maxRMSecc(r,c) = max(RMSecc);
maxINTcon(r,c) = max(intCon);
maxINTecc(r,c) = max(intEcc);
fourier_con = smooth_con.*1000;
fourier_ecc = smooth_ecc.*1000;
fs = 2000;
tau = 1/fs;
lenCon = length(fourier_con);
lenEcc = length(fourier_ecc);
fftCon = abs(fft(fourier_con));
fftEcc = abs(fft(fourier_ecc));
truncCon = fftCon(1:lenCon/2+1);
truncEcc = fftEcc(1:lenEcc/2+1);
truncCon(2:end-1) = truncCon(2:end-1).*2;
truncEcc(2:end-1) = truncEcc(2:end-1).*2;
fc = ((0:(lenCon/2))./lenCon).*fs;
fe = ((0:(lenEcc/2))./lenEcc).*fs;
freqrange = 20:10:500;
for fr = 1:length(freqrange)-1
freqcon = [];
freqecc = [];
freqcon(fr) = medfreq(truncCon(freqrange(fr):freqrange(fr+1)),fs);
freqecc(fr) = medfreq(truncEcc(freqrange(fr):freqrange(fr+1)),fs);
end
[pMaxCon,pMaxConLocs] = max(freqcon);
[pMaxEcc,pMaxEccLocs] = max(freqecc);
figure(newfig)
subplot(1,2,1)
xline(pMaxCon, '--r','LineWidth',2);
hold on
plot(fc,truncCon,'b-o')
hold on
xlim([20 500])
xlabel('Frequency (Hz)')
ylabel('Power Spectrum')
title(sprintf('%s %s EMG: Concentric Power Spectrum',timetrials{t},musc_c))
legend(sprintf('Median f = %d Hz', pMaxCon),sprintf('Power: %s Concentric %s', musc_c, timetrials{t}))
hold off
subplot(1,2,2)
xline(pMaxEcc, '--r','LineWidth',2);
hold on
plot(fe,truncEcc,'b-o')
hold on
xlim([20 500])
xlabel('Frequency (Hz)')
ylabel('Power Spectrum')
title(sprintf('%s %s EMG: Eccentric Power Spectrum',timetrials{t},musc_c))
legend(sprintf('Median f = %d Hz', pMaxEcc),sprintf('Power: %s Eccentric %s', musc_c, timetrials{t}))
hold off
newfig = newfig+1;
for myfig = 1:newfig-1
saveas(myfig,sprintf('%s_Fig%d.png', musc_c, myfig))
end
end
r = r+1;
end
end

Answers (1)

Abhinaya Kennedy
Abhinaya Kennedy on 4 Jun 2024
Hi Manav,
The provided code lays a good foundation for muscle fatigue analysis. Here's how to enhance it:
  • Data Format: Ensure your text files (".txt") match the expected format: time in the first column, followed by Bicep EMG (first 8 columns) and Tricep EMG (next 8 columns). Modify the code if your format differs.
  • Phase Identification: Your code relies on filenames ("2sec", "5sec") for phase identification. If your filenames lack this convention, adjust the logic to identify phases based on markers within the data or additional filename information.
  • Multiple Trials: The code assumes one trial per time duration. If you have multiple trials, adapt the loop structure to handle them separately (e.g., additional loop or conditional statements).

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!