Added firdes_carrier_c

This commit is contained in:
ha7ilm 2017-04-08 12:39:52 +02:00
parent 50c885748e
commit cdc2996dcc
3 changed files with 75 additions and 0 deletions

46
csdr.c
View file

@ -136,6 +136,7 @@ char usage[]=
" bpsk_costas_loop_cc <samples_per_bits>\n" " bpsk_costas_loop_cc <samples_per_bits>\n"
" binary_slicer_f_u8\n" " binary_slicer_f_u8\n"
" simple_agc_cc <rate> [reference [max_gain]]\n" " simple_agc_cc <rate> [reference [max_gain]]\n"
" firdes_carrier_c <rate> <length> [window [--octave]]\n"
" ?<search_the_function_list>\n" " ?<search_the_function_list>\n"
" =<evaluate_python_expression>\n" " =<evaluate_python_expression>\n"
" \n" " \n"
@ -2700,6 +2701,51 @@ int main(int argc, char *argv[])
} }
} }
if(!strcmp(argv[1],"firdes_carrier_c")) //<rate> <length> [window [--octave]]
{
//Process the params
if(argc<=3) return badsyntax("need required parameters (rate, length)");
float rate;
sscanf(argv[2],"%g",&rate);
int length;
sscanf(argv[3],"%d",&length);
if(length%2==0) return badsyntax("number of symmetric FIR filter taps should be odd");
window_t window = WINDOW_DEFAULT;
if(argc>=5)
{
window=firdes_get_window_from_string(argv[4]);
}
else fprintf(stderr,"firdes_carrier_c: window = %s\n",firdes_get_string_from_window(window));
int octave=(argc>=6 && !strcmp("--octave",argv[5]));
complexf* taps=(complexf*)malloc(sizeof(complexf)*length);
//Make the filter
firdes_carrier_c(taps, length, rate, window);
//Do the output
if(octave) printf("taps=[");
for(int i=0;i<length;i++) printf("(%g)+(%g)*i ",iof(taps,i),qof(taps,i));
int fft_length=1024;
while(fft_length<length) fft_length*=2;
//if(octave) printf("];\n");
if(octave) printf(
"];figure(\"Position\",[0 0 1000 1000]);fser=fft([taps,zeros(1,%d)]);ampl=abs(fser).^2;halfindex=floor(1+size(ampl)(2)/2);\n"
"amplrev=[ampl(halfindex:end),ampl(1:halfindex)];\n" //we have to swap the output of FFT
"subplot(2,1,1);plot(amplrev);\n"
"subplot(2,1,2);plot(arg(fser));\n"
"#figure(2);freqz(taps);\n"
"#figur(3);plot3(taps);\n",fft_length-length);
//Wait forever, so that octave won't close just after popping up the window.
//You can close it with ^C.
if(octave) { fflush(stdout); getchar(); }
return 0;
}
if(!strcmp(argv[1],"none")) if(!strcmp(argv[1],"none"))
{ {
return 0; return 0;

View file

@ -2009,6 +2009,34 @@ void simple_agc_cc(complexf* input, complexf* output, int input_size, float rate
} }
} }
void firdes_carrier_c(complexf* output, int length, float rate, window_t window)
{
int middle=length/2;
float phase = 0, phase_addition = rate*M_PI*2;
float (*window_function)(float) = firdes_get_window_kernel(window);
for(int i=0; i<length; i++) //@@firdes_carrier_c: calculate taps
{
e_powj(&output[i], phase);
float window_multiplier = window_function(fabs((float)(middle-i)/middle));
output[i].i *= window_multiplier;
output[i].q *= window_multiplier;
phase += phase_addition;
while(phase>2*M_PI) phase-=2*M_PI;
}
//Normalize filter kernel
float sum=0;
for(int i=0;i<length;i++) //@firdes_carrier_c: normalize pass 1
{
sum+=sqrt(output[i].i*output[i].i + output[i].q*output[i].q);
}
for(int i=0;i<length;i++) //@firdes_carrier_c: normalize pass 2
{
output[i].i/=sum;
output[i].q/=sum;
}
}

View file

@ -338,3 +338,4 @@ typedef struct bpsk_costas_loop_state_s
bpsk_costas_loop_state_t init_bpsk_costas_loop_cc(float samples_per_bits); 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 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); void simple_agc_cc(complexf* input, complexf* output, int input_size, float rate, float reference, float max_gain, float* current_gain);
void firdes_carrier_c(complexf* output, int length, float rate, window_t window);