%%程序说明
%%本程序对光谱进行重采样
clc,clear
result=zeros(256,2);%存放输出数组,根据重采样之后波段数是256。
%file_1是重采样的标准波段设置文件;
file_1='E:\岩心matlab计算样本\new.txt';
rfid_1=fopen(file_1);
count_1=0;data1=[];
while 1
tline_1=fgetl(rfid_1);
count_1=count_1+1;
if ~ischar(tline_1), break, end
if count_1>=1 %从文件起始位置开始读数据
t_1=str2num(tline_1);
data1=vertcat(data1,t_1);
end
end
%批处理
%以下为需要设置参数
fpath='E:\岩心matlab计算样本\';%输出文件路径
sname='resample_mineral'; %编号前缀,如veg,soil.rock,water
fouttype='.txt'; %设置输出文件类型,xls或txt
num=1;
file=dir([fpath,'*.xls']);
for i=1:length(file)
%依次打开文件
rfid=fopen([fpath,file(i).name]);
count=0;data2=[];
%读PSR数据到d
while 1
tline = fgetl(rfid);
count=count+1;
if ~ischar(tline), break, end
if count>=1 %xls行开始写入光谱数据
t=str2num(tline);
data2=vertcat(data2,t);
end
end
tsl = timeseries(data2(:,4),(data2(:,1))','Name','resample');
%(data1(:,1))光谱wavelength;data1(:,2)光谱对应的value
% View the time, data. amd interpolation method:
tsl.time;
tsl.data;
tsl.getinterpmethod;
% Resample ts1 using its default interpolation method:
res_ts=resample(tsl,(data1(:,1))');
% View the time, data, and interpolation method for the resampled object:
res_ts.time;
res_ts.data;
res_ts.getinterpmethod;
%将重采样结果保存在矩阵中,方便下一步的保存;
result(:,1)=res_ts.time;result(:,2)=res_ts.data;
%将结果保存
fileout=[fpath,sname,num2str(num),fouttype]
fidout=fopen(fileout,'wt');
dlmwrite(fileout, result, 'delimiter', '\t', 'precision', 6, 'newline', 'pc')
%fprintf(fidout,'%f\n',data);%输出格式不对
fclose(fidout);
num=num+1;
end