In [4]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
result = "20180514_5.0GHz.txt"
data = pd.read_csv(result,sep="\t")
In [5]:
data[0:1601]
Out[5]:
B_mT S11_dB S11_phase S11_real S11_imaginary S21_dB S21_phase S21_real S21_imaginary S12_dB S12_phase S12_real S12_imaginary S22_dB S22_real S22_imaginary S22_phase
0 0.000 -11.783080 0.0 0.047650 0.253094 -4.067295 0.0 0.602849 0.168994 -4.014208 0.0 0.607438 0.166810 -12.612795 0.0 -0.040415 0.230562
1 0.062 -11.783219 0.0 0.047653 0.253090 -4.067432 0.0 0.602840 0.168991 -4.014021 0.0 0.607449 0.166822 -12.612729 0.0 -0.040417 0.230564
2 0.125 -11.783155 0.0 0.047653 0.253091 -4.067382 0.0 0.602842 0.168996 -4.014062 0.0 0.607448 0.166817 -12.612816 0.0 -0.040419 0.230561
3 0.187 -11.783134 0.0 0.047656 0.253092 -4.067434 0.0 0.602839 0.168992 -4.013960 0.0 0.607454 0.166821 -12.612739 0.0 -0.040417 0.230564
4 0.250 -11.783302 0.0 0.047652 0.253087 -4.067490 0.0 0.602835 0.168992 -4.014032 0.0 0.607448 0.166825 -12.612862 0.0 -0.040418 0.230560
5 0.312 -11.783409 0.0 0.047654 0.253084 -4.067497 0.0 0.602834 0.168992 -4.013979 0.0 0.607451 0.166828 -12.612778 0.0 -0.040418 0.230562
6 0.375 -11.783501 0.0 0.047651 0.253081 -4.067420 0.0 0.602840 0.168992 -4.014090 0.0 0.607443 0.166824 -12.612747 0.0 -0.040417 0.230563
7 0.437 -11.783439 0.0 0.047642 0.253085 -4.067319 0.0 0.602846 0.168999 -4.014253 0.0 0.607434 0.166815 -12.612899 0.0 -0.040416 0.230559
8 0.500 -11.783543 0.0 0.047644 0.253082 -4.067291 0.0 0.602847 0.169001 -4.014226 0.0 0.607434 0.166821 -12.612884 0.0 -0.040416 0.230560
9 0.562 -11.783554 0.0 0.047644 0.253081 -4.067148 0.0 0.602857 0.169004 -4.014062 0.0 0.607442 0.166835 -12.612854 0.0 -0.040418 0.230560
10 0.625 -11.783507 0.0 0.047643 0.253083 -4.067090 0.0 0.602861 0.169007 -4.014099 0.0 0.607437 0.166845 -12.612757 0.0 -0.040422 0.230562
11 0.687 -11.783765 0.0 0.047640 0.253075 -4.067279 0.0 0.602847 0.169005 -4.014019 0.0 0.607443 0.166844 -12.612840 0.0 -0.040424 0.230560
12 0.750 -11.783334 0.0 0.047650 0.253087 -4.067166 0.0 0.602856 0.169004 -4.013978 0.0 0.607446 0.166846 -12.612874 0.0 -0.040431 0.230557
13 0.812 -11.783447 0.0 0.047648 0.253084 -4.067240 0.0 0.602850 0.169004 -4.013993 0.0 0.607445 0.166844 -12.612778 0.0 -0.040426 0.230561
14 0.875 -11.783470 0.0 0.047645 0.253083 -4.067349 0.0 0.602843 0.169000 -4.014141 0.0 0.607432 0.166850 -12.612839 0.0 -0.040429 0.230559
15 0.937 -11.783562 0.0 0.047654 0.253079 -4.067289 0.0 0.602847 0.169002 -4.014195 0.0 0.607428 0.166851 -12.612954 0.0 -0.040429 0.230556
16 1.000 -11.783636 0.0 0.047652 0.253077 -4.067123 0.0 0.602859 0.169003 -4.014012 0.0 0.607441 0.166854 -12.612903 0.0 -0.040435 0.230556
17 1.062 -11.783477 0.0 0.047656 0.253081 -4.066982 0.0 0.602869 0.169005 -4.014035 0.0 0.607439 0.166855 -12.612964 0.0 -0.040438 0.230554
18 1.125 -11.783324 0.0 0.047659 0.253085 -4.067163 0.0 0.602856 0.169005 -4.014096 0.0 0.607435 0.166854 -12.613019 0.0 -0.040437 0.230552
19 1.187 -11.783345 0.0 0.047655 0.253085 -4.067107 0.0 0.602862 0.169000 -4.014297 0.0 0.607420 0.166853 -12.613004 0.0 -0.040438 0.230553
20 1.250 -11.783292 0.0 0.047654 0.253087 -4.067101 0.0 0.602860 0.169006 -4.014206 0.0 0.607426 0.166855 -12.612895 0.0 -0.040440 0.230555
21 1.312 -11.783446 0.0 0.047654 0.253083 -4.067042 0.0 0.602864 0.169009 -4.014292 0.0 0.607418 0.166861 -12.613001 0.0 -0.040441 0.230552
22 1.375 -11.783513 0.0 0.047658 0.253080 -4.067165 0.0 0.602857 0.169000 -4.014223 0.0 0.607423 0.166861 -12.612997 0.0 -0.040437 0.230553
23 1.437 -11.783436 0.0 0.047659 0.253082 -4.067172 0.0 0.602857 0.168999 -4.014273 0.0 0.607419 0.166864 -12.612932 0.0 -0.040442 0.230554
24 1.500 -11.783214 0.0 0.047658 0.253089 -4.067166 0.0 0.602856 0.169005 -4.014325 0.0 0.607414 0.166865 -12.612942 0.0 -0.040443 0.230553
25 1.562 -11.783388 0.0 0.047655 0.253084 -4.067291 0.0 0.602847 0.169001 -4.014182 0.0 0.607424 0.166868 -12.613011 0.0 -0.040450 0.230550
26 1.625 -11.783263 0.0 0.047657 0.253087 -4.067237 0.0 0.602851 0.169003 -4.014209 0.0 0.607421 0.166874 -12.613059 0.0 -0.040446 0.230550
27 1.687 -11.783302 0.0 0.047659 0.253086 -4.067242 0.0 0.602852 0.168996 -4.014210 0.0 0.607420 0.166876 -12.613070 0.0 -0.040448 0.230549
28 1.750 -11.783380 0.0 0.047652 0.253085 -4.067217 0.0 0.602854 0.168997 -4.014209 0.0 0.607419 0.166881 -12.612977 0.0 -0.040448 0.230552
29 1.812 -11.783181 0.0 0.047652 0.253091 -4.067046 0.0 0.602867 0.168997 -4.014222 0.0 0.607417 0.166882 -12.612953 0.0 -0.040452 0.230552
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1571 98.187 -11.766663 0.0 0.047799 0.253562 -4.060539 0.0 0.603301 0.169188 -4.008798 0.0 0.607985 0.166299 -12.605172 0.0 -0.039858 0.230868
1572 98.250 -11.766541 0.0 0.047799 0.253566 -4.060622 0.0 0.603297 0.169179 -4.008872 0.0 0.607983 0.166287 -12.605033 0.0 -0.039859 0.230872
1573 98.312 -11.766532 0.0 0.047804 0.253565 -4.060551 0.0 0.603302 0.169181 -4.008792 0.0 0.607989 0.166286 -12.604947 0.0 -0.039850 0.230875
1574 98.375 -11.766634 0.0 0.047809 0.253561 -4.060516 0.0 0.603304 0.169182 -4.008720 0.0 0.607996 0.166283 -12.604816 0.0 -0.039850 0.230879
1575 98.437 -11.766609 0.0 0.047810 0.253562 -4.060478 0.0 0.603308 0.169177 -4.008696 0.0 0.607999 0.166279 -12.604905 0.0 -0.039850 0.230877
1576 98.500 -11.766284 0.0 0.047820 0.253569 -4.060420 0.0 0.603314 0.169173 -4.008605 0.0 0.608004 0.166284 -12.604885 0.0 -0.039848 0.230878
1577 98.562 -11.766559 0.0 0.047816 0.253562 -4.060458 0.0 0.603313 0.169166 -4.008514 0.0 0.608011 0.166282 -12.604778 0.0 -0.039848 0.230880
1578 98.625 -11.766268 0.0 0.047814 0.253571 -4.060227 0.0 0.603329 0.169171 -4.008522 0.0 0.608012 0.166277 -12.604882 0.0 -0.039849 0.230877
1579 98.687 -11.766484 0.0 0.047820 0.253564 -4.060305 0.0 0.603323 0.169172 -4.008458 0.0 0.608020 0.166266 -12.604792 0.0 -0.039847 0.230880
1580 98.750 -11.766279 0.0 0.047815 0.253571 -4.060149 0.0 0.603334 0.169174 -4.008357 0.0 0.608027 0.166266 -12.604677 0.0 -0.039851 0.230883
1581 98.812 -11.766288 0.0 0.047814 0.253571 -4.060343 0.0 0.603320 0.169171 -4.008565 0.0 0.608013 0.166261 -12.604790 0.0 -0.039846 0.230880
1582 98.875 -11.766396 0.0 0.047815 0.253567 -4.060351 0.0 0.603321 0.169165 -4.008542 0.0 0.608016 0.166257 -12.604689 0.0 -0.039840 0.230884
1583 98.937 -11.766460 0.0 0.047819 0.253564 -4.060331 0.0 0.603322 0.169166 -4.008669 0.0 0.608007 0.166254 -12.604608 0.0 -0.039839 0.230887
1584 99.000 -11.766287 0.0 0.047821 0.253569 -4.060274 0.0 0.603328 0.169162 -4.008532 0.0 0.608018 0.166253 -12.604501 0.0 -0.039840 0.230889
1585 99.062 -11.766116 0.0 0.047817 0.253575 -4.060100 0.0 0.603340 0.169166 -4.008785 0.0 0.608000 0.166248 -12.604620 0.0 -0.039839 0.230886
1586 99.125 -11.766071 0.0 0.047820 0.253576 -4.060165 0.0 0.603337 0.169160 -4.008715 0.0 0.608006 0.166245 -12.604540 0.0 -0.039840 0.230888
1587 99.187 -11.766048 0.0 0.047822 0.253576 -4.060031 0.0 0.603347 0.169157 -4.008807 0.0 0.608000 0.166243 -12.604606 0.0 -0.039839 0.230887
1588 99.250 -11.765949 0.0 0.047820 0.253580 -4.059779 0.0 0.603363 0.169170 -4.008630 0.0 0.608011 0.166251 -12.604641 0.0 -0.039844 0.230885
1589 99.312 -11.765816 0.0 0.047822 0.253583 -4.059769 0.0 0.603365 0.169164 -4.008637 0.0 0.608012 0.166246 -12.604518 0.0 -0.039841 0.230889
1590 99.375 -11.765831 0.0 0.047828 0.253582 -4.059905 0.0 0.603356 0.169160 -4.008675 0.0 0.608011 0.166238 -12.604595 0.0 -0.039843 0.230886
1591 99.437 -11.765835 0.0 0.047838 0.253580 -4.060087 0.0 0.603346 0.169147 -4.008645 0.0 0.608014 0.166236 -12.604459 0.0 -0.039840 0.230891
1592 99.500 -11.765818 0.0 0.047832 0.253581 -4.059957 0.0 0.603355 0.169149 -4.008621 0.0 0.608017 0.166231 -12.604446 0.0 -0.039837 0.230891
1593 99.562 -11.765703 0.0 0.047836 0.253584 -4.060020 0.0 0.603353 0.169141 -4.008632 0.0 0.608017 0.166230 -12.604398 0.0 -0.039834 0.230893
1594 99.625 -11.765751 0.0 0.047840 0.253582 -4.059976 0.0 0.603356 0.169142 -4.008379 0.0 0.608034 0.166236 -12.604278 0.0 -0.039835 0.230896
1595 99.687 -11.765719 0.0 0.047841 0.253583 -4.059949 0.0 0.603356 0.169147 -4.008572 0.0 0.608019 0.166237 -12.604256 0.0 -0.039834 0.230897
1596 99.750 -11.765705 0.0 0.047840 0.253583 -4.059735 0.0 0.603372 0.169148 -4.008449 0.0 0.608030 0.166230 -12.604304 0.0 -0.039833 0.230896
1597 99.812 -11.765629 0.0 0.047839 0.253586 -4.059782 0.0 0.603369 0.169146 -4.008316 0.0 0.608040 0.166231 -12.604244 0.0 -0.039834 0.230897
1598 99.875 -11.765483 0.0 0.047841 0.253590 -4.059764 0.0 0.603372 0.169142 -4.008387 0.0 0.608036 0.166225 -12.604230 0.0 -0.039835 0.230898
1599 99.937 -11.765469 0.0 0.047836 0.253591 -4.059749 0.0 0.603372 0.169143 -4.008229 0.0 0.608048 0.166228 -12.604107 0.0 -0.039839 0.230900
1600 100.000 -11.765598 0.0 0.047840 0.253587 -4.059790 0.0 0.603370 0.169141 -4.008427 0.0 0.608032 0.166232 -12.604194 0.0 -0.039841 0.230898

1601 rows × 17 columns

In [6]:
S21=data['S21_dB'].values
S21=S21[:-1]
print(S21)
B = data['B_mT'].values
B=B[:-1]
print(B)
[-4.06729548 -4.06743248 -4.06738234 ... -4.05976357 -4.05974941
 -4.05978993]
[0.0000e+00 6.2000e-02 1.2500e-01 ... 9.9875e+01 9.9937e+01 1.0000e+02]
In [51]:
import plotly.plotly as py
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot, offline
init_notebook_mode(connected=True)
import plotly.graph_objs as go
trace = go.Scatter(x=B, y=S21, name='Exp. S21')
iplot([trace], filename='S21')

Building Model for Lorentzian

$S_{21} = const+slope*(x-c) + d\cdot(x-e)^2 + I_{she} \cdot \frac{T^2}{(x-H_{fmr})^2+T^2} -I_{ahe} \cdot \frac{2T(x-H_{fmr})}{(x-H_{fmr})^2+T^2} $

In [52]:
import numpy
from lmfit import minimize, Parameters

x= B
data = S21

def residual(params, x, data):
    const = params['const'].value
    slope = params['slope'].value
    c = params['c'].value
    d = params['d'].value
    e = params['e'].value
    Ishe = params['Ishe'].value
    T = params['T'].value
    Hfmr = params['Hfmr'].value
    Iahe = params['Iahe'].value
    
    model = const + slope * (x-c) + d*(x-e)**2 +Ishe* T**2/((x-Hfmr)**2+T**2) - Iahe * 2*T*(x-Hfmr)/((x-Hfmr)**2+T**2)
    return (data-model)**2

params = Parameters()
params.add('const', value= 0)
params.add('slope', value=0)
params.add('c', value=0)
params.add('d',value=0)
params.add('e',value=0)
params.add('Ishe', value =-1)
params.add('T', value=10)
params.add('Hfmr', value=25)
params.add('Iahe', value=0)

out = minimize(residual, params, args=(x,data))
In [53]:
out.__dict__
Out[53]:
{'params': Parameters([('const',
              <Parameter 'const', value=-2.9193636103160445 +/- 1.92, bounds=[-inf:inf]>),
             ('slope',
              <Parameter 'slope', value=-0.00118085580186286 +/- 0.00336, bounds=[-inf:inf]>),
             ('c',
              <Parameter 'c', value=-1153.0788613795135 +/- 571, bounds=[-inf:inf]>),
             ('d',
              <Parameter 'd', value=1.4079586095632953e-06 +/- 1.18e-08, bounds=[-inf:inf]>),
             ('e',
              <Parameter 'e', value=-390.25428021689726 +/- 1.2e+03, bounds=[-inf:inf]>),
             ('Ishe',
              <Parameter 'Ishe', value=-0.005272418052077748 +/- 6.68e-05, bounds=[-inf:inf]>),
             ('T',
              <Parameter 'T', value=1.8893523227915066 +/- 0.0334, bounds=[-inf:inf]>),
             ('Hfmr',
              <Parameter 'Hfmr', value=26.306425815447337 +/- 0.0354, bounds=[-inf:inf]>),
             ('Iahe',
              <Parameter 'Iahe', value=-0.0010832646501224834 +/- 4.38e-05, bounds=[-inf:inf]>)]),
 'var_names': ['const', 'slope', 'c', 'd', 'e', 'Ishe', 'T', 'Hfmr', 'Iahe'],
 'init_vals': [0, 0, 0, 0, 0, -1, 10, 25, 0],
 'nfev': 265,
 'errorbars': True,
 'aborted': False,
 'nvarys': 9,
 'init_values': {'const': 0,
  'slope': 0,
  'c': 0,
  'd': 0,
  'e': 0,
  'Ishe': -1,
  'T': 10,
  'Hfmr': 25,
  'Iahe': 0},
 'method': 'leastsq',
 'residual': array([3.13759750e-07, 4.78274964e-07, 4.04234027e-07, ...,
        7.68447882e-07, 7.71671974e-07, 6.81287024e-07]),
 'chisqr': 4.610865285164739e-11,
 'ndata': 1601,
 'nfree': 1592,
 'redchi': 2.8962721640482025e-14,
 'aic': -49898.62512276378,
 'bic': -49850.21966934681,
 'ier': 2,
 'lmdif_message': 'The relative error between two consecutive iterates is at most 0.000000',
 'success': True,
 'message': 'Fit succeeded.',
 'covar': array([[ 3.70035197e+00, -6.43839569e-03,  1.02319299e+03,
         -1.41070498e-08, -2.29088930e+03, -2.91006823e-05,
         -3.50301803e-03, -2.38191269e-02,  3.87953477e-05],
        [-6.43839748e-03,  1.13070550e-05, -1.84785488e+00,
          2.46806861e-11,  4.02321333e+00,  5.11924834e-08,
          6.62157118e-06,  4.16265703e-05, -6.75037723e-08],
        [ 1.02319414e+03, -1.84785639e+00,  3.26553934e+05,
         -3.97410911e-06, -6.57476667e+05, -8.38055922e-03,
         -1.30150838e+00, -6.68356690e+00,  1.06967240e-02],
        [-1.41070521e-08,  2.46806834e-11, -3.97410536e-06,
          1.38252752e-16,  8.80835298e-06,  2.72024134e-13,
          4.98317827e-11,  2.12961462e-10, -3.36722237e-13],
        [-2.29088993e+03,  4.02321333e+00, -6.57476128e+05,
          8.80835394e-06,  1.43152576e+06,  1.82660744e-02,
          2.36828902e+00,  1.48502857e+01, -2.40792304e-02],
        [-2.91006919e-05,  5.11924861e-08, -8.38055280e-03,
          2.72024152e-13,  1.82660754e-02,  4.46763433e-09,
          1.30636735e-06,  9.95232003e-07, -7.93240257e-10],
        [-3.50302703e-03,  6.62158515e-06, -1.30150978e+00,
          4.98318148e-11,  2.36829399e+00,  1.30636741e-06,
          1.11613966e-03,  1.03390780e-04,  7.05366904e-08],
        [-2.38191301e-02,  4.16265643e-05, -6.68356034e+00,
          2.12961459e-10,  1.48502836e+01,  9.95231966e-07,
          1.03390725e-04,  1.25403912e-03, -1.19407595e-06],
        [ 3.87953478e-05, -6.75037538e-08,  1.06967119e-02,
         -3.36722212e-13, -2.40792238e-02, -7.93240157e-10,
          7.05367847e-08, -1.19407592e-06,  1.91577892e-09]])}
In [54]:
# Plot the fitting line and the experimental result
f_const = out.params['const'].value
f_slope = out.params['slope'].value
f_c = out.params['c'].value
f_d = out.params['d'].value
f_e = out.params['e'].value
f_Ishe = out.params['Ishe'].value
f_T = out.params['T'].value
f_Hfmr = out.params['Hfmr'].value
f_Iahe = out.params['Iahe'].value
f_x = np.linspace(0,100,1601)
f_data = f_const+ f_slope * (f_x-f_c) + f_d*(f_x-f_e)**2 + f_Ishe * f_T**2/((f_x-f_Hfmr)**2+f_T**2) - f_Iahe * 2*f_T*(f_x-f_Hfmr)/((f_x-f_Hfmr)**2+f_T**2)
trace2 = go.Scatter(x=f_x, y=f_data, name= 'Fit. S21')
iplot([trace, trace2], filename='S21')
In [85]:
layout = go.Layout(
    margin = dict(l = 105, r = 50, b = 70, t = 50, pad = 4),
    title = result[:-4]+': S21',
    titlefont = dict(size=32),
    font = dict(size=16),
    xaxis=dict(
        title = 'B (mT)',
        titlefont=dict(
            size=22,
        ),
        showgrid=True,
        zeroline=True,
        showline=True,
        mirror='ticks',
        gridcolor='#bdbdbd',
        gridwidth=2,
        zerolinecolor='#969696',
        zerolinewidth=4,
        linecolor='#636363',
        linewidth=6
    ),
    yaxis=dict(
        title = 'S21 (dB)',
        titlefont=dict(
            size=22,
        ),
        showgrid=True,
        zeroline=True,
        showline=True,
        mirror='ticks',
        gridcolor='#bdbdbd',
        gridwidth=2,
        zerolinecolor='#969696',
        zerolinewidth=4,
        linecolor='#636363',
        linewidth=6
    )
)
iplot({'data': [trace, trace2],
               'layout': layout})