mkogg.py: Fix 'self.get_mp4_metadata(self, source)'
[blog.git] / posts / calibcant.mdwn_itex
1 [[!meta  title="calibcant"]]
2 [[!template id=gitrepo repo=calibcant]]
3
4 Here is my Python module for AFM cantilever calibration via the
5 thermal tune method.
6
7 The `README` is posted on the [PyPI page][pypi], and you might also be
8 interested in the [[package dependency graph|calibcant.svg]] generated
9 with Yu-Jie Lin's [PDepGraph.py][]:
10
11     # python PDepGraph.py -o calibcant.dot \
12         -D matplotlib,scipy,numpy,pyyaml,h5py,python,eselect-python \
13         calibcant
14     # dot -T svg -o calibcant.svg calibcant.dot
15
16 Thermal calibration requires three separate measurements: photodiode
17 sensitivity (via surface bumps), fluid temperature (estimated, or via
18 thermocouple), and thermal vibration (watching the cantilever vibrate
19 in far from the surface).  The calibcant package takes repeated
20 measurements ([[!ltio statistics.png]]) of each of these parameters to
21 allow estimation of statistical uncertainty:
22
23     # calibcant-analyze.py calibcant/examples/calibration.h5
24     ...
25     ... variable (units)         : mean +/- std. dev. (relative error)
26     ... cantilever k (N/m)       : 0.0629167 +/- 0.00439057 (0.0697838)
27     ... photo sensitivity (V/m)  : 2.4535e+07 +/- 616119 (0.0251118)
28     ... T (K)                    : 295.15 +/- 0 (0)
29     ... vibration variance (V^2) : 3.89882e-05 +/- 1.88897e-06 (0.0484497)
30     ...
31
32 While this cannot account for systematic errors, calibration numbers
33 are fairly meaninless without at least statistical error estimates.
34
35 Extracting the photodiode sensitivity and thermal deflection variance
36 from the raw data can vary a suprising amount depending on your
37 cantilever/photodiode linearity and drift and signal/noise ratio in
38 the vibration data.  To help deal with this, calibcant provides a
39 choice of models for fitting each measurement type.
40
41 The contact region of surface bumps can be fit with either linear or
42 quadratic models.  Here is an example of a single surface bump fit
43 with a quadratic model.  The green line is the initial guess (before
44 fitting), the red line is the final model (after fitting), and the
45 blue dots are measured data points.  The red dots in the bottom panel
46 are the residual, which looks cubic because we've subtracted a
47 quadratic model.
48
49 [[!img bump.png alt="Surface bump for photodiode sensitivity"
50   title="Surface bump for photodiode sensitivity" ]]
51
52 Extracting the thermal vibration variance is also trickier than it
53 might seem.  Fitting the data in frequency-space to a Lorentzian
54 ([Breit-Wigner][]?)  model helps filter out low frequency drift, as
55 well as white noise from the measurement equipment.
56
57 \[
58   \text{PSD}(x, \omega) =
59     \frac{2 k_{B} T \beta}
60          { \pi m \left[{(\omega_0^2-\omega^2)^2 + \beta^2\omega^2}\right] }
61     + W \;.
62 \]
63
64 where $\beta$ and $\omega_0$ come from the damped-forced harmonic
65 oscillator equation of motion
66
67 \[
68   \ddot{x} + \beta \dot{x} + \omega_0^2 x = \frac{F(t)}{m} \;,
69 \]
70
71 the cantilever's effective mass is $m$, and $W$ is an optional
72 white-noise offset.
73
74 Here is an example of a one-second thermal vibration fit with the
75 offset Breit-Wigner model.  The top panel is the time-series
76 deflection voltage (bits vs sample index).  The center pannel is a
77 histogram of the deflection voltage, showing an approximately Gaussian
78 distribution.  The bottom panel shows the power spectral density fit
79 (red dots) fit with an offset Breit-Wigner model (blue curve).  The
80 horizontal blue line marks the white-noise offset, and the vertical
81 blue line marks the resonant frequency.  Points outside the light blue
82 region were not considered during the fitting.  This allows us to
83 isolate the cantilever's thermal vibration from other noise sources,
84 which leads to more accurate and reproducible spring constant
85 estimates.
86
87 [[!img vibration.png alt="Thermal vibration measurement"
88   title="Thermal vibration measurement" ]]
89
90 Finally, all data and analysis results are stored in the standard,
91 portable [[HDF5]] file format, so it's easy to reanalyze earlier
92 calibration data with different models if you decide to do so at a
93 later date, or just look back and see exactly what calculations went
94 into your spring constant calibration in the first place.
95
96 I tried to build calibcant on top of a chain of packages to make
97 swapping out the hardware interface easier, but [[Comedi]] is at the
98 bottom of the current chain, so it may be hard to use this package if
99 you're not running Linux.  If you're not running Linux, but are
100 interested in getting calibcant working on your system anyway, send me
101 an email!  I'd be happy to help generalize calibcant, but it's hard
102 for me to imagine hardware control from Windows (do people run
103 experiments from Macs?).  If you've figured that part out, I can
104 probably graft calibcant onto your system.
105
106 [pypi]: http://pypi.python.org/pypi/calibcant/
107 [PDepgraph.py]:
108   http://code.google.com/p/yjl/source/browse/Miscellaneous/PDepGraph.py
109 [Breit-Wigner]:
110   http://en.wikipedia.org/wiki/Relativistic_Breit%E2%80%93Wigner_distribution
111
112 [[!tag tags/code]]
113 [[!tag tags/linux]]
114 [[!tag tags/programming]]
115 [[!tag tags/pypi]]
116 [[!tag tags/python]]