% =========================================================================
% ERP PEAK DETECTIVE - Analysis Template
% =========================================================================
% Course: [Your Course Name]
% Student Name: _______________________
% Date: ___________
%
% Description: This script analyzes Event-Related Potential (ERP) data
% from a P300 oddball paradigm. You will plot the data, identify the P300
% peak, and measure its amplitude and latency.
%
% =========================================================================

%% SECTION 1: LOAD AND VISUALIZE DATA
% -------------------------------------------------------------------------
% This section loads the ERP data and creates an initial plot

% Clear workspace and command window (good practice!)
clear all
close all
clc

% Load the data file
load('P300_data.mat')

% Display what variables we loaded
disp('Loaded variables:')
whos

% Create a new figure window
figure('Name', 'ERP Analysis', 'Position', [100, 100, 800, 600])

% Plot voltage over time
plot(time, voltage, 'LineWidth', 2, 'Color', 'b')

% STUDENT: Add x-axis label (include units!)
xlabel('_____________________')

% STUDENT: Add y-axis label (include units!)
ylabel('_____________________')

% STUDENT: Add a descriptive title
title('_____________________')

% Make the plot easier to read
grid on
set(gca, 'FontSize', 12)

%% SECTION 2: FIND THE PEAK
% -------------------------------------------------------------------------
% This section automatically finds the maximum (peak) value

% STUDENT: Use the max() function to find the peak amplitude and its index
% Hint: [maximum_value, index_of_maximum] = max(array)
[peak_amplitude, peak_index] = max(_____);

% STUDENT: Find the time corresponding to the peak
% Hint: Use the peak_index to look up the time value
peak_time = time(_____);

% Display the results
fprintf('\n========== PEAK MEASUREMENTS ==========\n')
fprintf('Peak Amplitude: %.2f μV\n', peak_amplitude)
fprintf('Peak Latency: %.1f ms\n', peak_time)
fprintf('========================================\n\n')

%% SECTION 3: MARK THE PEAK ON THE PLOT
% -------------------------------------------------------------------------
% This section adds a visual marker showing where the peak is

% STUDENT: Add a marker at the peak location
% Hint: Use "hold on" to add to existing plot, then plot(x, y, 'marker_style')
hold on
plot(_____, _____, 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r', 'LineWidth', 2)
hold off

%% SECTION 4: ADD REFERENCE LINES (Optional but helpful!)
% -------------------------------------------------------------------------
% These lines help interpret the ERP

% STUDENT: Add a horizontal line at voltage = 0 (baseline)
% Hint: Use yline(value, 'style', 'label')
yline(_____, '--k', 'Baseline', 'LineWidth', 1.5)

% STUDENT: Add a vertical line at time = 0 (stimulus onset)
% Hint: Use xline(value, 'style', 'label')
xline(_____, '--k', 'Stimulus', 'LineWidth', 1.5)

%% SECTION 5: ADD A LEGEND (Optional)
% -------------------------------------------------------------------------

% STUDENT: Create a legend showing what the different elements are
% Hint: legend({'label1', 'label2', ...})
legend({'ERP Waveform', 'P300 Peak'}, 'Location', 'best')

%% SECTION 6: SAVE YOUR FIGURE (Optional)
% -------------------------------------------------------------------------
% Uncomment the line below to save your figure automatically

% saveas(gcf, 'ERP_figure_YOUR_NAME.png')

%% SECTION 7: VALIDATION CHECK
% -------------------------------------------------------------------------
% This section helps you check if your measurements are reasonable

fprintf('========== VALIDATION CHECK ==========\n')

% Check if peak is in expected time range (250-600 ms for P300)
if peak_time >= 250 && peak_time <= 600
    fprintf('✓ Peak latency is in expected P300 range (250-600 ms)\n')
else
    fprintf('⚠ Warning: Peak latency seems unusual for P300\n')
end

% Check if amplitude is positive (P300 is a positive component)
if peak_amplitude > 0
    fprintf('✓ Peak amplitude is positive (as expected for P300)\n')
else
    fprintf('⚠ Warning: Peak amplitude is negative (unexpected for P300)\n')
end

fprintf('========================================\n')

%% END OF SCRIPT
% =========================================================================
% 
% REFLECTION QUESTIONS (answer in your worksheet):
% 1. Does your peak measurement match what you saw in Part A?
% 2. Why is it useful to have automatic peak detection rather than 
%    clicking on the plot manually?
% 3. What could go wrong with automatic peak detection?
% 
% =========================================================================
