How to compute the correlation coefficient in the two data set.
52 views (last 30 days)
Show older comments
How to compute the correlation coefficient between the two data sets (this is a graphic with 2 function) I need some helps or see any example, and the image is on the paper
Answers (1)
Mathieu NOE
on 21 Nov 2022
hello
so once we have digitized the picture with grabit , we have two data mat files (attached) available for further processing
first step is to make sure the two datasets have common x axis so we must sort out , remove duplicates and interpolate.
then we can compute either one single cor coefficient for the entire data length
%% full data length single cor coeff value
M = corrcoef(y1_new,y2_new);
corcoefficient = M(2,1)
will give : corcoefficient = 0.6209
or make a kind of running buffer version of it
full code :
%% load data
load('Data001.mat')
x1 = Data001(:,1);
y1 = Data001(:,2);
% make sure data are unique and sorted in ascending order
[x1,ind] = sort(x1);
x1 = x1(ind);
y1 = y1(ind);
% remove duplicates
[x1,IA,IC] = unique(x1);
y1 = y1(IA);
load('Data002.mat')
x2 = Data002(:,1);
y2 = Data002(:,2);
% make sure data are unique and sorted in ascending order
[x2,ind] = sort(x2);
x2 = x2(ind);
y2 = y2(ind);
% remove duplicates
[x2,IA,IC] = unique(x2);
y2 = y2(IA);
% interp data on common x axis
x_min = ceil(max(x1(1),x2(1)));
x_max = floor(min(x1(end),x2(end)));
x = x_min:x_max;
y1_new = interp1(x1,y1,x);
y2_new = interp1(x2,y2,x);
%% full data length single cor coeff value
M = corrcoef(y1_new,y2_new);
corcoefficient = M(2,1)
%% running buffer cor coeff value
mybuffer = 10; % nb of samples in one buffer (buffer size)
overlap = mybuffer-1; % overlap expressed in samples
%%%% main loop %%%%
m = length(x);
shift = mybuffer-overlap; % nb of samples between 2 contiguous buffers
for ci=1:fix((m-mybuffer)/shift +1)
start_index = 1+(ci-1)*shift;
stop_index = min(start_index+ mybuffer-1,m);
time_index(ci) = floor((start_index+stop_index)/2); % time index expressed as sample unit (dt = 1 in this simulation)
M = corrcoef(y1_new(start_index:stop_index),y2_new(start_index:stop_index));
corcoefficient(ci) = M(2,1);
end
t2 = x(time_index);
figure(1),
subplot(2,1,1),plot(x,y1_new,x,y2_new);
legend('signal x ','signal y ');
subplot(2,1,2),plot(t2,corcoefficient,'-+r');
legend('cor coefficient');
0 Comments
See Also
Categories
Find more on Spectral Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!