diff --git a/MMS24-25.c b/MMS24-25.c index 1fae61b..d973c09 100644 --- a/MMS24-25.c +++ b/MMS24-25.c @@ -227,25 +227,30 @@ void rescaleDoubleArray( return; } - // First find the old minimum - double dOldMin = pdIn[0]; + // Rescale the array + for (int i = 0; i < iLen; i++) { + pdOut[i] = pdIn[i] * dFactor; + } + + // First find the minimum + double dOldMin = pdOut[0]; for (int i = 1; i < iLen; i++) { - if (pdIn[i] < dOldMin) - dOldMin = pdIn[i]; + if (pdOut[i] < dOldMin) + dOldMin = pdOut[i]; } // Calculate the offset double dOffset = dMin - dOldMin; + // Move the values to the new minimum + for (int i = 0; i < iLen; i++) { + pdOut[i] += dOffset; + } + #ifdef _DEBUG _debug("rescaleDoubleArray: dOldMin = %lf", dOldMin); _debug("rescaleDoubleArray: dOffset = %lf", dOffset); #endif - - // Rescale the array - for (int i = 0; i < iLen; i++) { - pdOut[i] = pdIn[i] * dFactor + dOffset; - } } /* @@ -280,12 +285,14 @@ double *generateSineValues( */ _error("*generateSineValues: iNumValues must be equal to or greater than 0!"); - if (iNumValues == 0) + if (iNumValues == 0) { /* * This would result in an empty array; Won't lead to a fatal error but * is still invalid; Warn the user. */ _warn("*generateSineValues: iNumValues is 0!"); + return NULL; + } // Initialize the output array double *sineArray = NULL; @@ -550,7 +557,6 @@ MMSignal *createSignalFromFile(char *pcInName) { MMSignal *signal = createSignal(); - signal->pdValues = NULL; signal->iNumValues = readDoubleArray(pcInName, &signal->pdValues); if (signal->iNumValues == 0) @@ -707,14 +713,15 @@ double calculateMedian(double *pdIn, int iLen) { * \frac{pdIn_{\frac{|pdIn|}{2}} + pdIn_{\frac{|pdIn|}{2}+1}}{2} & \text{if } |pdIn| \text{ is odd} * \end{cases} \] */ - double *pdInSorted = NULL; - pdInSorted = (double*) malloc(sizeof(double) *iLen); if (iLen == 0) { _warn("calculateMedian: iLen is 0"); return 0; } + double *pdInSorted = NULL; + pdInSorted = (double*) malloc(sizeof(double) *iLen); + if (pdInSorted == NULL) _mem_error("calculateMedian: Failed to allocate memory for pdInSorted"); @@ -1003,10 +1010,9 @@ MMSignal *convoluteSignals(MMSignal *pmmsInA, MMSignal *pmmsInB) { // Calculate output length int iOutLen = pmmsInA->iNumValues + pmmsInB->iNumValues - 1; - double *pdOut = NULL; // FIXME: This should be added in the final version - pdOut = (double*) malloc(sizeof(double) *iOutLen); - if (pdOut == NULL) - _mem_error("convoluteSignals: Failed to allocate memory for pdOut"); + // Create the output signal to write directly to the destination + MMSignal *signal = createSignalWithDefault(iOutLen, 0); + double *pdOut = signal->pdValues; for (int i = 0; i < iOutLen; i++) { pdOut[i] = 0; @@ -1018,7 +1024,7 @@ MMSignal *convoluteSignals(MMSignal *pmmsInA, MMSignal *pmmsInB) { } } - MMSignal *signal = createSignalFromDoubleArray(iOutLen, pdOut); + // Initialize the features of the signal initMMSignalFeatures(signal); return signal; @@ -1032,23 +1038,20 @@ MMSignal *getPascalLine(int iLinenum) { _error("getPascalLine: iLinenum must be greater than 0"); // Calculate a kernel signal using pascales triangle without using the factorial - double *pdValues = NULL; - pdValues = (double*) malloc(sizeof(double) * iLinenum); + MMSignal *signal = createSignalWithDefault(iLinenum, 1); + double *pdValues = signal->pdValues; + if (pdValues == NULL) _mem_error("getPascalLine: Failed to allocate memory for pdValues"); for (int i = 0; i < iLinenum; i++) { - pdValues[i] = 1; for (int j = i-1; j > 0; j--) { - pdValues[j] = pdValues[j] + pdValues[j-1]; + pdValues[j] += pdValues[j-1]; } } - MMSignal *signal = createSignalFromDoubleArray(iLinenum, pdValues); initMMSignalFeatures(signal); - free(pdValues); - return signal; } diff --git a/err.txt b/err.txt deleted file mode 100644 index 50696ef..0000000 --- a/err.txt +++ /dev/null @@ -1,4 +0,0 @@ -[ WARN ] 'Test!' -[ WARN ] 'Test!' -[ WARN ] 'Test!' -[ WARN ] 'Test!' diff --git a/result.txt b/result.txt deleted file mode 100644 index 0786535..0000000 --- a/result.txt +++ /dev/null @@ -1,4 +0,0 @@ -6.000000 -7.000000 -8.000000 -9.000000