-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkingSpace2.m
164 lines (148 loc) · 5.28 KB
/
WorkingSpace2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
%% Assignment Number 2
% Course : Movement Neuroscience: Connections between the Brain and Muscles in Humans
% Student Name : Muhammad Hasnat
% Description : Various surface EMG data postprocessing and analysis steps.
% FAU Erlangen
%% Matlab script settings
clc;
clear all;
close all;
format compact;
rng('default');
warning('off', 'MATLAB:dispatcher:InexactMatch');
format long;
customColors = [0.2 0.4 0.6;
0.8 0.1 0.3;
0.1 0.6 0.2];
customFontName = 'Arial';
customFontSize = 12;
customFontWeight = 'bold';
customLineWidth = 2;
legendLocation = 'northeast';
customFigureWidth = 800;
customFigureHeight = 500;
screenSize = get(0, 'ScreenSize');
centerX = (screenSize(3) - customFigureWidth) / 2;
centerY = (screenSize(4) - customFigureHeight) / 2;
customFigure = [centerX, centerY, customFigureWidth, customFigureHeight];
set(groot, 'defaultAxesColorOrder', customColors);
set(groot, 'defaultLineLineWidth', customLineWidth);
set(groot, 'defaultAxesFontName', customFontName);
set(groot, 'defaultAxesFontSize', customFontSize);
set(groot, 'defaultAxesFontWeight', customFontWeight);
set(groot, 'defaultLegendLocation', legendLocation);
set(groot, 'defaultAxesXGrid', 'on');
set(groot, 'defaultAxesYGrid', 'on');
set(groot, 'defaultAxesZGrid', 'on');
%% Pre-Processing the data and visulizing the preprocessed data
slow_contraction = load('Slow_Contraction.mat');
C = 0.02;
g = 9.81;
s_signal=slow_contraction.ref_signal/C*g;
mu_pulse = slow_contraction.MUPulses;
sig = slow_contraction.SIG;
freq = slow_contraction.fsamp;
%% 1 : Spike trains
signalLength = 69540;
fMatrix_Sparse = sparse(length(mu_pulse), signalLength);
for i = 1:length(mu_pulse)
fMatrix_Sparse(i, mu_pulse{i}) = 1;
end
% Convert to logical matrix
fMatrix_logical = logical(fMatrix_Sparse);
% Ploting motor unit spike trains using plotSpikeRaster function
figure('Position', customFigure);
plotSpikeRaster(fMatrix_logical, 'PlotType', 'vertline', 'VertSpikeHeight', 0.9);
xlabel('Time(s)');
ylabel('Motor Unit');
title('MU Spike Trains');
hold on;
%% 1.1 : Spike Trains and the reference signal
yyaxis right;
plot(s_signal);
title('Spike Trains and the reference signal');
xlabel('Time');
ylabel('Force in N');
hold off;
%% Task 2 : Spike triggered averaging
average_sp = spikeTriggeredAveraging(sig,mu_pulse,0.01,freq);
samplemu = 10;
figure('Position', customFigure);
for i = 1:size(sig,1)*size(sig,2)
subplot(size(sig,1), size(sig,2), i);
plot(average_sp{samplemu}{ceil(i/size(sig,2)), mod(i-1, size(sig,2))+1});
title(['Channel ' num2str(i)]);
end
sgtitle(['MUAP Shapes for Exemplary MU (' num2str(samplemu) ') - All Channels']);
%% Task 3.1 Compute the maximum peak-to-peak value
% Flatten each inner cell array into a numeric array
flattened = cellfun(@(c) [c{:}], average_sp, 'UniformOutput', false);
p2p_values = cellfun(@(i) max(i(:)) - min(i(:)), flattened, 'UniformOutput', false);
amplitude = cellfun(@max, p2p_values);
recruitment_order = 1:length(amplitude);
%% 3.2 Visualize the dependency between MUAP amplitude and order of recruitment
figure('Position', customFigure);
% Line plot
subplot(3, 1, 1);
plot(recruitment_order, amplitude);
xlabel('Order of Recruitment');
ylabel('Amplitude of the MUAP');
title('Line Plot: Relation b/w Amplitude and Recruitment');
grid on;
% Scatter plot
subplot(3, 1, 2);
scatter(recruitment_order, amplitude, 'filled');
xlabel('Order of Recruitment');
ylabel('Amplitude of the MUAP');
title('Scatter Plot: Relation b/w Amplitude and Recruitment');
grid on;
% Bar graph
subplot(3, 1, 3);
bar(recruitment_order, amplitude, 'FaceColor', [0.5 0.5 0.5]);
xlabel('Order of Recruitment');
ylabel('Amplitude of the MUAP');
title('Bar Graph: Relation b/w Amplitude and Recruitment');
grid on;
figure('Position', customFigure);
% Histogram
subplot(2, 1, 1);
histogram(amplitude, 'FaceColor', 'r');
xlabel('Amplitude of the MUAP');
ylabel('Frequency');
title('Histogram: Distribution of Amplitude of the MUAP');
grid on;
% Heatmap
subplot(2, 1, 2);
% Convert cell array of peak-to-peak values to a matrix
p2p_matrix = cell2mat(p2p_values);
% Create heatmap
heatmap(p2p_matrix, 'Colormap', hot);
xlabel('Channel');
ylabel('Motor Unit');
title('Heatmap: Peak-to-Peak Values for Each MUAP Shape in Each Motor Unit');
%% Task 4.1 Root-mean-square (RMS)
mu_indices = 5;
Cal_RMS_T4 = cellfun(@(x) rms(x(:)), average_sp{mu_indices}, 'UniformOutput', false);
RMS_T4 = cell2mat(Cal_RMS_T4);
%% Task 4.2 Resulting matrix as a heatmap
create_heatmaps(average_sp, 5, customFigure);
%% Task 4.3 For two more motor units
create_heatmaps(average_sp, [5,15,25],customFigure);
%% Function
function create_heatmaps(signals, mu_indices, fig_position)
% Create a new figure
figure('Position', fig_position);
% Iterate over the motor unit indices
for i = 1:length(mu_indices)
Cal_RMS = cellfun(@(x) rms(x(:)), signals{mu_indices(i)}, 'UniformOutput', false);
RMS = cell2mat(Cal_RMS);
RMS = fillmissing(RMS,'nearest');
subplot(1, length(mu_indices), i);
imagesc(RMS)
colorbar
colormap hot
xlabel('Column');
ylabel('Row');
title(sprintf('Heatmap for Motor Unit %d', mu_indices(i)))
end
end