Fits peaks to IR spectra with noise and many peaks


In [6]:
%matplotlib inline
from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
sys.path.append('..')
from spectra import getxy, find_peaks, fit_data, output_results, copy_range

In [8]:
x1, y1 = getxy('../tests/data/ir.txt')
plt.plot(x1, y1,'r-')


Out[8]:
[<matplotlib.lines.Line2D at 0x7fce3e0cf450>]

In [16]:
x, y2 = copy_range(x1, y1, 1730,1860)
y = y2 - min(y2)
plt.plot(x, y,'r-')
print len(x), len(y)


270 270

In [19]:
peak_pos = find_peaks(x, y, width=4, threshold=2)
plt.plot(x, y, 'b-', x[peak_pos], y[peak_pos], 'oy')


Out[19]:
[<matplotlib.lines.Line2D at 0x7fce3da542d0>,
 <matplotlib.lines.Line2D at 0x7fce3da544d0>]

In [20]:
out1 = fit_data(x, y, peak_pos, bg_ord=2)

In [21]:
output_results(out1)


Out[21]:
array([['bg_c0', '116.801432639', '29.5185518587'],
       ['bg_c1', '-0.129535160292', '0.0331257235763'],
       ['bg_c2', '3.5822539202e-05', '9.28601517424e-06'],
       ['p0_x0', '1752.85803753', '0.817247152999'],
       ['p0_amp', '0.240418839717', '0.0273642154126'],
       ['p0_fwhm', '16.8607868744', '3.85219505654'],
       ['p1_x0', '1771.20264889', '0.12984561467'],
       ['p1_amp', '1.38397185094', '0.0287477608983'],
       ['p1_fwhm', '14.1412441139', '0.620793021799'],
       ['p2_x0', '1792.09141916', '0.192997582209'],
       ['p2_amp', '1.78380213467', '0.0356992842252'],
       ['p2_fwhm', '18.7473456182', '0.861391587268'],
       ['p3_x0', '1805.07490544', '0.0976998162195'],
       ['p3_amp', '1.97997440291', '0.0480121302177'],
       ['p3_fwhm', '11.3394988099', '0.385076347815'],
       ['p4_x0', '1849.4910998', '0.06320575246'],
       ['p4_amp', '1.5255047622', '0.0290417375182'],
       ['p4_fwhm', '7.11125010028', '0.635105934408']], 
      dtype='|S17')

In [23]:
plt.plot(x, y, 'r-', x, out1.init_fit, 'b-')


Out[23]:
[<matplotlib.lines.Line2D at 0x7fce3d8f0a90>,
 <matplotlib.lines.Line2D at 0x7fce3d8f0c90>]

In [24]:
plt.plot(x, y, 'r-', x, out1.best_fit, 'b-')


Out[24]:
[<matplotlib.lines.Line2D at 0x7fce3dc1dd50>,
 <matplotlib.lines.Line2D at 0x7fce3dc1de50>]
# Not sure where these results are from (maybe igor pro or simliar), need to fit and check Voight results value stderr bg_c0 -9.384806 0 bg_c1 0.016558 0 bg_c2 -0.000007 0 p0_sigma 1.250000 0 p0_gamma 1.250000 0 p0_fwhm 4.501638 0 p0_amplitude 2.538092 0 p0_center 1065.314448 0 p1_sigma 1.250000 0 p1_gamma 1.250000 0 p1_fwhm 4.501638 0 p1_amplitude 1.414498 0 p1_center 1139.320155 0 p2_sigma 4.151534 0 p2_gamma 4.151534 0 p2_fwhm 14.950961 0 p2_amplitude 17.803132 0 p2_center 1184.345972 0 p3_sigma 2.567481 0 p3_gamma 2.567481 0 p3_fwhm 9.246296 0 p3_amplitude 17.375988 0 p3_center 1202.289114 0 p4_sigma 3.679720 0 p4_gamma 3.679720 0 p4_fwhm 13.251814 0 p4_amplitude 35.572812 0 p4_center 1222.724261 0 Lorentzian results value stderr bg_c0 -7.664510 5.954837e-01 bg_c1 0.013640 1.037670e-03 bg_c2 -0.000006 4.486933e-07 p0_sigma 1.439319 1.500126e-01 p0_fwhm 2.878638 3.000251e-01 p0_center 1065.442533 1.023172e-01 p0_amplitude 2.459493 1.878728e-01 p1_sigma 2.081789 3.822113e-01 p1_fwhm 4.163577 7.644226e-01 p1_center 1139.358966 2.508612e-01 p1_amplitude 1.746279 2.447761e-01 p2_sigma 7.003252 2.598720e-01 p2_fwhm 14.006505 5.197440e-01 p2_center 1184.175481 1.497438e-01 p2_amplitude 20.004475 6.529516e-01 p3_sigma 4.262647 1.298110e-01 p3_fwhm 8.525295 2.596219e-01 p3_center 1202.326301 7.283298e-02 p3_amplitude 18.786240 4.936277e-01 p4_sigma 5.780955 9.048515e-02 p4_fwhm 11.561909 1.809703e-01 p4_center 1222.955138 5.423523e-02 p4_amplitude 38.694216 4.970259e-01 Gaussian results value stderr bg_c0 -11.351931 5.742617e-01 bg_c1 0.019899 1.000551e-03 bg_c2 -0.000009 4.332050e-07 p0_sigma 1.485014 1.234517e-01 p0_fwhm 3.496942 2.907066e-01 p0_center 1065.377945 1.222493e-01 p0_amplitude 1.763530 1.293640e-01 p1_sigma 1.293947 2.138860e-01 p1_fwhm 3.047012 5.036631e-01 p1_center 1139.406807 2.107725e-01 p1_amplitude 0.831903 1.223004e-01 p2_sigma 7.004599 2.044291e-01 p2_fwhm 16.494569 4.813938e-01 p2_center 1184.797391 1.808091e-01 p2_amplitude 15.710914 4.081192e-01 p3_sigma 4.098608 8.873515e-02 p3_fwhm 9.651484 2.089553e-01 p3_center 1202.444133 8.389577e-02 p3_amplitude 15.203886 3.306228e-01 p4_sigma 6.248518 7.164588e-02 p4_fwhm 14.714136 1.687132e-01 p4_center 1222.461888 6.448730e-02 p4_amplitude 30.484000 3.145497e-01