From 7e20e4eb199a82bec69917e066b5e39ef2888db1 Mon Sep 17 00:00:00 2001 From: Frederik Beimgraben Date: Sat, 11 Jan 2025 19:40:49 +0100 Subject: [PATCH] Add Ex. 2 and 3; FIXMEs remain open --- MMS24-25.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) diff --git a/MMS24-25.c b/MMS24-25.c index c924e69..c589041 100644 --- a/MMS24-25.c +++ b/MMS24-25.c @@ -331,3 +331,168 @@ void deleteSignal(MMSignal *pmmsIn) { // #endregion // #region Aufgabe 2 +double calculateArea(double *pdIn, int iLen) { + double area = 0; + + // #FIXME: Which one should we use according to the task? + for (int i = 0; i < iLen-1; i++) { + area += (pdIn[i] + pdIn[i+1]) / 2; + } + + return area; +} + +double calculateMean(double *pdIn, int iLen) { + double mean = 0; + + for (int i = 0; i < iLen; i++) { + mean += pdIn[i]; + } + + return mean / iLen; +} + +double calculateStddev(double *pdIn, int iLen) { + double mean = calculateMean(pdIn, iLen); + double stdDev = 0; + + for (int i = 0; i < iLen; i++) { + stdDev += pow(pdIn[i] - mean, 2); + } + + return sqrt(stdDev / iLen); +} + +Extrema *initExtrema(double *pdIn, int iLen) { + Extrema *extrema = (Extrema*) malloc(sizeof(Extrema)); + if (extrema == NULL) + _error("initExtrema: Failed to allocate memory for extrema\n"); + + extrema->pexLocalMax = (LocalExtrema*) malloc(sizeof(LocalExtrema)); + if (extrema->pexLocalMax == NULL) + _error("initExtrema: Failed to allocate memory for extrema->pexLocalMax\n"); + + extrema->pexLocalMin = (LocalExtrema*) malloc(sizeof(LocalExtrema)); + if (extrema->pexLocalMin == NULL) + _error("initExtrema: Failed to allocate memory for extrema->pexLocalMin\n"); + + extrema->pexLocalMax->piLocalExtremaPos = (int*) malloc(sizeof(int) *iLen); + if (extrema->pexLocalMax->piLocalExtremaPos == NULL) + _error("initExtrema: Failed to allocate memory for extrema->pexLocalMax->piLocalExtremaPos\n"); + + extrema->pexLocalMin->piLocalExtremaPos = (int*) malloc(sizeof(int) *iLen); + if (extrema->pexLocalMin->piLocalExtremaPos == NULL) + _error("initExtrema: Failed to allocate memory for extrema->pexLocalMin->piLocalExtremaPos\n"); + + extrema->iGlobalMaxPos = 0; + extrema->iGlobalMinPos = 0; + + return extrema; +} + +void deleteExtrema(Extrema *pexIn) { + free(pexIn->pexLocalMax->piLocalExtremaPos); + free(pexIn->pexLocalMax); + free(pexIn->pexLocalMin->piLocalExtremaPos); + free(pexIn->pexLocalMin); + free(pexIn); +} + +void initMMSignalFeatures(MMSignal *pmmsIn) { + pmmsIn->dArea = calculateArea(pmmsIn->pdValues, pmmsIn->iNumValues); + pmmsIn->dMean = calculateMean(pmmsIn->pdValues, pmmsIn->iNumValues); + pmmsIn->dStdDev = calculateStddev(pmmsIn->pdValues, pmmsIn->iNumValues); + pmmsIn->dMedian = calculateMedian(pmmsIn->pdValues, pmmsIn->iNumValues); + pmmsIn->pexExtrema = initExtrema(pmmsIn->pdValues, pmmsIn->iNumValues); +} + +Histogram *initHistogram(double *pdIn, int iLen, int iNumBins) { + Histogram *histogram = (Histogram*) malloc(sizeof(Histogram)); + if (histogram == NULL) + _error("initHistogram: Failed to allocate memory for histogram\n"); + + histogram->dIntervalMin = pdIn[0]; + histogram->dIntervalMax = pdIn[0]; + for (int i = 0; i < iLen; i++) { + if (pdIn[i] < histogram->dIntervalMin) + histogram->dIntervalMin = pdIn[i]; + if (pdIn[i] > histogram->dIntervalMax) + histogram->dIntervalMax = pdIn[i]; + } + + histogram->dBinWidth = (histogram->dIntervalMax - histogram->dIntervalMin) / iNumBins; + histogram->iNumBins = iNumBins; + + histogram->pdBinValues = (double*) malloc(sizeof(double) *iNumBins); + if (histogram->pdBinValues == NULL) + _error("initHistogram: Failed to allocate memory for histogram->pdBinValues\n"); + + for (int i = 0; i < iNumBins; i++) { + histogram->pdBinValues[i] = 0; + } + + return histogram; +} + +void deleteHistogram(Histogram *phIn) { + free(phIn->pdBinValues); + free(phIn); +} + +// #FIXME: Check if this checks out +double calculateEntropy(Histogram *phisIn) { + double entropy = 0; + + for (int i = 0; i < phisIn->iNumBins; i++) { + if (phisIn->pdBinValues[i] != 0) { + entropy += phisIn->pdBinValues[i] * log2(phisIn->pdBinValues[i]); + } + } + + return -entropy; +} + +// #endregion + +// #region Aufgabe 3 + +MMSignal *convoluteSignals(MMSignal *pmmsInA, MMSignal *pmmsInB) { + MMSignal *signal = createSignal(); + + if (pmmsInA->iNumValues != pmmsInB->iNumValues) + _error("convoluteSignals: Signals must have the same length\n"); + + signal->pdValues = (double*) malloc(sizeof(double) *pmmsInA->iNumValues); + if (signal->pdValues == NULL) + _error("convoluteSignals: Failed to allocate memory for signal->pdValues\n"); + + for (int i = 0; i < pmmsInA->iNumValues; i++) { + signal->pdValues[i] = pmmsInA->pdValues[i] * pmmsInB->pdValues[i]; + } + + signal->iNumValues = pmmsInA->iNumValues; + + return signal; +} + +// #FIXME: Check if this checks out +MMSignal *getPascalLine(int iLinenum) { + MMSignal *signal = createSignal(); + + signal->pdValues = (double*) malloc(sizeof(double) *iLinenum); + if (signal->pdValues == NULL) + _error("getPascalLine: Failed to allocate memory for signal->pdValues\n"); + + signal->pdValues[0] = 1; + signal->pdValues[iLinenum-1] = 1; + + for (int i = 1; i < iLinenum-1; i++) { + signal->pdValues[i] = signal->pdValues[i-1] *(iLinenum-i) / i; + } + + signal->iNumValues = iLinenum; + + return signal; +} + +// #endregion