Add readDoubleArray

This commit is contained in:
Frederik Beimgraben 2025-01-11 19:19:41 +01:00
parent 840d5b0c4f
commit 500aba94d2
2 changed files with 85 additions and 36 deletions

View File

@ -19,12 +19,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
// Define NULL if not already provided void _error(char *fmt, ...) {
#ifndef NULL
#define NULL 0
#endif
void _error(char* fmt, ...) {
fprintf(stderr, "<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'"); fprintf(stderr, "<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'");
va_list args; va_list args;
@ -35,11 +30,11 @@ void _error(char* fmt, ...) {
fprintf(stderr, "'\x1B[0m >\n"); fprintf(stderr, "'\x1B[0m >\n");
#ifndef _TESTS_NONSTOP #ifndef _TESTS_NONSTOP
exit(-1); exit(EXIT_FAILURE);
#endif #endif
} }
void _debug(char* fmt, ...) { void _debug(char *fmt, ...) {
fprintf(stderr, "[\x1B[31;1m DEBUG \x1B[0m] \x1B[34m'"); fprintf(stderr, "[\x1B[31;1m DEBUG \x1B[0m] \x1B[34m'");
va_list args; va_list args;
@ -50,7 +45,7 @@ void _debug(char* fmt, ...) {
fprintf(stderr, "'\x1B[0m\n"); fprintf(stderr, "'\x1B[0m\n");
} }
void _warn(char* fmt, ...) { void _warn(char *fmt, ...) {
fprintf(stderr, "[\x1B[33;1m WARN \x1B[0m] \x1B[34m'"); fprintf(stderr, "[\x1B[33;1m WARN \x1B[0m] \x1B[34m'");
va_list args; va_list args;
@ -71,9 +66,9 @@ double interpolateLine(
if (dX2 == dX1) if (dX2 == dX1)
_error("interpolateLine: dX1 and dX2 must differ!"); _error("interpolateLine: dX1 and dX2 must differ!");
double y = dY1 * ( double y = dY1 *(
(dX2 - dXq) / (dX2 - dX1) (dX2 - dXq) / (dX2 - dX1)
) + dY2 * ( ) + dY2 *(
(dXq - dX1) / (dX2 - dX1) (dXq - dX1) / (dX2 - dX1)
); );
@ -97,12 +92,12 @@ void rescaleDoubleArray(
* Given $ |pdIn| = |pdOut| = iLen $ it should be, that: * Given $ |pdIn| = |pdOut| = iLen $ it should be, that:
* $ min(pdIn) > dMin $, as well as * $ min(pdIn) > dMin $, as well as
* \[ \forall i, j \in (0,\dots,iLen-1): * \[ \forall i, j \in (0,\dots,iLen-1):
* (pdOut_i - pdOut_j) = (pdIn_i - pdIn_j) * dFactor \], * (pdOut_i - pdOut_j) = (pdIn_i - pdIn_j) *dFactor \],
* which naturally results from: * which naturally results from:
* $ pdOut_i = pdIn_i * dFactor + dMin $ * $ pdOut_i = pdIn_i *dFactor + dMin $
*/ */
for (int i = 0; i < iLen; i++) { for (int i = 0; i < iLen; i++) {
pdOut[i] = pdIn[i] * dFactor + dMin; pdOut[i] = pdIn[i] *dFactor + dMin;
} }
} }
@ -130,14 +125,14 @@ double *generateSineValues(
// Initialize the output array // Initialize the output array
double* sineArray = NULL; double *sineArray = NULL;
sineArray = (double*) malloc(sizeof(double) * iNumValues); sineArray = (double*) malloc(sizeof(double) *iNumValues);
if (sineArray == NULL) if (sineArray == NULL)
_error("*generateSineValues: Failed to allocate memory for Sine Array\n"); _error("*generateSineValues: Failed to allocate memory for Sine Array\n");
for (int i=0; i<iNumValues;i++){ for (int i=0; i<iNumValues;i++){
sineArray[i] = dAmp*sin(((2*M_PI)/iNumSamplesPerPeriod) * i); sineArray[i] = dAmp*sin(((2*M_PI)/iNumSamplesPerPeriod) *i);
} }
return sineArray; return sineArray;
@ -145,13 +140,45 @@ double *generateSineValues(
void writeDoubleArray(double *pdOut, int iLen, char *pcOutName) { void writeDoubleArray(double *pdOut, int iLen, char *pcOutName) {
// Write a list of double values to a file // Write a list of double values to a file
FILE* datei = fopen(pcOutName, "w"); FILE *file = fopen(pcOutName, "w");
if (datei == NULL) if (file == NULL)
_error("writeDoubleArray: Error while opening file '%s'.\n", pcOutName); _error("writeDoubleArray: Error while opening file '%s'.\n", pcOutName);
for (int i = 0; i < iLen; i++) { for (int i = 0; i < iLen; i++) {
fprintf(datei, "%f\n", pdOut[i]); fprintf(file, "%f\n", pdOut[i]);
} }
fclose(datei); fclose(file);
} }
int readDoubleArray(char *pcInName, double **ppdIn) {
char *line = NULL;
size_t len = 0;
ssize_t read;
int i = 0;
*ppdIn = NULL;
FILE *file = fopen(pcInName, "r");
if (file == NULL)
_error("readDoubleArray: Error while opening file '%s'.\n", pcInName);
while ((read = getline(&line, &len, file)) != -1) {
if (*ppdIn == NULL) {
*ppdIn = (double*) malloc(sizeof(double));
} else {
*ppdIn = (double*) realloc(*ppdIn, sizeof(double) *(i+1));
}
if (*ppdIn == NULL)
_error("readDoubleArray: Failed to allocate or re-allocate memory for pdIn\n");
(*ppdIn)[i] = atof(line);
i++;
}
free(line);
fclose(file);
return i;
}

48
tests.c
View File

@ -70,7 +70,7 @@ void testInterpolation() {
success("testInterpolation();"); success("testInterpolation();");
} }
void testSineAndWriteDoubleArraySingle(int nValues, int dPLen, double dAmp) { void testSineAndReadWriteDoubleArraySingle(int nValues, int dPLen, double dAmp) {
info("Trying: %d, %d, %lf", nValues, dPLen, dAmp); info("Trying: %d, %d, %lf", nValues, dPLen, dAmp);
char* fileName = NULL; char* fileName = NULL;
@ -82,35 +82,57 @@ void testSineAndWriteDoubleArraySingle(int nValues, int dPLen, double dAmp) {
sprintf(fileName, "sine_%d_%d_%lf.results.txt", nValues, dPLen, dAmp); sprintf(fileName, "sine_%d_%d_%lf.results.txt", nValues, dPLen, dAmp);
double *pdSineValues = generateSineValues(
nValues,
dPLen,
dAmp
);
writeDoubleArray( writeDoubleArray(
generateSineValues( pdSineValues,
nValues,
dPLen,
dAmp
),
nValues, nValues,
fileName fileName
); );
success("Wrote %d values to %s", nValues, fileName);
double *pdReadValues = NULL;
int iReadValues = readDoubleArray(
fileName,
&pdReadValues
);
if (iReadValues != nValues) {
error("Failed to read the correct number of values: %d != %d", iReadValues, nValues);
}
success("Read %d values from %s", iReadValues, fileName);
for (int i=0; i<nValues; i++) {
if (round(pdSineValues[i]*1e6) != round(pdReadValues[i]*1e6)) {
error("Failed to read the correct value at index %d: %lf != %lf", i, pdSineValues[i], pdReadValues[i]);
}
}
info("use `plot \"%s\", \"%s\" with lines` to view.", fileName, fileName); info("use `plot \"%s\", \"%s\" with lines` to view.", fileName, fileName);
free(fileName); free(fileName);
} }
void testSineAndWriteDoubleArray() { void testSineAndReadWriteDoubleArray() {
info("testSineAndWriteDoubleArray"); info("testSineAndWriteDoubleArray");
testSineAndWriteDoubleArraySingle(1000, 300, 1); testSineAndReadWriteDoubleArraySingle(1000, 300, 1);
testSineAndWriteDoubleArraySingle(10, 10, 0.5); testSineAndReadWriteDoubleArraySingle(10, 10, 0.5);
testSineAndWriteDoubleArraySingle(10, -10, 0.5); testSineAndReadWriteDoubleArraySingle(10, -10, 0.5);
testSineAndWriteDoubleArraySingle(10, 10, 0); testSineAndReadWriteDoubleArraySingle(10, 10, 0);
success("testSineAndWriteDoubleArray"); success("testSineAndWriteDoubleArray");
} }
int main (int iArgC, char** ppcArgV){ int main (int iArgC, char** ppcArgV){
testInterpolation(); testInterpolation();
testSineAndWriteDoubleArray(); testSineAndReadWriteDoubleArray();
return 0; return 0;
} }