Plot a graph with this code.

1 次查看(过去 30 天)
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');

采纳的回答

Kevin Holly
Kevin Holly 2023-2-23
% 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');

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by