How can I fit a concave baseline curve to a spectrum?
Show older comments
I have a number of reflectance spectra with a consistent wavelength range (350-2500nm) (x). The reflectance (y) shows an overall convex shape that I want to fit for each spectrum, similar to a baseline. Here are two examples
Spectrum 1:

Spectrum 2:

Ideally the fitted curves would look something like this:
Spectrum 1 ideal:

Spectrum 2 ideal:

I have tried the "continuum removed" function: "CR = continuum_removed(v,'Sampling',x,'Plots','yes')
The result of which returned the error: "Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in continuum_removed (line 56) x_ext = [x(1)-small x x(end)+small];
Error in Untitled3 (line 12) CR = continuum_removed(v,'Sampling',x,'Plots','yes');"
Any help in fitting a curve would be greatly appreciated.
1 Comment
John BG
on 13 Jun 2016
Mr Longridge
I am half way to what you are asking for.
If I give you working code, not just guidelines, will you switch your accepted answer to mine?
on this question or posting a new question, with same problem I mean.
Appreciating time and attention
awaiting answer
John Bofarull Guix
Accepted Answer
More Answers (1)
John D'Errico
on 9 Jun 2016
2 votes
Um, the red curves as you have dawn them are not in fact convex. :)
Anyway, the simple answer is...
1. Using the curve as pairs of points in the (x,y) plane, generate the convex hull. Essentially, this will be a set of line segments in the (x,y) plane, a polygon.
2. Then compute the normal vectors for each line segment in that polygon.
3. Ensure the normal vectors are pointing outwards from the convex hull.
4. Drop all line segments with a normal vector that points downwards, so has a negative y-component.
What remains will be a convex polygon that touches the curve at a limited set of points. So it will be a piecewise linear upper envelope on the curve, or a least upper bound function, that has the property of being convex.
That is indeed what you have requested, although not what you have drawn. You can form a smooth approximation to that curve using pchip. Do NOT use a traditional spline here though. In fact, even pchip might be problematic, in the sense that there MAY be some points where it does not exactly satisfy the envelope condition. (I'd need to think about that to be sure.)
Alternatively, you could use my SLM toolbox, which has the capability to fit a least upper bound function, that is also convex. Since it is indeed a spline, it will also be as smooth as possible.
3 Comments
Jacob Longridge
on 9 Jun 2016
Image Analyst
on 9 Jun 2016
Are you going to try the convhull() function John suggested to get the convex hull? This will give the "tips" of the peaks.
John D'Errico
on 9 Jun 2016
No. Look carefully at what you have drawn. Pick the second IR curve for example, or the first one.
Do you see regions in that curve where despite having an overall negative second derivative, there are regions where the curvature is a positive number as you have drawn it?
I honestly don't care if you call it convex or concave. It cannot be both. Period.
As for using SLM, Did you read the help? Apparently you did enough to find a typo on my part. But how do you think it will know that you want an upper or lower bound function unless you tell it so????? The mind reading option is not working.
From the help for slmset:
'envelope' - allows the user to solve for an envelope of the data
= 'off' --> the curve will be a simple least squares spline
= 'supremum' --> comute a model such that all residuals
(yhat - y) are positive. In effect, the curve will be a
"supremum" (least upper bound) function.
= 'infimum' --> comute a model such that all residuals
(yhat - y) are negative. In effect, the curve will be a
"infimum" (greatest lower bound) function.
DEFAULT VALUE: 'off'
READ THE HELP, instead of running a spell checker on it.
And finally, the simple solution is as I outlined it, using a convex hull.
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




