diff --git a/csdr.c b/csdr.c index 3f7521c..6ddaa78 100755 --- a/csdr.c +++ b/csdr.c @@ -135,6 +135,7 @@ char usage[]= " duplicate_samples_ntimes_u8_u8 \n" " bpsk_costas_loop_cc \n" " binary_slicer_f_u8\n" +" simple_agc_cc [reference [max_gain]]\n" " ?\n" " =\n" " \n" @@ -2652,7 +2653,7 @@ int main(int argc, char *argv[]) float samples_per_bits; if(argc<=2) return badsyntax("need required parameter (samples_per_bits)"); sscanf(argv[2],"%f",&samples_per_bits); - if(samples_per_bits<=0) badsyntax("samples_per_bits should be >= 0"); + if(samples_per_bits<=0) badsyntax("samples_per_bits should be > 0"); bpsk_costas_loop_state_t state = init_bpsk_costas_loop_cc(samples_per_bits); @@ -2669,6 +2670,36 @@ int main(int argc, char *argv[]) } } + if(!strcmp(argv[1],"simple_agc_cc")) // [reference [max_gain]] + { + float rate; + if(argc<=2) return badsyntax("need required parameter (rate)"); + sscanf(argv[2],"%f",&rate); + if(rate<=0) badsyntax("rate should be > 0"); + + float reference = 1.; + if(argc>3) sscanf(argv[3],"%f",&reference); + if(reference<=0) badsyntax("reference should be > 0"); + + float max_gain = 65535.; + if(argc>4) sscanf(argv[4],"%f",&max_gain); + if(max_gain<=0) badsyntax("max_gain should be > 0"); + + float current_gain = 1.; + + if(!initialize_buffers()) return -2; + sendbufsize(the_bufsize); + + for(;;) + { + FEOF_CHECK; + FREAD_C; + simple_agc_cc((complexf*)input_buffer, (complexf*)output_buffer, the_bufsize, rate, reference, max_gain, ¤t_gain); + FWRITE_C; + TRY_YIELD; + } + } + if(!strcmp(argv[1],"none")) { return 0; diff --git a/libcsdr.c b/libcsdr.c index 2121f52..14f997e 100644 --- a/libcsdr.c +++ b/libcsdr.c @@ -1990,6 +1990,26 @@ void bpsk_costas_loop_cc(complexf* input, complexf* output, int input_size, bpsk } } +void simple_agc_cc(complexf* input, complexf* output, int input_size, float rate, float reference, float max_gain, float* current_gain) +{ + float rate_1minus=1-rate; + int debugn = 0; + for(int i=0;imax_gain) ideal_gain = max_gain; + if(ideal_gain<=0) ideal_gain = 0; + //*current_gain += (ideal_gain-(*current_gain))*rate; + *current_gain = (ideal_gain-(*current_gain))*rate + (*current_gain)*rate_1minus; + if(debugn<100) fprintf(stderr, "cgain: %g\n", *current_gain), debugn++; + output[i].i=(*current_gain)*input[i].i; + output[i].q=(*current_gain)*input[i].q; + } +} + + + /* _____ _ _ diff --git a/libcsdr.h b/libcsdr.h index 1c15f90..988bba8 100644 --- a/libcsdr.h +++ b/libcsdr.h @@ -337,3 +337,4 @@ typedef struct bpsk_costas_loop_state_s bpsk_costas_loop_state_t init_bpsk_costas_loop_cc(float samples_per_bits); void bpsk_costas_loop_cc(complexf* input, complexf* output, int input_size, bpsk_costas_loop_state_t* state); +void simple_agc_cc(complexf* input, complexf* output, int input_size, float rate, float reference, float max_gain, float* current_gain);