diff filter/phase.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/filter/phase.c	Fri May 20 15:19:45 2011 +0100
@@ -0,0 +1,132 @@
+/*
+    Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989
+    ===========================================================================
+
+    Permission to use, copy, modify, and distribute this software without fee 
+    is hereby granted for research purposes, provided that this copyright 
+    notice appears in all copies and in all supporting documentation, and that 
+    the software is not redistributed for any fee (except for a nominal shipping 
+    charge). Anyone wanting to incorporate all or part of this software in a
+    commercial product must obtain a license from the Medical Research Council.
+
+    The MRC makes no representations about the suitability of this 
+    software for any purpose.  It is provided "as is" without express or implied 
+    warranty.
+ 
+    THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
+    ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
+    A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 
+    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 
+    AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+/*
+	phase.c
+	=======
+
+    looks after phase compensation of all filter types
+
+
+*/
+
+#include "stitch.h"
+#include "phase.h"
+
+
+PhaseCompensatorState *NewPhaseCompensator( filter_state, filter_module, sample_delay )
+FilterState  filter_state ;
+FilterModule filter_module ;
+double       sample_delay ;
+{
+    DeclareNew( PhaseCompensatorState *, compensator_state ) ;
+
+    compensator_state->filter_state  = filter_state  ;
+    compensator_state->filter_module = filter_module ;
+    compensator_state->sample_delay  = sample_delay + 0.5 ;
+
+    compensator_state->delay_buffer  = ( Pointer ) 0 ;
+
+    return( compensator_state ) ;
+}
+
+PointCount PhaseCompensateFilter( compensator_state, input, output, points )
+PhaseCompensatorState *compensator_state ;
+Pointer input ;
+Pointer output ;
+PointCount points ;
+{
+    unsigned output_size ;
+    int byte_delay, bytes ;
+    PointCount process ;
+    Pointer input_ptr ;
+    static int zero = 0 ;
+
+    /* support for new and old zero point transfers to get any remaining
+	data from filters - in short a mess */
+
+    if( compensator_state->sample_delay < 0 && ( input == ( Pointer ) 0 || points == 0 ) ) {
+	process = - ( int ) compensator_state->sample_delay ;
+
+	if( process > points && points != 0 )
+	    process = points ;
+
+	compensator_state->sample_delay = ( int ) compensator_state->sample_delay + process ;
+
+	input_ptr = ( Pointer ) 0 ;
+    }
+    else {
+	process = points ;
+	input_ptr = input ;
+    }
+
+    /* call filter pod */
+
+    output_size = compensator_state->filter_module(
+		      &compensator_state->filter_state,
+		      &zero,
+		      input_ptr,
+		      output,
+		      process,
+		      1
+		  ) ;
+
+    byte_delay = ( int ) compensator_state->sample_delay * output_size ;
+    bytes = points * output_size ;
+
+    if( compensator_state->delay_buffer == ( Pointer ) 0 )
+	if( ( int ) compensator_state->sample_delay < 0 ) {
+	    stitch_bcopy( output - byte_delay, output, bytes - byte_delay ) ;
+	    compensator_state->delay_buffer   = ( Pointer ) 0 + 1 ;
+
+	    return( points + ( int ) compensator_state->sample_delay ) ;
+	}
+	else
+	    compensator_state->delay_buffer = stitch_calloc(
+				 ( unsigned ) compensator_state->sample_delay * 2,
+					      output_size, "phase.c for delay buffer\n" ) ;
+
+    if( byte_delay > bytes ) {
+	stitch_bcopy( output, compensator_state->delay_buffer + byte_delay, bytes ) ;
+	stitch_bcopy( compensator_state->delay_buffer, output, bytes ) ;
+	stitch_bcopy( compensator_state->delay_buffer + bytes, compensator_state->delay_buffer, byte_delay ) ;
+    }
+    else if( byte_delay > 0 ) {
+	stitch_bcopy( output + bytes - byte_delay, compensator_state->delay_buffer + byte_delay, byte_delay ) ;
+	stitch_bcopy( output, output + byte_delay,  bytes - byte_delay ) ;
+	stitch_bcopy( compensator_state->delay_buffer, output, byte_delay ) ;
+	stitch_bcopy( compensator_state->delay_buffer + byte_delay, compensator_state->delay_buffer, byte_delay ) ;
+    }
+
+    return( process ) ;
+}
+
+void FreeCompensatedFilter( compensator_state )
+PhaseCompensatorState *compensator_state ;
+{
+    stitch_free( compensator_state->filter_state ) ;
+    stitch_free( ( Pointer ) compensator_state ) ;
+
+    return ;
+}
+