forked from tiesmaaj/trial_and_error
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDM_SNR.m
More file actions
93 lines (79 loc) · 3.95 KB
/
DDM_SNR.m
File metadata and controls
93 lines (79 loc) · 3.95 KB
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
function DDM_SNR(processedData)
% Initialize the drift variable
numTrials = height(processedData);
drift = zeros(1, numTrials);
initial_state = 0; % Start drift at zero
drift(1) = initial_state;
% Normalize Response for drift calculations
normalizedResponse = processedData.Response; % Copy original response
normalizedResponse(normalizedResponse == 2) = -1; % Right -> -1 (toward bottom boundary)
normalizedResponse(normalizedResponse == 1) = 1; % Left -> +1 (toward top boundary)
% Normalize SNR (assuming SNR is in processedData.SNR)
SNR = processedData.SNR;
% Loop through trials and calculate drift
for t = 2:numTrials
if ~isnan(normalizedResponse(t)) && ~isnan(processedData.PrevOutcome(t)) && ~isnan(SNR(t))
% Adjust drift based on outcome and SNR
if processedData.PrevOutcome(t) == 1 % Correct
drift(t) = drift(t-1) + SNR(t) * normalizedResponse(t); % Positive drift scaled by SNR
elseif processedData.PrevOutcome(t) == 0 % Incorrect
drift(t) = drift(t-1) - SNR(t) * normalizedResponse(t); % Negative drift scaled by SNR
end
else
drift(t) = drift(t-1); % No change if any value is NaN
end
end
% Normalize drift to range [-1, 1] for better scaling
drift = drift / max(abs(drift));
% Plot the drift-diffusion model
figure;
plot(1:numTrials, drift, '-k', 'LineWidth', 3); % Drift line
hold on;
% Plot markers for responses
for t = 1:numTrials
if processedData.Response(t) == 1 % Left response
marker_color = [230, 159, 0] / 255; % Orange for left
if processedData.PrevOutcome(t) == 1 % Correct
marker = 'o'; % Circle for correct
else
marker = '^'; % Triangle for incorrect
end
elseif processedData.Response(t) == 2 % Right response
marker_color = [0, 114, 178] / 255; % Blue for right
if processedData.PrevOutcome(t) == 1 % Correct
marker = 'o'; % Circle for correct
else
marker = '^'; % Triangle for incorrect
end
else
continue; % Skip if response is invalid
end
% Plot marker
plot(t, drift(t), marker, 'MarkerSize', 8, 'MarkerFaceColor', marker_color, 'MarkerEdgeColor', 'k');
end
% Add horizontal dashed lines for boundaries
yline(1, '--k', 'Left Boundary', 'LabelHorizontalAlignment', 'left', 'LineWidth', 2);
yline(-1, '--k', 'Right Boundary', 'LabelHorizontalAlignment', 'left', 'LineWidth', 2);
% Add horizontal dashed line for the initial state
yline(0, '--k', ' ', 'LabelHorizontalAlignment', 'left', 'LineWidth', 2);
% Customize plot
xlabel('Trial', 'FontSize', 14, 'FontWeight', 'bold');
ylabel('Drift State (Normalized)', 'FontSize', 14, 'FontWeight', 'bold');
title('Drift-Diffusion Model with SNR', 'FontSize', 16, 'FontWeight', 'bold');
xlim([1 numTrials]);
ylim([-1.1, 1.1]); % Adjust y-axis for normalized drift
grid on;
% Add dummy plots for legend entries
h1 = plot(nan, nan, 'o', 'MarkerSize', 12, 'MarkerFaceColor', [230, 159, 0] / 255, 'MarkerEdgeColor', 'k'); % Correct Left
h2 = plot(nan, nan, 'o', 'MarkerSize', 12, 'MarkerFaceColor', [0, 114, 178] / 255, 'MarkerEdgeColor', 'k'); % Correct Right
h3 = plot(nan, nan, '^', 'MarkerSize', 12, 'MarkerFaceColor', [230, 159, 0] / 255, 'MarkerEdgeColor', 'k'); % Incorrect Left
h4 = plot(nan, nan, '^', 'MarkerSize', 12, 'MarkerFaceColor', [0, 114, 178] / 255, 'MarkerEdgeColor', 'k'); % Incorrect Right
% Add legend
legend([h1, h2, h3, h4], ...
{'Correct Left (Orange Circle)', ...
'Correct Right (Blue Circle)', ...
'Incorrect Left (Orange Triangle)', ...
'Incorrect Right (Blue Triangle)'}, ...
'Location', 'best', 'FontSize', 12);
hold off;
end