Plot a graph with this code.

1 visualización (últimos 30 días)
Michael
Michael el 23 de Feb. de 2023
Respondida: Kevin Holly el 23 de Feb. de 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');

Respuesta aceptada

Kevin Holly
Kevin Holly el 23 de Feb. de 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');

Más respuestas (0)

Categorías

Más información sobre Curve Fitting Toolbox en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by