135 lines
3.4 KiB
C
135 lines
3.4 KiB
C
/* =========================STYLE-GUIDELINES===========================
|
|
* All comments use MD-Syntax;
|
|
* Formulas are written in KaTeX/TeX Syntax.
|
|
* We adhere by the `kernel.org` style-guidelines.
|
|
* See: https://www.kernel.org/doc/html/v4.10/process/coding-style.html
|
|
* =============================AUTHORS================================
|
|
* Created by Frederik Beimgraben, Minh Dan Cam and Lea Hornberger in
|
|
* the winter term of 2024/25 at the faculty of computer science at
|
|
* Reutlingen University.
|
|
* After grading the full source code will be available at:
|
|
* https://git.beimgraben.net/MMS-2024-25/MMS-Loesung-2024-25.git
|
|
*/
|
|
|
|
#include "MMS24-25.h"
|
|
#include "math.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "MMS24-25.h"
|
|
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
|
|
// Define NULL if not already provided
|
|
#ifndef NULL
|
|
#define NULL 0
|
|
#endif
|
|
|
|
void _error(char* fmt,...) {
|
|
printf("<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'");
|
|
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
printf(fmt, args);
|
|
va_end(args);
|
|
|
|
printf("'\x1B[0m >\n");
|
|
|
|
exit(-1);
|
|
}
|
|
|
|
void _debug(char* fmt,...) {
|
|
printf("[\x1B[31;1m DEBUG \x1B[0m] \x1B[34m'");
|
|
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
printf(fmt, args);
|
|
va_end(args);
|
|
|
|
printf("'\x1B[0m\n");
|
|
|
|
exit(-1);
|
|
}
|
|
|
|
double interpolateLine(
|
|
double dX1,
|
|
double dY1,
|
|
double dX2,
|
|
double dY2,
|
|
double dXq
|
|
) {
|
|
if (dX2 == dX1)
|
|
_error("interpolateLine: dX1 and dX2 must differ!");
|
|
|
|
double y = dY1 * (
|
|
(dX2 - dXq) / (dX2 - dX1)
|
|
) + dY2 * (
|
|
(dXq - dX1) / (dX2 - dX1)
|
|
);
|
|
|
|
#ifdef _DEBUG_
|
|
// DEBUG: print the parameters and result
|
|
_debug("interpolateLine(%lf, %lf, %lf, %lf, %lf) -> %lf\n",
|
|
dX1,dY1,dX2,dY2,dXq,y);
|
|
#endif
|
|
|
|
return y;
|
|
}
|
|
|
|
void rescaleDoubleArray(
|
|
double *pdIn,
|
|
int iLen,
|
|
double dMin,
|
|
double dFactor,
|
|
double *pdOut
|
|
) {
|
|
/* Rescale an array of double values, so that the following rules apply:
|
|
* Given $ |pdIn| = |pdOut| = iLen $ it should be, that:
|
|
* $ min(pdIn) > dMin $, as well as
|
|
* \[ \forall i, j \in (0,\dots,iLen-1):
|
|
* (pdOut_i - pdOut_j) = (pdIn_i - pdIn_j) * dFactor \],
|
|
* which naturally results from:
|
|
* $ pdOut_i = pdIn_i * dFactor + dMin $
|
|
*/
|
|
for (int i = 0; i < iLen; i++) {
|
|
pdOut[i] = pdIn[i] * dFactor + dMin;
|
|
}
|
|
}
|
|
|
|
double *generateSineValues(
|
|
int iNumValues,
|
|
int iNumSamplesPerPeriod,
|
|
double dAmp
|
|
) {
|
|
/* Generate an array of `iNumValues` double value, so that the values
|
|
* represent the Y-coordinates of a sine curve of which one full period
|
|
* occupies `iNumSamplesPerPeriod` values
|
|
*/
|
|
|
|
// Initialize the output array
|
|
double* sineArray = NULL;
|
|
sineArray = (double*) malloc(sizeof(double) * iNumValues);
|
|
|
|
if (sineArray == NULL)
|
|
_error("*generateSineValues: Failed to allocate memory for Sine Array\n");
|
|
|
|
for (int i=0; i<iNumValues;i++){
|
|
sineArray[i] = dAmp*sin(((2*M_PI)/iNumSamplesPerPeriod) * i);
|
|
}
|
|
|
|
return sineArray;
|
|
}
|
|
|
|
void writeDoubleArray(double *pdOut, int iLen, char *pcOutName) {
|
|
/* Write a list of double values to a file
|
|
*/
|
|
|
|
FILE* datei = fopen(pcOutName, "w");
|
|
if (datei == NULL)
|
|
_error("writeDoubleArray: Error while opening file '%s'.\n", pcOutName);
|
|
|
|
for (int i = 0; i < iLen; i++) {
|
|
fprintf(datei, "%f\n", pdOut[i]);
|
|
}
|
|
|
|
fclose(datei);
|
|
} |