Generates 1-D look-up table (LUT) optimal (least-square sense with continuity constraint) y-axis points from experimental (x,y) data given a vector of x-axis break points.
Note that x-axis break points should be chosen such that every bin has enough data points for correct estimation.
Please see lsq_lut_piecewise_test.m for an example with (synthetic) experimental data points.
No toolbox required.
% LSQ_LUT_PIECEWISE Piecewise linear interpolation for 1-D interpolation (table lookup)
% YI = lsq_lut_piecewise( x, y, XI ) obtain optimal (least-square sense)
% vector to be used with linear interpolation routine.
% The target is finding Y given X the minimization of function
% f = |y-interp1(XI,YI,x)|^2
% x measured data vector
% y measured data vector
% XI break points of 1-D table
% YI interpolation points of 1-D table
% y = interp1(XI,YI,x)
Guido Albertin (2020). Piecewise linear least square fit (https://www.mathworks.com/matlabcentral/fileexchange/40913-piecewise-linear-least-square-fit), MATLAB Central File Exchange. Retrieved .
Works lovely in 2017b - thank you!
Works like a charm!
Thanks a lot for this contribution. One quick question: how do you calculate slope of each segment furthermore?
Thanks and cheers,
Can we force this piecewise fit across some specific points?
Thanks a lot.
Excellent work, thanks so much.
i need it for multiparameters. may anyone help me please.
exactly what i was looking for. thanks.
Note that this does a very restricted subset of what my own SLM tools will compute. But given what it claims to do, does it do so well? Is the documentation good? Is it well written? Is the code robust?
The help is reasonable. I found internal comments that explain what is being done. I found an H1 line, so lookfor will be able to help you to find the tool when you forget the name. So I'll give it 5 stars in terms of documentation.
How about the code? I was happy to see backslash used to solve the linear system. A loop is used to put the points into bins, when histc will do this very cleanly and efficiently. In fact, the entire design matrix can be built in a vectorized form, much more efficiently than is done here. I'll also point out that a vector y is built up in an incremental fashion, increasing in size at each step of the loop. If there are many bins and many data points, this will be quite inefficient. I'd give the code 3 stars for this aspect because the fix is easy.
Finally, robustness issues. I would point out that this code will indeed fail when there are bins with no data points at all. This is a classical problem with least squares splines of any order, but it is easily resolved using a tiny amount of regularization to penalize non-smooth behavior. If the regularization parameter is set small enough, then no serious penalty happens, but the linear algebra no longer fails, and you still get out a nice smooth curve, regardless of the empty knot segments. I'm not really sure I can assign a significant downgrade of the code in this respect, because knowing how to implement a smoothness penalizing regularizer is not something one sees every day. The code simply needs to append a properly constructed matrix to the design matrix for the smoothness penalty.
Overall, 4 stars. Not bad, but could be better.
did exactly what I wanted in a fast and easy way, only 2 seconds work