clear
load('D:\1tempformatlab\R.txt')
load('D:\1tempformatlab\CIE color matching functions.txt')
m=1000;
xx=linspace(0.38,0.78,m);%用于三次样条差值拟合的横坐标
xlambda=spline(CIE_color_matching_functions(:,1)./1000,CIE_color_matching_functions(:,2),xx)';%CIE1931标准色度系统色匹配函数x
ylambda=spline(CIE_color_matching_functions(:,1)./1000,CIE_color_matching_functions(:,3),xx)';%CIE1931标准色度系统色匹配函数y
zlambda=spline(CIE_color_matching_functions(:,1)./1000,CIE_color_matching_functions(:,4),xx)';%CIE1931标准色度系统色匹配函数z
Ilambda=linspace(100,100,m)';%光源函数
xr=R(:,1);
yr=R(:,2);
index=intersect(find(xr>=0.38),find(xr<=0.78));%寻找可见光波段横坐标的交集
xvisible=xr(index);%可见光波段的横坐标
yvisible=yr(index);%可见光波段的纵坐标
Rvisible=spline(xvisible,yvisible,xx)';%可见光波段反射谱拟合
N=trapz(xx,ylambda.*Ilambda);
X=(1/N).*(trapz(xx,Ilambda.*Rvisible.*xlambda));
Y=(1/N).*(trapz(xx,Ilambda.*Rvisible.*ylambda));
Z=(1/N).*(trapz(xx,Ilambda.*Rvisible.*zlambda));
x=X/(X+Y+Z);
y=Y/(X+Y+Z);