Standard deviation for RMS comparison of two windows
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Just looking for guidance on my math to calculate some standard deviation values. I am trying to compare some EMG motor unit data by calculating the RMS in two time windows around motor unit spikes. I have the code where it calculates the RMS value in the two units and comapres the data for which window has more dominance. In my graph I would like to add standard deviation bars to the points to visualize the data accurately. I am pretty sure I am not doing my math right so any guidance would be appreciated. Below is a picture of one of the motor units that I am using to calculate the two windows out of the total window of 0 to 50ms.
% Add a single indicator for each motor unit based on the RMS percentage
for j = 1:numMuscles
if rmsPercentage(j) < 40
text(j, rmsPercentage(j) + 2, 'M Wave', 'HorizontalAlignment', 'center', 'Color', 'b');
elseif rmsPercentage(j) > 60
text(j, rmsPercentage(j) + 2, 'H Wave', 'HorizontalAlignment', 'center', 'Color', 'r');
else
text(j, rmsPercentage(j) + 2, 'M/H Wave', 'HorizontalAlignment', 'center', 'Color', 'g'); % Use a different color for combination
end
end
% Define the time windows
window1 = [0, 30]; % Window 1: 0 to 30 ms
window2 = [31, 50]; % Window 2: 31 to 50 ms
% Initialize arrays to store the RMS activity, percentages, and variability measures
rmsActivityWindow1 = zeros(numMuscles, 1);
rmsActivityWindow2 = zeros(numMuscles, 1);
rmsPercentage = zeros(numMuscles, 1); % For percentage calculation
stdRMSPercentage = zeros(numMuscles, 1); % Standard deviation expressed as a percentage
% Calculate the corresponding sample indices for each window
startIdx1 = round(window1(1) * samplingFrequency / 1000) + 1;
endIdx1 = round(window1(2) * samplingFrequency / 1000);
startIdx2 = round(window2(1) * samplingFrequency / 1000) + 1;
endIdx2 = round(window2(2) * samplingFrequency / 1000);
% Ensure that indices do not exceed array bounds
if endIdx1 > size(averagePulseTrainSignals, 2)
endIdx1 = size(averagePulseTrainSignals, 2);
end
if endIdx2 > size(averagePulseTrainSignals, 2)
endIdx2 = size(averagePulseTrainSignals, 2);
end
% Loop through each motor unit to calculate RMS activity and standard deviation
for j = 1:numMuscles
% Calculate RMS for Window 1
rmsActivity1 = sqrt(mean(averagePulseTrainSignals(j, startIdx1:endIdx1).^2, 'omitnan'));
rmsActivityWindow1(j) = rmsActivity1;
% Calculate RMS for Window 2
rmsActivity2 = sqrt(mean(averagePulseTrainSignals(j, startIdx2:endIdx2).^2, 'omitnan'));
rmsActivityWindow2(j) = rmsActivity2;
% Calculate the mean for both windows
meanWindow1 = mean(averagePulseTrainSignals(j, startIdx1:endIdx1), 'omitnan');
meanWindow2 = mean(averagePulseTrainSignals(j, startIdx2:endIdx2), 'omitnan');
% Calculate the percentage for dominance
if rmsActivity1 + rmsActivity2 ~= 0
rmsPercentage(j) = (rmsActivity2 / (rmsActivity1 + rmsActivity2)) * 100; % Calculate percentage
% Calculate standard deviation for each window
std1 = sqrt(rmsActivity1^2 - meanWindow1^2);
std2 = sqrt(rmsActivity2^2 - meanWindow2^2);
% Combine standard deviations as a percentage of RMS, then divide by 10
combinedStd = (std1 + std2) / 2; % Average of standard deviations
stdRMSPercentage(j) = (combinedStd / mean([rmsActivity1, rmsActivity2])) * 100 / 10; % Express as percentage and divide by 10
else
rmsPercentage(j) = NaN; % Avoid division by zero
stdRMSPercentage(j) = NaN; % Avoid division by zero
end
end
% Create a figure for the RMS percentage plot
figure;
xPositions = 1:numMuscles; % X positions for motor units
scatter(xPositions, rmsPercentage, 'filled', 'MarkerFaceColor', 'b');
% Add error bars for the standard deviation percentage
hold on;
errorbar(xPositions, rmsPercentage, stdRMSPercentage, 'k', 'LineStyle', 'none');
% Add a single indicator for each motor unit based on the RMS percentage
for j = 1:numMuscles
if rmsPercentage(j) < 40
text(j, rmsPercentage(j) + 2, 'M Wave', 'HorizontalAlignment', 'center', 'Color', 'b', 'FontSize', 10);
elseif rmsPercentage(j) > 60
text(j, rmsPercentage(j) + 2, 'H Wave', 'HorizontalAlignment', 'center', 'Color', 'r', 'FontSize', 10);
else
text(j, rmsPercentage(j) + 2, 'M/H Wave', 'HorizontalAlignment', 'center', 'Color', 'g', 'FontSize', 10);
end
end
% Set labels and title with increased font size
xlabel('Motor Units', 'FontSize', 14);
ylabel('RMS Percentage', 'FontSize', 14);
title('RMS Activity of Motor Units as Percentage with Std Dev Error Bars', 'FontSize', 16);
ylim([0 100]); % Set y-limits from 0 to 100 percent
xticks(1:numMuscles); % Set x-ticks to correspond to motor units
xticklabels(1:numMuscles); % Label the x-ticks with numbers
grid on; % Add a grid for clarity
% Adjust font size for axes
set(gca, 'FontSize', 12);
hold off;
0 Commenti
Risposte (1)
Star Strider
il 21 Set 2024
Modificato: Star Strider
il 21 Set 2024
Root-mean-square (RMS) calculations are best suited to estimate the D-C amplitude of a signal over long periods or the entire signal. To look at individual MUAPS or other pulses in an ensemble, I characteristically calculate the mean, and then 95% confidence intervals using the standard error of the mean.
t = linspace(0, 1, 101);
y = sinc(-5:0.1:5);
y = y + randn(50,numel(y))*0.25;
figure
plot(t, y)
grid
xlabel('t')
ylabel('V')
ymean = mean(y)
ySEM = std(y)/sqrt(size(y,1))
cv = tinv([0.025 0.975], size(y,1)-1)
ci95 = ySEM .* cv(:)
figure
hp1 = plot(t, ymean, '-', 'DisplayName','\mu');
hold on
hp2 = patch([t flip(t)], [ci95(1,:)+ymean flip(ci95(2,:)+ymean)], 'r', 'FaceAlpha',0.5, 'DisplayName','±95% CI', 'EdgeColor','none');
hold off
legend('Location','best')
grid
EDIT — Corrected typographical errorrs.
.
Vedere anche
Categorie
Scopri di più su Spectral Measurements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!