From b6a9fe574ac3b374e8cccebcb439ba35a10b42f9 Mon Sep 17 00:00:00 2001 From: FrancoisGrondin Date: Tue, 24 Oct 2017 11:11:17 -0400 Subject: [PATCH] Now possible to send separated stream via socket. New message with power spectrum added --- CMakeLists.txt | 12 +++ demo/server.c | 50 +++++++-- src/connector/con_powers.c | 57 ++++++++++ src/connector/con_powers.h | 24 +++++ src/message/msg_powers.c | 91 ++++++++++++++++ src/message/msg_powers.h | 40 +++++++ src/signal/env.c | 101 ++++++++++++++++++ src/signal/env.h | 28 +++++ src/sink/snk_hops.c | 72 +++++++++++-- src/sink/snk_hops.h | 10 +- src/system/env2env.c | 210 +++++++++++++++++++++++++++++++++++++ src/system/env2env.h | 43 ++++++++ src/system/freq2env.c | 46 ++++++++ src/system/freq2env.h | 21 ++++ 14 files changed, 785 insertions(+), 20 deletions(-) create mode 100644 src/connector/con_powers.c create mode 100644 src/connector/con_powers.h create mode 100644 src/message/msg_powers.c create mode 100644 src/message/msg_powers.h create mode 100644 src/signal/env.c create mode 100644 src/signal/env.h create mode 100644 src/system/env2env.c create mode 100644 src/system/env2env.h create mode 100644 src/system/freq2env.c create mode 100644 src/system/freq2env.h diff --git a/CMakeLists.txt b/CMakeLists.txt index adf7e5b..9cb1132 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ set(SRC src/signal/coherence.c src/signal/delta.c src/signal/demixing.c + src/signal/env.c src/signal/frame.c src/signal/freq.c src/signal/gain.c @@ -74,10 +75,12 @@ set(SRC src/system/acorr2pitch.c src/system/demixing2freq.c + src/system/env2env.c src/system/hop2hop.c src/system/hop2frame.c src/system/frame2freq.c src/system/freq2acorr.c + src/system/freq2env.c src/system/freq2freq.c src/system/freq2frame.c src/system/freq2xcorr.c @@ -133,11 +136,13 @@ set(SRC src/connector/con_categories.c src/connector/con_hops.c src/connector/con_pots.c + src/connector/con_powers.c src/connector/con_spectra.c src/connector/con_tracks.c src/message/msg_categories.c src/message/msg_hops.c src/message/msg_pots.c + src/message/msg_powers.c src/message/msg_spectra.c src/message/msg_tracks.c src/module/mod_classify.c @@ -188,6 +193,13 @@ target_link_libraries(debug odas ) +add_executable(server + demo/server.c +) +target_link_libraries(server + odas +) + install(TARGETS odas DESTINATION lib) diff --git a/demo/server.c b/demo/server.c index 683d473..1cf731c 100644 --- a/demo/server.c +++ b/demo/server.c @@ -4,6 +4,8 @@ #include #include #include + #include + #include int main(int argc, char* argv[]) { @@ -14,40 +16,66 @@ char * message; int messageSize; - if (argc != 2) { + int c; + unsigned int portNumber; + char * fileName; + FILE * fp; + + const unsigned int nBytes = 10240; + + while ((c = getopt(argc,argv, "p:o:")) != -1) { + + switch(c) { + + case 'p': + + portNumber = atoi(optarg); + + break; + + case 'o': + + fileName = (char *) malloc(sizeof(char) * (strlen(optarg)+1)); + strcpy(fileName, optarg); + + break; + + } - printf("Expecting one argument\n"); - exit(EXIT_FAILURE); } server_id = socket(AF_INET, SOCK_STREAM, 0); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(INADDR_ANY); - server_address.sin_port = htons(atoi(argv[1])); + server_address.sin_port = htons(portNumber); + + printf("Opening file............. "); fflush(stdout); + fp = fopen(fileName, "wb"); + printf("[OK]\n"); - printf("Binding socket... "); fflush(stdout); + printf("Binding socket........... "); fflush(stdout); bind(server_id, (struct sockaddr *) &server_address, sizeof(server_address)); printf("[OK]\n"); - printf("Listening socket... "); fflush(stdout); + printf("Listening socket......... "); fflush(stdout); listen(server_id, 1); printf("[OK]\n"); - printf("Waiting for connection..."); fflush(stdout); + printf("Waiting for connection... "); fflush(stdout); connection_id = accept(server_id, (struct sockaddr*) NULL, NULL); printf("[OK]\n"); - message = (char *) malloc(sizeof(char) * 1024 * 10); + message = (char *) malloc(sizeof(char) * nBytes); - while( (messageSize = recv(connection_id, message, 1024 * 10, 0)) > 0) { + while( (messageSize = recv(connection_id, message, nBytes, 0)) > 0) { message[messageSize] = 0x00; - printf("Received (%u): %s\n",messageSize,message); + fwrite(message, messageSize, sizeof(char), fp); } - printf("messageSize = %d\n",messageSize); + fclose(fp); } \ No newline at end of file diff --git a/src/connector/con_powers.c b/src/connector/con_powers.c new file mode 100644 index 0000000..09accb6 --- /dev/null +++ b/src/connector/con_powers.c @@ -0,0 +1,57 @@ + + #include "con_powers.h" + + con_powers_obj * con_powers_construct(const unsigned int nOuts, const msg_powers_cfg * msg_powers_config) { + + con_powers_obj * obj; + unsigned int iOut; + + obj = (con_powers_obj *) malloc(sizeof(con_powers_obj)); + + obj->nOuts = nOuts; + + obj->in = msg_powers_construct(msg_powers_config); + + obj->outs = (msg_powers_obj **) malloc(sizeof(msg_powers_obj *) * nOuts); + for (iOut = 0; iOut < obj->nOuts; iOut++) { + obj->outs[iOut] = msg_powers_construct(msg_powers_config); + } + + return obj; + + } + + void con_powers_destroy(con_powers_obj * obj) { + + unsigned int iOut; + + for (iOut = 0; iOut < obj->nOuts; iOut++) { + msg_powers_destroy(obj->outs[iOut]); + } + free((void *) obj->outs); + + msg_powers_destroy(obj->in); + + free((void *) obj); + + } + + int con_powers_process(con_powers_obj * obj) { + + unsigned int iOut; + int rtnValue; + + for (iOut = 0; iOut < obj->nOuts; iOut++) { + msg_powers_copy(obj->outs[iOut], obj->in); + } + + if (msg_powers_isZero(obj->in) == 1) { + rtnValue = -1; + } + else { + rtnValue = 0; + } + + return rtnValue; + + } \ No newline at end of file diff --git a/src/connector/con_powers.h b/src/connector/con_powers.h new file mode 100644 index 0000000..5541b4c --- /dev/null +++ b/src/connector/con_powers.h @@ -0,0 +1,24 @@ +#ifndef __ODAS_CONNECTOR_POWERS +#define __ODAS_CONNECTOR_POWERS + + #include "../message/msg_powers.h" + + #include + #include + + typedef struct con_powers_obj { + + msg_powers_obj * in; + msg_powers_obj ** outs; + + unsigned int nOuts; + + } con_powers_obj; + + con_powers_obj * con_powers_construct(const unsigned int nOuts, const msg_powers_cfg * msg_powers_config); + + void con_powers_destroy(con_powers_obj * obj); + + int con_powers_process(con_powers_obj * obj); + +#endif \ No newline at end of file diff --git a/src/message/msg_powers.c b/src/message/msg_powers.c new file mode 100644 index 0000000..6eff2b1 --- /dev/null +++ b/src/message/msg_powers.c @@ -0,0 +1,91 @@ + + #include "msg_powers.h" + + msg_powers_obj * msg_powers_construct(const msg_powers_cfg * msg_powers_config) { + + msg_powers_obj * obj; + + obj = (msg_powers_obj *) malloc(sizeof(msg_powers_obj)); + + obj->timeStamp = 0; + obj->fS = msg_powers_config->fS; + obj->envs = envs_construct_zero(msg_powers_config->nChannels, msg_powers_config->halfFrameSize); + + return obj; + + } + + void msg_powers_destroy(msg_powers_obj * obj) { + + envs_destroy(obj->envs); + free((void *) obj); + + } + + void msg_powers_copy(msg_powers_obj * dest, const msg_powers_obj * src) { + + dest->timeStamp = src->timeStamp; + dest->fS = src->fS; + envs_copy(dest->envs, src->envs); + + } + + void msg_powers_zero(msg_powers_obj * obj) { + + obj->timeStamp = 0; + obj->fS = 0; + envs_zero(obj->envs); + + } + + unsigned int msg_powers_isZero(const msg_powers_obj * obj) { + + unsigned int rtnValue; + + if (obj->timeStamp == 0) { + rtnValue = 1; + } + else { + rtnValue = 0; + } + + return rtnValue; + + } + + msg_powers_cfg * msg_powers_cfg_construct(void) { + + msg_powers_cfg * cfg; + + cfg = (msg_powers_cfg *) malloc(sizeof(msg_powers_cfg)); + + cfg->halfFrameSize = 0; + cfg->nChannels = 0; + cfg->fS = 0; + + return cfg; + + } + + void msg_powers_cfg_destroy(msg_powers_cfg * msg_powers_config) { + + free((void *) msg_powers_config); + + } + + void msg_powers_cfg_printf(const msg_powers_cfg * msg_powers_config) { + + if (msg_powers_config != NULL) { + + printf("halfFrameSize = %u\n", msg_powers_config->halfFrameSize); + printf("nChannels = %u\n", msg_powers_config->nChannels); + printf("fS = %u\n", msg_powers_config->fS); + + } + else { + + printf("(null)\n"); + + } + + } \ No newline at end of file diff --git a/src/message/msg_powers.h b/src/message/msg_powers.h new file mode 100644 index 0000000..6c3635a --- /dev/null +++ b/src/message/msg_powers.h @@ -0,0 +1,40 @@ +#ifndef __ODAS_MESSAGE_POWERS +#define __ODAS_MESSAGE_POWERS + + #include + + #include "../signal/env.h" + + typedef struct msg_powers_obj { + + unsigned long long timeStamp; + unsigned int fS; + envs_obj * envs; + + } msg_powers_obj; + + typedef struct msg_powers_cfg { + + unsigned int halfFrameSize; + unsigned int nChannels; + unsigned int fS; + + } msg_powers_cfg; + + msg_powers_obj * msg_powers_construct(const msg_powers_cfg * msg_powers_config); + + void msg_powers_destroy(msg_powers_obj * obj); + + void msg_powers_copy(msg_powers_obj * dest, const msg_powers_obj * src); + + void msg_powers_zero(msg_powers_obj * obj); + + unsigned int msg_powers_isZero(const msg_powers_obj * obj); + + msg_powers_cfg * msg_powers_cfg_construct(void); + + void msg_powers_cfg_destroy(msg_powers_cfg * msg_powers_config); + + void msg_powers_cfg_printf(const msg_powers_cfg * msg_powers_config); + +#endif \ No newline at end of file diff --git a/src/signal/env.c b/src/signal/env.c new file mode 100644 index 0000000..392b741 --- /dev/null +++ b/src/signal/env.c @@ -0,0 +1,101 @@ + + #include "env.h" + + envs_obj * envs_construct_zero(const unsigned int nSignals, const unsigned int halfFrameSize) { + + envs_obj * obj; + unsigned int iSignal; + + obj = (envs_obj *) malloc(sizeof(envs_obj)); + + obj->nSignals = nSignals; + obj->halfFrameSize = halfFrameSize; + + obj->array = (float **) malloc(sizeof(float *) * nSignals); + for (iSignal = 0; iSignal < nSignals; iSignal++) { + obj->array[iSignal] = (float *) malloc(sizeof(float) * halfFrameSize); + memset(obj->array[iSignal], 0x00, halfFrameSize * sizeof(float)); + } + + return obj; + + } + + envs_obj * envs_clone(const envs_obj * obj) { + + envs_obj * clone; + unsigned int iSignal; + + clone = (envs_obj *) malloc(sizeof(envs_obj)); + + clone->nSignals = obj->nSignals; + clone->halfFrameSize = obj->halfFrameSize; + + clone->array = (float **) malloc(sizeof(float *) * obj->nSignals); + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + clone->array[iSignal] = (float *) malloc(sizeof(float) * obj->halfFrameSize); + memcpy(clone->array[iSignal], obj->array[iSignal], obj->halfFrameSize * sizeof(float)); + } + + return clone; + + } + + void envs_copy(envs_obj * dest, const envs_obj * src) { + + unsigned int iSignal; + + for (iSignal = 0; iSignal < src->nSignals; iSignal++) { + memcpy(dest->array[iSignal], src->array[iSignal], src->halfFrameSize * sizeof(float)); + } + + } + + void envs_zero(envs_obj * obj) { + + unsigned int iSignal; + + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + memset(obj->array[iSignal], 0x00, obj->halfFrameSize * sizeof(float)); + } + + } + + void envs_destroy(envs_obj * obj) { + + unsigned int iSignal; + + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + free((void *) obj->array[iSignal]); + } + free((void *) obj->array); + + free((void *) obj); + + } + + void envs_printf(const envs_obj * obj) { + + unsigned int iSignal; + unsigned int iSample; + + for (iSignal = 0; iSignal < obj->nSignals; iSignal++) { + + printf("(%04u): ",iSignal); + + for (iSample = 0; iSample < obj->halfFrameSize; iSample++) { + + printf("%+1.5f ", + obj->array[iSignal][iSample]); + + if ((((iSample+1) % 16) == 0) && ((iSample+1)!=obj->halfFrameSize)) { + printf("\n "); + } + + } + + printf("\n"); + + } + + } \ No newline at end of file diff --git a/src/signal/env.h b/src/signal/env.h new file mode 100644 index 0000000..7803ec2 --- /dev/null +++ b/src/signal/env.h @@ -0,0 +1,28 @@ +#ifndef __ODAS_SIGNAL_ENV +#define __ODAS_SIGNAL_ENV + + #include + #include + #include + + typedef struct envs_obj { + + unsigned int nSignals; + unsigned int halfFrameSize; + float ** array; + + } envs_obj; + + envs_obj * envs_construct_zero(const unsigned int nSignals, const unsigned int halfFrameSize); + + envs_obj * envs_clone(const envs_obj * obj); + + void envs_copy(envs_obj * dest, const envs_obj * src); + + void envs_zero(envs_obj * obj); + + void envs_destroy(envs_obj * obj); + + void envs_printf(const envs_obj * obj); + +#endif \ No newline at end of file diff --git a/src/sink/snk_hops.c b/src/sink/snk_hops.c index b7bde9d..21138c8 100644 --- a/src/sink/snk_hops.c +++ b/src/sink/snk_hops.c @@ -33,6 +33,9 @@ } + obj->smessage = (char *) malloc(sizeof(char) * msg_hops_config->nChannels * msg_hops_config->hopSize * 4); + memset(obj->smessage, 0x00, sizeof(char) * msg_hops_config->nChannels * msg_hops_config->hopSize * 4); + obj->in = (msg_hops_obj *) NULL; return obj; @@ -43,6 +46,7 @@ format_destroy(obj->format); interface_destroy(obj->interface); + free((void *) obj->smessage); free((void *) obj); @@ -78,8 +82,18 @@ case interface_socket: - printf("Not implemented yet.\n"); - exit(EXIT_FAILURE); + obj->sserver.sin_family = AF_INET; + obj->sserver.sin_port = htons(obj->interface->port); + inet_aton(obj->interface->ip, &(obj->sserver.sin_addr)); + + obj->sid = socket(AF_INET, SOCK_STREAM, 0); + + if ( (connect(obj->sid, (struct sockaddr *) &(obj->sserver), sizeof(obj->sserver))) < 0 ) { + + printf("Cannot connect to server\n"); + exit(EXIT_FAILURE); + + } break; @@ -119,8 +133,7 @@ case interface_socket: - printf("Not implemented yet.\n"); - exit(EXIT_FAILURE); + close(obj->sid); break; @@ -250,8 +263,55 @@ int snk_hops_process_socket(snk_hops_obj * obj) { - printf("Not implemented\n"); - exit(EXIT_FAILURE); + unsigned int iChannel; + unsigned int iSample; + float sample; + unsigned int nBytes; + unsigned int nBytesTotal; + int rtnValue; + + if (obj->in->timeStamp != 0) { + + switch (obj->format->type) { + + case format_binary_int08: nBytes = 1; break; + case format_binary_int16: nBytes = 2; break; + case format_binary_int24: nBytes = 3; break; + case format_binary_int32: nBytes = 4; break; + + } + + nBytesTotal = 0; + + for (iSample = 0; iSample < obj->hopSize; iSample++) { + + for (iChannel = 0; iChannel < obj->nChannels; iChannel++) { + + nBytesTotal += nBytes; + + sample = obj->in->hops->array[iChannel][iSample]; + pcm_normalized2signedXXbits(sample, nBytes, obj->bytes); + memcpy(&(obj->smessage[nBytesTotal]), &(obj->bytes[4-nBytes]), sizeof(char) * nBytes); + + } + + } + + if (send(obj->sid, obj->smessage, nBytesTotal, 0) < 0) { + printf("Could not send message.\n"); + exit(EXIT_FAILURE); + } + + rtnValue = 0; + + } + else { + + rtnValue = -1; + + } + + return rtnValue; } diff --git a/src/sink/snk_hops.h b/src/sink/snk_hops.h index 990c41a..e043b74 100644 --- a/src/sink/snk_hops.h +++ b/src/sink/snk_hops.h @@ -4,6 +4,10 @@ #include #include #include + #include + #include + #include + #include #include "../general/format.h" #include "../general/interface.h" @@ -23,9 +27,9 @@ FILE * fp; - //struct sockaddr_in sserver; - //char * smessage; - //int sid; + struct sockaddr_in sserver; + char * smessage; + int sid; char bytes[4]; diff --git a/src/system/env2env.c b/src/system/env2env.c new file mode 100644 index 0000000..68e7f37 --- /dev/null +++ b/src/system/env2env.c @@ -0,0 +1,210 @@ + + #include "env2env.h" + + env2env_mcra_obj * env2env_mcra_construct_zero(const unsigned int nChannels, const unsigned int halfFrameSize, const unsigned int bSize, const float alphaS, const unsigned int L, const float delta, const float alphaD) { + + env2env_mcra_obj * obj; + unsigned int n; + unsigned int iChannel; + float bTotal; + + obj = (env2env_mcra_obj *) malloc(sizeof(env2env_mcra_obj)); + + obj->nChannels = nChannels; + obj->halfFrameSize = halfFrameSize; + + obj->bSize = bSize; + obj->alphaS = alphaS; + obj->L = L; + obj->delta = delta; + obj->alphaD = alphaD; + + obj->l = 0; + + obj->b = (float *) malloc(sizeof(float) * bSize); + for (n = 0; n < obj->bSize; n++) { + obj->b[n] = 0.5f * (1.0f * cosf(2.0f * M_PI * ((float) (n + 1)) / ((float) (obj->bSize + 1)))); + bTotal += (float) (obj->b[n]); + } + for (n = 0; n < obj->bSize; n++) { + obj->b[n] /= bTotal; + } + + + obj->Sf = (float **) malloc(sizeof(float *) * obj->nChannels); + obj->S = (float **) malloc(sizeof(float *) * obj->nChannels); + obj->SPrev = (float **) malloc(sizeof(float *) * obj->nChannels); + obj->Smin = (float **) malloc(sizeof(float *) * obj->nChannels); + obj->SminPrev = (float **) malloc(sizeof(float *) * obj->nChannels); + obj->Stmp = (float **) malloc(sizeof(float *) * obj->nChannels); + obj->StmpPrev = (float **) malloc(sizeof(float *) * obj->nChannels); + obj->lambdaD = (float **) malloc(sizeof(float *) * obj->nChannels); + obj->lambdaDnext = (float **) malloc(sizeof(float *) * obj->nChannels); + + for (iChannel = 0; iChannel < nChannels; iChannel++) { + + obj->Sf[iChannel] = (float *) malloc(sizeof(float) * halfFrameSize); + memset(obj->Sf[iChannel], 0x00, sizeof(float) * halfFrameSize); + obj->S[iChannel] = (float *) malloc(sizeof(float) * halfFrameSize); + memset(obj->S[iChannel], 0x00, sizeof(float) * halfFrameSize); + obj->SPrev[iChannel] = (float *) malloc(sizeof(float) * halfFrameSize); + memset(obj->SPrev[iChannel], 0x00, sizeof(float) * halfFrameSize); + obj->Smin[iChannel] = (float *) malloc(sizeof(float) * halfFrameSize); + memset(obj->Smin[iChannel], 0x00, sizeof(float) * halfFrameSize); + obj->SminPrev[iChannel] = (float *) malloc(sizeof(float) * halfFrameSize); + memset(obj->SminPrev[iChannel], 0x00, sizeof(float) * halfFrameSize); + obj->Stmp[iChannel] = (float *) malloc(sizeof(float) * halfFrameSize); + memset(obj->Stmp[iChannel], 0x00, sizeof(float) * halfFrameSize); + obj->StmpPrev[iChannel] = (float *) malloc(sizeof(float) * halfFrameSize); + memset(obj->StmpPrev[iChannel], 0x00, sizeof(float) * halfFrameSize); + obj->lambdaD[iChannel] = (float *) malloc(sizeof(float) * halfFrameSize); + memset(obj->lambdaD[iChannel], 0x00, sizeof(float) * halfFrameSize); + obj->lambdaDnext[iChannel] = (float *) malloc(sizeof(float) * halfFrameSize); + memset(obj->lambdaDnext[iChannel], 0x00, sizeof(float) * halfFrameSize); + + } + + return obj; + + } + + void env2env_mcra_destroy(env2env_mcra_obj * obj) { + + free((void *) obj); + + } + + void env2env_mcra_process(env2env_mcra_obj * obj, const envs_obj * noisy, envs_obj * noise) { + + unsigned int iChannel; + unsigned int iBin; + signed int iSample; + unsigned int iBinWrapped; + float sample; + + for (iChannel = 0; iChannel < obj->nChannels; iChannel++) { + + // N-1 + // +-- + // Sf_l[k] = > b[n] |X_l[k+n-(N-1)/2]|^2 + // +-- + // n=0 + + for (iBin = 0; iBin < obj->halfFrameSize; iBin++) { + + sample = 0.0f; + + for (iSample = 0; iSample < obj->bSize; iSample++) { + + iBinWrapped = (unsigned int) abs(((signed int) iBin) + iSample - ((obj->bSize-1)/2)); + sample += obj->b[iSample] * noisy->array[iChannel][iBinWrapped]; + + } + + obj->Sf[iChannel][iBin] = sample; + + } + + // + // S_l[k] = (1 - alphaS) * S_l-1[k] + alphaS * Sf_l[k] + // + + for (iBin = 0; iBin < obj->halfFrameSize; iBin++) { + + obj->S[iChannel][iBin] = (1 - obj->alphaS) * obj->SPrev[iChannel][iBin] + obj->alphaS * obj->Sf[iChannel][iBin]; + + } + + // \ + // Smin_l[k] = min{Smin_l-1[k],S_l[k]} | l < L + // Stmp_l[k] = min{Stmp_l-1[k],S_l[k]} | + // / + // \ + // Smin_l[k] = min{Stmp_l-1[k],S_l[k]} | l = L + // Stmp_l[k] = S_l[k] | + // / + + if (obj->l < obj->L) { + + // Smin_l[k] = min{Smin_l-1[k],S_l[k]} + // Stmp_l[k] = min{Stmp_l-1[k],S_l[k]} + + for (iBin = 0; iBin < obj->halfFrameSize; iBin++) { + + if (obj->SminPrev[iChannel][iBin] < obj->S[iChannel][iBin]) { + obj->Smin[iChannel][iBin] = obj->SminPrev[iChannel][iBin]; + } + else { + obj->Smin[iChannel][iBin] = obj->S[iChannel][iBin]; + } + + if (obj->StmpPrev[iChannel][iBin] < obj->S[iChannel][iBin]) { + obj->Stmp[iChannel][iBin] = obj->StmpPrev[iChannel][iBin]; + } + else { + obj->Stmp[iChannel][iBin] = obj->S[iChannel][iBin]; + } + + } + + } + else { + + // Smin_l[k] = min{Stmp_l-1[k],S_l[k]} + // Stmp_l[k] = S_l[k] + + for (iBin = 0; iBin < obj->halfFrameSize; iBin++) { + + if (obj->StmpPrev[iChannel][iBin] < obj->S[iChannel][iBin]) { + obj->Smin[iChannel][iBin] = obj->StmpPrev[iChannel][iBin]; + } + else { + obj->Smin[iChannel][iBin] = obj->S[iChannel][iBin]; + } + + obj->Stmp[iChannel][iBin] = obj->S[iChannel][iBin]; + + } + + } + + // \ + // | S_l[k] < Smin_l[k] * delta + // lambdaD_l+1[k] = (1-alphaD) * lambdaD_l[k] + alphaD * |Y_l[k]|^2 | lambdaD_l[k] > |Y_l[k]|^2 + // | + // / + // + // lambdaD_l+1[k] = lambdaD_l[k] otherwise + // + + for (iBin = 0; iBin < obj->halfFrameSize; iBin++) { + + if ((obj->S[iChannel][iBin] < (obj->Smin[iChannel][iBin] * obj->delta)) || (obj->lambdaD[iChannel][iBin] > noisy->array[iChannel][iBin])) { + + obj->lambdaDnext[iChannel][iBin] = obj->lambdaD[iChannel][iBin]; + + } + + } + + // Smin_l-1 = Smin_l + // S_l-1 = S_l + // Stmp_l-1 = Stmp_l + // lambdaD_l = lambdaD_l+1 + + memcpy(obj->SminPrev[iChannel], obj->Smin[iChannel], sizeof(float) * obj->halfFrameSize); + memcpy(obj->SPrev[iChannel], obj->S[iChannel], sizeof(float) * obj->halfFrameSize); + memcpy(obj->StmpPrev[iChannel], obj->Stmp[iChannel], sizeof(float) * obj->halfFrameSize); + memcpy(obj->lambdaD[iChannel], obj->lambdaDnext[iChannel], sizeof(float) * obj->halfFrameSize); + + } + + // Count frames + + (obj->l)++; + + if (obj->l >= obj->L) { + obj->l = 0; + } + + } diff --git a/src/system/env2env.h b/src/system/env2env.h new file mode 100644 index 0000000..55cb3db --- /dev/null +++ b/src/system/env2env.h @@ -0,0 +1,43 @@ +#ifndef __ODAS_SYSTEM_ENV2ENV +#define __ODAS_SYSTEM_ENV2ENV + + #include + #include + #include + + #include "../signal/env.h" + + typedef struct env2env_mcra_obj { + + unsigned int nChannels; + unsigned int halfFrameSize; + + unsigned int bSize; + float alphaS; + unsigned int L; + float delta; + float alphaD; + + unsigned int l; + + float * b; + float ** S; + float ** Smin; + float ** SminPrev; + float ** SPrev; + float ** Stmp; + float ** StmpPrev; + float ** Sf; + float ** lambdaD; + float ** lambdaDnext; + + } env2env_mcra_obj; + + env2env_mcra_obj * env2env_mcra_construct_zero(const unsigned int nChannels, const unsigned int halfFrameSize, const unsigned int bSize, const float alphaS, const unsigned int L, const float delta, const float alphaD); + + void env2env_mcra_destroy(env2env_mcra_obj * obj); + + void env2env_mcra_process(env2env_mcra_obj * obj, const envs_obj * noisy, envs_obj * noise); + + +#endif \ No newline at end of file diff --git a/src/system/freq2env.c b/src/system/freq2env.c new file mode 100644 index 0000000..0e58cc3 --- /dev/null +++ b/src/system/freq2env.c @@ -0,0 +1,46 @@ + + #include "freq2env.h" + + freq2env_obj * freq2env_construct_zero(const unsigned int halfFrameSize) { + + freq2env_obj * obj; + + obj = (freq2env_obj *) malloc(sizeof(freq2env_obj)); + + obj->halfFrameSize = halfFrameSize; + + return obj; + + } + + void freq2env_destroy(freq2env_obj * obj) { + + free((void *) obj); + + } + + void freq2env_process(freq2env_obj * obj, const freqs_obj * freqs, envs_obj * envs) { + + unsigned int iSignal; + unsigned int iBin; + + float Xreal; + float Ximag; + float X2; + + for (iSignal = 0; iSignal < freqs->nSignals; iSignal++) { + + for (iBin = 0; iBin < obj->halfFrameSize; iBin++) { + + Xreal = freqs->array[iSignal][iBin * 2 + 0]; + Ximag = freqs->array[iSignal][iBin * 2 + 1]; + + X2 = Xreal * Xreal + Ximag * Ximag; + + envs->array[iSignal][iBin] = X2; + + } + + } + + } \ No newline at end of file diff --git a/src/system/freq2env.h b/src/system/freq2env.h new file mode 100644 index 0000000..49035e5 --- /dev/null +++ b/src/system/freq2env.h @@ -0,0 +1,21 @@ +#ifndef __ODAS_SYSTEM_FREQ2ENV +#define __ODAS_SYSTEM_FREQ2ENV + + #include + + #include "../signal/freq.h" + #include "../signal/env.h" + + typedef struct freq2env_obj { + + unsigned int halfFrameSize; + + } freq2env_obj; + + freq2env_obj * freq2env_construct_zero(const unsigned int halfFrameSize); + + void freq2env_destroy(freq2env_obj * obj); + + void freq2env_process(freq2env_obj * obj, const freqs_obj * freqs, envs_obj * envs); + +#endif \ No newline at end of file -- 2.34.1