BandPassTest.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Jolien Creighton, Teviet Creighton
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="BandPassTestCV">
00021 Author: Creighton, T. D.
00022 $Id: BandPassTest.c,v 1.16 2007/06/08 14:41:56 bema Exp $
00023 **************************************************** </lalVerbatim> */
00024 
00025 /********************************************************** <lalLaTeX>
00026 
00027 \subsection{Program \texttt{BandPassTest.c}}
00028 \label{s:BandPassTest.c}
00029 
00030 Tests time-domain high- and low-pass filters.
00031 
00032 \subsubsection*{Usage}
00033 \begin{verbatim}
00034 BandPassTest [-d debuglevel] [-i infile | -n npts dt offset] [-o outfile]
00035              [-f f1 f2 a1 a2 order]
00036 \end{verbatim}
00037 
00038 \subsubsection*{Description}
00039 
00040 This program applies a Butterworth time-domain low-pass or high-pass
00041 filter to a time series, using the routine
00042 \verb@LALDButterworthREAL4TimeSeries()@.  The following option flags
00043 are accepted:
00044 \begin{itemize}
00045 \item[\texttt{-d}] Changes the default debug level from 0 to
00046 \verb@debuglevel@.
00047 \item[\texttt{-i}] Reads the input time series from \verb@infile@
00048 using the routine \verb@LALSReadTSeries()@; see \verb@StreamInput.h@
00049 for a description of the file format.
00050 \item[\texttt{-n}] Generates an input time series of length
00051 \verb@npts@ and sampling interval \verb@dt@, containing just an
00052 impulse at sample index \verb@offset@.  If the \verb@-i@ option is
00053 also given, it overrides this option.  If neither are given,
00054 \verb@-n 4096 1.0 1024@ is assumed.
00055 \item[\texttt{-o}] Writes the output time series to \verb@outfile@,
00056 using the routine \verb@LALSWriteTSeries()@; see \verb@StreamOutput.h@
00057 for a description of the file format.  If not specified, the routines
00058 are exercised, but no output is written.
00059 \item[\texttt{-f}] Sets the filter to have attenuation \verb@a1@ and
00060 \verb@a2@ at frequencies \verb@f1@ and \verb@f2@, with a maximum
00061 filter order of \verb@order@; see \verb@ButterworthTimeSeries.c@ for a
00062 description of how these values are interpreted.  If not specified,
00063 \verb@-f 0.01 0.015 0.9 0.1 20@ is assumed.
00064 \end{itemize}
00065 
00066 \subsubsection*{Exit codes}
00067 ****************************************** </lalLaTeX><lalErrTable> */
00068 #define BANDPASSTESTC_ENORM 0
00069 #define BANDPASSTESTC_ESUB  1
00070 #define BANDPASSTESTC_EARG  2
00071 #define BANDPASSTESTC_EBAD  3
00072 #define BANDPASSTESTC_EFILE 4
00073 
00074 #define BANDPASSTESTC_MSGENORM "Normal exit"
00075 #define BANDPASSTESTC_MSGESUB  "Subroutine failed"
00076 #define BANDPASSTESTC_MSGEARG  "Error parsing arguments"
00077 #define BANDPASSTESTC_MSGEBAD  "Bad argument values"
00078 #define BANDPASSTESTC_MSGEFILE "Could not open file"
00079 /******************************************** </lalErrTable><lalLaTeX>
00080 
00081 \subsubsection*{Uses}
00082 \begin{verbatim}
00083 lalDebugLevel
00084 LALPrintError()                         LALSnprintf()
00085 LALSCreateVector()                      LALSDestroyVector()
00086 LALSReadTSeries()                       LALSWriteTSeries()
00087 LALDButterworthREAL4TimeSeries()        LALCheckMemoryLeaks()
00088 \end{verbatim}
00089 
00090 \subsubsection*{Notes}
00091 
00092 \vfill{\footnotesize\input{BandPassTestCV}}
00093 
00094 ******************************************************* </lalLaTeX> */
00095 
00096 #include <lal/LALStdio.h>
00097 #include <lal/LALStdlib.h>
00098 #include <string.h>
00099 #include <stdlib.h>
00100 #include <stdio.h>
00101 #include <lal/AVFactories.h>
00102 #include <lal/BandPassTimeSeries.h>
00103 #include <lal/StreamInput.h>
00104 #include <lal/StreamOutput.h>
00105 
00106 NRCSID(BANDPASSTESTC,"$Id: BandPassTest.c,v 1.16 2007/06/08 14:41:56 bema Exp $");
00107 
00108 /* Default parameters. */
00109 INT4 lalDebugLevel=0;
00110 #define NPTS 4096   /* Length of time series. */
00111 #define DT 1.0      /* Sampling interval of time series. */
00112 #define OFFSET 1024 /* Offset of the impulse from the start. */
00113 #define F1 0.01     /* Lower frequency of transition band. */
00114 #define F2 0.015    /* Upper frequency of transition band. */
00115 #define A1 0.9      /* Desired attenuation at F1. */
00116 #define A2 0.1      /* Desired attenuation at F2. */
00117 #define ORDER 20    /* Maximum filter order. */
00118 
00119 /* Usage format string. */
00120 #define USAGE "Usage: %s [-d debuglevel] [-i infile | -n npts dt offset]\n" \
00121 "\t[-o outfile] [-f f1 f2 a1 a2 order]\n"
00122 
00123 /* Macros for printing errors and testing subroutines. */
00124 #define ERROR( code, msg, statement )                                \
00125 do {                                                                 \
00126   if ( lalDebugLevel & LALERROR )                                    \
00127     LALPrintError( "Error[0] %d: program %s, file %s, line %d, %s\n" \
00128                    "        %s %s\n", (code), *argv, __FILE__,       \
00129                    __LINE__, BANDPASSTESTC, statement ? statement :  \
00130                    "", (msg) );                                      \
00131 } while (0)
00132 
00133 #define INFO( statement )                                            \
00134 do {                                                                 \
00135   if ( lalDebugLevel & LALINFO )                                     \
00136     LALPrintError( "Info[0]: program %s, file %s, line %d, %s\n"     \
00137                    "        %s\n", *argv, __FILE__, __LINE__,        \
00138                    BANDPASSTESTC, (statement) );                     \
00139 } while (0)
00140 
00141 #define SUB( func, statusptr )                                       \
00142 do {                                                                 \
00143   if ( (func), (statusptr)->statusCode ) {                           \
00144     ERROR( BANDPASSTESTC_ESUB, BANDPASSTESTC_MSGESUB,                \
00145            "Function call \"" #func "\" failed:" );                  \
00146     return BANDPASSTESTC_ESUB;                                       \
00147   }                                                                  \
00148 } while (0)
00149 
00150 /* A global pointer for debugging. */
00151 #ifndef NDEBUG
00152 char *lalWatch;
00153 #endif
00154 
00155 int
00156 main(int argc, char **argv)
00157 {
00158   static LALStatus stat; /* LALStatus pointer */
00159   CHAR *infile = NULL;   /* The input filename */
00160   CHAR *outfile = NULL;  /* The output filename */
00161   INT4 arg;              /* Argument counter */
00162   UINT4 npts = NPTS;     /* Number of points in time series */
00163   UINT4 offset = OFFSET; /* Position of delta function */
00164   REAL8 dt = DT;         /* Sampling interval. */
00165   static REAL4TimeSeries series;    /* Time series */
00166   static PassBandParamStruc params; /* Filter parameters */
00167 
00168   XLALSetErrorHandler( XLALAbortErrorHandler );
00169 
00170   /* Set up the default filter parameters. */
00171   params.f1 = F1;
00172   params.f2 = F2;
00173   params.a1 = A1;
00174   params.a2 = A2;
00175   params.nMax = ORDER;
00176 
00177   /* Parse argument list.  i stores the current position. */
00178   arg = 1;
00179   while ( arg < argc ) {
00180     /* Parse debuglevel option. */
00181     if ( !strcmp( argv[arg], "-d" ) ) {
00182       if ( argc > arg + 1 ) {
00183         arg++;
00184         lalDebugLevel = atoi( argv[arg++] );
00185       } else {
00186         ERROR( BANDPASSTESTC_EARG, BANDPASSTESTC_MSGEARG, 0 );
00187         LALPrintError( USAGE, *argv );
00188         return BANDPASSTESTC_EARG;
00189       }
00190     }
00191     /* Parse input file option. */
00192     else if ( !strcmp( argv[arg], "-i" ) ) {
00193       if ( argc > arg + 1 ) {
00194         arg++;
00195         infile = argv[arg++];
00196       } else {
00197         ERROR( BANDPASSTESTC_EARG, BANDPASSTESTC_MSGEARG, 0 );
00198         LALPrintError( USAGE, *argv );
00199         return BANDPASSTESTC_EARG;
00200       }
00201     }
00202     /* Parse output file option. */
00203     else if ( !strcmp( argv[arg], "-o" ) ) {
00204       if ( argc > arg + 1 ) {
00205         arg++;
00206         outfile = argv[arg++];
00207       } else {
00208         ERROR( BANDPASSTESTC_EARG, BANDPASSTESTC_MSGEARG, 0 );
00209         LALPrintError( USAGE, *argv );
00210         return BANDPASSTESTC_EARG;
00211       }
00212     }
00213     /* Parse filter options. */
00214     else if ( !strcmp( argv[arg], "-f" ) ) {
00215       if ( argc > arg + 5 ) {
00216         arg++;
00217         params.f1=atof(argv[arg++]);
00218         params.f2=atof(argv[arg++]);
00219         params.a1=atof(argv[arg++]);
00220         params.a2=atof(argv[arg++]);
00221         params.nMax=atoi(argv[arg++]);
00222       } else {
00223         ERROR( BANDPASSTESTC_EARG, BANDPASSTESTC_MSGEARG, 0 );
00224         LALPrintError( USAGE, *argv );
00225         return BANDPASSTESTC_EARG;
00226       }
00227     }
00228     /* Parse time series options. */
00229     else if ( !strcmp( argv[arg], "-n" ) ) {
00230       if ( argc > arg + 3 ) {
00231         arg++;
00232         npts=atoi(argv[arg++]);
00233         dt=atof(argv[arg++]);
00234         offset=atoi(argv[arg++]);
00235       } else {
00236         ERROR( BANDPASSTESTC_EARG, BANDPASSTESTC_MSGEARG, 0 );
00237         LALPrintError( USAGE, *argv );
00238         return BANDPASSTESTC_EARG;
00239       }
00240     }
00241     /* Unrecognized option. */
00242     else {
00243       ERROR( BANDPASSTESTC_EARG, BANDPASSTESTC_MSGEARG, 0 );
00244       LALPrintError( USAGE, *argv );
00245       return BANDPASSTESTC_EARG;
00246     }
00247   } /* End of argument parsing loop. */
00248 
00249   /* Check input values. */
00250   if ( !infile ) {
00251     if ( offset >= npts ) {
00252       ERROR( BANDPASSTESTC_EBAD, BANDPASSTESTC_MSGEBAD, 0 );
00253       LALPrintError( "\toffset=%i must be less than npts=%i\n", offset,
00254                      npts );
00255       return BANDPASSTESTC_EBAD;
00256     }
00257   }
00258 
00259   /* Create the time series. */
00260   if ( infile ) {
00261     FILE *fp = fopen( infile, "r" );
00262     if ( !fp ) {
00263       ERROR( BANDPASSTESTC_EFILE, BANDPASSTESTC_MSGEFILE, infile );
00264       return BANDPASSTESTC_EFILE;
00265     }
00266     SUB( LALSReadTSeries( &stat, &series, fp ), &stat );
00267     fclose( fp );
00268   } else {
00269     LALSnprintf( series.name, LALNameLength, "%s", "Impulse" );
00270     series.deltaT = dt;
00271     SUB( LALSCreateVector( &stat, &(series.data), npts ), &stat );
00272     memset( series.data->data, 0, npts*sizeof(REAL4) );
00273     series.data->data[offset] = 1.0;
00274   }
00275 
00276   /* Filter the time series. */
00277   SUB( LALDButterworthREAL4TimeSeries( &stat, &series, &params ),
00278        &stat );
00279 
00280   /* Print the output, if the -o option was given. */
00281   if ( outfile ) {
00282     FILE *fp = fopen( outfile, "w" );
00283     if ( !fp ){
00284       ERROR( BANDPASSTESTC_EFILE, BANDPASSTESTC_MSGEFILE, outfile );
00285       return BANDPASSTESTC_EFILE;
00286     }
00287     SUB( LALSWriteTSeries( &stat, fp, &series ), &stat );
00288     fclose( fp );
00289   }
00290 
00291   /* Free memory and exit. */
00292   SUB( LALSDestroyVector( &stat, &(series.data) ), &stat );
00293   LALCheckMemoryLeaks();
00294   INFO( BANDPASSTESTC_MSGENORM );
00295   return BANDPASSTESTC_ENORM;
00296 }

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