Changed memory allocation in fsk.c to allow for
authorbaobrien <baobrien@01035d8c-6547-0410-b346-abe4f91aad63>
Sat, 13 Feb 2016 23:44:07 +0000 (23:44 +0000)
committerbaobrien <baobrien@01035d8c-6547-0410-b346-abe4f91aad63>
Sat, 13 Feb 2016 23:44:07 +0000 (23:44 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@2700 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/src/fsk.c
codec2-dev/src/fsk_demod.c
codec2-dev/src/fsk_mod.c

index 8333f8edf52c24348805b1b718942f98817c82bb..9456257ca87998d969f1015e2fffdd0e98cd21b7 100644 (file)
@@ -38,6 +38,9 @@
 /* This needs square roots, may take more cpu time than it's worth */
 #define EST_EBNO
 
+/* This is a flag to make the mod/demod allocate their memory on the stack instead of the heap */
+/* At large sample rates, there's not enough stack space to run the demod */
+#define DEMOD_ALLOC_STACK
 /*---------------------------------------------------------------------------*\
 
                                INCLUDES
@@ -377,8 +380,15 @@ void fsk_demod_freq_est(struct FSK *fsk, float fsk_in[],float *freqs,int M){
     
     /* Array to do complex FFT from using kiss_fft */
     /* It'd probably make more sense here to use kiss_fftr */
+    
+    #ifdef DEMOD_ALLOC_STACK
     kiss_fft_scalar *fftin = (kiss_fft_scalar*)alloca(sizeof(kiss_fft_scalar)*Ndft);
     kiss_fft_cpx *fftout = (kiss_fft_cpx*)alloca(sizeof(kiss_fft_cpx)*(Ndft/2)+1);
+    #else
+    kiss_fft_scalar *fftin  = (kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar)*(Ndft+4));
+    kiss_fft_cpx    *fftout = (kiss_fft_cpx*)   malloc(sizeof(kiss_fft_cpx)  *((Ndft/2)+4));
+    #endif
+
     fft_samps = Ndft;
     
     f_min  = (fsk->est_min*Ndft)/Fs;
@@ -466,6 +476,11 @@ void fsk_demod_freq_est(struct FSK *fsk, float fsk_in[],float *freqs,int M){
        for(i=0; i<M; i++){
                freqs[i] = (float)(freqi[i])*((float)Fs/(float)Ndft);
        }
+    
+    #ifndef DEMOD_ALLOC_STACK
+    free(fftin);
+    free(fftout);
+    #endif
 }
 
 void fsk2_demod(struct FSK *fsk, uint8_t rx_bits[], float fsk_in[]){
@@ -501,6 +516,8 @@ void fsk2_demod(struct FSK *fsk, uint8_t rx_bits[], float fsk_in[]){
     fsk_demod_freq_est(fsk,fsk_in,f_est,M);
     modem_probe_samp_f("t_f_est",f_est,M);
     
+    /* allocate memory for the integrated samples */
+    #ifdef DEMOD_ALLOC_STACK
     /* allocate memory for the integrated samples */
     /* Note: This must be kept after fsk_demod_freq_est for memory usage reasons */
     f1_int = (COMP*) alloca(sizeof(COMP)*(nsym+1)*P);
@@ -509,6 +526,13 @@ void fsk2_demod(struct FSK *fsk, uint8_t rx_bits[], float fsk_in[]){
     /* Allocate circular buffers for integration */
     f1_intbuf = (COMP*) alloca(sizeof(COMP)*Ts);
     f2_intbuf = (COMP*) alloca(sizeof(COMP)*Ts);
+    #else
+    f1_int = (COMP*) malloc(sizeof(COMP)*(nsym+1)*P);
+    f2_int = (COMP*) malloc(sizeof(COMP)*(nsym+1)*P);
+    
+    f1_intbuf = (COMP*) malloc(sizeof(COMP)*Ts);
+    f2_intbuf = (COMP*) malloc(sizeof(COMP)*Ts);
+    #endif
     
     /* If this is the first run, we won't have any valid f_est */
     if(fsk->f1_est<1){
@@ -722,6 +746,13 @@ void fsk2_demod(struct FSK *fsk, uint8_t rx_bits[], float fsk_in[]){
     fsk->EbNodB = 1;
     #endif
     
+    #ifndef DEMOD_ALLOC_STACK
+    free(f1_int);
+    free(f2_int);
+    free(f1_intbuf);
+    free(f2_intbuf);
+    #endif
+    
     /* Dump some internal samples */
     modem_probe_samp_f("t_EbNodB",&(fsk->EbNodB),1);
     modem_probe_samp_f("t_ppm",&(fsk->ppm),1);
@@ -765,7 +796,8 @@ void fsk4_demod(struct FSK *fsk, uint8_t rx_bits[], float fsk_in[]){
     /* Estimate tone frequencies */
     fsk_demod_freq_est(fsk,fsk_in,f_est,M);
     modem_probe_samp_f("t_f_est",f_est,M);
-    
+
+    #ifdef DEMOD_ALLOC_STACK
     /* allocate memory for the integrated samples */
     /* Note: This must be kept after fsk_demod_freq_est for memory usage reasons */
     f1_int = (COMP*) alloca(sizeof(COMP)*(nsym+1)*P);
@@ -778,7 +810,17 @@ void fsk4_demod(struct FSK *fsk, uint8_t rx_bits[], float fsk_in[]){
     f2_intbuf = (COMP*) alloca(sizeof(COMP)*Ts);
     f3_intbuf = (COMP*) alloca(sizeof(COMP)*Ts);
     f4_intbuf = (COMP*) alloca(sizeof(COMP)*Ts);
-    
+    #else
+    f1_int = (COMP*) malloc(sizeof(COMP)*(nsym+1)*P);
+    f2_int = (COMP*) malloc(sizeof(COMP)*(nsym+1)*P);
+    f3_int = (COMP*) malloc(sizeof(COMP)*(nsym+1)*P);
+    f4_int = (COMP*) malloc(sizeof(COMP)*(nsym+1)*P);
+    
+    f1_intbuf = (COMP*) malloc(sizeof(COMP)*Ts);
+    f2_intbuf = (COMP*) malloc(sizeof(COMP)*Ts);
+    f3_intbuf = (COMP*) malloc(sizeof(COMP)*Ts);
+    f4_intbuf = (COMP*) malloc(sizeof(COMP)*Ts);
+    #endif
     /* If this is the first run, we won't have any valid f_est */
     if(fsk->f1_est<1){
                fsk->f1_est = f_est[0];
@@ -1070,6 +1112,17 @@ void fsk4_demod(struct FSK *fsk, uint8_t rx_bits[], float fsk_in[]){
     fsk->EbNodB = 0;
     #endif
     
+    #ifndef DEMOD_ALLOC_STACK
+    free(f1_int);
+    free(f2_int);
+    free(f3_int);
+    free(f4_int);
+    free(f1_intbuf);
+    free(f2_intbuf);
+    free(f3_intbuf);
+    free(f4_intbuf);
+    #endif
+    
     /* Dump some internal samples */
     modem_probe_samp_f("t_EbNodB",&(fsk->EbNodB),1);
     modem_probe_samp_f("t_ppm",&(fsk->ppm),1);
index 9a21d636763b74c190f33b2f2e379e4b35116460..5368e9900b6507a36971bf2427a69a89f2e5386b 100644 (file)
@@ -94,9 +94,9 @@ int main(int argc,char *argv[]){
     }
     
     /* allocate buffers for processing */
-    bitbuf = (uint8_t*)alloca(sizeof(uint8_t)*fsk->Nbits);
-    rawbuf = (int16_t*)alloca(sizeof(int16_t)*(fsk->N+fsk->Ts*2));
-    modbuf = (float*)alloca(sizeof(float)*(fsk->N+fsk->Ts*2));
+    bitbuf = (uint8_t*)malloc(sizeof(uint8_t)*fsk->Nbits);
+    rawbuf = (int16_t*)malloc(sizeof(int16_t)*(fsk->N+fsk->Ts*2));
+    modbuf = (float*)malloc(sizeof(float)*(fsk->N+fsk->Ts*2));
     
     /* Demodulate! */
     while( fread(rawbuf,sizeof(int16_t),fsk_nin(fsk),fin) == fsk_nin(fsk) ){
@@ -117,6 +117,9 @@ int main(int argc,char *argv[]){
        }
     }
     
+    free(bitbuf);
+    free(rawbuf);
+    free(modbuf);
     
     modem_probe_close();
     cleanup:
index c5a399911c8a795dda05d44fdac79969ad0d16aa..cb890c4ce89d7871c844307f9a699ae6ae52b0e7 100644 (file)
@@ -76,9 +76,9 @@ int main(int argc,char *argv[]){
     }
     
     /* allocate buffers for processing */
-    bitbuf = (uint8_t*)alloca(sizeof(uint8_t)*fsk->Nbits);
-    rawbuf = (int16_t*)alloca(sizeof(int16_t)*fsk->N);
-    modbuf = (float*)alloca(sizeof(float)*fsk->N);
+    bitbuf = (uint8_t*)malloc(sizeof(uint8_t)*fsk->Nbits);
+    rawbuf = (int16_t*)malloc(sizeof(int16_t)*fsk->N);
+    modbuf = (float*)malloc(sizeof(float)*fsk->N);
     
     /* Modulate! */
     while( fread(bitbuf,sizeof(uint8_t),fsk->Nbits,fin) == fsk->Nbits ){
@@ -93,6 +93,9 @@ int main(int argc,char *argv[]){
                        fflush(fout);
                }
     }
+    free(bitbuf);
+    free(rawbuf);
+    free(modbuf);
     
     cleanup:
     fclose(fin);