Plot a graph with this code.

4 views (last 30 days)
Michael
Michael on 23 Feb 2023
Answered: Kevin Holly on 23 Feb 2023
I was trying to plot a graph when it was not working. It should plot the cross-correlation function. Can anyone look at the code and make it plot my graph. Here is also the data for sines1
% 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/fs,r);
xlabel('Time Lag (s)');
ylabel('Cross-Correlation');
title('Cross-Correlation of x and y');

Accepted Answer

Kevin Holly
Kevin Holly on 23 Feb 2023
% 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
phase_shift_deg = 367200000
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');

More Answers (0)

Tags

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!