Resistribute vib_analyze pylab.hold()s to avoid auto-generated axis.
[calibcant.git] / calibcant / vib_analyze.py
index 837045ca3c3150285fa3ce12a9e591a738996838..dc4af3b494684117127e619e3098ddbefd87024f 100755 (executable)
@@ -95,9 +95,10 @@ def vib_analyze(deflection_bits, freq, minFreq=500, maxFreq=7000,
         print "Fitted f(x) = C / ((A**2-x**2)**2 + (x*B)**2) with"
         print "A = %g, \t B = %g, \t C = %g" % (A, B, C)
 
-    vib_plot(deflection_bits, freq_axis, power, A, B, C, plotVerbose=plotVerbose)
+    vib_plot(deflection_bits, freq_axis, power, A, B, C,
+             plotVerbose=plotVerbose)
 
-    # Our A is in uV**2, so convert back to Volts
+    # Our A is in uV**2, so convert back to Volts**2
     return lorentzian_area(A,B,C) * 1e-12
 
 def fit_psd(freq_axis, psd_data, minFreq=500, maxFreq=7000) :
@@ -250,23 +251,25 @@ def vib_plot(deflection_bits, freq_axis, power, A, B, C,
      FFTed Vphoto data (Vphoto vs Frequency) (show major noise components).
     """
     if plotVerbose or config.PYLAB_VERBOSE :
+        print "plotting"
         common._import_pylab()
-        common._pylab.hold(False)
-        common._pylab.figure(BASE_FIGNUM+2)
+        common._pylab.figure(config.BASE_FIGNUM+2)
 
         # plot time series
         common._pylab.subplot(311)
-        common._pylab.plot(data["Deflection input"], 'r.')
+        common._pylab.hold(False)
+        common._pylab.plot(deflection_bits, 'r.')
         common._pylab.title("free oscillation")
 
         # plot histogram distribution and gaussian fit
         common._pylab.subplot(312)
+        common._pylab.hold(False)
         n, bins, patches = \
-            common._pylab.hist(data["Deflection input"], bins=30,
-                        normed=1, align='center')
-        gaus = numpy.zeros((len(bins),))
-        mean = data["Deflection input"].mean()
-        std = data["Deflection input"].std()
+            common._pylab.hist(deflection_bits, bins=30,
+                               normed=1, align='center')
+        gaus = numpy.zeros((len(bins),), dtype=numpy.float)
+        mean = deflection_bits.mean()
+        std = deflection_bits.std()
         pi = numpy.pi
         exp = numpy.exp
         for i in range(len(bins)) :
@@ -276,10 +279,19 @@ def vib_plot(deflection_bits, freq_axis, power, A, B, C,
         common._pylab.hold(False)
 
         # plot FFTed data
-        common._pylab.subplot(313)
+        axes = common._pylab.subplot(313)
+        common._pylab.hold(False)
         common._pylab.semilogy(freq_axis, power, 'r.-')
+        fitdata = C / ((A**2-freq_axis**2)**2 + (B*freq_axis)**2)
+        xmin,xmax = axes.get_xbound()
+        ymin,ymax = axes.get_ybound()
+        common._pylab.hold(True)
+        common._pylab.plot(freq_axis, fitdata, 'b-');
+        common._pylab.hold(False)
+        axes.axis([xmin,xmax,ymin,ymax])
+
         common._flush_plot()
-    if (plotVerbose or config.GNUPLOT_VERBOSE) and False : # TODO, clean up and double check...
+    if (plotVerbose or config.GNUPLOT_VERBOSE): # TODO: cleanup and test
         # write all the ft data now
         fd = file(datafilename, 'w')
         for i in range(len(freq_axis)) :
@@ -379,6 +391,9 @@ if __name__ == '__main__' :
     parser.add_option('-g', '--gnuplot', dest='gnuplot', action='store_true',
                       help='Print gnuplot fit check script to stderr',
                       default=False)
+    parser.add_option('-p', '--pylab', dest='pylab', action='store_true',
+                      help='Produce pylab fit checks during execution',
+                      default=False)
     parser.add_option('-t', '--tweak-mode', dest='tweakmode', action='store_true',
                       help='Run in tweak-file mode',
                       default=False)
@@ -401,7 +416,8 @@ if __name__ == '__main__' :
     else :
         vfile = None
     config.TEXT_VERBOSE = options.verbose
-    config.PYLAB_VERBOSE = False
+    config.PYLAB_INTERACTIVE = False
+    config.PYLAB_VERBOSE = options.pylab
     config.GNUPLOT_VERBOSE = options.gnuplot
 
     if options.tweakmode == False :
@@ -421,5 +437,9 @@ if __name__ == '__main__' :
             sep = '\n'
         common.write_array(ofile, Vphoto_vars, sep)
 
+    if common._final_flush_plot != None:
+        common._final_flush_plot()
+    
     if options.ofilename != None :
         ofile.close()
+