资源简介
可以计算两个时间序列的超前滞后相关的matlab程序。并可同时计算对应相关系数的置信水平,置信度可以自己设置
代码片段和文件信息
function [cortimelowup]=lag_corr_sig(data1data2alp)
%%% a simple function for caculate the lag correlation of two timeseries
%
% inputs:
% data1 and data2 are two time series.
% alp : (Optioal) A number between 0 and 1 to specify a confidence level
% of 100*(1 - alpha)%. Default is 0.05 for 95% confidence intervals.
% outputs:
% cor: the correlation of data1 and tata2
% time: lag and lead time for data1 and data2.
% low: lower bounds for a confidence interval for each coefficient if
% nargin is 2 then the interval level is 95% for default.
% up: uper bounds for a confidence interval for each coefficient.
%
% Example
% x=rand(1100);y=rand(1100);
% [cortimelowup]=lag_corr_sig(xy);
% plot(timecor);errorbar(time(1:10:end)cor(1:10:end)low(1:10:end)up(1:1
% 0:end));
% or you can use the simple output just calculate the lag correlation
% [cortimelowup]=lag_corr_sig(xy);plot(timecor);
len=length(data1);
lag_time=floor([len/3]);%usually the lagtime is 1/3 of the whole length of the data.
cor=zeros(lag_time1);p=zeros(lag_time1);l=lag_time;
if len~=length(data2)
error(‘you must use two same length series‘);
end
if size(data11) data1=data1‘;
data2=data2‘;
end
if nargin == 2
for j=1:lag_time %data1 lead data2
ab=data1(1:len-j);
ac=data2(j+1:len);
avex=mean(ab);avey=mean(ac);
sx=sqrt(sum((ab-avex).^2)/(len-j));sy=sqrt(sum((ac-avey).^2)/(len-j));
cor1(j)=sum(((ab-avex)/sx).*((ac-avey)/sy))/(len-j);
if nargout == 4
[RPlow_matup_mat]=corrcoef(abac);
low1(j)=low_mat(12);up1(j)=up_mat(12);
end
clear ab ac sx
end
for j=1:lag_time
ab=data1(j+1:len);
ac=data2(1:len-j);
avex=mean(ab);avey=mean(ac);
sx=sqrt(sum((ab-avex).^2)/(len-j));sy=sqrt(sum((ac-avey).^2)/(len-j));
cor2(j)=sum(((ab-avex)/sx).*((ac-avey)/sy))/(len-j)
评论
共有 条评论