-
Notifications
You must be signed in to change notification settings - Fork 14
/
my_cc_circ_shift.m
37 lines (30 loc) · 1.03 KB
/
my_cc_circ_shift.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
%Compute the cross covariance between x and y.
%
%INPUT
% x = signal #1.
% y = signal #2.
%
%OUTPUT
% rxy = the cross covariance between x and y.
% lag = the lag axis, useful for plotting.
function [rxy, lag] = my_cc_circ_shift(x,y)
N = length(x); %The size of the data.
rxyP = zeros(1,N-1); %The cross covariance at positive shifts.
rxyN = zeros(1,N-1); %The cross covariance at negative shifts.
lagP = zeros(1,N-1); %Lag axis for positive shifts.
lagN = zeros(1,N-1); %Lag axis for negative shifts.
for h=1:N-1 %Positive shifts.
temp = sum(circshift(x,[1,h]).*y);
rxyP(h)=temp;
lagP(h)=h;
end
for h=1:N-1 %Negative shifts.
temp = sum(circshift(x,[1,-h]).*y);
rxyN(N-1-(h-1))=temp;
lagN(N-1-(h-1))=-h;
end
temp = sum(x.*y); %Zero shift.
rxy0 = temp;
rxy = [rxyN rxy0 rxyP]/N; %Organize the results for output.
lag = [lagN 0 lagP];
end