From e937a6dcb76961cda9fde04556fb82d722c42e6d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fran=C3=A7ois=20Grondin?= Date: Mon, 3 Jul 2017 15:22:31 -0400 Subject: [PATCH] Add files via upload --- src/utils/fft.c | 75 ++++++ src/utils/fft.h | 66 +++++ src/utils/fifo.c | 92 +++++++ src/utils/fifo.h | 34 +++ src/utils/gaussian.c | 558 +++++++++++++++++++++++++++++++++++++++++++ src/utils/gaussian.h | 275 +++++++++++++++++++++ src/utils/matrix.c | 308 ++++++++++++++++++++++++ src/utils/matrix.h | 96 ++++++++ src/utils/pcm.c | 123 ++++++++++ src/utils/pcm.h | 23 ++ src/utils/random.c | 186 +++++++++++++++ src/utils/random.h | 96 ++++++++ 12 files changed, 1932 insertions(+) create mode 100644 src/utils/fft.c create mode 100644 src/utils/fft.h create mode 100644 src/utils/fifo.c create mode 100644 src/utils/fifo.h create mode 100644 src/utils/gaussian.c create mode 100644 src/utils/gaussian.h create mode 100644 src/utils/matrix.c create mode 100644 src/utils/matrix.h create mode 100644 src/utils/pcm.c create mode 100644 src/utils/pcm.h create mode 100644 src/utils/random.c create mode 100644 src/utils/random.h diff --git a/src/utils/fft.c b/src/utils/fft.c new file mode 100644 index 0000000..65fbc4b --- /dev/null +++ b/src/utils/fft.c @@ -0,0 +1,75 @@ + + #include "fft.h" + + fft_obj * fft_construct(const unsigned int frameSize) { + + fft_obj * obj; + + obj = (fft_obj *) malloc(sizeof(fft_obj)); + + obj->frameSize = frameSize; + obj->halfFrameSize = frameSize/2+1; + obj->realArray = (float *) fftwf_malloc(sizeof(float) * obj->frameSize); + obj->complexArray = (fftwf_complex *) fftwf_malloc(sizeof(fftwf_complex) * obj->halfFrameSize); + + obj->R2C = fftwf_plan_dft_r2c_1d( obj->frameSize, obj->realArray, obj->complexArray, FFTW_ESTIMATE ); + obj->C2R = fftwf_plan_dft_c2r_1d( obj->frameSize, obj->complexArray, obj->realArray, FFTW_ESTIMATE ); + + return obj; + + } + + void fft_destroy(fft_obj * obj) { + + fftwf_destroy_plan( obj->R2C ); + fftwf_destroy_plan( obj->C2R ); + + fftwf_free(obj->realArray); + fftwf_free(obj->complexArray); + + free((void *) obj); + + } + + void fft_r2c(fft_obj * obj, const float * in, float * out) { + + unsigned int iSample; + + for (iSample = 0; iSample < obj->frameSize; iSample++) { + + obj->realArray[iSample] = in[iSample]; + + } + + fftwf_execute( obj->R2C ); + + for (iSample = 0; iSample < obj->halfFrameSize; iSample++) { + + out[iSample*2+0] = obj->complexArray[iSample][0]; + out[iSample*2+1] = obj->complexArray[iSample][1]; + + } + + + } + + void fft_c2r(fft_obj * obj, const float * in, float * out) { + + unsigned int iSample; + + for (iSample = 0; iSample < obj->halfFrameSize; iSample++) { + + obj->complexArray[iSample][0] = in[iSample*2+0]; + obj->complexArray[iSample][1] = in[iSample*2+1]; + + } + + fftwf_execute( obj->C2R ); + + for (iSample = 0; iSample < obj->frameSize; iSample++) { + + out[iSample] = obj->realArray[iSample] / ((float) obj->frameSize); + + } + + } \ No newline at end of file diff --git a/src/utils/fft.h b/src/utils/fft.h new file mode 100644 index 0000000..4246abf --- /dev/null +++ b/src/utils/fft.h @@ -0,0 +1,66 @@ +#ifndef __ODAS_UTILS_FFT +#define __ODAS_UTILS_FFT + + /** + * \file fft.h + * \author François Grondin + * \version 2.0 + * \date 2017-01-25 + * \copyright + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + + #include + #include + + //! FFT object. + typedef struct fft_obj { + + unsigned int frameSize; ///< Frame size (in samples). + unsigned int halfFrameSize; ///< Half frame size (frame size divided by 2 plus 1). + fftwf_plan R2C; ///< FFTW plan for FFT from real to complex. + fftwf_plan C2R; ///< FFTW plan for FFT from complex to real. + float * realArray; ///< Array that holds all real values. + fftwf_complex * complexArray; ///< Array that holds all complex values. + + } fft_obj; + + /** Constructor. Initialize all fields to perform FFT. + \param size FFT size (in samples). + \return Pointer to the instantiated object. + */ + fft_obj * fft_construct(const unsigned int size); + + /** Destructor. Free memory. + \param obj Object to be destroyed. + */ + void fft_destroy(fft_obj * obj); + + /** Perform Fast Fourier Transform (real vector as input, complex vector as output). + \param obj Object used. + \param in Pointer to the real array used for input. + \param out Pointer to the complex array used for output. + */ + void fft_r2c(fft_obj * obj, const float * in, float * out); + + /** Perform Inverse Fast Fourier Transform (complex vector as input, real vector as output). + \param obj Object used. + \param in Pointer to the complex array used for input. + \param out Pointer to the real array used for output. + */ + void fft_c2r(fft_obj * obj, const float * in, float * out); + +#endif \ No newline at end of file diff --git a/src/utils/fifo.c b/src/utils/fifo.c new file mode 100644 index 0000000..2fe8247 --- /dev/null +++ b/src/utils/fifo.c @@ -0,0 +1,92 @@ + + #include "fifo.h" + + fifo_obj * fifo_construct_zero(const unsigned int nMaxElements) { + + fifo_obj * obj; + + obj = (fifo_obj *) malloc(sizeof(fifo_obj)); + + obj->array = (void **) malloc(sizeof(void *) * nMaxElements); + memset(obj->array, 0x00, sizeof(void *) * nMaxElements); + + obj->nElements = 0; + obj->nMaxElements = nMaxElements; + + pthread_mutex_init(&(obj->use), NULL); + sem_init(&(obj->pop), 0, 0); + sem_init(&(obj->push), 0, nMaxElements); + + return obj; + + } + + void fifo_destroy(fifo_obj * obj) { + + sem_destroy(&(obj->pop)); + sem_destroy(&(obj->push)); + pthread_mutex_destroy(&(obj->use)); + free((void *) obj->array); + free((void *) obj); + + } + + void fifo_push(fifo_obj * obj, void * element) { + + sem_wait(&(obj->push)); + + pthread_mutex_lock(&(obj->use)); + + obj->array[obj->nElements] = element; + obj->nElements++; + + pthread_mutex_unlock(&(obj->use)); + + sem_post(&(obj->pop)); + + } + + void * fifo_pop(fifo_obj * obj) { + + void * rtnPtr; + + sem_wait(&(obj->pop)); + + pthread_mutex_lock(&(obj->use)); + + rtnPtr = obj->array[0]; + memmove(&(obj->array[0]), &(obj->array[1]), (obj->nElements-1) * sizeof(void *)); + obj->nElements--; + memset(&(obj->array[obj->nElements]), 0x00, (obj->nMaxElements-obj->nElements) * sizeof(void *)); + + pthread_mutex_unlock(&(obj->use)); + + sem_post(&(obj->push)); + + return rtnPtr; + + } + + int fifo_nElements(fifo_obj * obj) { + + int nElements; + + pthread_mutex_lock(&(obj->use)); + nElements = obj->nElements; + pthread_mutex_unlock(&(obj->use)); + + return nElements; + + } + + void fifo_printf(const fifo_obj * obj) { + + unsigned int iElement; + + for (iElement = 0; iElement < obj->nMaxElements; iElement++) { + + printf("(%03u): %p\n",iElement,obj->array[iElement]); + + } + + } \ No newline at end of file diff --git a/src/utils/fifo.h b/src/utils/fifo.h new file mode 100644 index 0000000..8e3e05c --- /dev/null +++ b/src/utils/fifo.h @@ -0,0 +1,34 @@ +#ifndef __ODAS_UTILS_FIFO +#define __ODAS_UTILS_FIFO + + #include + #include + #include + #include + #include + + typedef struct fifo_obj { + + void ** array; + unsigned int nElements; + unsigned int nMaxElements; + + pthread_mutex_t use; + sem_t push; + sem_t pop; + + } fifo_obj; + + fifo_obj * fifo_construct_zero(const unsigned int nMaxElements); + + void fifo_destroy(fifo_obj * obj); + + void fifo_push(fifo_obj * obj, void * element); + + void * fifo_pop(fifo_obj * obj); + + int fifo_nElements(fifo_obj * obj); + + void fifo_printf(const fifo_obj * obj); + +#endif \ No newline at end of file diff --git a/src/utils/gaussian.c b/src/utils/gaussian.c new file mode 100644 index 0000000..7440ee2 --- /dev/null +++ b/src/utils/gaussian.c @@ -0,0 +1,558 @@ + + #include "gaussian.h" + + gaussian_1d_obj * gaussian_1d_construct_zero(void) { + + gaussian_1d_obj * obj; + + obj = (gaussian_1d_obj *) malloc(sizeof(gaussian_1d_obj)); + + obj->weight = 1.0f; + obj->mu_x = 0.0f; + obj->sigma_x = 1.0f; + obj->scaleFactor = (obj->weight / (obj->sigma_x * powf(2*M_PI,(1.0f/2.0f)))); + + return obj; + + } + + gaussian_2d_obj * gaussian_2d_construct_zero(void) { + + gaussian_2d_obj * obj; + + obj = (gaussian_2d_obj *) malloc(sizeof(gaussian_2d_obj)); + + obj->weight = 1.0f; + obj->mu_x = 0.0f; + obj->mu_y = 0.0f; + obj->sigma_x = 1.0f; + obj->sigma_y = 1.0f; + obj->scaleFactor = (obj->weight / (obj->sigma_x*obj->sigma_y * powf(2*M_PI,(2.0f/2.0f)))); + + return obj; + + } + + gaussian_3d_obj * gaussian_3d_construct_zero(void) { + + gaussian_3d_obj * obj; + + obj = (gaussian_3d_obj *) malloc(sizeof(gaussian_3d_obj)); + + obj->weight = 1.0f; + obj->mu_x = 0.0f; + obj->mu_y = 0.0f; + obj->mu_z = 0.0f; + obj->sigma_x = 1.0f; + obj->sigma_y = 1.0f; + obj->sigma_z = 1.0f; + obj->scaleFactor = (obj->weight / (obj->sigma_x*obj->sigma_y*obj->sigma_z * powf(2*M_PI,(3.0f/2.0f)))); + + return obj; + + } + + gaussian_1d_obj * gaussian_1d_construct_weightmusigma(const float weight, const float mu_x, const float sigma_x) { + + gaussian_1d_obj * obj; + + obj = (gaussian_1d_obj *) malloc(sizeof(gaussian_1d_obj)); + + obj->weight = weight; + obj->mu_x = mu_x; + obj->sigma_x = sigma_x; + obj->scaleFactor = (obj->weight / (obj->sigma_x * powf(2*M_PI,(1.0f/2.0f)))); + + return obj; + + } + + gaussian_2d_obj * gaussian_2d_construct_weigthmusigma(const float weight, const float mu_x, const float mu_y, const float sigma_x, const float sigma_y) { + + gaussian_2d_obj * obj; + + obj = (gaussian_2d_obj *) malloc(sizeof(gaussian_2d_obj)); + + obj->weight = weight; + obj->mu_x = mu_x; + obj->mu_y = mu_y; + obj->sigma_x = sigma_x; + obj->sigma_y = sigma_y; + obj->scaleFactor = (obj->weight / (obj->sigma_x*obj->sigma_y * powf(2*M_PI,(2.0f/2.0f)))); + + return obj; + + } + + gaussian_3d_obj * gaussian_3d_construct_weightmusigma(const float weight, const float mu_x, const float mu_y, const float mu_z, const float sigma_x, const float sigma_y, const float sigma_z) { + + gaussian_3d_obj * obj; + + obj = (gaussian_3d_obj *) malloc(sizeof(gaussian_3d_obj)); + + obj->weight = weight; + obj->mu_x = mu_x; + obj->mu_y = mu_y; + obj->mu_z = mu_z; + obj->sigma_x = sigma_x; + obj->sigma_y = sigma_y; + obj->sigma_z = sigma_z; + obj->scaleFactor = (obj->weight / (obj->sigma_x*obj->sigma_y*obj->sigma_z * powf(2*M_PI,(3.0f/2.0f)))); + + return obj; + + } + + gaussian_1d_obj * gaussian_1d_construct_gaussian(const gaussian_1d_obj * gaussian) { + + gaussian_1d_obj * obj; + + obj = (gaussian_1d_obj *) malloc(sizeof(gaussian_1d_obj)); + + obj->weight = gaussian->weight; + obj->mu_x = gaussian->mu_x; + obj->sigma_x = gaussian->sigma_x; + obj->scaleFactor = (obj->weight / (obj->sigma_x * powf(2*M_PI,(1.0f/2.0f)))); + + return obj; + + } + + gaussian_2d_obj * gaussian_2d_construct_gaussian(const gaussian_2d_obj * gaussian) { + + gaussian_2d_obj * obj; + + obj = (gaussian_2d_obj *) malloc(sizeof(gaussian_2d_obj)); + + obj->weight = gaussian->weight; + obj->mu_x = gaussian->mu_x; + obj->mu_y = gaussian->mu_y; + obj->sigma_x = gaussian->sigma_x; + obj->sigma_y = gaussian->sigma_y; + obj->scaleFactor = (obj->weight / (obj->sigma_x*obj->sigma_y * powf(2*M_PI,(2.0f/2.0f)))); + + return obj; + + } + + gaussian_3d_obj * gaussian_3d_construct_gaussian(const gaussian_3d_obj * gaussian) { + + gaussian_3d_obj * obj; + + obj = (gaussian_3d_obj *) malloc(sizeof(gaussian_3d_obj)); + + obj->weight = gaussian->weight; + obj->mu_x = gaussian->mu_x; + obj->mu_y = gaussian->mu_y; + obj->mu_z = gaussian->mu_z; + obj->sigma_x = gaussian->sigma_x; + obj->sigma_y = gaussian->sigma_y; + obj->sigma_z = gaussian->sigma_z; + obj->scaleFactor = (obj->weight / (obj->sigma_x*obj->sigma_y*obj->sigma_z * powf(2*M_PI,(3.0f/2.0f)))); + + return obj; + + } + + void gaussian_1d_destroy(gaussian_1d_obj * obj) { + + free((void *) obj); + + } + + void gaussian_2d_destroy(gaussian_2d_obj * obj) { + + free((void *) obj); + + } + + void gaussian_3d_destroy(gaussian_3d_obj * obj) { + + free((void *) obj); + + } + + float gaussian_1d_eval(const gaussian_1d_obj * obj, const float x) { + + float value; + float diff_x; + float diff2_x; + float sigma_x; + float sigma2_x; + float expr; + + diff_x = x - obj->mu_x; + diff2_x = diff_x * diff_x; + + sigma_x = obj->sigma_x; + sigma2_x = sigma_x * sigma_x; + + expr = -0.5f * (diff2_x/sigma2_x); + + value = obj->scaleFactor * expf(expr); + + return value; + + } + + float gaussian_2d_eval(const gaussian_2d_obj * obj, const float x, const float y) { + + float value; + float diff_x, diff_y; + float diff2_x, diff2_y; + float sigma_x, sigma_y; + float sigma2_x, sigma2_y; + float expr; + + diff_x = x - obj->mu_x; + diff_y = y - obj->mu_y; + diff2_x = diff_x * diff_x; + diff2_y = diff_y * diff_y; + sigma_x = obj->sigma_x; + sigma_y = obj->sigma_y; + sigma2_x = sigma_x * sigma_x; + sigma2_y = sigma_y * sigma_y; + + expr = -0.5f * ((diff2_x/sigma2_x)+(diff2_y/sigma2_y)); + + value = obj->scaleFactor * expf(expr); + + return value; + + } + + float gaussian_3d_eval(const gaussian_3d_obj * obj, const float x, const float y, const float z) { + + float value; + float diff_x, diff_y, diff_z; + float diff2_x, diff2_y, diff2_z; + float sigma_x, sigma_y, sigma_z; + float sigma2_x, sigma2_y, sigma2_z; + float expr; + + diff_x = x - obj->mu_x; + diff_y = y - obj->mu_y; + diff_z = z - obj->mu_z; + diff2_x = diff_x * diff_x; + diff2_y = diff_y * diff_y; + diff2_z = diff_z * diff_z; + sigma_x = obj->sigma_x; + sigma_y = obj->sigma_y; + sigma_z = obj->sigma_z; + sigma2_x = sigma_x * sigma_x; + sigma2_y = sigma_y * sigma_y; + sigma2_z = sigma_z * sigma_z; + + expr = -0.5f * ((diff2_x/sigma2_x)+(diff2_y/sigma2_y)+(diff2_z/sigma2_z)); + + value = obj->scaleFactor * expf(expr); + + return value; + + } + + void gaussian_1d_printf(const gaussian_1d_obj * obj) { + + printf("weight = %+1.3f, mu = %+1.3f, sigma = %+1.3f\n",obj->weight, obj->mu_x,obj->sigma_x); + + } + + void gaussian_2d_printf(const gaussian_2d_obj * obj) { + + printf("weight = %+1.3f, mu = (%+1.3f,%+1.3f), sigma = (%+1.3f,%+1.3f)\n",obj->weight, obj->mu_x,obj->mu_y,obj->sigma_x,obj->sigma_y); + + } + + void gaussian_3d_printf(const gaussian_3d_obj * obj) { + + printf("weight = %+1.3f, mu = (%+1.3f,%+1.3f,%+1.3f), sigma = (%+1.3f,%+1.3f,%+1.3f)\n",obj->weight, obj->mu_x,obj->mu_y,obj->mu_z,obj->sigma_x,obj->sigma_y,obj->sigma_z); + + } + + gaussians_1d_obj * gaussians_1d_construct_null(const unsigned int nSignals) { + + gaussians_1d_obj * obj; + unsigned int iSignal; + + obj = (gaussians_1d_obj *) malloc(sizeof(gaussians_1d_obj)); + + obj->nSignals = nSignals; + obj->array = (gaussian_1d_obj **) malloc(sizeof(gaussian_1d_obj *) * nSignals); + + for (iSignal = 0; iSignal < nSignals; iSignal++) { + obj->array[iSignal] = NULL; + } + + return obj; + + } + + gaussians_2d_obj * gaussians_2d_construct_null(const unsigned int nSignals) { + + gaussians_2d_obj * obj; + unsigned int iSignal; + + obj = (gaussians_2d_obj *) malloc(sizeof(gaussians_2d_obj)); + + obj->nSignals = nSignals; + obj->array = (gaussian_2d_obj **) malloc(sizeof(gaussian_2d_obj *) * nSignals); + + for (iSignal = 0; iSignal < nSignals; iSignal++) { + obj->array[iSignal] = NULL; + } + + return obj; + + } + + gaussians_3d_obj * gaussians_3d_construct_null(const unsigned int nSignals) { + + gaussians_3d_obj * obj; + unsigned int iSignal; + + obj = (gaussians_3d_obj *) malloc(sizeof(gaussians_3d_obj)); + + obj->nSignals = nSignals; + obj->array = (gaussian_3d_obj **) malloc(sizeof(gaussian_3d_obj *) * nSignals); + + for (iSignal = 0; iSignal < nSignals; iSignal++) { + obj->array[iSignal] = NULL; + } + + return obj; + + } + + gaussians_1d_obj * gaussians_1d_construct_gaussians(const gaussians_1d_obj * gaussians) { + + gaussians_1d_obj * obj; + unsigned int iSignal; + + obj = (gaussians_1d_obj *) malloc(sizeof(gaussians_1d_obj)); + + obj->nSignals = gaussians->nSignals; + obj->array = (gaussian_1d_obj **) malloc(sizeof(gaussian_1d_obj *) * gaussians->nSignals); + + for (iSignal = 0; iSignal < gaussians->nSignals; iSignal++) { + obj->array[iSignal] = gaussian_1d_construct_gaussian(gaussians->array[iSignal]); + } + + return obj; + + } + + gaussians_2d_obj * gaussians_2d_construct_gaussians(const gaussians_2d_obj * gaussians) { + + gaussians_2d_obj * obj; + unsigned int iSignal; + + obj = (gaussians_2d_obj *) malloc(sizeof(gaussians_2d_obj)); + + obj->nSignals = gaussians->nSignals; + obj->array = (gaussian_2d_obj **) malloc(sizeof(gaussian_2d_obj *) * gaussians->nSignals); + + for (iSignal = 0; iSignal < gaussians->nSignals; iSignal++) { + obj->array[iSignal] = gaussian_2d_construct_gaussian(gaussians->array[iSignal]); + } + + return obj; + + } + + gaussians_3d_obj * gaussians_3d_construct_gaussians(const gaussians_3d_obj * gaussians) { + + gaussians_3d_obj * obj; + unsigned int iSignal; + + obj = (gaussians_3d_obj *) malloc(sizeof(gaussians_3d_obj)); + + obj->nSignals = gaussians->nSignals; + obj->array = (gaussian_3d_obj **) malloc(sizeof(gaussian_3d_obj *) * gaussians->nSignals); + + for (iSignal = 0; iSignal < gaussians->nSignals; iSignal++) { + obj->array[iSignal] = gaussian_3d_construct_gaussian(gaussians->array[iSignal]); + } + + return obj; + + } + + void gaussians_1d_destroy(gaussians_1d_obj * obj) { + + unsigned int iSignal; + + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + + if (obj->array[iSignal] != NULL) { + gaussian_1d_destroy(obj->array[iSignal]); + } + + } + + free((void *) obj->array); + + free((void *) obj); + + } + + void gaussians_2d_destroy(gaussians_2d_obj * obj) { + + unsigned int iSignal; + + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + + if (obj->array[iSignal] != NULL) { + gaussian_2d_destroy(obj->array[iSignal]); + } + + } + + free((void *) obj->array); + + free((void *) obj); + + } + + void gaussians_3d_destroy(gaussians_3d_obj * obj) { + + unsigned int iSignal; + + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + + if (obj->array[iSignal] != NULL) { + gaussian_3d_destroy(obj->array[iSignal]); + } + + } + + free((void *) obj->array); + + free((void *) obj); + + } + + float gaussians_1d_eval(const gaussians_1d_obj * obj, const float x) { + + unsigned int iSignal; + float value; + + value = 0.0f; + + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + + value += gaussian_1d_eval(obj->array[iSignal], x); + + } + + return value; + + } + + float gaussians_2d_eval(const gaussians_2d_obj * obj, const float x, const float y) { + + unsigned int iSignal; + float value; + + value = 0.0f; + + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + + value += gaussian_2d_eval(obj->array[iSignal], x, y); + + } + + return value; + + } + + float gaussians_3d_eval(const gaussians_3d_obj * obj, const float x, const float y, const float z) { + + unsigned int iSignal; + float value; + + value = 0.0f; + + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + + value += gaussian_3d_eval(obj->array[iSignal], x, y, z); + + } + + return value; + + } + + void gaussians_1d_printf(const gaussians_1d_obj * obj) { + + unsigned int iSignal; + + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + + printf("(%03u): ",iSignal); + + if (obj->array[iSignal] != NULL) { + + printf("weight = %+1.3f, mu = %+1.3f, sigma = %+1.3f",obj->array[iSignal]->weight, obj->array[iSignal]->mu_x,obj->array[iSignal]->sigma_x); + + } + else { + + printf("(null)"); + + } + + printf("\n"); + + } + + } + + void gaussians_2d_printf(const gaussians_2d_obj * obj) { + + unsigned int iSignal; + + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + + printf("(%03u): ",iSignal); + + if (obj->array[iSignal] != NULL) { + + printf("weight = %+1.3f, mu = (%+1.3f,%+1.3f), sigma = (%+1.3f,%+1.3f)",obj->array[iSignal]->weight, obj->array[iSignal]->mu_x,obj->array[iSignal]->mu_y,obj->array[iSignal]->sigma_x,obj->array[iSignal]->sigma_y); + + } + else { + + printf("(null)"); + + } + + printf("\n"); + + } + + } + + void gaussians_3d_printf(const gaussians_3d_obj * obj) { + + unsigned int iSignal; + + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + + printf("(%03u): ",iSignal); + + if (obj->array[iSignal] != NULL) { + + printf("weight = %+1.3f, mu = (%+1.3f,%+1.3f,%+1.3f), sigma = (%+1.3f,%+1.3f,%+1.3f)",obj->array[iSignal]->weight, obj->array[iSignal]->mu_x,obj->array[iSignal]->mu_y,obj->array[iSignal]->mu_z,obj->array[iSignal]->sigma_x,obj->array[iSignal]->sigma_y,obj->array[iSignal]->sigma_z); + + } + else { + + printf("(null)"); + + } + + printf("\n"); + + } + + } \ No newline at end of file diff --git a/src/utils/gaussian.h b/src/utils/gaussian.h new file mode 100644 index 0000000..a026b8d --- /dev/null +++ b/src/utils/gaussian.h @@ -0,0 +1,275 @@ +#ifndef __ODAS_SIGNAL_GAUSSIAN +#define __ODAS_SIGNAL_GAUSSIAN + + #include + #include + #include + + //! 1-D gaussian + typedef struct gaussian_1d_obj { + + float weight; ///< Weight of the gaussian. + float mu_x; ///< Mean of the gaussian. + float sigma_x; ///< Standard deviation of the gaussian. + float scaleFactor; ///< Precomputed scale factor for the exponential function. + + } gaussian_1d_obj; + + //! 2-D gaussian + typedef struct gaussian_2d_obj { + + float weight; ///< Weight of the gaussian. + float mu_x; ///< X-component of the mean of the gaussian. + float mu_y; ///< Y-component of the mean of the gaussian. + float sigma_x; ///< X-component of the standard deviation of the gaussian. + float sigma_y; ///< Y-component of the standard deviation of the gaussian. + float scaleFactor; ///< Precomputed scale factor for the exponential function. + + } gaussian_2d_obj; + + //! 3-D gaussian + typedef struct gaussian_3d_obj { + + float weight; ///< Weight of the gaussian. + float mu_x; ///< X-component of the mean of the gaussian. + float mu_y; ///< Y-component of the mean of the gaussian. + float mu_z; ///< Z-component of the mean of the gaussian. + float sigma_x; ///< X-component of the standard deviation of the gaussian. + float sigma_y; ///< Y-component of the standard deviation of the gaussian. + float sigma_z; ///< Z-component of the standard deviation of the gaussian. + float scaleFactor; ///< Precomputed scale factor for the exponential function. + + } gaussian_3d_obj; + + //! 1-D gaussian mixture model + typedef struct gaussians_1d_obj { + + unsigned int nSignals; ///< Number of gaussians. + gaussian_1d_obj ** array; ///< List of gaussians. + + } gaussians_1d_obj; + + //! 2-D gaussian mixture model + typedef struct gaussians_2d_obj { + + unsigned int nSignals; ///< Number of gaussians. + gaussian_2d_obj ** array; ///< List of gaussians. + + } gaussians_2d_obj; + + //! 3-D gaussian mixture model + typedef struct gaussians_3d_obj { + + unsigned int nSignals; ///< Number of gaussians. + gaussian_3d_obj ** array; ///< List of gaussians. + + } gaussians_3d_obj; + + /** Constructor. Initialize all fields to zero (weight to 1, mean to 0, standard deviation to 1). + \return Pointer to the instantiated object. + */ + gaussian_1d_obj * gaussian_1d_construct_zero(void); + + /** Constructor. Initialize all fields to zero (weight to 1, means to 0, standard deviations to 1). + \return Pointer to the instantiated object. + */ + gaussian_2d_obj * gaussian_2d_construct_zero(void); + + /** Constructor. Initialize all fields to zero (weight to 1, means to 0, standard deviations to 1). + \return Pointer to the instantiated object. + */ + gaussian_3d_obj * gaussian_3d_construct_zero(void); + + /** Constructor. Initialize all fields to weight, mu and sigma. + \param weight Weight value. + \param mu_x Mean value. + \param sigma_x Standard deviation value. + \return Pointer to the instantiated object. + */ + gaussian_1d_obj * gaussian_1d_construct_weightmusigma(const float weight, const float mu_x, const float sigma_x); + + /** Constructor. Initialize all fields to weight, mus and sigmas. + \param weight Weight value. + \param mu_x X-coordinate of the mean. + \param mu_y Y-coordinate of the mean. + \param sigma_x X-coordinate of the standard deviation. + \param sigma_y Y-coordinate of the standard deviation. + \return Pointer to the instantiated object. + */ + gaussian_2d_obj * gaussian_2d_construct_weightmusigma(const float weight, const float mu_x, const float mu_y, const float sigma_x, const float sigma_y); + + /** Constructor. Initialize all fields to weight, mus and sigmas. + \param weight Weight value. + \param mu_x X-coordinate of the mean. + \param mu_y Y-coordinate of the mean. + \param mu_z Z-coordinate of the mean. + \param sigma_x X-coordinate of the standard deviation. + \param sigma_y Y-coordinate of the standard deviation. + \param sigma_z Z-coordinate of the standard deviation. + \return Pointer to the instantiated object. + */ + gaussian_3d_obj * gaussian_3d_construct_weightmusigma(const float weight, const float mu_x, const float mu_y, const float mu_z, const float sigma_x, const float sigma_y, const float sigma_z); + + /** Constructor. Clone the object. + \param gaussian Object to cloned. + \return Pointer to the instantiated object. + */ + gaussian_1d_obj * gaussian_1d_construct_gaussian(const gaussian_1d_obj * gaussian); + + /** Constructor. Clone the object. + \param gaussian Object to cloned. + \return Pointer to the instantiated object. + */ + gaussian_2d_obj * gaussian_2d_construct_gaussian(const gaussian_2d_obj * gaussian); + + /** Constructor. Clone the object. + \param gaussian Object to cloned. + \return Pointer to the instantiated object. + */ + gaussian_3d_obj * gaussian_3d_construct_gaussian(const gaussian_3d_obj * gaussian); + + /** Destructor. Free memory. + \param obj Object to be destroyed. + */ + void gaussian_1d_destroy(gaussian_1d_obj * obj); + + /** Destructor. Free memory. + \param obj Object to be destroyed. + */ + void gaussian_2d_destroy(gaussian_2d_obj * obj); + + /** Destructor. Free memory. + \param obj Object to be destroyed. + */ + void gaussian_3d_destroy(gaussian_3d_obj * obj); + + /** Evaluate the gaussian at x. + \param obj Object to be used. + \param x Value of x. + \return Value of the gaussian. + */ + float gaussian_1d_eval(const gaussian_1d_obj * obj, const float x); + + /** Evaluate the gaussian at x and y. + \param obj Object to be used. + \param x Value of x. + \param y Value of y. + \return Value of the gaussian. + */ + float gaussian_2d_eval(const gaussian_2d_obj * obj, const float x, const float y); + + /** Evaluate the gaussian at x, y and z. + \param obj Object to be used. + \param x Value of x. + \param y Value of y. + \param z Value of z. + \return Value of the gaussian. + */ + float gaussian_3d_eval(const gaussian_3d_obj * obj, const float x, const float y, const float z); + + /** Print the content of the gaussian. + \param obj Object to be used. + */ + void gaussian_1d_printf(const gaussian_1d_obj * obj); + + /** Print the content of the gaussian. + \param obj Object to be used. + */ + void gaussian_2d_printf(const gaussian_2d_obj * obj); + + /** Print the content of the gaussian. + \param obj Object to be used. + */ + void gaussian_3d_printf(const gaussian_3d_obj * obj); + + /** Constructor. Fill the array of objects with null pointers. + \param nSignals Number of gaussians. + \return Pointer to the instantiated object. + */ + gaussians_1d_obj * gaussians_1d_construct_null(const unsigned int nSignals); + + /** Constructor. Fill the array of objects with null pointers. + \param nSignals Number of gaussians. + \return Pointer to the instantiated object. + */ + gaussians_2d_obj * gaussians_2d_construct_null(const unsigned int nSignals); + + /** Constructor. Fill the array of objects with null pointers. + \param nSignals Number of gaussians. + \return Pointer to the instantiated object. + */ + gaussians_3d_obj * gaussians_3d_construct_null(const unsigned int nSignals); + + /** Constructor. Clone the gaussians. + \param gaussians Object to be cloned. + \return Pointer to the instantiated object. + */ + gaussians_1d_obj * gaussians_1d_construct_gaussians(const gaussians_1d_obj * gaussians); + + /** Constructor. Clone the gaussians. + \param gaussians Object to be cloned. + \return Pointer to the instantiated object. + */ + gaussians_2d_obj * gaussians_2d_construct_gaussians(const gaussians_2d_obj * gaussians); + + /** Constructor. Clone the gaussians. + \param gaussians Object to be cloned. + \return Pointer to the instantiated object. + */ + gaussians_3d_obj * gaussians_3d_construct_gaussians(const gaussians_3d_obj * gaussians); + + /** Destructor. Free memory. + \param obj Object to be destroyed. + */ + void gaussians_1d_destroy(gaussians_1d_obj * obj); + + /** Destructor. Free memory. + \param obj Object to be destroyed. + */ + void gaussians_2d_destroy(gaussians_2d_obj * obj); + + /** Destructor. Free memory. + \param obj Object to be destroyed. + */ + void gaussians_3d_destroy(gaussians_3d_obj * obj); + + /** Evaluate the gaussian mixture model (GMM) at x. + \param obj Object to be used. + \param x Value of x. + \return Value of the GMM. + */ + float gaussians_1d_eval(const gaussians_1d_obj * obj, const float x); + + /** Evaluate the gaussian mixture model (GMM) at x and y. + \param obj Object to be used. + \param x Value of x. + \param y Value of y. + \return Value of the GMM. + */ + float gaussians_2d_eval(const gaussians_2d_obj * obj, const float x, const float y); + + /** Evaluate the gaussian mixture model (GMM) at x, y and z. + \param obj Object to be used. + \param x Value of x. + \param y Value of y. + \param z Value of z. + \return Value of the GMM. + */ + float gaussians_3d_eval(const gaussians_3d_obj * obj, const float x, const float y, const float z); + + /** Print the content of the gaussian mixture model (GMM). + \param obj Object to be used. + */ + void gaussians_1d_printf(const gaussians_1d_obj * obj); + + /** Print the content of the gaussian mixture model (GMM). + \param obj Object to be used. + */ + void gaussians_2d_printf(const gaussians_2d_obj * obj); + + /** Print the content of the gaussian mixture model (GMM). + \param obj Object to be used. + */ + void gaussians_3d_printf(const gaussians_3d_obj * obj); + +#endif \ No newline at end of file diff --git a/src/utils/matrix.c b/src/utils/matrix.c new file mode 100644 index 0000000..33c102c --- /dev/null +++ b/src/utils/matrix.c @@ -0,0 +1,308 @@ + + #include "matrix.h" + + matrix_obj * matrix_construct_zero(const unsigned int nRows, const unsigned int nCols) { + + matrix_obj * obj; + unsigned int iRow; + unsigned int iCol; + + obj = (matrix_obj *) malloc(sizeof(matrix_obj)); + + obj->nRows = nRows; + obj->nCols = nCols; + obj->array = (float **) malloc(sizeof(float *) * nRows); + + for (iRow = 0; iRow < nRows; iRow++) { + + obj->array[iRow] = (float *) malloc(sizeof(float) * nCols); + + for (iCol = 0; iCol < nCols; iCol++) { + + obj->array[iRow][iCol] = 0.0f; + + } + + } + + return obj; + + } + + matrix_obj * matrix_construct_matrix(const matrix_obj * matrix) { + + matrix_obj * obj; + + unsigned int iRow; + unsigned int iCol; + + obj = (matrix_obj *) malloc(sizeof(matrix_obj)); + + obj->nRows = matrix->nRows; + obj->nCols = matrix->nCols; + obj->array = (float **) malloc(sizeof(float *) * matrix->nRows); + + for (iRow = 0; iRow < matrix->nRows; iRow++) { + + obj->array[iRow] = (float *) malloc(sizeof(float) * matrix->nCols); + + for (iCol = 0; iCol < matrix->nCols; iCol++) { + + obj->array[iRow][iCol] = matrix->array[iRow][iCol]; + + } + + } + + return obj; + + } + + void matrix_copy_zero(matrix_obj * obj) { + + unsigned int iRow; + unsigned int iCol; + + for (iRow = 0; iRow < obj->nRows; iRow++) { + + for (iCol = 0; iCol < obj->nCols; iCol++) { + + obj->array[iRow][iCol] = 0.0f; + + } + + } + + } + + void matrix_copy_matrix(matrix_obj * obj, const matrix_obj * matrix) { + + unsigned int iRow; + unsigned int iCol; + + for (iRow = 0; iRow < matrix->nRows; iRow++) { + + obj->array[iRow] = (float *) malloc(sizeof(float) * matrix->nCols); + + for (iCol = 0; iCol < matrix->nCols; iCol++) { + + obj->array[iRow][iCol] = matrix->array[iRow][iCol]; + + } + + } + + } + + void matrix_destroy(matrix_obj * obj) { + + unsigned int iRow; + + for (iRow = 0; iRow < obj->nRows; iRow++) { + + free((void *) obj->array[iRow]); + + } + + free((void *) obj->array); + free((void *) obj); + + } + + void matrix_transpose(matrix_obj * obj, const matrix_obj * src) { + + unsigned int iRow; + unsigned int iCol; + + for (iRow = 0; iRow < obj->nRows; iRow++) { + + for (iCol = 0; iCol < obj->nCols; iCol++) { + + obj->array[iRow][iCol] = src->array[iCol][iRow]; + + } + + } + + } + + void matrix_add(matrix_obj * obj, const matrix_obj * src1, const matrix_obj * src2) { + + unsigned int iRow; + unsigned int iCol; + + for (iRow = 0; iRow < obj->nRows; iRow++) { + + for (iCol = 0; iCol < obj->nCols; iCol++) { + + obj->array[iRow][iCol] = src1->array[iRow][iCol] + src2->array[iRow][iCol]; + + } + + } + + } + + void matrix_sub(matrix_obj * obj, const matrix_obj * src1, const matrix_obj * src2) { + + unsigned int iRow; + unsigned int iCol; + + for (iRow = 0; iRow < obj->nRows; iRow++) { + + for (iCol = 0; iCol < obj->nCols; iCol++) { + + obj->array[iRow][iCol] = src1->array[iRow][iCol] - src2->array[iRow][iCol]; + + } + + } + + } + + void matrix_mul(matrix_obj * obj, const matrix_obj * src1, const matrix_obj * src2) { + + unsigned int iRow; + unsigned int iCol; + unsigned int iElement; + + for (iRow = 0; iRow < src1->nRows; iRow++) { + + for (iCol = 0; iCol < src2->nCols; iCol++) { + + obj->array[iRow][iCol] = 0.0f; + + for (iElement = 0; iElement < src1->nCols; iElement++) { + + obj->array[iRow][iCol] += (src1->array[iRow][iElement] * src2->array[iElement][iCol]); + + } + + } + + } + + } + + void matrix_inv(matrix_obj * obj, const matrix_obj * src) { + + float a00, a01, a02, a10, a11, a12, a20, a21, a22; + float det; + + if ((src->nRows == 2) && (src->nCols == 2)) { + + a00 = src->array[0][0]; + a01 = src->array[0][1]; + a10 = src->array[1][0]; + a11 = src->array[1][1]; + + det = a00*a11 - a01*a10; + + obj->array[0][0] = +a11 / det; + obj->array[0][1] = -a01 / det; + obj->array[1][0] = -a10 / det; + obj->array[1][1] = +a00 / det; + + } + + if ((src->nRows == 3) && (src->nCols == 3)) { + + a00 = src->array[0][0]; + a01 = src->array[0][1]; + a02 = src->array[0][2]; + a10 = src->array[1][0]; + a11 = src->array[1][1]; + a12 = src->array[1][2]; + a20 = src->array[2][0]; + a21 = src->array[2][1]; + a22 = src->array[2][2]; + + det = a00*a11*a22 - a00*a12*a21 - a01*a10*a22 + a01*a12*a20 + a02*a10*a21 - a02*a11*a20; + + obj->array[0][0] = +(a11*a22 - a12*a21) / det; + obj->array[0][1] = -(a01*a22 - a02*a21) / det; + obj->array[0][2] = +(a01*a12 - a02*a11) / det; + obj->array[1][0] = -(a10*a22 - a12*a20) / det; + obj->array[1][1] = +(a00*a22 - a02*a20) / det; + obj->array[1][2] = -(a00*a12 - a02*a10) / det; + obj->array[2][0] = +(a10*a21 - a11*a20) / det; + obj->array[2][1] = -(a00*a21 - a01*a20) / det; + obj->array[2][2] = +(a00*a11 - a01*a10) / det; + + } + + } + + float matrix_det(const matrix_obj * obj) { + + float a00, a01, a02, a10, a11, a12, a20, a21, a22; + float det; + + det = 0.0f; + + if ((obj->nRows == 3) && (obj->nCols == 3)) { + + a00 = obj->array[0][0]; + a01 = obj->array[0][1]; + a10 = obj->array[1][0]; + a11 = obj->array[1][1]; + + det = a00*a11 - a01*a10; + + } + + if ((obj->nRows == 3) && (obj->nCols == 3)) { + + a00 = obj->array[0][0]; + a01 = obj->array[0][1]; + a02 = obj->array[0][2]; + a10 = obj->array[1][0]; + a11 = obj->array[1][1]; + a12 = obj->array[1][2]; + a20 = obj->array[2][0]; + a21 = obj->array[2][1]; + a22 = obj->array[2][2]; + + det = a00*a11*a22 - a00*a12*a21 - a01*a10*a22 + a01*a12*a20 + a02*a10*a21 - a02*a11*a20; + + } + + return det; + + } + + void matrix_scale(matrix_obj * obj, const matrix_obj * src, const float scale) { + + unsigned int iRow; + unsigned int iCol; + + for (iRow = 0; iRow < src->nRows; iRow++) { + + for (iCol = 0; iCol < src->nCols; iCol++) { + + obj->array[iRow][iCol] = scale * src->array[iRow][iCol]; + + } + + } + + } + + void matrix_printf(const matrix_obj * obj) { + + unsigned int iRow; + unsigned int iCol; + + for (iRow = 0; iRow < obj->nRows; iRow++) { + + for (iCol = 0; iCol < obj->nCols; iCol++) { + + printf("%+1.5f ",obj->array[iRow][iCol]); + + } + + printf("\n"); + + } + + } \ No newline at end of file diff --git a/src/utils/matrix.h b/src/utils/matrix.h new file mode 100644 index 0000000..596d5bd --- /dev/null +++ b/src/utils/matrix.h @@ -0,0 +1,96 @@ +#ifndef __ODAS_SIGNAL_MATRIX +#define __ODAS_SIGNAL_MATRIX + + #include + #include + + //! Matrix + typedef struct matrix_obj { + + float ** array; ///< Array of elements in the matrix. + unsigned int nRows; ///< Number of rows. + unsigned int nCols; ///< Number of columns. + + } matrix_obj; + + /** Constructor. Initialize all elements to zero in a matrix nRows x nCols. + \param nRows Number of rows. + \param nCols Number of columns. + \return Pointer to the instantiated object. + */ + matrix_obj * matrix_construct_zero(const unsigned int nRows, const unsigned int nCols); + + /** Constructor. Clone the object. + \param matrix Object to be cloned. + \return Pointer to the instantiated object. + */ + matrix_obj * matrix_construct_matrix(const matrix_obj * matrix); + + /** Set elements to 0. + \param obj Object to be used. + */ + void matrix_copy_zero(matrix_obj * obj); + + /** Set fields to the field values of the model object. No memory allocated. + \param matrix Object to be copied. + \param obj Object to be modified. + */ + void matrix_copy_matrix(matrix_obj * obj, const matrix_obj * matrix); + + /** Destructor. Free memory. + \param obj Object to be destroyed. + */ + void matrix_destroy(matrix_obj * obj); + + /** Compute the matrix transpose. + \param src Matrix to be transpose. + \param obj Transposed matrix. + */ + void matrix_transpose(matrix_obj * obj, const matrix_obj * src); + + /** Add two matrices. + \param src1 First matrix to be added. + \param src2 Second matrix to be added. + \param obj Result of the addition. + */ + void matrix_add(matrix_obj * obj, const matrix_obj * src1, const matrix_obj * src2); + + /** Subtract two matrices. + \param src1 Matrix to be subtracted from. + \param src2 Matrix to subtract. + \param obj Result of the subtraction. + */ + void matrix_sub(matrix_obj * obj, const matrix_obj * src1, const matrix_obj * src2); + + /** Multiply two matrices. + \param src1 First matrix to be multiplied. + \param src2 Second matrix to be multiplied. + \param obj Result of the multiplication. + */ + void matrix_mul(matrix_obj * obj, const matrix_obj * src1, const matrix_obj * src2); + + /** Inverse a matrix. + \param src Matrix to be inverted. + \param obj Inverted matrix. + */ + void matrix_inv(matrix_obj * obj, const matrix_obj * src); + + /** Compute the determinant. + \param obj Matrix which determinant is computed. + \return Determinant value. + */ + float matrix_det(const matrix_obj * obj); + + /** Multiply a matrix by a scalar. + \param src Matrix to be scaled. + \param scale Scaling factor. + \param obj Scaled matrix. + */ + void matrix_scale(matrix_obj * obj, const matrix_obj * src, const float scale); + + /** Print the content of the matrix. + \param obj Object to be used. + */ + void matrix_printf(const matrix_obj * obj); + +#endif \ No newline at end of file diff --git a/src/utils/pcm.c b/src/utils/pcm.c new file mode 100644 index 0000000..9c0c6c7 --- /dev/null +++ b/src/utils/pcm.c @@ -0,0 +1,123 @@ + #include "pcm.h" + + float pcm_signed08bits2normalized(const char * bytes) { + + int sample32; + float sampleFloat; + + sample32 = 0; + sample32 += ((((signed int) bytes[3]) << 0) & 0xFFFFFFFF); + + sampleFloat = (float) sample32; + + sampleFloat /= 128.0f; + + return sampleFloat; + + } + + float pcm_signed16bits2normalized(const char * bytes) { + + int sample32; + float sampleFloat; + + sample32 = 0; + sample32 += ((((signed int) bytes[2]) << 0) & 0x000000FF); + sample32 += ((((signed int) bytes[3]) << 8) & 0xFFFFFF00); + + sampleFloat = (float) sample32; + + sampleFloat /= 32768.0f; + + return sampleFloat; + + } + + float pcm_signed24bits2normalized(const char * bytes) { + + int sample32; + float sampleFloat; + + sample32 = 0; + sample32 += ((((signed int) bytes[1]) << 0) & 0x000000FF); + sample32 += ((((signed int) bytes[2]) << 8) & 0x0000FF00); + sample32 += ((((signed int) bytes[3]) << 16) & 0xFFFF0000); + + sampleFloat = (float) sample32; + + sampleFloat /= 8388608.0f; + + return sampleFloat; + + } + + float pcm_signed32bits2normalized(const char * bytes) { + + int sample32; + float sampleFloat; + + sample32 = 0; + sample32 += ((((signed int) bytes[0]) << 0) & 0x000000FF); + sample32 += ((((signed int) bytes[1]) << 8) & 0x0000FF00); + sample32 += ((((signed int) bytes[2]) << 16) & 0x00FF0000); + sample32 += ((((signed int) bytes[3]) << 24) & 0xFF000000); + + sampleFloat = (float) sample32; + + sampleFloat /= 2147483648.0f; + + return sampleFloat; + + } + + void pcm_normalized2signed08bits(const float sample, char * bytes) { + + int sample32; + + sample32 = (signed int) (roundf(sample * 128.0f)); + + bytes[0] = 0x00; + bytes[1] = 0x00; + bytes[2] = 0x00; + bytes[3] = (signed char) ((sample32 & 0x000000FF) >> 0); + + } + + void pcm_normalized2signed16bits(const float sample, char * bytes) { + + int sample32; + + sample32 = (signed int) (roundf(sample * 32768.0f)); + + bytes[0] = 0x00; + bytes[1] = 0x00; + bytes[2] = (signed char) ((sample32 & 0x000000FF) >> 0); + bytes[3] = (signed char) ((sample32 & 0x0000FF00) >> 8); + + } + + void pcm_normalized2signed24bits(const float sample, char * bytes) { + + int sample32; + + sample32 = (signed int) (roundf(sample * 8388608.0f)); + + bytes[0] = 0x00; + bytes[1] = (signed char) ((sample32 & 0x000000FF) >> 0); + bytes[2] = (signed char) ((sample32 & 0x0000FF00) >> 8); + bytes[3] = (signed char) ((sample32 & 0x00FF0000) >> 16); + + } + + void pcm_normalized2signed32bits(const float sample, char * bytes) { + + int sample32; + + sample32 = (signed int) (roundf(sample * 2147483648.0f)); + + bytes[0] = (signed char) ((sample32 & 0x000000FF) >> 0); + bytes[1] = (signed char) ((sample32 & 0x0000FF00) >> 8); + bytes[2] = (signed char) ((sample32 & 0x00FF0000) >> 16); + bytes[3] = (signed char) ((sample32 & 0xFF000000) >> 24); + + } diff --git a/src/utils/pcm.h b/src/utils/pcm.h new file mode 100644 index 0000000..ab0ac94 --- /dev/null +++ b/src/utils/pcm.h @@ -0,0 +1,23 @@ +#ifndef __ODAS_UTILS_PCM +#define __ODAS_UTILS_PCM + + #include + #include + + float pcm_signed08bits2normalized(const char * bytes); + + float pcm_signed16bits2normalized(const char * bytes); + + float pcm_signed24bits2normalized(const char * bytes); + + float pcm_signed32bits2normalized(const char * bytes); + + void pcm_normalized2signed08bits(const float sample, char * bytes); + + void pcm_normalized2signed16bits(const float sample, char * bytes); + + void pcm_normalized2signed24bits(const float sample, char * bytes); + + void pcm_normalized2signed32bits(const float sample, char * bytes); + +#endif \ No newline at end of file diff --git a/src/utils/random.c b/src/utils/random.c new file mode 100644 index 0000000..846d724 --- /dev/null +++ b/src/utils/random.c @@ -0,0 +1,186 @@ + + #include "random.h" + + random_obj * random_construct_zero(const unsigned int N) { + + random_obj * obj; + unsigned int n; + time_t t; + + obj = (random_obj *) malloc(sizeof(random_obj)); + + obj->N = N; + obj->pdf = (float *) malloc(sizeof(float) * N); + obj->cdf = (float *) malloc(sizeof(float) * (N+1)); + + for (n = 0; n < N; n++) { + obj->pdf[n] = 0.0f; + obj->cdf[n] = 0.0f; + } + + obj->I = (unsigned int) floorf(logf(N)/logf(2.0f)) + 1; + + srand((unsigned) time(&t)); + + return obj; + + } + + void random_destroy(random_obj * obj) { + + free((void *) obj->pdf); + free((void *) obj->cdf); + free((void *) obj); + + } + + void random_pdf(random_obj * obj, const float * pdf) { + + unsigned int n; + float cdfTotal; + + cdfTotal = 0.0f; + + for (n = 0; n < obj->N; n++) { + + obj->pdf[n] = pdf[n]; + obj->cdf[n] = cdfTotal; + cdfTotal += pdf[n]; + + } + + obj->cdf[obj->N] = cdfTotal; + + } + + void random_generate(const random_obj * obj, const unsigned int nSamples, unsigned int * indexes) { + + unsigned int iSample; + float rndNumber; + + unsigned int indexLow; + unsigned int indexHigh; + unsigned int indexMiddle; + unsigned int i; + + for (iSample = 0; iSample < nSamples; iSample++) { + + rndNumber = (float) ((double)rand() / (double)RAND_MAX); + + indexLow = 0; + indexHigh = obj->N; + + for (i = 0; i < obj->I; i++) { + + indexMiddle = (indexLow + indexHigh) / 2; + + if (obj->cdf[indexMiddle] <= rndNumber) { + + indexLow = indexMiddle; + + } + else { + + indexHigh = indexMiddle; + + } + + } + + indexes[iSample] = indexLow; + + } + + } + + normal_obj * normal_construct_zero(const double epsilon) { + + normal_obj * obj; + time_t t; + + obj = (normal_obj *) malloc(sizeof(normal_obj)); + + obj->epsilon = epsilon; + + srand((unsigned) time(&t)); + + return obj; + + } + + void normal_destroy(normal_obj * obj) { + + free((void *) obj); + + } + + void normal_generate(const normal_obj * obj, const unsigned int nSamples, float * values) { + + unsigned int iSample; + double u1, u2; + double z0, z1; + + for (iSample = 0; iSample < nSamples; iSample+=2) { + + do { + + u1 = ((double)rand() / (double)RAND_MAX); + u2 = ((double)rand() / (double)RAND_MAX); + + } while (u1 <= obj->epsilon); + + z0 = sqrt(-2.0 * log(u1)) * cos(2.0*M_PI*u2); + values[iSample] = (float) z0; + + if ((iSample+1) < nSamples) { + z1 = sqrt(-2.0 * log(u1)) * sin(2.0*M_PI*u2); + values[iSample+1] = (float) z1; + } + + } + + } + + normaloff_obj * normaloff_construct_rnd(const double epsilon, const unsigned int N) { + + normaloff_obj * obj; + normal_obj * normal; + unsigned int n; + time_t t; + + obj = (normaloff_obj *) malloc(sizeof(normaloff_obj)); + + obj->N = N; + + obj->values = (float *) malloc(sizeof(float) * N); + + normal = normal_construct_zero(epsilon); + normal_generate(normal, N, obj->values); + normal_destroy(normal); + + srand((unsigned) time(&t)); + + return obj; + + } + + void normaloff_destroy(normaloff_obj * obj) { + + free((void *) obj->values); + free((void *) obj); + + } + + void normaloff_generate(const normaloff_obj * obj, const unsigned int nSamples, float * values) { + + unsigned int iSample; + unsigned int index; + + for (iSample = 0; iSample < nSamples; iSample++) { + + index = rand() % (obj->N); + values[iSample] = obj->values[index]; + + } + + } diff --git a/src/utils/random.h b/src/utils/random.h new file mode 100644 index 0000000..254d029 --- /dev/null +++ b/src/utils/random.h @@ -0,0 +1,96 @@ +#ifndef __ODAS_UTILS_RANDOM +#define __ODAS_UTILS_RANDOM + + #include + #include + #include + #include + + //! Random number generator + typedef struct random_obj { + + unsigned int N; ///< Number of elements in the space + unsigned int I; ///< Number of iterations to perform a binary search + float * pdf; ///< Probability density function (probability mass function) + float * cdf; ///< Cumulative density function + + } random_obj; + + //! Standard normal number generator + typedef struct normal_obj { + + double epsilon; ///< Small value to perform the Box-Muller transform. + + } normal_obj; + + //! Standard normal number generator offline + typedef struct normaloff_obj { + + unsigned int N; ///< Number of samples. + float * values; ///< Normally distributed samples. + + } normaloff_obj; + + /** Constructor. Initialize all elements to zero. + \param N Number of elements in the space. + \return Pointer to the instantiated object. + */ + random_obj * random_construct_zero(const unsigned int N); + + /** Destructor. Free memory. + \param obj Object to be destroyed. + */ + void random_destroy(random_obj * obj); + + /** Set the PDF. + \param pdf Array that contains the PDF values. + \param obj Object to be modified. + */ + void random_pdf(random_obj * obj, const float * pdf); + + /** Pick element randomly according to the PDF, and return the index. + \param nSamples Number of elements to pick. + \param indexes Array that contains the returned indexes. + \param obj Object to be used. + */ + void random_generate(const random_obj * obj, const unsigned int nSamples, unsigned int * indexes); + + /** Constructor. + \param epsilon Small value to perform the Box-Muller transform. + \return Pointer of the instantiated object. + */ + normal_obj * normal_construct_zero(const double epsilon); + + /** Destructor. Free memory. + \param obj Object to be destroyed. + */ + void normal_destroy(normal_obj * obj); + + /** Generate a random number according to a standard normal distribution. + \param nSamples Number of random numbers to generate. + \param values Array that contains the generated random numbers. + \param obj Object to be used. + */ + void normal_generate(const normal_obj * obj, const unsigned int nSamples, float * values); + + /** Constructor. + \param epsilon Small value to perform the Box-Muller transform. + \param N Number of random numbers generated offline and stored. + \return Pointer of the instantiated object. + */ + normaloff_obj * normaloff_construct_rnd(const double epsilon, const unsigned int N); + + /** Destructor. Free memory. + \param obj Object to be destroyed. + */ + void normaloff_destroy(normaloff_obj * obj); + + /** Generate a random number according to a standard normal distribution. + \param nSamples Number of random numbers to generate. + \param values Array that contains the generated random numbers. + \param obj Object to be used. + */ + void normaloff_generate(const normaloff_obj * obj, const unsigned int nSamples, float * values); + + +#endif \ No newline at end of file -- 2.34.1