Global Fitting

In this example, the global fitting routine is introduced. The routine allows the fitting of several spectra based on one initial model. The routine is based on the lmfit approach for Fit Multiple Data Sets. In this case of SpectraFit the following parameter are set global and local:

  • global_parameters: The global parameters are the parameters that are fitted for all spectra: center, width, offset.
  • local_parameters: The local parameters are the parameters that are fitted for each spectrum: amplitude
global_data.csv

Correct Data Format for Global Fits

For the correct fitting the data file has to contain only spectra data; meaning energy and intensity columns. No other columns are allowed!!

Scdoc
energy,y_1,y_2,y_3
0.0,0.2419718395587794,0.35036325018282816,0.2512212899764966
0.10101010101010101,0.26632895799481815,0.3502175834343554,0.28006135290389794
0.20202020202020202,0.29016252748280824,0.3688802798218712,0.30229415962900624
0.30303030303030304,0.3129204023527142,0.35590273389466653,0.31880069003423034
0.40404040404040403,0.3340384218160627,0.4079933177466349,0.336761909067558
0.5050505050505051,0.3529632658317558,0.41643148980037836,0.35539945022604585
0.6060606060606061,0.3691764316805938,0.46375789383451615,0.37271770008458227
0.7070707070707071,0.3822179175787332,0.5222843189540483,0.3860967111690663
0.8080808080808081,0.3917081371880747,0.4153968013642272,0.4013122337333337
0.9090909090909091,0.397366645658234,0.5063128037564405,0.4093568810547312
1.0101010101010102,0.3990264346416763,0.5189243778487673,0.41093627737932836
1.1111111111111112,0.3966428389866639,0.4723987120004229,0.40264135720513744
1.2121212121212122,0.3902964683703953,0.523373089149355,0.40479369100416324
1.3131313131313131,0.38019000044460144,0.5004826262922106,0.3858795084933437
1.4141414141414141,0.36663910960570206,0.437478027495442,0.3831990083425011
1.5151515151515151,0.35005821694987166,0.46115202813417366,0.3649819458319575
1.6161616161616161,0.33094209463941376,0.48825084507172944,0.33130913571620707
1.7171717171717171,0.3098446108459917,0.37885952432959014,0.31748366332749206
1.8181818181818181,0.287356038731983,0.4059277305681741,0.29259472917496165
1.9191919191919191,0.2640803657495641,0.37375331255000677,0.27314767477281415
2.0202020202020203,0.24061393173411683,0.28595557660589094,0.2580392464764451
2.121212121212121,0.21752651153225627,0.365690892871207,0.2277438249619412
2.2222222222222223,0.19534566560640101,0.35264473499692367,0.20521034856723713
2.323232323232323,0.17454484214240149,0.2941591260029418,0.18671320449947262
2.4242424242424243,0.15553536153197817,0.19544175985914733,0.16001844132350285
2.525252525252525,0.1386620828033239,0.17611240410628526,0.14454635034071794
2.6262626262626263,0.12420227157981423,0.21647111918646172,0.13311287088664925
2.727272727272727,0.11236698357892479,0.20923638404829742,0.12298637732616499
2.8282828282828283,0.10330416103067669,0.24301214839783356,0.1141890415115003
2.929292929292929,0.0971026167947168,0.1846390705598982,0.10285485971267169
3.0303030303030303,0.09379614821941375,0.17417527833709784,0.10050867917308377
3.131313131313131,0.09336716745011825,0.15440776056953087,0.10069244637808268
3.2323232323232323,0.09574943780385352,0.1614710310818758,0.10920472448510052
3.3333333333333335,0.10082974318827102,0.23917063086821988,0.11113979445013676
3.4343434343434343,0.10844856319827069,0.23906519657442696,0.11964554423637763
3.5353535353535355,0.11840005430305738,0.185137492297554,0.13382467129696818
3.6363636363636362,0.13043182350565388,0.21178124555631211,0.1342855248182133
3.7373737373737375,0.14424510535201246,0.16143234568542197,0.1560526436789465
3.8383838383838382,0.15949600249002968,0.20933015208380787,0.1683435892119578
3.9393939393939394,0.17579841764880832,0.25019527891171345,0.1784696397037549
4.040404040404041,0.19272919236172995,0.32552581942798375,0.20615239344598849
4.141414141414141,0.20983578440772235,0.2932708677541437,0.22194355270855873
4.242424242424242,0.22664657854430872,0.3081215629135999,0.23530374144895616
4.343434343434343,0.24268365638392028,0.38520056536274083,0.2452834351104565
4.444444444444445,0.2574775779153426,0.4036517972055121,0.2734912799066593
4.545454545454545,0.2705834773153452,0.36523358566038555,0.2827626322195374
4.646464646464646,0.2815975760415347,0.4038570079917979,0.2983007893094095
4.747474747474747,0.29017308919843954,0.38568444109761335,0.3032663221822144
4.848484848484849,0.2960344624877284,0.3728118956346913,0.3101873787210576
4.94949494949495,0.2989889336697853,0.38606631982800826,0.311033627204741
5.05050505050505,0.2989345616844333,0.46599952623182306,0.30890775906787193
5.151515151515151,0.29586409612082076,0.41720009712347705,0.31446405506941183
5.252525252525253,0.2898643489097391,0.3978805373834097,0.29598603855889627
5.353535353535354,0.28111105204361964,0.39825962134996457,0.28686237466767855
5.454545454545454,0.2698595096365714,0.33923478930509804,0.28012202957855487
5.555555555555555,0.25643164957939424,0.32397898668915726,0.2661958444768996
5.656565656565657,0.24120032260087373,0.29482429157056955,0.2528426126583375
5.757575757575758,0.2245718640529986,0.34440618635570847,0.23528514295626335
5.858585858585858,0.20696801373394533,0.321043737549401,0.2225829393766783
5.959595959595959,0.18880827830906066,0.22982453226120228,0.19541682400166696
6.0606060606060606,0.17049372526515405,0.3524689792639276,0.17633235416182982
6.161616161616162,0.15239303067775103,0.2916758060392339,0.15754756928059752
6.262626262626262,0.13483138518048135,0.1701532093125915,0.14588505862135276
6.363636363636363,0.1180826165999828,0.25486367153484046,0.1280652681440698
6.4646464646464645,0.1023646376222611,0.22609431802203062,0.10864517025852855
6.565656565656566,0.08783809454258623,0.1827569415348753,0.09215509779192388
6.666666666666667,0.07460789656344895,0.2578564973920618,0.07797855687466268
6.767676767676767,0.06272715674885718,0.1681908664576616,0.08069974587228344
6.8686868686868685,0.05220298203877285,0.1439421621017304,0.06184815934481232
6.96969696969697,0.04300351112765476,0.18915960212382008,0.06092207828108523
7.070707070707071,0.03506561077034345,0.11924863750148582,0.04219088166179979
7.171717171717171,0.028302694492219067,0.1899023278636362,0.04320449878330073
7.2727272727272725,0.02261221157731021,0.16987745012151412,0.02870127060116931
7.373737373737374,0.017882456478267986,0.11920741950473637,0.028317322454312833
7.474747474747475,0.013998457765025014,0.10525464615149546,0.019351481239332922
7.575757575757575,0.010846811260906253,0.09710457629794428,0.019610610074285557
7.6767676767676765,0.008319416212832886,0.11226703986573822,0.01787005836621023
7.777777777777778,0.006316150905310992,0.14858385560327475,0.016018189152513816
7.878787878787879,0.004746582342620543,0.10826397760767079,0.012281358555432859
7.979797979797979,0.0035308430828626595,0.09626654714870186,0.01691507822920174
8.080808080808081,0.002599828444403308,0.10801343336430928,0.0158984153824461
8.181818181818182,0.0018948718360943435,0.08312078984411232,0.015356442053566066
8.282828282828282,0.0013670483232129816,0.0800649227546427,0.01020942150687729
8.383838383838384,0.0009762403818145284,0.047820231796681156,0.01142454441107308
8.484848484848484,0.000690078577489922,0.09445547295854599,0.007485526347581316
8.585858585858587,0.0004828466235725318,0.07606281824881235,0.01146403478705961
8.686868686868687,0.00033441729366460055,0.09578053378172673,0.01263455963109994
8.787878787878787,0.00022926466913550123,0.09118251265798016,0.013146056664029184
8.88888888888889,0.00015558023158815387,0.0751349614928704,0.008979428550340542
8.98989898989899,0.00010450584235670396,0.10306972998885719,0.00656350219728153
9.09090909090909,6.948572649469218e-05,0.1298194212311276,0.0025187244137908613
9.191919191919192,4.573192796581058e-05,0.12557640592551106,0.013055941245999134
9.292929292929292,2.9792866786525854e-05,0.17756855532180232,0.009447900246448688
9.393939393939394,1.921206089655364e-05,0.10472988345894148,0.008516838659603503
9.494949494949495,1.226321917195423e-05,0.05128980491588907,0.010542137767284956
9.595959595959595,7.748254936027263e-06,0.07522248420973705,0.007639600090337453
9.696969696969697,4.845874794501611e-06,0.12426871691096668,0.005829177868155318
9.797979797979798,2.9999177793186746e-06,0.14884376088021908,0.018783274063962605
9.8989898989899,1.8382957822092802e-06,0.1084150307608363,0.015388290419153256
10.0,1.1150396360517513e-06,0.007827194075870354,0.016627220257220902

How to use Global Fit

Global fit can be used in two ways:

  1. Automatic global fit via option 1 via Input File:
    JSON
    {
    "settings": {
      "column": ["energy"],
      "decimal": ".",
      "header": 0,
      "infile": "data_global.csv",
      "outfile": "example_6",
      "oversampling": false,
      "separator": ",",
      "shift": 0.2,
      "smooth": false,
      "verbose": 1,
    
      "noplot": false,
      "global": 1
    },
    
    or via Command Line.
    Bash
    spectrafit global_data.csv -i input.json -g 1
    
  2. Hand defined global fit via option 2 via Input File or Command Line.
    Bash
    spectrafit global_data.csv -i input.json -g 2
    
  3. Similiar to classic local fits, the global fits only requires the peak definition for the first spectra, and the global fit will be automatically generate the corresponding peaks for the other spectra.
    JSON
        "peaks": {
          "1": {
            "pseudovoigt": {
              "amplitude": {
                "max": 200,
                "min": 0,
                "vary": true,
                "value": 1
              },
              ...
            }
          },
          "2": {
            "pseudovoigt": {
              "amplitude": {
                "max": 200,
                "min": 0,
                "vary": true,
                "value": 1
              },
              ...
            }
          }
        }
    

In case of using hand defined peaks for global fit, the input has to look like:

JSON
  "peaks": {
     "1": {
         "1": {
             "pseudovoigt": {
                 "amplitude": {
                     "max": 200,
                     "min": 0,
                     "vary": True,
                     "value": 1,
                 },
                 "center": {
                     "max": 200,
                     "min": -200,
                     "vary": True,
                     "value": 0,
                 },
                 "fwhmg": {
                     "max": 2.5,
                     "min": 0.00002,
                     "vary": True,
                     "value": 0.1,
                 },
                 "fwhml": {
                     "max": 2.5,
                     "min": 0.00001,
                     "vary": True,
                     "value": 1,
                 },
             }
         }
     }
This will allow separating between globally defined peaks and locally defined peaks in between the spectra. Important is that the first number corresponds to the spectra, the second number corresponds to the peak.

_

The global fit of two peaks for three independent spectra is shown. The position of the peaks and the broadening is dependently optimized for all three spectra, but the amplitude is optimized independently.