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