Plot a graph with this code.

1 回表示 (過去 30 日間)
Michael
Michael 2023 年 2 月 23 日
回答済み: Kevin Holly 2023 年 2 月 23 日
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 Center および File ExchangeAnnotations についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by