Use double instead of float variables to limit the drift issues.
authorMarc-Antoine Maheux <Marc-Antoine.Maheux@USherbrooke.ca>
Tue, 12 Jul 2022 15:01:14 +0000 (11:01 -0400)
committerMarc-Antoine Maheux <Marc-Antoine.Maheux@USherbrooke.ca>
Tue, 12 Jul 2022 15:01:14 +0000 (11:01 -0400)
include/odas/module/mod_resample.h
include/odas/system/hop2hop.h
src/module/mod_resample.c
src/system/hop2hop.c

index 2ce123bac9790b41c1ff5486b15df2e025781872..5bfa6a8a0e0a9ef1d9bdbb905852759a97b6389e 100644 (file)
@@ -17,7 +17,7 @@
     * 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 <http://www.gnu.org/licenses/>.
     *
@@ -53,8 +53,8 @@
         unsigned int fSout;
         unsigned int hopSizeIn;
         unsigned int hopSizeOut;
-        float ratio;
-        
+        double ratio;
+
         unsigned int frameSize;
         unsigned int halfFrameSize;
         unsigned int lowPassCut;
@@ -62,7 +62,7 @@
         hop2hop_buffer_obj * hop2hop;
 
         hop2frame_obj * hop2frame;
-        frames_obj * framesAnalysis; 
+        frames_obj * framesAnalysis;
         frame2freq_obj * frame2freq;
         freqs_obj * freqsAnalysis;
         freq2freq_lowpass_obj * freq2freq_lowpass;
@@ -80,7 +80,7 @@
     } mod_resample_obj;
 
     typedef struct mod_resample_cfg {
-        
+
         unsigned int fSin;
         unsigned int fSout;
 
 
     int mod_resample_process_push_up(mod_resample_obj * obj);
 
-    int mod_resample_process_push_same(mod_resample_obj * obj);    
+    int mod_resample_process_push_same(mod_resample_obj * obj);
 
     int mod_resample_process_pop_down(mod_resample_obj * obj);
 
     int mod_resample_process_pop_up(mod_resample_obj * obj);
 
-    int mod_resample_process_pop_same(mod_resample_obj * obj);    
+    int mod_resample_process_pop_same(mod_resample_obj * obj);
 
     void mod_resample_connect(mod_resample_obj * obj, msg_hops_obj * in, msg_hops_obj * out);
 
 
     void mod_resample_cfg_printf(const mod_resample_cfg * cfg);
 
-#endif
\ No newline at end of file
+#endif
index 311aa7ba4475ffbf804c8b28face88f9fa1a1da3..c36077498696af31bb5691cb2f1f19ec5b2299a5 100644 (file)
@@ -17,7 +17,7 @@
     * 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 <http://www.gnu.org/licenses/>.
     *
@@ -33,7 +33,7 @@
     typedef struct hop2hop_multiplex_obj {
 
         unsigned int hopSize;
-                
+
     } hop2hop_multiplex_obj;
 
     typedef struct hop2hop_buffer_obj {
         unsigned int nSignals;
         unsigned int hopSizeIn;
         unsigned int hopSizeOut;
-        float intervalIn;
-        float intervalOut;
-        float intervalSize;
+        double intervalIn;
+        double intervalOut;
+        double intervalSize;
         unsigned int bufferSize;
-        float ratio;
-        float delta;
+        double ratio;
+        double delta;
 
-        float iWrite;
-        float iRead;
+        double iWrite;
+        double iRead;
 
         float ** array;
 
@@ -66,9 +66,9 @@
 
     void hop2hop_multiplex_destroy(hop2hop_multiplex_obj * obj);
 
-    void hop2hop_multiplex_process(hop2hop_multiplex_obj * obj, const links_obj * links, const hops_obj * src, hops_obj * dest);    
+    void hop2hop_multiplex_process(hop2hop_multiplex_obj * obj, const links_obj * links, const hops_obj * src, hops_obj * dest);
 
-    hop2hop_buffer_obj * hop2hop_buffer_construct_zero(const unsigned int nSignals, const unsigned int hopSizeIn, const unsigned int hopSizeOut, const float ratio);
+    hop2hop_buffer_obj * hop2hop_buffer_construct_zero(const unsigned int nSignals, const unsigned int hopSizeIn, const unsigned int hopSizeOut, const double ratio);
 
     void hop2hop_buffer_destroy(hop2hop_buffer_obj * obj);
 
index 3f6939341410a8f944e4556b533701e13d154b33..7de7235699f2aa1926bb77afae432e20731fea24 100644 (file)
     * 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 <http://www.gnu.org/licenses/>.
     *
     */
-    
+
     #include <module/mod_resample.h>
 
     mod_resample_obj * mod_resample_construct(const mod_resample_cfg * mod_resample_config, const msg_hops_cfg * msg_hops_in_config, const msg_hops_cfg * msg_hops_out_config) {
@@ -47,7 +47,7 @@
         obj->fSout = mod_resample_config->fSout;
         obj->hopSizeIn = msg_hops_in_config->hopSize;
         obj->hopSizeOut = msg_hops_out_config->hopSize;
-        obj->ratio = ((float) obj->fSout) / ((float) obj->fSin);
+        obj->ratio = ((double) obj->fSout) / ((double) obj->fSin);
 
         switch (obj->type) {
 
@@ -55,7 +55,7 @@
 
                 obj->frameSize = obj->hopSizeIn * 2;
                 obj->halfFrameSize = obj->frameSize / 2 + 1;
-                obj->lowPassCut = (unsigned int) floorf(((float) (obj->frameSize/2)) * obj->ratio);
+                obj->lowPassCut = (unsigned int) floor(((double) (obj->frameSize/2)) * obj->ratio);
 
                 obj->hop2hop = hop2hop_buffer_construct_zero(obj->nChannels, obj->hopSizeIn, obj->hopSizeOut, obj->ratio);
 
                 obj->framesSynthesis = frames_construct_zero(obj->nChannels, obj->frameSize);
                 obj->frame2hop = frame2hop_construct_zero(obj->hopSizeIn, obj->frameSize, obj->nChannels);
                 obj->hops = hops_construct_zero(obj->nChannels, obj->hopSizeIn);
-                
+
             break;
 
             case 'u':
 
                 obj->frameSize = obj->hopSizeOut * 2;
                 obj->halfFrameSize = obj->frameSize / 2 + 1;
-                obj->lowPassCut = (unsigned int) floorf(((float) (obj->frameSize/2)) / obj->ratio);
+                obj->lowPassCut = (unsigned int) floor(((double) (obj->frameSize/2)) / obj->ratio);
 
                 obj->hop2hop = hop2hop_buffer_construct_zero(obj->nChannels, obj->hopSizeIn, obj->hopSizeOut, obj->ratio);
-                
+
                 obj->hop2frame = hop2frame_construct_zero(obj->hopSizeOut, obj->frameSize, obj->nChannels);
                 obj->framesAnalysis = frames_construct_zero(obj->nChannels, obj->frameSize);
                 obj->frame2freq = frame2freq_construct_zero(obj->frameSize, obj->halfFrameSize);
@@ -89,7 +89,7 @@
                 obj->freq2frame = freq2frame_construct_zero(obj->frameSize, obj->halfFrameSize);
                 obj->framesSynthesis = frames_construct_zero(obj->nChannels, obj->frameSize);
                 obj->frame2hop = frame2hop_construct_zero(obj->hopSizeOut, obj->frameSize, obj->nChannels);
-                obj->hops = hops_construct_zero(obj->nChannels, obj->hopSizeOut);                
+                obj->hops = hops_construct_zero(obj->nChannels, obj->hopSizeOut);
 
             break;
 
 
                 if (obj->enabled == 1) {
 
-                    hop2frame_process(obj->hop2frame, 
+                    hop2frame_process(obj->hop2frame,
                                       obj->in->hops,
                                       obj->framesAnalysis);
 
                     freq2frame_process(obj->freq2frame,
                                        obj->freqsSynthesis,
                                        obj->framesSynthesis);
-                 
+
                     frame2hop_process(obj->frame2hop,
                                       obj->framesSynthesis,
                                       obj->hops);
             obj->noMorePush = 1;
             rtnValue = -1;
 
-        }        
+        }
 
         return rtnValue;
 
         else {
 
             obj->noMorePush = 1;
-            rtnValue = -1;            
+            rtnValue = -1;
 
         }
 
                 hop2hop_buffer_pop(obj->hop2hop,
                                    obj->hops);
 
-                hop2frame_process(obj->hop2frame, 
+                hop2frame_process(obj->hop2frame,
                                   obj->hops,
                                   obj->framesAnalysis);
 
                 freq2frame_process(obj->freq2frame,
                                    obj->freqsSynthesis,
                                    obj->framesSynthesis);
-             
+
                 frame2hop_process(obj->frame2hop,
                                   obj->framesSynthesis,
                                   obj->out->hops);
index 74a87d422af15bd2faade22919031e694b7ceedc..dd86df16003912cca99d94ebdc8c095e7f1d1aa4 100644 (file)
     * 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 <http://www.gnu.org/licenses/>.
     *
     */
-    
+
     #include <system/hop2hop.h>
 
     hop2hop_multiplex_obj * hop2hop_multiplex_construct_zero(const unsigned int hopSize) {
@@ -62,7 +62,7 @@
 
     }
 
-    hop2hop_buffer_obj * hop2hop_buffer_construct_zero(const unsigned int nSignals, const unsigned int hopSizeIn, const unsigned int hopSizeOut, const float ratio) {
+    hop2hop_buffer_obj * hop2hop_buffer_construct_zero(const unsigned int nSignals, const unsigned int hopSizeIn, const unsigned int hopSizeOut, const double ratio) {
 
         hop2hop_buffer_obj * obj;
         unsigned int iSignal;
         obj->nSignals = nSignals;
         obj->hopSizeIn = hopSizeIn;
         obj->hopSizeOut = hopSizeOut;
-        obj->intervalIn = (float) hopSizeIn;
-        obj->intervalOut = ((float) hopSizeOut) / ratio;
-        obj->intervalSize = 0.0f;
+        obj->intervalIn = (double) hopSizeIn;
+        obj->intervalOut = ((double) hopSizeOut) / ratio;
+        obj->intervalSize = 0.0;
         obj->ratio = ratio;
-        obj->delta = 1.0f / ratio;
+        obj->delta = 1.0 / ratio;
 
         if (obj->intervalIn >= obj->intervalOut) {
 
 
         }
 
-        obj->iRead = 0.0f;
-        obj->iWrite = 0.0f;
+        obj->iRead = 0.0;
+        obj->iWrite = 0.0;
 
         obj->array = (float **) malloc(sizeof(float *) * nSignals);
 
         for (iSignal = 0; iSignal < obj->nSignals; iSignal++) {
-            
+
             obj->array[iSignal] = (float *) malloc(sizeof(float) * obj->bufferSize);
             memset(obj->array[iSignal], 0x00, sizeof(float) * obj->bufferSize);
 
 
         unsigned int iSignal;
 
-        if ((obj->intervalSize + obj->intervalIn) <= ((float) obj->bufferSize)) {
+        if ((obj->intervalSize + obj->intervalIn) <= ((double) obj->bufferSize)) {
 
             iWriteInt = (unsigned int) obj->iWrite;
 
-            if ((obj->iWrite + obj->intervalIn) <= ((float) obj->bufferSize)) {
+            if ((obj->iWrite + obj->intervalIn) <= ((double) obj->bufferSize)) {
 
                 iHop1 = 0;
                 iBuffer1 = iWriteInt;
 
             }
 
-            obj->intervalSize += (float) obj->intervalIn;
+            obj->intervalSize += (double) obj->intervalIn;
 
-            obj->iWrite += (float) obj->intervalIn;
-            obj->iWrite = fmod(obj->iWrite, (float) obj->bufferSize);
+            obj->iWrite += (double) obj->intervalIn;
+            obj->iWrite = fmod(obj->iWrite, (double) obj->bufferSize);
 
         }
 
         unsigned int iSample;
         unsigned int iReadFloor;
         unsigned int iReadCeil;
-        float yFloor;
-        float yCeil;
-        float yDelta;
-        float xDelta;
+        double yFloor;
+        double yCeil;
+        double yDelta;
+        double xDelta;
 
         unsigned int iSignal;
 
 
             for (iSample = 0; iSample < obj->hopSizeOut; iSample++) {
 
-                iReadFloor = (unsigned int) floorf(obj->iRead);
+                iReadFloor = (unsigned int) floor(obj->iRead);
                 iReadCeil = (iReadFloor + 1) % obj->bufferSize;
 
-                xDelta = obj->iRead - floorf(obj->iRead);
-                
+                xDelta = obj->iRead - floor(obj->iRead);
+
                 for (iSignal = 0; iSignal < obj->nSignals; iSignal++) {
 
                     yFloor = obj->array[iSignal][iReadFloor];
                 }
 
                 obj->iRead += obj->delta;
-                obj->iRead = fmod(obj->iRead, (float) obj->bufferSize);
+                obj->iRead = fmod(obj->iRead, (double) obj->bufferSize);
 
             }
 
-            obj->intervalSize -= (float) obj->intervalOut;
+            obj->intervalSize -= (double) obj->intervalOut;
 
         }
 
 
         rtnValue = 0;
 
-        if ((obj->intervalSize + obj->intervalIn) > ((float) obj->bufferSize)) {
-            
+        if ((obj->intervalSize + obj->intervalIn) > ((double) obj->bufferSize)) {
+
             rtnValue = 1;
 
         }
         rtnValue = 0;
 
         if (obj->intervalOut > obj->intervalSize) {
-            
+
             rtnValue = 1;
 
         }