% Problem2_28.mat
% Find the phase shift between 10-Hz sinusoids found in x and y in files
% sines1.mat
%Check
load('sines1.mat') % load data
fs = 1/2000; % sample frequency
lags = -length(x):length(x)-1; % lags for cross-correlation
r = xcorr(x,y); % cross-correlation
[~, idx] = max(r); % find index of maximum correlation
max_lag = lags(idx); % find lag at maximum correlation
max_shift_sec = max_lag/fs; % convert lag to time shift in seconds
phase_shift_deg = max_shift_sec/(1/10)*360 % convert time shift to phase shift in degrees
figure
plot(lags(1:length(r))/fs,r); %adjusted length of lags. It was 4000 datapoints were r was 3999.
xlabel('Time Lag (s)');
ylabel('Cross-Correlation');
title('Cross-Correlation of x and y');