Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a synthetic spectrum with a wavelength range of .3 to .7 microns. I have interpolated this spectrum for re-sampling purposes. However, I am also attempting to calculate the first and second derivatives of the interpolated spectrum to find the locations of local maxima. However, numerical differentiation keeps producing an error, claiming that my input value is outside of the range of the data in the interpolating function.

The important parts of my code are:

template = Import["C:\\Users\\John\\Documents\\Research\\KOI89\\template.txt","Table"];
t = Interpolation[template, InterpolationOrder -> 1]
ND[t[x], x, .4]

my "template" dataset has a domain of 0.3 to 0.7, and contains about 250,000 data points. However, the ND command produces the error:

InterpolatingFunction::dmval: "Input value {1.4} lies outside the range of data in the interpolating function. Extrapolation will be used. "
InterpolatingFunction::dmval: Input value {0.9} lies outside the range of data in the interpolating function. Extrapolation will be used.

I either need to figure out what is causing that error, or figure out another way of finding the location of all the peaks in my spectra. I tried a simple peak-finding For-loop but it is not giving me good results.

peaks = {};
For[n = 4, n <= Length[template] - 4, n++, 
If[0.08 < template[[n - 3, 2]] < template[[n - 2, 2]] < template[[n - 1, 2]] < template[[n, 2]] > template[[n + 1, 2]] >   template[[n + 2, 2]] > template[[n + 3, 2]] > 0.08, peaks = Append[peaks, template[[n, All]]]]]

I am looking for simple ideas on how to find the peaks of my spectrum.

share|improve this question
Why do you need numerical differentiation? Can you just use symbolic differentiation instead (which is possible for interpolating functions)? Also, if you use first order interpolation, then the derivative will be a step function: constant on each interval. –  Szabolcs Mar 27 '14 at 17:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.