From 57790eb9af6858e3e2975290fa41e97d803e8cf2 Mon Sep 17 00:00:00 2001 From: ha7ilm Date: Fri, 28 Apr 2017 19:34:55 +0200 Subject: [PATCH] Added agwn_cc --- README.md | 34 +- csdr.c | 46 ++- grc_tests/test_awgn.grc | 669 ++++++++++++++++++++++++++++++++++++++++ libcsdr.c | 56 +++- libcsdr.h | 9 +- 5 files changed, 799 insertions(+), 15 deletions(-) create mode 100644 grc_tests/test_awgn.grc diff --git a/README.md b/README.md index 576e757..6da9ce1 100755 --- a/README.md +++ b/README.md @@ -832,7 +832,7 @@ For this input, the output of `psk31_varicode_encoder_u8_u8` will be the followi Syntax: - repeat_u8 [resonator_rate × N]\n" + csdr repeat_u8 [resonator_rate × N]\n" It repeatedly outputs a set of data bytes (given with decimal numbers). @@ -845,6 +845,28 @@ For example, `csdr repeat_u8 1 1 0 0` will output: ---- +### [noise_f](#noise_f) + +Syntax: + + csdr noise_f + +It outputs white noise within the range [-1.0, 1.0]. + +---- + +### [awgn_cc](#awgn_cc) + +Syntax: + + csdr awgn_cc [--snrshow] + +It adds white noise with the given SNR to a signal assumed to be of 0 dB power. + +If the `--snrshow` switch is given, it also shows the actual SNR based on the calculated power of signal and noise components. + +---- + ### [?](#search_the_function_list) Syntax: @@ -879,6 +901,16 @@ Another solution is using single quotes to wrap the expression: csdr shift_addition_cc $(csdr '=(1200+300)/2400000.') +Current version of `csdr` executes the following python script for this function: + +```python +import os, sys +from math import * +print +``` + +This means that one can also call math functions like `sqrt()`. + #### Control via pipes Some parameters can be changed while the `csdr` process is running. To achieve this, some `csdr` functions have special parameters. You have to supply a fifo previously created by the `mkfifo` command. Processing will only start after the first control command has been received by `csdr` over the FIFO. diff --git a/csdr.c b/csdr.c index 686b3b1..7842726 100755 --- a/csdr.c +++ b/csdr.c @@ -140,6 +140,8 @@ char usage[]= " firdes_resonator_c [window [--octave]]\n" " resonators_fir_cc [resonator_rate × N]\n" " repeat_u8 \n" +" noise_f\n" +" awgn_cc [--snrshow]\n" " ?\n" " =\n" " \n" @@ -2892,25 +2894,55 @@ int main(int argc, char *argv[]) if(!strcmp(argv[1], "awgn_cc")) { - if(argc<=2) badsyntax("no data to repeat"); + FILE* urandom = init_get_random_samples_f(); + if(argc<=2) badsyntax("required parameter is missing."); float snr_db = 0; sscanf(argv[2],"%f",&snr_db); + int snrshow = 0; + if(argc>=4 && !strcmp(argv[3],"--snrshow")) snrshow = 1; float signal_amplitude_per_noise = pow(10,snr_db/20); float a_signal=signal_amplitude_per_noise/(signal_amplitude_per_noise+1.0); float a_noise=1.0/(signal_amplitude_per_noise+1.0); - - + fprintf(stderr, "csdr awgn_cc: a_signal = %f, a_noise = %f\n", a_signal, a_noise); + if(!initialize_buffers()) return -2; + sendbufsize(the_bufsize); + complexf* awgn_buffer = (complexf*)malloc(sizeof(complexf)*the_bufsize); + for(;;) + { + FEOF_CHECK; + FREAD_C; + //get_awgn_samples_f((float*)awgn_buffer, the_bufsize*2, urandom); + get_random_gaussian_samples_c(awgn_buffer, the_bufsize, urandom); + /*if(snrshow) + { + float power_signal = total_logpower_cf((complexf*)input_buffer, the_bufsize); + float power_noise = total_logpower_cf(awgn_buffer, the_bufsize); + fprintf(stderr, "csdr awgn_cc: at the beginning, power_signal = %f dB, power_noise = %f dB\n", power_signal, power_noise); + }*/ + gain_ff(input_buffer, input_buffer, the_bufsize*2, a_signal); + gain_ff((float*)awgn_buffer, (float*)awgn_buffer, the_bufsize*2, a_noise*0.707); + if(snrshow) + { + float power_signal = total_logpower_cf((complexf*)input_buffer, the_bufsize); + float power_noise = total_logpower_cf(awgn_buffer, the_bufsize); + //fprintf(stderr, "csdr awgn_cc: after gain_ff, power_signal = %f dB, power_noise = %f dB\n", power_signal, power_noise); + fprintf(stderr, "csdr awgn_cc: SNR = %f dB\n", power_signal - power_noise); + } + add_ff(input_buffer, (float*)awgn_buffer, (float*)output_buffer, the_bufsize*2); + FWRITE_C; + TRY_YIELD; + } } - if(!strcmp(argv[1], "noise_f")) + if(!strcmp(argv[1], "uniform_noise_f")) { - FILE* urandom = init_get_awgn_samples_f(); + FILE* urandom = init_get_random_samples_f(); if(!initialize_buffers()) return -2; sendbufsize(the_bufsize); for(;;) { FEOF_CHECK; - get_awgn_samples_f(output_buffer, the_bufsize, urandom); + get_random_samples_f(output_buffer, the_bufsize, urandom); FWRITE_R; TRY_YIELD; } @@ -2933,7 +2965,7 @@ int main(int argc, char *argv[]) if(argv[1][0]=='=') { char buffer[100]; - snprintf(buffer, 100-1, "python -c \"print %s\"", argv[1]+1); + snprintf(buffer, 100-1, "python -c \"import os, sys\nfrom math import *\nprint %s\"", argv[1]+1); system(buffer); return 0; } diff --git a/grc_tests/test_awgn.grc b/grc_tests/test_awgn.grc new file mode 100644 index 0000000..a55b924 --- /dev/null +++ b/grc_tests/test_awgn.grc @@ -0,0 +1,669 @@ + + + + Sun Nov 16 15:12:31 2014 + + options + + author + + + + window_size + 1280, 1024 + + + category + Custom + + + comment + + + + description + + + + _enabled + True + + + _coordinate + (10, 10) + + + _rotation + 0 + + + generate_options + wx_gui + + + id + top_block + + + max_nouts + 0 + + + realtime_scheduling + + + + run_options + prompt + + + run + True + + + thread_safe_setters + + + + title + + + + + variable_slider + + comment + + + + converver + float_converter + + + value + 1 + + + _enabled + True + + + _coordinate + (120, 147) + + + _rotation + 0 + + + grid_pos + + + + id + amplitude + + + label + + + + max + 2 + + + min + 0 + + + notebook + + + + num_steps + 100 + + + style + wx.SL_HORIZONTAL + + + + variable_slider + + comment + + + + converver + float_converter + + + value + 0 + + + _enabled + True + + + _coordinate + (8, 147) + + + _rotation + 0 + + + grid_pos + + + + id + frequency + + + label + + + + max + samp_rate/2 + + + min + -samp_rate/2 + + + notebook + + + + num_steps + 100 + + + style + wx.SL_HORIZONTAL + + + + variable + + comment + + + + _enabled + True + + + _coordinate + (8, 83) + + + _rotation + 0 + + + id + samp_rate + + + value + 40e3 + + + + analog_sig_source_x + + amp + 1 + + + alias + + + + comment + + + + affinity + + + + _enabled + True + + + freq + frequency + + + _coordinate + (184, 11) + + + _rotation + 0 + + + id + analog_sig_source_x_0 + + + maxoutbuf + 0 + + + minoutbuf + 0 + + + offset + 0 + + + type + complex + + + samp_rate + samp_rate + + + waveform + analog.GR_COS_WAVE + + + + blocks_multiply_const_vxx + + alias + + + + comment + + + + const + amplitude + + + affinity + + + + _enabled + True + + + _coordinate + (344, 43) + + + _rotation + 0 + + + id + blocks_multiply_const_vxx_0 + + + type + complex + + + maxoutbuf + 0 + + + minoutbuf + 0 + + + vlen + 1 + + + + blocks_throttle + + alias + + + + comment + + + + affinity + + + + _enabled + True + + + _coordinate + (496, 43) + + + _rotation + 0 + + + id + blocks_throttle_0_0 + + + ignoretag + True + + + maxoutbuf + 0 + + + minoutbuf + 0 + + + samples_per_second + samp_rate + + + type + complex + + + vlen + 1 + + + + ha5kfu_execproc_xx + + alias + + + + commandline + csdr awgn_cc 10 --snrshow + + + comment + + + + affinity + + + + _enabled + 1 + + + _coordinate + (344, 275) + + + _rotation + 0 + + + id + ha5kfu_execproc_xx_0_0 + + + maxoutbuf + 0 + + + minoutbuf + 0 + + + type + cc + + + + wxgui_fftsink2 + + avg_alpha + 0 + + + average + False + + + baseband_freq + 0 + + + alias + + + + comment + + + + affinity + + + + _enabled + True + + + fft_size + 1024 + + + freqvar + None + + + _coordinate + (616, 203) + + + _rotation + 0 + + + grid_pos + + + + id + wxgui_fftsink2_0_0 + + + notebook + + + + peak_hold + False + + + ref_level + 0 + + + ref_scale + 2.0 + + + fft_rate + 15 + + + samp_rate + samp_rate + + + title + FFT Plot + + + type + complex + + + win_size + + + + win + None + + + y_divs + 10 + + + y_per_div + 10 + + + + wxgui_fftsink2 + + avg_alpha + 0 + + + average + False + + + baseband_freq + 0 + + + alias + + + + comment + + + + affinity + + + + _enabled + True + + + fft_size + 1024 + + + freqvar + None + + + _coordinate + (856, 75) + + + _rotation + 0 + + + grid_pos + + + + id + wxgui_fftsink2_0_0_0 + + + notebook + + + + peak_hold + False + + + ref_level + 0 + + + ref_scale + 2.0 + + + fft_rate + 15 + + + samp_rate + samp_rate + + + title + FFT Plot + + + type + complex + + + win_size + + + + win + None + + + y_divs + 10 + + + y_per_div + 10 + + + + analog_sig_source_x_0 + blocks_multiply_const_vxx_0 + 0 + 0 + + + blocks_multiply_const_vxx_0 + blocks_throttle_0_0 + 0 + 0 + + + blocks_throttle_0_0 + ha5kfu_execproc_xx_0_0 + 0 + 0 + + + blocks_throttle_0_0 + wxgui_fftsink2_0_0_0 + 0 + 0 + + + ha5kfu_execproc_xx_0_0 + wxgui_fftsink2_0_0 + 0 + 0 + + diff --git a/libcsdr.c b/libcsdr.c index 0f97fc6..304870c 100755 --- a/libcsdr.c +++ b/libcsdr.c @@ -1306,6 +1306,12 @@ void log_ff(float* input, float* output, int size, float add_db) { for(int i=0;i1); + iof(output, i)=(1/0.82)*iof(output, i); + qof(output, i)=(1/0.82)*qof(output, i); + } +} +*/ + + +int deinit_get_random_samples_f(FILE* status) { return fclose(status); } +float* add_ff(float* input1, float* input2, float* output, int input_size) +{ + for(int i=0;i