1、xcorr
本质上是两个函数做内积运算
相关算法有两种:
在Matlab上既可以 1.用自带的xcorr函数计算互相关,2.通过在频域上乘以共轭复频谱来计算互相关;
网友验证程序
clc;clear;close all;
% s1,s2为样例数据
s1 = [-0.00430297851562500;-0.00366210937500000;-0.00466918945312500;-0.00332641601562500;-0.000640869140625000;0.00167846679687500;0.00204467773437500;0.00247192382812500;0.00323486328125000;0.00451660156250000;0.00607299804687500;0.00610351562500000;0.00582885742187500;0.00592041015625000;0.00631713867187500;0.00521850585937500;0.00320434570312500;0.00189208984375000;0.00158691406250000;0.000305175781250000;-0.00238037109375000;-0.00378417968750000;-0.00366210937500000;-0.00396728515625000;-0.00485229492187500;-0.00521850585937500;-0.00482177734375000;-0.00442504882812500;-0.00427246093750000;-0.00378417968750000;-0.00363159179687500;-0.00296020507812500];
s2 = [-0.00531005859375000;-0.00430297851562500;-0.00439453125000000;-0.00381469726562500;-0.00219726562500000;-0.000518798828125000;0.000823974609375000;0.00186157226562500;0.00250244140625000;0.00259399414062500;0.00372314453125000;0.00479125976562500;0.00427246093750000;0.00405883789062500;0.00549316406250000;0.00509643554687500;0.00390625000000000;0.00277709960937500;0.00231933593750000;0.00158691406250000;3.05175781250000e-05;-0.00137329101562500;-0.00161743164062500;-0.00149536132812500;-0.00299072265625000;-0.00402832031250000;-0.00408935546875000;-0.00411987304687500;-0.00442504882812500;-0.00424194335937500;-0.00387573242187500;-0.00323486328125000];
% 计算互相关1
cross_corr1 = xcorr(s1,s2,'coeff');
% 计算互相关2:频域计算互相关
in1 = zeros(2*length(s1)-1,1);
in2 = in1;
in1(1:length(s1)) = s1;
in2(1:length(s1)) = s2;
cross_corr2 = ifft(fft(in1).*conj(fft(in2)))/(norm(in1) * norm(in2)); % 3[Re|Im] 归一化
cross_corr2 = circshift(cross_corr2, length(s1)-1);
figure(1);
subplot(211);
plot(cross_corr1);
subplot(212);
plot(cross_corr2);
统计学习–三种常见的相关系数