[1] | 1 | |
---|
| 2 | /************************************************************************/ |
---|
| 3 | /* yrms : is a part of Yao utilities and tools */ |
---|
| 4 | /************************************************************************/ |
---|
| 5 | |
---|
| 6 | #include <stdio.h> |
---|
| 7 | #include <stdlib.h> |
---|
| 8 | #include <math.h> |
---|
| 9 | |
---|
| 10 | #define BUFSIZE 80 |
---|
| 11 | |
---|
| 12 | int main (int argc, char *argv[]) |
---|
| 13 | { /* . Syntaxe: yrms f1 f2 zspace [f3] */ |
---|
| 14 | /* . Purpose: calculer l'erreur rms entre 2 trajectoires d'espace |
---|
| 15 | dont les valeurs sont respectvement dans les fichiers |
---|
| 16 | f1 et f2 passes en parametres qui doivent contenir |
---|
| 17 | le meme nombre d'elements qui doit d'ailleurs etre |
---|
| 18 | un multiple de zspace qui est la taille de l'espace |
---|
| 19 | le resultat, {pour chaque pas d'espace, zspace} est |
---|
| 20 | ecrit dans f3 s'il est mentionne sinon sur la sortie |
---|
| 21 | standard. |
---|
| 22 | la somme des rms est affichee a la fin |
---|
| 23 | . Warning: la detection de ligne blanche ou vide n'est pas geree |
---|
| 24 | . Compilation : cc -Wall -o yrms yrms.c -lm */ |
---|
| 25 | |
---|
| 26 | FILE *f1, *f2, *f3; |
---|
| 27 | char buf1[BUFSIZE+1], buf2[BUFSIZE+1]; |
---|
| 28 | int zspace, rstelt, nbt; |
---|
| 29 | double x1, x2, errsumt, errsumallt, rms, sumrms; |
---|
| 30 | |
---|
| 31 | if (argc<4 || argc > 5) |
---|
| 32 | { printf("syntaxe error !!! : yrms f1 f2 zspace [f3]\n"); |
---|
| 33 | exit(0); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | if ((f1 = fopen(argv[1], "r")) <= 0) |
---|
| 37 | { printf ("file1 %s not found !!!\n", argv[1]); |
---|
| 38 | exit(0); |
---|
| 39 | } |
---|
| 40 | if ((f2 = fopen(argv[2], "r")) <= 0) |
---|
| 41 | { printf ("file2 %s not found !!!\n", argv[2]); |
---|
| 42 | exit(0); |
---|
| 43 | } |
---|
| 44 | if (argc==5) |
---|
| 45 | { if ((f3 = fopen(argv[4], "w")) <= 0) |
---|
| 46 | { printf ("failed while opening file3 %s !!!\n", argv[3]); |
---|
| 47 | exit(0); |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | else |
---|
| 51 | f3 = stdout; |
---|
| 52 | |
---|
| 53 | zspace = atoi(argv[3]); |
---|
| 54 | if (zspace<=0) |
---|
| 55 | { printf ("zspace must be a integer greater than zero !!!\n"); |
---|
| 56 | exit(0); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | rstelt=0; nbt=0; errsumt=0.0; errsumallt=0.0, sumrms=0.0; |
---|
| 60 | while (1) /* boucle de */ |
---|
| 61 | { /* lecture paralle des 2 fichiers */ |
---|
| 62 | if (fgets (buf1, BUFSIZE, f1) == NULL) |
---|
| 63 | { /* fin du 1er fichier => fin du 2eme fichier */ |
---|
| 64 | if (fgets (buf2, BUFSIZE, f2) != NULL) |
---|
| 65 | { printf("erratum 1: the files have not the same size !!!\n"); |
---|
| 66 | exit(0); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /* fin: le nombre d'elt devait etre un multiple de zspace */ |
---|
| 70 | if (rstelt!=0) |
---|
| 71 | { printf("erratum 3: number of element is not o multiple of zspace\n"); |
---|
| 72 | exit(0); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /* fin fin */ |
---|
| 76 | break; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /* on doit pouvoir lire le 2eme fichier */ |
---|
| 80 | if (fgets (buf2, BUFSIZE, f2) == NULL) |
---|
| 81 | { printf("erratum 2: the files have not the same size !!!\n"); |
---|
| 82 | exit(0); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | x1 = atof(buf1); x2 = atof(buf2); |
---|
| 86 | //printf("x1=%f x2=%f \n", x1, x2); |
---|
| 87 | |
---|
| 88 | errsumt += ((x1-x2)*(x1-x2)); |
---|
| 89 | errsumallt += errsumt; |
---|
| 90 | |
---|
| 91 | rstelt = (rstelt+1)%zspace; |
---|
| 92 | if (rstelt==0) |
---|
| 93 | { //printf ("errsumt=%f \n", errsumt); |
---|
| 94 | rms = sqrt(errsumt/zspace); |
---|
| 95 | fprintf(f3, "%f\n", rms); |
---|
| 96 | errsumt = 0.0; |
---|
| 97 | ++nbt; |
---|
| 98 | sumrms += rms; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | } /* fin du while de la boucle de lecture parallele */ |
---|
| 102 | printf("SUMt(rms) = %30.20e\n", sumrms); |
---|
| 103 | |
---|
| 104 | /* rms sur tout la trajectoire */ |
---|
| 105 | rms = sqrt(errsumallt/(zspace*nbt)); |
---|
| 106 | printf("RMS(trajectoire::%i pas de temps) = %30.20e\n", nbt, rms); |
---|
| 107 | |
---|
| 108 | printf("yrms: process end normaly\n"); |
---|
| 109 | return (0); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | |
---|