InjectVector.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Jolien Creighton, Teviet Creighton, John Whelan
00003 *
00004 *  This program is free software; you can redistribute it and/or modify
00005 *  it under the terms of the GNU General Public License as published by
00006 *  the Free Software Foundation; either version 2 of the License, or
00007 *  (at your option) any later version.
00008 *
00009 *  This program is distributed in the hope that it will be useful,
00010 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 *  GNU General Public License for more details.
00013 *
00014 *  You should have received a copy of the GNU General Public License
00015 *  along with with program; see the file COPYING. If not, write to the
00016 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00017 *  MA  02111-1307  USA
00018 */
00019 
00020 /********************************* <lalVerbatim file="InjectVectorCV">
00021 Author: Creighton, T. D.
00022 $Id: InjectVector.c,v 1.6 2007/06/08 14:41:47 bema Exp $
00023 **************************************************** </lalVerbatim> */
00024 
00025 /********************************************************** <lalLaTeX>
00026 
00027 \subsection{Module \texttt{InjectVector.c}}
00028 \label{ss:InjectVector.c}
00029 
00030 Injects a vector of floating-point numbers into a vector of integers,
00031 with dithering.
00032 
00033 \subsubsection*{Prototypes}
00034 \vspace{0.1in}
00035 \input{InjectVectorCP}
00036 \idx{LALSI2InjectVector()}
00037 \idx{LALSSInjectVector()}
00038 
00039 \subsubsection*{Description}
00040 
00041 The function \verb@LALSI2InjectVector()@ (i.e.\ ``Single-precision to
00042 \verb@INT2@'') dithers the contents of \verb@*output@, adds the
00043 contents of \verb@*signal@, and rounds to the nearest integer, storing
00044 the result back in \verb@*output@.  If desired, the random parameters
00045 for the dithering can be created outside this routine and passed in as
00046 \verb@*params@ (see \verb@Random.h@); if this pointer is \verb@NULL@,
00047 the parameters will be generated internally.
00048 
00049 The function \verb@LALSSInjectVector()@ (i.e.\ ``Single-precision to
00050 single-precision'') simply adds the contents of \verb@*signal@ to
00051 \verb@*output@ where they overlap, without performing any dithering.
00052 
00053 \subsubsection*{Algorithm}
00054 
00055 Dithering is done with a flat random distribution as described in
00056 \verb@Inject.h@.  Injected values outside the dynamic range of the
00057 output force the output to its ``rails'' of $-2^{8N-1}$ or
00058 $2^{8N-1}-1$, where $N$ is the number of bytes in the integer.  The
00059 two vectors need not be of equal length; the injection stops when
00060 either vector reaches its end.
00061 
00062 If \verb@params@ is \verb@NULL@, a \verb@RandomParams@ structure will
00063 be generated internally using a seed of zero (i.e.\ the current time
00064 will be used to initialize the pseudorandom sequence).
00065 
00066 \subsubsection*{Uses}
00067 \begin{verbatim}
00068 LALCreateRandomParams()
00069 LALDestroyRandomParams()
00070 LALUniformDeviate()
00071 \end{verbatim}
00072 
00073 \subsubsection*{Notes}
00074 
00075 \vfill{\footnotesize\input{InjectVectorCV}}
00076 
00077 ******************************************************* </lalLaTeX> */
00078 
00079 #include <math.h>
00080 #include <lal/LALStdlib.h>
00081 #include <lal/LALError.h>
00082 #include <lal/Random.h>
00083 #include <lal/Inject.h>
00084 
00085 NRCSID( INJECTVECTORC, "$Id: InjectVector.c,v 1.6 2007/06/08 14:41:47 bema Exp $" );
00086 
00087 /* <lalVerbatim file="InjectVectorCP"> */
00088 void
00089 LALSI2InjectVector( LALStatus    *stat,
00090                     INT2Vector   *output,
00091                     REAL4Vector  *signal,
00092                     RandomParams *params )
00093 { /* </lalVerbatim> */
00094   UINT4 n;  /* number of samples injected */
00095   UINT4 i;  /* an index */
00096   RandomParams *internal = NULL; /* internal random parameters */
00097 
00098   const INT2 max = 32767;  /* largest INT2 */
00099   const INT2 min = -32768; /* smallest INT2 */
00100 
00101   INITSTATUS( stat, "LALSI2InjectVector", INJECTVECTORC );
00102   ATTATCHSTATUSPTR( stat );
00103 
00104   /* Make sure parameter structures and their fields exist. */
00105   ASSERT( signal, stat, INJECTH_ENUL, INJECTH_MSGENUL );
00106   ASSERT( signal->data, stat, INJECTH_ENUL, INJECTH_MSGENUL );
00107   ASSERT( output, stat, INJECTH_ENUL, INJECTH_MSGENUL );
00108   ASSERT( output->data, stat, INJECTH_ENUL, INJECTH_MSGENUL );
00109 
00110   /* If params = NULL, generate an internal set of parameters. */
00111   if ( !params )
00112     TRY( LALCreateRandomParams( stat->statusPtr, &internal, 0 ),
00113          stat );
00114   else
00115     internal = params;
00116 
00117   /* Find out how many samples will be injected. */
00118   n = output->length;
00119   if ( n > signal->length )
00120     n = signal->length;
00121 
00122   /* Start injecting... */
00123   for ( i = 0; i < n; i++ ) {
00124     REAL4 x = (REAL4)( output->data[i] ); /* current output sample */
00125     REAL4 d;                              /* current dithering */
00126 
00127     /* Compute the dithering. */
00128     LALUniformDeviate( stat->statusPtr, &d, internal ); 
00129     BEGINFAIL( stat )
00130       if ( !params ) {
00131         TRY( LALDestroyRandomParams( stat->statusPtr, &internal ),
00132              stat );
00133       }
00134     ENDFAIL( stat );
00135 
00136     /* Dither and inject. */
00137     x += d + signal->data[i];
00138     if ( x > max )
00139       output->data[i] = max;
00140     else if ( x < min )
00141       output->data[i] = min;
00142     else
00143       output->data[i] = (INT2)( floor( x ) );
00144   }
00145 
00146   /* Cleanup and exit. */
00147   if ( !params ) {
00148     TRY( LALDestroyRandomParams( stat->statusPtr, &internal ), stat );
00149   }
00150   DETATCHSTATUSPTR( stat );
00151   RETURN( stat );
00152 }
00153 
00154 
00155 /* <lalVerbatim file="InjectVectorCP"> */
00156 void
00157 LALSSInjectVector( LALStatus    *stat,
00158                    REAL4Vector  *output,
00159                    REAL4Vector  *signal )
00160 { /* </lalVerbatim> */
00161   UINT4 n;  /* number of samples injected */
00162   UINT4 i;  /* an index */
00163 
00164   INITSTATUS( stat, "LALSSInjectVector", INJECTVECTORC );
00165 
00166   /* Make sure parameter structures and their fields exist. */
00167   ASSERT( signal, stat, INJECTH_ENUL, INJECTH_MSGENUL );
00168   ASSERT( signal->data, stat, INJECTH_ENUL, INJECTH_MSGENUL );
00169   ASSERT( output, stat, INJECTH_ENUL, INJECTH_MSGENUL );
00170   ASSERT( output->data, stat, INJECTH_ENUL, INJECTH_MSGENUL );
00171 
00172   /* Find out how many samples will be injected. */
00173   n = output->length;
00174   if ( n > signal->length )
00175     n = signal->length;
00176 
00177   /* Inject and exit. */
00178   for ( i = 0; i < n; i++ )
00179     output->data[i] += signal->data[i];
00180   RETURN( stat );
00181 }

Generated on Mon Oct 13 02:31:44 2008 for LAL by  doxygen 1.5.2