📄 snr_rcv.c
字号:
/*************************************************************************** * snr_rcv.c - Take the slot and calculate the SNR * * Changes * * 03/05/06 - ineiti - added a second input for random ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************//** * This module receives the stream from the matched filter * and the stream of random-signals that are supposed to be * the same that have been used by the snr_send. It then * calculates the amplitude, the variance and the snr. */#include "spc.h"#include "std.h"#define DBG_LVL 0typedef struct { // The QPSK-type, 0->in the corner, 1->on the axes int type; // 0}config_t;typedef struct { // The calculated SNR, in dB double snr; // The calculated amplitude int amp; // The calculated variance int var;}stats_t;typedef struct { int type;}private_t;/* * The initialisation function, or constructor, * is called the first time this module is instantiated. */int rcv_init( swr_sdb_t *context ) { // Begin system-definitions { config_t *config; stats_t *stats; MOD_INC_USE_COUNT; if ( sizeof( private_t ) > 0 ) context->private_data = swr_malloc( sizeof( private_t ) ); swr_sdb_get_config_struct( context->id, (void**)&config ); swr_sdb_get_stats_struct( context->id, (void**)&stats ); // } End of system-definitions SET_STATUS( RESIZE_BOTH ); config->type = 0; stats->snr = -2.3; // Begin system-definitions swr_sdb_free_stats_struct( context->id, (void**)&stats ); swr_sdb_free_config_struct( context->id, (void**)&config ); return 0; // End system-definitions}/* * To configure the outputs * Misused to set the size of the random-module */int rcv_configure_outputs( swr_sdb_t *context ) { // Definition of variables - don't touch config_t *config; swr_sdb_get_config_struct( context->id, (void**)&config ); size_in(1) = ( size_in(0) * 2 + 7 ) / 8; // Definition - don't touch swr_sdb_free_config_struct( context->id, (void**)&config ); return 0;}/* * Every time modules from the outside change the value of a configuration parameter, * this function is called. */int rcv_reconfig( swr_sdb_t *context ) { // Definition of variables - don't touch config_t *config; swr_sdb_get_config_struct( context->id, (void**)&config ); private->type = config->type; // Definition - don't touch swr_sdb_free_config_struct( context->id, (void**)&config ); return 0;}/* * This is the function that implements the `main method' of the class * Every class has got just ONE method/working-mode. */int rcv_pdata( swr_sdb_t *context ) { // Definition of variables - don't touch stats_t *stats; SYMBOL_COMPLEX *in, *buf_rnd; U8 *in_rnd; double signal = 0., noise = 0.; int i; if ( !data_available(0) || !data_available(1) ) { PR_DBG( 4, "Not all data available yet\n" ); return 0; } // Get the sent stream in_rnd = buffer_in(1); buf_rnd = swr_malloc( size_in(0) * sizeof( SYMBOL_COMPLEX ) ); for ( i=0; i<size_in(0); i++ ) { switch( private->type ) { case 0: buf_rnd[i].real = ( 2 * ( *in_rnd & 1 ) - 1 ); *in_rnd = *in_rnd >> 1; buf_rnd[i].imag = ( 2 * ( *in_rnd & 1 ) - 1 ); *in_rnd = *in_rnd >> 1; break; case 1: if ( *in_rnd & 1 ) { *in_rnd = *in_rnd >> 1; buf_rnd[i].real = ( 2 * ( *in_rnd & 1 ) - 1 ); buf_rnd[i].imag = 0; } else { *in_rnd = *in_rnd >> 1; buf_rnd[i].imag = ( 2 * ( *in_rnd & 1 ) - 1 ); buf_rnd[i].real = 0; } *in_rnd = *in_rnd >> 1; break; } // Get the next input-byte of random if ( i && !( i % 4 ) ) { in_rnd++; } } in = buffer_in(0); // Calculate signal energy for ( i=0; i<size_in(0); i++ ) { signal += (double)( in[i].real ) * buf_rnd[i].real + (double)( in[i].imag ) * buf_rnd[i].imag; PR_DBG( 4, "(%5i,%5i) * (%5i,%5i) = %s%i.%i\n", in[i].real, in[i].imag, buf_rnd[i].real, buf_rnd[i].imag, swr_ftosii( signal ) ); } signal = signal / size_in(0); if ( private->type == 0 ) { signal /= 2; } for ( i=0; i<size_in(0); i++ ) { noise += pow( (double)in[i].real - signal * buf_rnd[i].real, 2 ) + pow( (double)in[i].imag - signal * buf_rnd[i].imag, 2 ); } noise /= size_in(0); if ( private->type == 0 ) { signal *= sqrt( 2 ); } PR_DBG( 2, "signal_amp: %i, noise_amp: %g\n", (int)signal, noise ); // And write the snr swr_sdb_get_stats_struct( context->id, (void**)&stats ); if ( ( noise > 0 ) && ( signal > 0 ) ) { stats->snr = 10 * ( log10( signal ) * 2 - log10( noise ) ); } stats->amp = signal; stats->var = noise; swr_free( buf_rnd ); swr_sdb_free_stats_struct( context->id, (void**)&stats ); return(0);}/* * This is the `destructor'. */int rcv_finalize( swr_sdb_t *context ) { if ( sizeof( private_t ) > 0 ) swr_free( private ); MOD_DEC_USE_COUNT; return 0;}/* * This function is called upon "insmod" and is used to register the * different parts of the module to the SPM. */swr_spc_id_t rcv_id;int rcv_module_init(void) { swr_spc_desc_t *desc; /** * Get a description-part from SPM * Give the following parameters: * Input-ports, output-ports, config-params, stat-params */ desc = swr_spc_get_new_desc( 2, 0, 1, 3 ); if ( !desc ) { PR_DBG( 0, "Can't initialise the module. This is BAD!\n" ); return -1; } /** * Define the different parts of config and stats. You have to define * them in the same order as they appear in the structures. The names * can be freely chosen. * * UM_CONFIG_{INT,DOUBLE,STRING128,POINTER}( "name" ); * UM_STATS_{INT,DOUBLE,STRING128,POINTER,BLOCK}( "name" ); */ UM_CONFIG_INT( "type" ); UM_STATS_DOUBLE( "snr" ); UM_STATS_INT( "amp" ); UM_STATS_INT( "var" ); /** * The in- and outputs have also to be defined in the right order. First * port first. The additional flag is not used yet, but it will... * * UM_INPUT( SIG_{U8,SYMBOL_{S16,COMPLEX,MMX},SAMPLE_S12,S32}, 0 ); * UM_OUTPUT( SIG_{U8,SYMBOL_{S16,COMPLEX,MMX},SAMPLE_S12,S32}, 0 ); */ UM_INPUT( SIG_SYMBOL_COMPLEX, 0 ); UM_INPUT( SIG_U8, 0 ); // Initialise the callback-functions. Delete the ones you don't use desc->fn_init = rcv_init; desc->fn_reconfigure = rcv_reconfig; desc->fn_process_data = rcv_pdata; desc->fn_configure_outputs = rcv_configure_outputs; desc->fn_finalize = rcv_finalize; // And register the module in the SPM. Change the name! rcv_id = swr_cdb_register_spc( &desc, "snr_rcv" ); if ( rcv_id == SWR_SPM_INVALID_ID ) { swr_spc_free_desc( desc ); PR_DBG( 0, "Couldn't register the module!\n" ); return 1; } PR_DBG( 4, "Ready\n" ); return 0;}/* * This is called upon rmmod */void rcv_module_exit( void ) { PR_DBG( 4, "Freeing id: %i\n", rcv_id ); if ( swr_cdb_unregister_spc( rcv_id ) < 0 ) { PR_DBG( 0, "Still in use somewhere\n" ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -