%% ---------------------------------------------------------------------- % Script Name: PE_Tidal_Correlation.m % Author : umar % Date : 2025-09-10 % Version : 1.0 % Description: % This script analyzes two time series (PE and tidal data) to: % 1. Extract daily peaks % 2. Detrend the peaks % 3. Compute cross-correlation to find temporal relationships % 4. Plot peak comparisons and correlation results % % Requirements: % - MATLAB base (no additional toolboxes required) % - Data files: PE.mat (variable PE), tidalfile.mat (variables tidal and timetidal) % % Usage: % Place PE.mat and tidalfile.mat in the same folder as this script. % Run the script. Results will be plotted in a figure window. % -----------------------------------------------------------------------
%% --- Load Data --- clear; clc;
% Load PE data PE_struct = load('/MATLAB Drive/PE.mat'); % assumes PE.mat contains variable 'PE' PE = PE_struct.PE;
% Load Tidal data tidal_struct = load('/MATLAB Drive/tidalfile.mat'); % assumes 'tidal' and 'timetidal' exist tidal = tidal_struct.tidal; timetidal = tidal_struct.timetidal;
% Convert timetidal if stored as datenum if isnumeric(timetidal) timetidal = datetime(timetidal, 'ConvertFrom', 'datenum'); end
% Define PE time vector start_date = datetime(2023, 1, 1); end_date = datetime(2024, 5, 31); timePE = linspace(start_date, end_date, length(PE));
% Limit data to cutoff date cutoff_date = datetime(2024, 5, 31); PE_idx = timePE <= cutoff_date; PE = PE(PE_idx); timePE = timePE(PE_idx);
tidal_idx = timetidal <= cutoff_date; tidal = tidal(tidal_idx); timetidal = timetidal(tidal_idx);
%% --- Extract Daily Peaks --- winDays = 1; % 1-day window edges = timePE(1):days(winDays):timePE(end);
% Preallocate PE_peaks = zeros(1, length(edges)-1); PE_times = datetime.empty(1,0); Tide_peaks = zeros(1, length(edges)-1); Tide_times = datetime.empty(1,0);
% Extract PE peaks for i = 1:length(edges)-1 idx = timePE >= edges(i) & timePE < edges(i+1); if any(idx) [val, loc] = max(PE(idx)); PE_peaks(i) = val; tmp = timePE(idx); PE_times(end+1) = tmp(loc); end end
% Extract Tidal peaks for i = 1:length(edges)-1 idx = timetidal >= edges(i) & timetidal < edges(i+1); if any(idx) [val, loc] = max(tidal(idx)); Tide_peaks(i) = val; tmp = timetidal(idx); Tide_times(end+1) = tmp(loc); end end
%% --- Detrend Peaks --- PE_detr = detrend(PE_peaks); Tide_detr = detrend(Tide_peaks);
%% --- Cross-Correlation --- n = min(length(PE_detr), length(Tide_detr)); % align lengths [xc, lags] = xcorr(Tide_detr(1:n), PE_detr(1:n), 'normalized'); [~, I] = max(abs(xc)); bestLag = lags(I);
%% --- Plot Results --- figure;
subplot(2,1,1); plot(PE_times, PE_detr, '-', 'DisplayName', 'PE'); hold on; yyaxis right; plot(Tide_times, Tide_detr, '-', 'DisplayName', 'Tidal'); xlabel('Time'); ylabel('Detrended Values'); legend('Location','southoutside','Orientation','horizontal'); grid on; title('Daily Peaks Comparison');
subplot(2,1,2); plot(lags*winDays, xc, '-', 'LineWidth', 1.5); grid on; xlabel('Lag (days)'); ylabel('Correlation'); title(sprintf('Cross-correlation (Best Lag = %d days)', bestLag)); set(gcf, 'Position', [100, 100, 900, 350]);
Please see attached
