Grelet Jacques
/
Ni-MH_V3
example code using statis library for temperature measurement with LM35 and LM335
statis.cpp@3:0e06300940cd, 2012-11-22 (annotated)
- Committer:
- greletj
- Date:
- Thu Nov 22 14:47:20 2012 +0000
- Revision:
- 3:0e06300940cd
- Child:
- 4:ffc39af07ade
last version added and compile with outils but works with filter
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
greletj | 3:0e06300940cd | 1 | /***************************************************************************** |
greletj | 3:0e06300940cd | 2 | * Statis.c * |
greletj | 3:0e06300940cd | 3 | * * |
greletj | 3:0e06300940cd | 4 | * Module de calcul statistique utilisees par le logiciel THERMO * |
greletj | 3:0e06300940cd | 5 | * Ce module dans la version 1.0 calcule : la mediane * |
greletj | 3:0e06300940cd | 6 | * la moyenne * |
greletj | 3:0e06300940cd | 7 | * l'ecart type * |
greletj | 3:0e06300940cd | 8 | * Les fonctions prennent toutes comme argumemts un tableau FAR list[] * |
greletj | 3:0e06300940cd | 9 | * et les n_elem sur lequel est realise le calcul. * |
greletj | 3:0e06300940cd | 10 | * * |
greletj | 3:0e06300940cd | 11 | * Jacques Grelet Mars 1992 * |
greletj | 3:0e06300940cd | 12 | * Bruno Buisson Aout 92 * |
greletj | 3:0e06300940cd | 13 | * Septembre 1992 * |
greletj | 3:0e06300940cd | 14 | * Septembre 1994 * |
greletj | 3:0e06300940cd | 15 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 16 | #define DEBUG |
greletj | 3:0e06300940cd | 17 | #undef DEBUG |
greletj | 3:0e06300940cd | 18 | |
greletj | 3:0e06300940cd | 19 | #include <debug.h> |
greletj | 3:0e06300940cd | 20 | #include <sys.h> |
greletj | 3:0e06300940cd | 21 | #include <math.h> |
greletj | 3:0e06300940cd | 22 | #include <stdlib.h> // cf. qsort(),fabs() |
greletj | 3:0e06300940cd | 23 | |
greletj | 3:0e06300940cd | 24 | // ring |
greletj | 3:0e06300940cd | 25 | #include <ring.h> // cf. statis.h |
greletj | 3:0e06300940cd | 26 | |
greletj | 3:0e06300940cd | 27 | // statis |
greletj | 3:0e06300940cd | 28 | #include <bitmsk.h> // cf. statis.h |
greletj | 3:0e06300940cd | 29 | #define _STATIS |
greletj | 3:0e06300940cd | 30 | #include <statis.h> |
greletj | 3:0e06300940cd | 31 | #undef _STATIS |
greletj | 3:0e06300940cd | 32 | |
greletj | 3:0e06300940cd | 33 | /***************************************************************************** |
greletj | 3:0e06300940cd | 34 | * classe Mediane_item * |
greletj | 3:0e06300940cd | 35 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 36 | |
greletj | 3:0e06300940cd | 37 | /***************************************************************************** |
greletj | 3:0e06300940cd | 38 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 39 | procedure Mediane_item::raz( void ) |
greletj | 3:0e06300940cd | 40 | { |
greletj | 3:0e06300940cd | 41 | valeur = FLOAT_ERREUR; |
greletj | 3:0e06300940cd | 42 | indice = 0; |
greletj | 3:0e06300940cd | 43 | } |
greletj | 3:0e06300940cd | 44 | |
greletj | 3:0e06300940cd | 45 | /***************************************************************************** |
greletj | 3:0e06300940cd | 46 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 47 | Mediane_item::Mediane_item( void ) |
greletj | 3:0e06300940cd | 48 | { |
greletj | 3:0e06300940cd | 49 | raz(); |
greletj | 3:0e06300940cd | 50 | } |
greletj | 3:0e06300940cd | 51 | |
greletj | 3:0e06300940cd | 52 | /***************************************************************************** |
greletj | 3:0e06300940cd | 53 | * fonction utilitaire de qsort(),donc pas membre de classe * |
greletj | 3:0e06300940cd | 54 | * si a < b retourne -1 * |
greletj | 3:0e06300940cd | 55 | * si a = b 0 * |
greletj | 3:0e06300940cd | 56 | * si a > b 1 * |
greletj | 3:0e06300940cd | 57 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 58 | static int compare( const void *a,const void *b ) |
greletj | 3:0e06300940cd | 59 | { |
greletj | 3:0e06300940cd | 60 | register double c = ((Mediane_item *)a)->valeur - ((Mediane_item *)b)->valeur; |
greletj | 3:0e06300940cd | 61 | |
greletj | 3:0e06300940cd | 62 | return( ( c == 0.0 ) ? 0 : ( ( c > 0.0 ) ? 1 : -1 ) ); |
greletj | 3:0e06300940cd | 63 | } |
greletj | 3:0e06300940cd | 64 | |
greletj | 3:0e06300940cd | 65 | /***************************************************************************** |
greletj | 3:0e06300940cd | 66 | * classe Mediane * |
greletj | 3:0e06300940cd | 67 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 68 | |
greletj | 3:0e06300940cd | 69 | /***************************************************************************** |
greletj | 3:0e06300940cd | 70 | * n_elem est forcement impair (protege par Statis_data::mediane()) * |
greletj | 3:0e06300940cd | 71 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 72 | Mediane_item &Mediane::mediane( Mediane_item list[],int n_elem ) |
greletj | 3:0e06300940cd | 73 | { |
greletj | 3:0e06300940cd | 74 | switch( n_elem ) { |
greletj | 3:0e06300940cd | 75 | case 1 : return( list[ 0 ] ); |
greletj | 3:0e06300940cd | 76 | default : qsort( (Mediane_item *)list,n_elem,sizeof( list[ 0 ] ),compare ); |
greletj | 3:0e06300940cd | 77 | return( list[ ( n_elem - 1 ) / 2 ] ); |
greletj | 3:0e06300940cd | 78 | } |
greletj | 3:0e06300940cd | 79 | } |
greletj | 3:0e06300940cd | 80 | |
greletj | 3:0e06300940cd | 81 | /***************************************************************************** |
greletj | 3:0e06300940cd | 82 | * classe Mediane_circ * |
greletj | 3:0e06300940cd | 83 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 84 | |
greletj | 3:0e06300940cd | 85 | /***************************************************************************** |
greletj | 3:0e06300940cd | 86 | * n_elem est forcement impair (protege par Statis_data::mediane()) * |
greletj | 3:0e06300940cd | 87 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 88 | Mediane_item &Mediane_circ::mediane( Mediane_item list[],int n_elem ) |
greletj | 3:0e06300940cd | 89 | { |
greletj | 3:0e06300940cd | 90 | int delta_nord, |
greletj | 3:0e06300940cd | 91 | delta_sud, |
greletj | 3:0e06300940cd | 92 | borne_25, |
greletj | 3:0e06300940cd | 93 | borne_75; |
greletj | 3:0e06300940cd | 94 | Mediane_item *result; |
greletj | 3:0e06300940cd | 95 | |
greletj | 3:0e06300940cd | 96 | if( n_elem == 1 ) return( list[ 0 ] ); |
greletj | 3:0e06300940cd | 97 | qsort( (Mediane_item *)list,n_elem,sizeof( list[ 0 ] ),compare ); |
greletj | 3:0e06300940cd | 98 | borne_25 = n_elem / 4; // 25% |
greletj | 3:0e06300940cd | 99 | borne_75 = 3 * n_elem / 4; // 75% |
greletj | 3:0e06300940cd | 100 | delta_nord = int( list[ borne_75 ].valeur - list[ borne_25 ].valeur ); |
greletj | 3:0e06300940cd | 101 | delta_sud = abs( delta_nord - (int)(int)maxi ); |
greletj | 3:0e06300940cd | 102 | if( delta_sud < delta_nord ) { // si plus de 50% de valeurs dans |
greletj | 3:0e06300940cd | 103 | for( unsigned i = 0; i < n_elem; i++ ) // le nord, on change de repere |
greletj | 3:0e06300940cd | 104 | if( list[ i ].valeur > moitie && list[ i ].valeur < maxi ) // 180/360 |
greletj | 3:0e06300940cd | 105 | list[ i ].valeur -= maxi; // -180/180 |
greletj | 3:0e06300940cd | 106 | qsort( (Mediane_item *)list,n_elem,sizeof( list[ 0 ] ),compare ); |
greletj | 3:0e06300940cd | 107 | } |
greletj | 3:0e06300940cd | 108 | result = &list[ ( n_elem - 1 ) / 2 ]; |
greletj | 3:0e06300940cd | 109 | if( result->valeur < 0 ) result->valeur += maxi; |
greletj | 3:0e06300940cd | 110 | return( *result ); |
greletj | 3:0e06300940cd | 111 | } |
greletj | 3:0e06300940cd | 112 | |
greletj | 3:0e06300940cd | 113 | /***************************************************************************** |
greletj | 3:0e06300940cd | 114 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 115 | Mediane_circ::Mediane_circ( double a_maxi ) |
greletj | 3:0e06300940cd | 116 | { |
greletj | 3:0e06300940cd | 117 | moitie = ( maxi = fabs( a_maxi ) ) / 2; |
greletj | 3:0e06300940cd | 118 | } |
greletj | 3:0e06300940cd | 119 | |
greletj | 3:0e06300940cd | 120 | /***************************************************************************** |
greletj | 3:0e06300940cd | 121 | * classe Statis_data * |
greletj | 3:0e06300940cd | 122 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 123 | |
greletj | 3:0e06300940cd | 124 | /***************************************************************************** |
greletj | 3:0e06300940cd | 125 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 126 | booleen Statis_data::verifie( double &val ) |
greletj | 3:0e06300940cd | 127 | { |
greletj | 3:0e06300940cd | 128 | if( ( borne_inf == borne_sup ) || ( val >= borne_inf && val <= borne_sup ) ) |
greletj | 3:0e06300940cd | 129 | return( TRUE ); |
greletj | 3:0e06300940cd | 130 | val = FLOAT_ERREUR; |
greletj | 3:0e06300940cd | 131 | return( FALSE ); |
greletj | 3:0e06300940cd | 132 | } |
greletj | 3:0e06300940cd | 133 | |
greletj | 3:0e06300940cd | 134 | /***************************************************************************** |
greletj | 3:0e06300940cd | 135 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 136 | Mediane_item *Statis_data::cons_liste( void ) |
greletj | 3:0e06300940cd | 137 | { |
greletj | 3:0e06300940cd | 138 | for( unsigned i = 0; i < n_elem; i++ ) { |
greletj | 3:0e06300940cd | 139 | mediane_liste[ i ].indice = i; |
greletj | 3:0e06300940cd | 140 | mediane_liste[ i ].valeur = ring.list[ i ]; |
greletj | 3:0e06300940cd | 141 | } |
greletj | 3:0e06300940cd | 142 | return( mediane_liste ); |
greletj | 3:0e06300940cd | 143 | } |
greletj | 3:0e06300940cd | 144 | |
greletj | 3:0e06300940cd | 145 | /***************************************************************************** |
greletj | 3:0e06300940cd | 146 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 147 | booleen Statis_data::mediane( void ) |
greletj | 3:0e06300940cd | 148 | { |
greletj | 3:0e06300940cd | 149 | if( !mediane_methode || n_elem == 0 ) { |
greletj | 3:0e06300940cd | 150 | med.raz(); |
greletj | 3:0e06300940cd | 151 | return( FALSE ); |
greletj | 3:0e06300940cd | 152 | } |
greletj | 3:0e06300940cd | 153 | med = mediane_methode->mediane( cons_liste(), |
greletj | 3:0e06300940cd | 154 | ( n_elem % 2 == 0 ) ? n_elem - 1 : n_elem ); |
greletj | 3:0e06300940cd | 155 | return( verifie( med.valeur ) ); |
greletj | 3:0e06300940cd | 156 | } |
greletj | 3:0e06300940cd | 157 | |
greletj | 3:0e06300940cd | 158 | /***************************************************************************** |
greletj | 3:0e06300940cd | 159 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 160 | booleen Statis_data::moyenne( void ) |
greletj | 3:0e06300940cd | 161 | { |
greletj | 3:0e06300940cd | 162 | if( n_elem == 0 ) { |
greletj | 3:0e06300940cd | 163 | moy = FLOAT_ERREUR; |
greletj | 3:0e06300940cd | 164 | return( FALSE ); |
greletj | 3:0e06300940cd | 165 | } |
greletj | 3:0e06300940cd | 166 | moy = 0; |
greletj | 3:0e06300940cd | 167 | for( unsigned i = 0; i < n_elem; moy += ring.list[ i++ ] ); |
greletj | 3:0e06300940cd | 168 | moy /= (double) n_elem; |
greletj | 3:0e06300940cd | 169 | return( verifie( moy ) ); |
greletj | 3:0e06300940cd | 170 | } |
greletj | 3:0e06300940cd | 171 | |
greletj | 3:0e06300940cd | 172 | /***************************************************************************** |
greletj | 3:0e06300940cd | 173 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 174 | booleen Statis_data::ecart_type( void ) |
greletj | 3:0e06300940cd | 175 | { |
greletj | 3:0e06300940cd | 176 | double somme_xi_carre = 0.0, |
greletj | 3:0e06300940cd | 177 | somme_xi = 0.0, |
greletj | 3:0e06300940cd | 178 | x_bar = 0.0; |
greletj | 3:0e06300940cd | 179 | double *ptxi; |
greletj | 3:0e06300940cd | 180 | unsigned i; |
greletj | 3:0e06300940cd | 181 | |
greletj | 3:0e06300940cd | 182 | switch( n_elem ) { |
greletj | 3:0e06300940cd | 183 | case 0 : |
greletj | 3:0e06300940cd | 184 | case 1 : |
greletj | 3:0e06300940cd | 185 | case 2 : ecart = FLOAT_ERREUR; |
greletj | 3:0e06300940cd | 186 | return( FALSE ); |
greletj | 3:0e06300940cd | 187 | default : for( i = 0,ptxi = ring.list; i < n_elem; |
greletj | 3:0e06300940cd | 188 | i++,ptxi++ ) { |
greletj | 3:0e06300940cd | 189 | somme_xi += *ptxi; |
greletj | 3:0e06300940cd | 190 | somme_xi_carre += *ptxi * *ptxi; |
greletj | 3:0e06300940cd | 191 | } |
greletj | 3:0e06300940cd | 192 | x_bar = somme_xi / (double) n_elem; |
greletj | 3:0e06300940cd | 193 | ecart = sqrt( fabs( somme_xi_carre / (double) n_elem - |
greletj | 3:0e06300940cd | 194 | x_bar * x_bar ) ); |
greletj | 3:0e06300940cd | 195 | return( TRUE ); |
greletj | 3:0e06300940cd | 196 | } |
greletj | 3:0e06300940cd | 197 | } |
greletj | 3:0e06300940cd | 198 | |
greletj | 3:0e06300940cd | 199 | /***************************************************************************** |
greletj | 3:0e06300940cd | 200 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 201 | procedure Statis_data::calcule( void ) |
greletj | 3:0e06300940cd | 202 | { |
greletj | 3:0e06300940cd | 203 | DPRINTF( ("Statis::calcule:stat_on=%u,calculs=%x\n",stat_on,calculs) ); |
greletj | 3:0e06300940cd | 204 | if( stat_on ) { |
greletj | 3:0e06300940cd | 205 | n_elem = ring.store(); |
greletj | 3:0e06300940cd | 206 | DPRINTF( ("n_elem=%u.",n_elem) ); |
greletj | 3:0e06300940cd | 207 | if( calculs & STAT_MED ) mediane(); |
greletj | 3:0e06300940cd | 208 | if( calculs & STAT_MOYENNE ) { |
greletj | 3:0e06300940cd | 209 | moyenne(); |
greletj | 3:0e06300940cd | 210 | if( calculs & STAT_ECART_TYPE ) ecart_type(); |
greletj | 3:0e06300940cd | 211 | } |
greletj | 3:0e06300940cd | 212 | } |
greletj | 3:0e06300940cd | 213 | else { |
greletj | 3:0e06300940cd | 214 | ecart = 0; |
greletj | 3:0e06300940cd | 215 | moy = |
greletj | 3:0e06300940cd | 216 | med.valeur = ( n_elem > 0 ) ? instant : FLOAT_ERREUR; |
greletj | 3:0e06300940cd | 217 | med.indice = 0; |
greletj | 3:0e06300940cd | 218 | } |
greletj | 3:0e06300940cd | 219 | DPRINTF( ("med=%5.4g,moy=%5.4g,instant=%5.4g\n", |
greletj | 3:0e06300940cd | 220 | med.valeur,moy,instant) ); |
greletj | 3:0e06300940cd | 221 | n_elem = 0; |
greletj | 3:0e06300940cd | 222 | } |
greletj | 3:0e06300940cd | 223 | |
greletj | 3:0e06300940cd | 224 | /***************************************************************************** |
greletj | 3:0e06300940cd | 225 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 226 | double Statis_data::put( double val ) |
greletj | 3:0e06300940cd | 227 | { |
greletj | 3:0e06300940cd | 228 | instant = val; |
greletj | 3:0e06300940cd | 229 | if( stat_on ) ring.put( instant ); |
greletj | 3:0e06300940cd | 230 | else n_elem = 1; |
greletj | 3:0e06300940cd | 231 | return( instant ); |
greletj | 3:0e06300940cd | 232 | } |
greletj | 3:0e06300940cd | 233 | |
greletj | 3:0e06300940cd | 234 | /***************************************************************************** |
greletj | 3:0e06300940cd | 235 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 236 | Statis_data::Statis_data( BITMSK calc,unsigned taille, |
greletj | 3:0e06300940cd | 237 | double b_inf /*= 0*/,double b_sup /*= 0*/ ) |
greletj | 3:0e06300940cd | 238 | : ring( taille ), |
greletj | 3:0e06300940cd | 239 | med() |
greletj | 3:0e06300940cd | 240 | { |
greletj | 3:0e06300940cd | 241 | moy = |
greletj | 3:0e06300940cd | 242 | ecart = |
greletj | 3:0e06300940cd | 243 | instant = FLOAT_ERREUR; |
greletj | 3:0e06300940cd | 244 | borne_inf = b_inf; |
greletj | 3:0e06300940cd | 245 | borne_sup = b_sup; |
greletj | 3:0e06300940cd | 246 | n_elem = 0; |
greletj | 3:0e06300940cd | 247 | if( stat_on = ( taille > 1 ) ) { |
greletj | 3:0e06300940cd | 248 | // init_ring_d( &ring,taille ); |
greletj | 3:0e06300940cd | 249 | if( mediane_methode = ( ( calculs = calc ) & STAT_MED_NORM ) |
greletj | 3:0e06300940cd | 250 | ? new Mediane |
greletj | 3:0e06300940cd | 251 | : ( calc & STAT_MED_CIRC ) ? new Mediane_circ( b_sup ) |
greletj | 3:0e06300940cd | 252 | : NULL ) |
greletj | 3:0e06300940cd | 253 | mediane_liste = new Mediane_item[ taille ]; |
greletj | 3:0e06300940cd | 254 | else |
greletj | 3:0e06300940cd | 255 | mediane_liste = NULL; |
greletj | 3:0e06300940cd | 256 | } |
greletj | 3:0e06300940cd | 257 | else { |
greletj | 3:0e06300940cd | 258 | mediane_methode = NULL; |
greletj | 3:0e06300940cd | 259 | mediane_liste = NULL; |
greletj | 3:0e06300940cd | 260 | } |
greletj | 3:0e06300940cd | 261 | } |
greletj | 3:0e06300940cd | 262 | |
greletj | 3:0e06300940cd | 263 | /***************************************************************************** |
greletj | 3:0e06300940cd | 264 | *****************************************************************************/ |
greletj | 3:0e06300940cd | 265 | Statis_data::~Statis_data() |
greletj | 3:0e06300940cd | 266 | { |
greletj | 3:0e06300940cd | 267 | if( stat_on ) { |
greletj | 3:0e06300940cd | 268 | // term_ring_d( &ring ); |
greletj | 3:0e06300940cd | 269 | if( mediane_methode ) delete mediane_methode; |
greletj | 3:0e06300940cd | 270 | if( mediane_liste ) delete [] mediane_liste; |
greletj | 3:0e06300940cd | 271 | } |
greletj | 3:0e06300940cd | 272 | } |