-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterpolateSpectraTo1nm.m
64 lines (54 loc) · 2.19 KB
/
InterpolateSpectraTo1nm.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function result = InterpolateSpectraTo1nm(input_values, input_range, output_range)
%Interpolates input spectra to 1nm interval
%
%input_values = Spectra, Spectrum in each column
%
%input_range = [1st input sample waveleght;
% last input sample waveleght;
% sampling_interval]
%or same data in columns for all spectra in columns
%
%output_range = [1st output sample waveleght;
% last output sample waveleght]
%
%result = 1nm sampled spectra, Spectrum in each column
%
%If input_range has only one column same sampling and range is
%used for all input spectra
%
%Output range needs to be defined as integer values (1nm accuracy)
%If only one column in input range, repeat for all columns
if(size(input_range,2) == 1)
input_range_colums = repmat(input_range, 1, size(input_values,2));
%else use input_range directly
%(no validity check, must have correct size)
else
input_range_colums = input_range;
end
%Initialize result with zeros
result = zeros((1 + output_range(2,:)-output_range(1,:)),size(input_values,2));
%Result is sampled with 1nm interval, 1st value is the start wavelength,
%last value is the end wavelenght
result_sampling = output_range(1,:):output_range(2,:);
%Loop through all spectra in columns
for spectrum_index = 1:size(input_values,2)
%If there is some input range given
if(input_range_colums(2,spectrum_index)-input_range_colums(1,spectrum_index) > 0),
%Input sampling
input_sampling = input_range_colums(1,spectrum_index): ...
input_range_colums(3,spectrum_index): ...
input_range_colums(2,spectrum_index);
%Interpolate result to 1nm interval, fill values outside input range with 0
%Method can be chosen, initially 'linear'
result(:,spectrum_index) = ...
interp1(input_sampling, ...
input_values(1:length(input_sampling), ...
spectrum_index), ...
result_sampling, 'linear', 0);
%If input range was 0 or less
else
%result column is full of zeros
result(:,spectrum_index) = zeros((1 + output_range(2,:)-output_range(1,:)),1);
end
end
end