1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int generateSinus(double * sinus,
int nbPts,
double physicalRange,
double amplitudeFactor,
double yOffset);
int generateLinear(double * linear,
int nbPts,
double physicalRange,
double a,
double b);
int save2Ddata_dbl(char const * filename, double * data, int nbPts, double physicalRange);
int substractBdataFromAdata(const double * a,
const double * b,
int nbPts,
double * res);
/**
* Program entry point
*/
int main(void) {
static const double CST_PI = 4 * atan (1.0);
int nbPoints = 1000;
double * sinusoide = NULL;
double * linear = NULL;
double * sinMinuslinear = NULL;
sinusoide = malloc(nbPoints*sizeof(*sinusoide));
if (!sinusoide) {
fprintf(stderr, "Allocation failure. Exiting...\n");
exit(EXIT_FAILURE);
}
generateSinus(sinusoide, nbPoints, 4*CST_PI, 1.0, -0.5);
save2Ddata_dbl("sinus.txt", sinusoide, nbPoints, 4*CST_PI);
linear = malloc(nbPoints*sizeof(*linear));
if (!linear) {
fprintf(stderr, "Allocation failure. Exiting...\n");
free(sinusoide);
exit(EXIT_FAILURE);
}
generateLinear(linear, nbPoints, 4*CST_PI, 0.25, -1.0);
save2Ddata_dbl("linear.txt", linear, nbPoints, 4*CST_PI);
sinMinuslinear = malloc(nbPoints*sizeof(*sinMinuslinear));
if (!sinMinuslinear) {
fprintf(stderr, "Allocation failure. Exiting...\n");
free(sinusoide); free(linear);
exit(EXIT_FAILURE);
}
substractBdataFromAdata(sinusoide, linear, nbPoints, sinMinuslinear);
save2Ddata_dbl("diffSinusLinear.txt", sinMinuslinear, nbPoints, 4*CST_PI);
free(linear);
free(sinusoide);
free(sinMinuslinear);
return 0;
}
/**
* Generate a sinus
*/
int generateSinus(double * sinus,
int nbPts,
double physicalRange,
double amplitudeFactor,
double yOffset) {
int rc = -1;
if (sinus && nbPts > 0) {
double step = physicalRange / nbPts;
double physicalValue = 0.0;
int i;
for (i=0; i<nbPts; ++i, physicalValue+=step) {
sinus[i] = ( amplitudeFactor * sin(physicalValue) ) + yOffset;
}
rc = 0;
}
return rc;
}
/**
* Generate a line (ax+ b)
*/
int generateLinear(double * linear,
int nbPts,
double physicalRange,
double a,
double b) {
int rc = -1;
if (linear && nbPts > 0) {
double step = physicalRange / nbPts;
double physicalValue = 0.0;
int i;
for (i=0; i<nbPts; ++i, physicalValue+=step) {
linear[i] = a*physicalValue + b;
}
rc = 0;
}
return rc;
}
/**
* Substract b data from a data (result stored in res)
*/
int substractBdataFromAdata(const double * a,
const double * b,
int nbPts,
double * res) {
int rc = -1;
if (a && b && res && nbPts > 0) {
int i;
for (i=0; i<nbPts; ++i) {
res[i] = a[i] - b[i];
}
rc = 0;
}
return rc;
}
/**
* Save data to a text file (usable with gnuplot)
*/
int save2Ddata_dbl(char const * filename, double * data, int nbPts, double physicalRange) {
int rc = -1;
if (filename) {
FILE * f = fopen(filename, "w");
if (f) {
int i;
double step = physicalRange / nbPts;
double physicalValue = 0.0;
char buff[50]= {0};
for (i=0; i<nbPts; ++i, physicalValue+=step) {
sprintf(buff, "%lf %lf\n", physicalValue, data[i]);
fputs(buff, f);
}
fclose(f);
rc = 0;
}
else {
fprintf(stderr, "Failed to open %s for writing\n", filename);
}
}
return rc;
} |
Partager