# Applying a weighting function which is sampled at different points

I have a set of data to which I want to apply a weighting function, so basically a simple multiplication.

My sampled data looks like this:

(for those interested, it's the relative luminous intensity of an LED)

The weighting function is only available in a tabular form as well, and looks like this:

(it's the standard luminosity function, which would probably be available in other formats, but I have another weighting which isn't)

As it happens, the points don't coincide on the X-Axis, so I can't just multiply the values, but have to do some interpolation of the weighting function to get the "correct" weighting factor for the points of my sampled data.

I was wondering if there is already a function / package available for Octave which handles the multiplication of the Y-values with interpolation of the X-values.

As I'm not a native speaker and am working mostly on electrical engineering problems, my vocabulary of the right keywords ran out quickly. It's probably not all that hard to implement this, but I try not to reinvent the wheel over and over again.

• Use interp1 to interpolate the weighting function to the sampled data point. – Biswajit Banerjee Nov 14 '15 at 21:35

I implemented this in a crude way (I'm not using Octave or Matlab very often so I'm not used to vector and matrix data types and how to make the most use of them).

The function accepts a X-axis value, needs a weighting matrix where the first column contains the x-values and the second column the weighting factors. It also needs two standard values which are chosen if the supplied X-axis value is outside of the range of weighting matrix.

It returns a linear interpolation of the weighting factor, or if the weighting matrix is malformed (multiple entries for one x-point) the larger weighting factor.

It is horrible, does no checks and probably fails for everyone but me. Maybe it's a starting point for someone else.

function retval = Weighting ( x, weightingMatrix, weightingBelowMinimum, weightingAboveMaximum)

if (x < weightingMatrix(1,1))
retval = weightingBelowMinimum;
elseif ( x > weightingMatrix(size(weightingMatrix,1),1))
retval = weightingAboveMaximum;
else
smallerWeightIndex = 0;
biggerWeightIndex = 0;
for i=1:size(weightingMatrix,1)
if (x<weightingMatrix(i,1))
biggerWeightIndex = i;
smallerWeightIndex = i-1;
break;
endif
endfor

if (weightingMatrix(biggerWeightIndex,2)==weightingMatrix(smallerWeightIndex,2))
retval = weightingMatrix(biggerWeightIndex,2);
elseif (weightingMatrix(biggerWeightIndex,1)==weightingMatrix(smallerWeightIndex,1))
retval = max(weightingMatrix(biggerWeightIndex,2),weightingMatrix(smallerWeightIndex,2));
else
m = (weightingMatrix(biggerWeightIndex,2)-weightingMatrix(smallerWeightIndex,2))/(weightingMatrix(biggerWeightIndex,1)-weightingMatrix(smallerWeightIndex,1));
c = weightingMatrix(biggerWeightIndex,2)-(m*weightingMatrix(biggerWeightIndex,1));
retval = m*x+c;
endif
endif

endfunction