Added optional ability to use naive vibration variance calculation.
authorW. Trevor King <wking@drexel.edu>
Tue, 16 Jun 2009 19:05:10 +0000 (15:05 -0400)
committerW. Trevor King <wking@drexel.edu>
Tue, 16 Jun 2009 19:05:10 +0000 (15:05 -0400)
Now you don't have to fit the vibration power spectral density if you
don't want to.  The only reason I can think of for not fitting would be
to `emulate' older calibration software that doesn't have the fitting
capability, or to provide a simple point-of-reference for comparing
various fitting models.

calibcant/vib_analyze.py

index 77b84020d24ee44d7172bb822a8ed675052d5480..0a9e1e55734740c56dd25c004490f66c12eb19bb 100755 (executable)
@@ -55,8 +55,12 @@ def vib_analyze_naive(deflection_bits, Vphoto_in2V=config.Vphoto_in2V,
     Vphoto_in2V is a function converting Vphoto values from bits to Volts.
     This function is assumed linear, see bump_analyze().
     """
+    if config.TEXT_VERBOSE :
+        print "Calculating the naive (raw) variance"
     Vphoto_std_bits = deflection_bits.std()
     Vphoto_std = Vphoto_in2V(Vphoto_std_bits + zeroVphoto_bits)
+    if config.TEXT_VERBOSE :
+        print "Average deflection (Volts):", Vphoto_in2V(deflection_bits.mean())
     return Vphoto_std**2
 
 #@splittableKwargsFunction((fit_psd, 'freq_axis', 'psd_data'),
@@ -391,12 +395,12 @@ make_splittable_kwargs_function(vib_analyze,
      'minFreq', 'maxFreq'))
 
 @splittableKwargsFunction((vib_analyze, 'deflection_bits', 'freq'))
-def vib_load_analyze_tweaked(tweak_file, **kwargs) :
+def vib_load_analyze_tweaked(tweak_file, analyze=vib_analyze, **kwargs) :
     """
     Read the vib files listed in tweak_file.
     Return an array of Vphoto variances (due to the cantilever) in Volts**2
     """
-    vib_analyze_kwargs, = \
+    analyze_kwargs, = \
         vib_load_analyze_tweaked._splitargs(vib_load_analyze_tweaked, kwargs)
     dl = data_logger.data_load()
     Vphoto_var = []
@@ -411,8 +415,11 @@ def vib_load_analyze_tweaked(tweak_file, **kwargs) :
         if config.TEXT_VERBOSE :
             print "Analyzing '%s' at %g Hz" % (path, freq)
         # analyze
-        Vphoto_var.append(vib_analyze(deflection_bits, freq,
-                                      **vib_analyze_kwargs))
+        if 'freq' in analyze._kwargs(analyze):
+            var = analyze(deflection_bits, freq, **analyze_kwargs)
+        else:
+            var = analyze(deflection_bits, **analyze_kwargs)
+        Vphoto_var.append(var)
     return numpy.array(Vphoto_var, dtype=numpy.float)
 
 # commandline interface functions
@@ -485,6 +492,10 @@ if __name__ == '__main__' :
     parser.add_option('-d', '--datalogger-mode', dest='datalogger_mode', action='store_true',
                       help='Read input files with datalogger.read_dict_of_arrays().  This is useful, for example, to test a single line from a tweakfile.',
                       default=False)
+    parser.add_option('-s', '--disable-spectrum-fitting', dest='spectrum_fitting',
+                      action='store_false',
+                      help='Disable PSD fitting, just use the raw variance',
+                      default=True)
     parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
                       help='Print lots of debugging information',
                       default=False)
@@ -504,6 +515,16 @@ if __name__ == '__main__' :
     config.PYLAB_VERBOSE = options.pylab
     config.GNUPLOT_VERBOSE = options.gnuplot
     PLOT_GUESSED_LORENTZIAN = options.plot_guess
+    if options.spectrum_fitting == True:
+        analyze = vib_analyze
+        analyze_args = {'freq':options.freq,
+                        'minFreq':options.min_freq,
+                        'maxFreq':options.max_freq}
+    else:
+        analyze = vib_analyze_naive
+        analyze_args = dict()
+        make_splittable_kwargs_function(vib_load_analyze_tweaked,
+                                        (vib_analyze, 'deflection_bits'))
 
     if options.tweakmode == False : # single file mode
         if options.datalogger_mode:
@@ -511,14 +532,13 @@ if __name__ == '__main__' :
             deflection_bits = data['Deflection input']
         else:
             deflection_bits = read_data(ifilename)
-        Vphoto_var = vib_analyze(deflection_bits, freq=options.freq,
-                                 minFreq=options.min_freq,
-                                 maxFreq=options.max_freq)
+        Vphoto_var = analyze(deflection_bits, **analyze_args)
         print >> ofile, Vphoto_var
     else : # tweak mode
-        Vphoto_vars = vib_load_analyze_tweaked(ifilename,
-                                               minFreq=options.min_freq,
-                                               maxFreq=options.max_freq)
+        if 'freq' in analyze_args:
+            analyze_args.pop('freq') # freq extracted from vib file names
+        Vphoto_vars = vib_load_analyze_tweaked(ifilename, analyze=analyze,
+                                               **analyze_args)
         if options.comma_out :
             sep = ','
         else :