2

I am having problems trying to find the FWHM of some data. I initially tried to fit a curve using interpolate.interp1d. With this I was able to create a function that when I entered an x value it would return an interpolated y value. The issue is that I need the inverse of this functionality. In other words, I want to switch my independent and dependent variables. When I try to switch them, I get errors because the independent data has to be sorted. If I sort the data, I will lose the indexes, and therefore lose the shape of my graph.

I tried:

x = np.linspace(0, line.shape[0], line.shape[0])
self.x_curve = interpolate.interp1d(x, y, 'linear')

where y is my data.

To get the inverse, I tried:

self.x_curve = interpolate.interp1d(sorted(y), x, 'linear')

but the values are off.

I then moved on and tried to use UnivariateSpline and get the roots to find the FWHM (from this question here: Finding the full width half maximum of a peak), but the roots() method keeps giving me an empty list [].

This is what I used:

x_curve = interpolate.UnivariateSpline(x, y)
r = x_curve.roots()
print(r)

Here is an image of the data (with the UnivariateSpline):

Data plot

Any ideas? Thanks.

Community
  • 1
  • 1
SFBA26
  • 870
  • 3
  • 12
  • 24

1 Answers1

2

Using UnivariateSpline.roots() to get FWHM will only work if you shift the data so that its value is 0 at FWHM.

Seeing that the background of the data is noisy, I'd first estimate the baseline. For example:

y_baseline = y[(x<200) & (x>350)].mean()

(adjust the limits for x as you see fit). Then shift the data so that the middle of the baseline and the peak is at 0. Seeing that your data has a minimum and not a maximum as in the example, I'm using y.min():

y_shifted = y - (y.min()+y_baseline)/2.0

Now fit a spline to this shifted data and roots() should be able to find the roots, the difference of which is the FWHM.

x_curve = interpolate.UnivariateSpline(x, y_shifted, s=0)
x_curve.roots()

Increase the s parameter if you want to estimate the FWHM from smoothed data.

Jsl
  • 842
  • 1
  • 5
  • 10
  • Thank you for your response. This what I tried, but I am still not getting the roots: `x = np.linspace(0, line.shape[0], line.shape[0], False) x = map(int, x) y_baseline = y[(x<200) & (x>350)].mean() y_shifted = y - (y.min() + y_baseline)/2.0 curve = interpolate.UnivariateSpline(x, y_shifted, s=0) r = curve.roots() print(r)` I am still getting an empty list. The curve shifted down to below zero. – SFBA26 Jun 24 '14 at 16:22
  • Nevermind, the min function had some conflict with numpy and wasn't working correctly. I fixed it and I am now getting the roots. Thanks for your help! – SFBA26 Jun 24 '14 at 16:37