Finish translating the hooke.plugin.curve Commands from procplots.
authorW. Trevor King <wking@drexel.edu>
Tue, 18 May 2010 01:58:57 +0000 (21:58 -0400)
committerW. Trevor King <wking@drexel.edu>
Tue, 18 May 2010 01:58:57 +0000 (21:58 -0400)
hooke/plugin/curve.py

index bf219180721bc8e88755c2a5d9f70d51a025ba30..594f158c5d8462da398580060c01f7e1ab25d725 100644 (file)
@@ -29,6 +29,7 @@ from ..curve import Data
 from ..plugin import Builtin
 from ..plugin.playlist import current_playlist_callback
 from ..util.calculus import derivative
+from ..util.fft import unitary_avg_power_spectrum
 
 
 class CurvePlugin (Builtin):
@@ -36,7 +37,9 @@ class CurvePlugin (Builtin):
         super(CurvePlugin, self).__init__(name='curve')
 
     def commands(self):
-        return [InfoCommand(), ExportCommand()]
+        return [InfoCommand(), ExportCommand(),
+                DifferenceCommand(), DerivativeCommand(),
+                PowerSpectrumCommand()]
 
 
 # Define common or complicated arguments
@@ -170,7 +173,7 @@ approacing curve and `1` selects the retracting curve.
                          help="""
 Column of data block to differentiate with respect to.
 """.strip()),
-                Argument(name='y column', type='int', default=1,
+                Argument(name='f column', type='int', default=1,
                          help="""
 Column of data block to differentiate.
 """.strip()),
@@ -183,7 +186,7 @@ Column of data block to differentiate.
         assert a[:,params['x column']] == b[:,params['x column']]:
         out = Data((a.shape[0],2), dtype=a.dtype)
         out[:,0] = a[:,params['x column']]
-        out[:,1] = a[:,params['y column']] - b[:,params['y column']]:
+        out[:,1] = a[:,params['f column']] - b[:,params['f column']]
         outqueue.put(out)
 
 class DerivativeCommand (Command):
@@ -203,7 +206,7 @@ selects the approacing curve and `1` selects the retracting curve.
                          help="""
 Column of data block to differentiate with respect to.
 """.strip()),
-                Argument(name='y column', type='int', default=1,
+                Argument(name='f column', type='int', default=1,
                          help="""
 Column of data block to differentiate.
 """.strip()),
@@ -218,5 +221,45 @@ central differencing.
     def _run(self, hooke, inqueue, outqueue, params):
         data = params['curve'].data[params['block']]
         outqueue.put(derivative(
-                block, x_col=params['x column'], y_col=params['y column'],
+                block, x_col=params['x column'], f_col=params['f column'],
                 weights=params['weights']))
+
+class PowerSpectrumCommand (Command):
+    """Calculate the power spectrum of a data block.
+    """
+    def __init__(self):
+        super(PowerSpectrumCommand, self).__init__(
+            name='block power spectrum',
+            arguments=[
+                CurveArgument,
+                Argument(name='block', aliases=['set'], type='int', default=0,
+                         help="""
+Data block to act on.  For an approach/retract force curve, `0`
+selects the approacing curve and `1` selects the retracting curve.
+""".strip()),
+                Argument(name='f column', type='int', default=1,
+                         help="""
+Column of data block to differentiate with respect to.
+""".strip()),
+                Argument(name='freq', type='float', default=1.0,
+                         help="""
+Sampling frequency.
+""".strip()),
+                Argument(name='chunk size', type='int', 2048,
+                         help="""
+Number of samples per chunk.  Use a power of two.
+""".strip()),
+            Argument(name='overlap', type='bool', default=False,
+                     help="""
+If `True`, each chunk overlaps the previous chunk by half its length.
+Otherwise, the chunks are end-to-end, and not overlapping.
+""".strip()),
+                ],
+            help=self.__doc__)
+
+    def _run(self, hooke, inqueue, outqueue, params):
+        data = params['curve'].data[params['block']]
+        outqueue.put(unitary_avg_power_spectrum(
+                data[:,params['f column']], freq=params['freq'],
+                chunk_size=params['chunk size'],
+                overlap=params['overlap']))