From de627ab555b7e8f01f19b610ddb6ec196de37f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Sandstr=C3=B6m?= Date: Mon, 6 Apr 2020 14:53:47 +0200 Subject: [PATCH] WIP --- .../ecmcPlugin_FFTApp/src/ecmcFFT.cpp | 30 ++++++++++++------- .../ecmcPlugin_FFTApp/src/ecmcFFT.h | 8 +++-- .../ecmcPlugin_FFTApp/src/ecmcFFTWrap.cpp | 17 ++++++++++- .../ecmcPlugin_FFTApp/src/ecmcFFTWrap.h | 1 + .../ecmcPlugin_FFTApp/src/ecmcPluginFFT.c | 6 ++-- 5 files changed, 43 insertions(+), 19 deletions(-) diff --git a/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFT.cpp b/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFT.cpp index 8752f46..b9d2bec 100644 --- a/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFT.cpp +++ b/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFT.cpp @@ -69,7 +69,6 @@ ecmcFFT::ecmcFFT(int fftIndex, // index of this object (if several is cr cfgNfft_ = ECMC_PLUGIN_DEFAULT_NFFT; // samples in fft (must be n^2) cfgDcRemove_ = 0; cfgApplyScale_ = 1; // Scale as default to get correct amplitude in fft - asynPort_ = (ecmcAsynPortDriver*) getEcmcAsynPortDriver(); if(!asynPort_) { throw std::runtime_error("Asyn port NULL"); @@ -81,9 +80,7 @@ ecmcFFT::ecmcFFT(int fftIndex, // index of this object (if several is cr throw std::out_of_range("NFFT must be > 0 and even N^2."); } // set scale factor - scale_ = 1.0 / (double)cfgNfft_; - - connectToDataSource(); // Also assigns dataItem_ + scale_ = 1.0 / cfgNfft_; // sqrt((double)cfgNfft_); // Allocate buffers dataBuffer_ = new double[cfgNfft_]; @@ -204,6 +201,7 @@ void ecmcFFT::dataUpdatedCallback(uint8_t* data, //Buffer full if(!fftCalcDone_){ calcFFT(); + scaleFFT(); if(cfgDbgMode_){ printComplexArray(fftBuffer_, cfgNfft_, @@ -264,11 +262,11 @@ void ecmcFFT::dataUpdatedCallback(uint8_t* data, void ecmcFFT::addDataToBuffer(double data) { if(dataBuffer_ && (elementsInBuffer_ < cfgNfft_) ) { - if(cfgApplyScale_) { - dataBuffer_[elementsInBuffer_] = data*scale_; - } else { + //if(cfgApplyScale_) { + // dataBuffer_[elementsInBuffer_] = data * scale_; + //} else { dataBuffer_[elementsInBuffer_] = data; - } + //} } elementsInBuffer_ ++; } @@ -285,6 +283,16 @@ void ecmcFFT::calcFFT() { fftCalcDone_ = 1; } +void ecmcFFT::scaleFFT() { + if(!cfgApplyScale_) { + return; + } + + for(unsigned int i = 0 ; i < cfgNfft_ ; ++i ) { + fftBuffer_[i] = fftBuffer_[i] * scale_; + } +} + void ecmcFFT::printEcDataArray(uint8_t* data, size_t size, ecmcEcDataType dt, @@ -335,11 +343,11 @@ void ecmcFFT::printEcDataArray(uint8_t* data, } void ecmcFFT::printComplexArray(std::complex* fftBuff, - size_t elements, - int objId) { + size_t elements, + int objId) { printf("fft id: %d, results: \n",objId); for(unsigned int i = 0 ; i < elements ; ++i ) { - printf("%lf\n", std::abs(fftBuff[i])); + printf("%d: %lf\n", i, std::abs(fftBuff[i])); } } diff --git a/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFT.h b/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFT.h index 49b850a..215d9fa 100644 --- a/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFT.h +++ b/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFT.h @@ -36,13 +36,15 @@ class ecmcFFT { void dataUpdatedCallback(uint8_t* data, size_t size, ecmcEcDataType dt); + // Call just before realtime because then all data sources should be available + void connectToDataSource(); + private: void parseConfigStr(char *configStr); - void connectToDataSource(); void clearBuffers(); - void calcFFT(); void addDataToBuffer(double data); - + void calcFFT(); + void scaleFFT(); static int dataTypeSupported(ecmcEcDataType dt); ecmcDataItem *dataItem_; diff --git a/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFTWrap.cpp b/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFTWrap.cpp index be6e2c1..0b54476 100644 --- a/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFTWrap.cpp +++ b/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFTWrap.cpp @@ -47,7 +47,22 @@ int createFFT(char* configStr) { void deleteAllFFTs() { for(std::vector::iterator pfft = ffts.begin(); pfft != ffts.end(); ++pfft) { if(*pfft) { - delete *pfft; + delete (*pfft); } } } + +int linkDataToFFTs() { + for(std::vector::iterator pfft = ffts.begin(); pfft != ffts.end(); ++pfft) { + if(*pfft) { + try { + (*pfft)->connectToDataSource(); + } + catch(std::exception& e) { + printf("Exception: %s.",e.what()); + return ECMC_PLUGIN_FFT_ERROR_CODE; + } + } + } + return 0; +} diff --git a/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFTWrap.h b/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFTWrap.h index 32e8cd1..102f019 100644 --- a/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFTWrap.h +++ b/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcFFTWrap.h @@ -17,6 +17,7 @@ extern "C" { # endif // ifdef __cplusplus int createFFT(char *source); +int linkDataToFFTs(); void deleteAllFFTs(); # ifdef __cplusplus diff --git a/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcPluginFFT.c b/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcPluginFFT.c index 8014893..e497884 100644 --- a/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcPluginFFT.c +++ b/ecmcPlugin_FFT-loc/ecmcPlugin_FFTApp/src/ecmcPluginFFT.c @@ -64,12 +64,10 @@ int adv_exampleRealtime(int ecmcError) return 0; } -/** Optional function. - * Will be called once just before going to realtime mode - * Return value other than 0 will be considered error. +/** Link to data source here since all sources should be availabe at this stage **/ int adv_exampleEnterRT(){ - return 0; + return linkDataToFFTs(); } /** Optional function.