SourceForge.net Logo timeSeries: timeSeries Class Library

hmi04_utilities.cpp

Utility functions for the hmi04.cpp example

/*
 *  hmi04_utilities.cpp
 *  hmi04
 *
 *  Created by Jeff Greenberg on 4/4/05.
 *  Copyright Jeff Greenberg.  
 
 *
 *  This library is free software; you can redistribute it and/or modify it under the 
 *  terms of the GNU Lesser General Public License as published by the Free Software 
 *  Foundation; either version 2.1 of the License, or (at your option) any later 
 *  version.

 *  This library is distributed in the hope that it will be useful, but WITHOUT ANY 
 *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 *  PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

 *  You should have received a copy of the GNU Lesser General Public License along 
 *  with this library; if not, write to the Free Software Foundation, Inc., 59 Temple 
 *  Place, Suite 330, Boston, MA 02111-1307 USA 
 
 *
 *  This library is free software; you can redistribute it and/or modify it under the 
 *  terms of the GNU Lesser General Public License as published by the Free Software 
 *  Foundation; either version 2.1 of the License, or (at your option) any later 
 *  version.

 *  This library is distributed in the hope that it will be useful, but WITHOUT ANY 
 *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 *  PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

 *  You should have received a copy of the GNU Lesser General Public License along 
 *  with this library; if not, write to the Free Software Foundation, Inc., 59 Temple 
 *  Place, Suite 330, Boston, MA 02111-1307 USA 
 
 *
 */

#include "hmi04_utilities.h"

using namespace std;

/*
 remove the extension from a file name
*/
string removePathExt(string& file) {
    size_t end=file.find_first_of(".");
    
    return file.substr(0,end);
}

/*
    return only the leaf name, with file path
    and extension removed
*/
string getLeafName(string& file) {
#ifdef WIN32
    size_t beg=file.find_last_of("\\")+1;
#else
    size_t beg=file.find_last_of("/")+1;
    
#endif
    size_t end=file.find_first_of(".",beg)-1;
    
    return file.substr(beg,end-beg+1);
}


/* sw_lp is a 3rd order low pass Chebychev1 filter with a break frequency of
5 hz and 1db of passband ripple at 50Hz sampling rate
*/


iir_t *init_sw_lp()
{   double coeff[][6]= {
{1, 1.00000959440837, 0, 1, -0.723297433052849, 0},
{1, 1.99999040559162, 0.999990405683675, 1, -1.41449248783418, 0.746246746816894}
};
double gain=0.0114746568820209;
iir_t *f;

f=init_iir_filter(coeff, gain, 2);

return f;
}

/*
 template function for doing a phaseless, acausal 
 filter of a timeSeries class object. This routine
 is not sophisticated and makes no attempt to
 correct for edge effects. It also supports only
 iir filters
*/

template <class T>
void filtfilt(iir_t *lpf,timeSeries<T>& ts) {
    // filter in place....
    
    // forward...
    for ( int i=0;i<ts.length();i++) {
        ts[i] = iir_filter(lpf,ts[i]);
    }
    
    // then backward...
    for ( int i=ts.length()-1;i>=0;i--) {
        ts[i] = iir_filter(lpf,ts[i]);
    }
}
template void filtfilt<float>(iir_t *, timeSeries<float>&);
template void filtfilt<double>(iir_t *, timeSeries<double>&);


/* 
    get a stateFlag from a VIRTTEX data file.
*/

stateFlag getVirttexStateFlag(dataDef& ddef, 
                              dataFile& df, 
                              string s) {
    timeSeries<float> rawSeries(ddef,df);
    stateFlag sf(s.c_str(),"",rawSeries);
    return sf;
}


/* a bit of brutal c++ stuff here. Since we
can't avoid a complier warning when converting from
an int to an enum we have to do it explicitly here.
The point is that out-of-range values can be 
signalled immediatly at the conversion stage and
don't have to be handled downstream.

The C way was easier though

I've changed my mind about this! We now have to determine the scenario
type from *two* bits of info: the scenario number and the radar state.
If the radar state is on (indicated by num_vehs > 0) then scenarios 7 & 8
are FCW events. If the radar is off these are reaction time events and
need to be recorded differently.

*/

scenario_t intToScenario(int i, int num_vehs) {
    switch (i) {
        case 1:
            return foil;
            break;
        case 2:
            return ldw_tp_d;
            break;
        case 3:
            return acc_tp_a;
            break;
        case 4:
            return acc_tp_d;
            break;
        case 5:
            return acc_fp_a;
            break;
        case 6:
            return acc_fp_d;
            break;
        case 7:
            if (num_vehs == 0) {
                return rti_tp_a;
            } else {
                return fcw_tp_a;
            }
            break;
        case 8:
            if (num_vehs == 0) {
                return rti_tp_d;
            } else {
                return fcw_tp_d;
            }
            break;
        case 9:
            return fcw_fp_a;
            break;
        case 10:
            return fcw_fp_d;
            break;
        case 11:
            return ldw_fp_a_L;
            break;
        case 12:
            return ldw_fp_d_L;
            break;
        case 13:
            return ldw_fp_a_R;
            break;
        case 14:
            return ldw_fp_d_R;
            break;
        case 15:
            return ldw_tp_a;
            break;
        case 18:
            return no_op;
            break;
        default:
            cout << "illegal scenario value: "<< i << endl;
            return no_op;   // should throw an exception here
            break;
    }
}

/*
    translate an integer representing an LDW
    type to a descriptive string
*/

string intToLdwType(int ldw_no) {
    string ldw_type_name;
    
    switch (ldw_no) {
        case 0:
            ldw_type_name = "none";
            break;
        case 1:
            ldw_type_name = "tone";
            break;
        case 2:
            ldw_type_name = "rumble";
            break;
        case 3:
            ldw_type_name = "swvib";
            break;
        case 4:
            ldw_type_name = "seatvib";
            break;
        default:
            ldw_type_name = "error";
            break;
    }
    
    return ldw_type_name;
}

/* 
    translate a scenario_t enum to a string.
    C and C++ still have no built-in way to get
    string rep of an enum so we do it the hard way
*/

#define casename(x) case x: return #x   
string scenarioName(scenario_t scenario) {
    switch(scenario) {
        casename(foil);
        casename(ldw_tp_d);
        casename(acc_tp_a);
        casename(acc_tp_d);
        casename(acc_fp_a);
        casename(acc_fp_d);
        casename(fcw_tp_a);
        casename(fcw_tp_d);
        casename(fcw_fp_a);
        casename(fcw_fp_d);
        casename(ldw_fp_a_L);
        casename(ldw_fp_d_L);
        casename(ldw_fp_a_R);
        casename(ldw_fp_d_R);
        casename(ldw_tp_a);
        casename(no_op);
        casename(rti_tp_a);
        casename(rti_tp_d);
        default:
            cout << "bad scenario name!!" << endl;
    }
}
    
    
Generated on Tue Mar 16 15:10:49 2010 for timeSeries by  doxygen 1.6.3