% Clear workspace and command window
clc;
clear;

% Load data from the Excel file
filename = 'Trial7_Power.xlsx';
data = readtable(filename, 'ReadVariableNames', true);

% Extract relevant columns
frame = data{:, 1}; % Frame column (1st)
Power = data{:, 13}; % Power column (13th)

% Check for missing or invalid data
if isempty(frame) || isempty(Power)
    error('One or both columns (frame or Power) are empty. Verify the column indices and file structure.');
end

% Remove invalid data
validIndices = ~isnan(frame) & ~isnan(Power);
frame = frame(validIndices);
Power = Power(validIndices);

% Filter data for angular velocity range [0.1, 0.4]
%rangeIndices = Power >= 0.1 & Power <= 0.3;
%frame = frame(rangeIndices);
%Power = Power(rangeIndices);

% Calculate the average power within the range
averagePower = mean(Power);

% Plot data
figure;
plot(frame, Power, 'b-', 'LineWidth', 1.5); % Main plot
hold on; % Keep the plot to add the average line
% Uncomment below to show the average line with label
% yline(averagePower, 'r--', 'LineWidth', 1.5, 'Label', 'Average Power');
hold off;

% Add title and labels
title('Trial 7: Angular Velocity vs. Frame');
xlabel('Frame');
ylabel('Angular Velocity');
ylim([0.1, 0.4]); % Set y-axis limits to match the filtered range
grid on;

% Display completion message
disp('Filtered angular velocity visualization complete.');
