📄 rnddouble.c
字号:
/*************************************************************************** * rnddouble.c - Module to produce some double randoms * ------------------- * begin : 2003 * authors : Linus Gasser * emails : Linus.Gasser@epfl.ch ***************************************************************************//*************************************************************************** * Changes * ------- * date - name - description * 2003/08/22 - ineiti - begin * 04/03/05 - ineiti - adjusted description * **************************************************************************//*************************************************************************** * * * 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. * * * ***************************************************************************//** * Generates random numbers with double-precision. The resolution of the * random-numbers should be in the range of 2^-32 * range. Two outputs exist: * one for real numbers and one for complex numbers. Chose the appropriate * one, and the numbers will be calculated for this output. */#include "spc.h"#define DBG_LVL 4#define MODULO 2147483647#define FACTOR 16807#define LASTXN 127773#define UPTOMOD -2836typedef struct { // The random-numbers will be from -range..range double range; // 1 // This gives the range for both the real and complex part. The // generated numbers will be // -real( range_complex )..real( range_complex ) + // -imag( range_complex )..imag( range_complex ) * I double complex range_complex; // 1 + I // if the seed < 0, then the random-number generator will be initialised // with the time. Else the corresponding seed will be taken for initialisation. int seed; // 0}config_t;typedef struct { // ADD HERE, int, double or char[128]}stats_t;typedef struct { double range; double complex range_complex; int seed;}private_t;/* * The initialisation function, or constructor, * is called the first time this module is instantiated. */int spc_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 config->range = 1; config->range_complex = 1 + I; config->seed = 0; // 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}/* * Every time modules from the outside change the value of a configuration parameter, * this function is called. */int spc_reconfig( swr_sdb_t *context ) { // Definition of variables - don't touch config_t *config; swr_sdb_get_config_struct( context->id, (void**)&config ); private->range = config->range; private->range_complex = config->range_complex; if ( config->seed<=0 ) { private->seed = (int)get_time_usec()%100000000; } else { private->seed = config->seed; } // 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 spc_pdata( swr_sdb_t *context ) { // Definition of variables - don't touch stats_t *stats; double *out; double complex *out2; int i; int times, rest, prod1, prod2; out = buffer_out( 0 ); for ( i=0; i<size_out(0); i++ ) { times = private->seed / LASTXN; rest = private->seed - times * LASTXN; prod1 = times * UPTOMOD; prod2 = rest * FACTOR; private->seed = prod1 + prod2; if (private->seed < 0) private->seed = private->seed + MODULO; out[i] = ( private->seed / ( MODULO / 2. ) - 1 ) * private->range; } PR_DBG( 0, "Going to create %i double complex\n", size_out(1) ); out2 = buffer_out( 1 ); for ( i=0; i<size_out(1) * 2; i++ ) { times = private->seed / LASTXN; rest = private->seed - times * LASTXN; prod1 = times * UPTOMOD; prod2 = rest * FACTOR; private->seed = prod1 + prod2; if (private->seed < 0){ private->seed = private->seed + MODULO; } if ( i % 2 ){ out2[i/2] = ( private->seed / ( MODULO / 2. ) - 1 ) * creal( private->range_complex ); } else { out2[i/2] += ( private->seed / ( MODULO / 2. ) - 1 ) * cimag( private->range_complex ) * I; } } swr_sdb_get_stats_struct( context->id, (void**)&stats ); // Put your code here // ADD HERE swr_sdb_free_stats_struct( context->id, (void**)&stats ); return(0);}int spc_custom_msg(swr_sdb_t *context, swr_usr_msg_t* msg_data, swr_msgq ret ) { spc_pdata( context ); return 0;}/* * This is the `destructor'. */int spc_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 cdb_id;int spc_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( 0, 2, 3, 0 ); 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_DOUBLE( "range" ); UM_CONFIG_DOUBLE_COMPLEX( "range_complex" ); UM_CONFIG_INT( "seed" ); /** * 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,DOUBLE}, 0 ); * UM_OUTPUT( SIG_{U8,SYMBOL_{S16,COMPLEX,MMX},SAMPLE_S12,S32,DOUBLE}, 0 ); */ UM_OUTPUT( SIG_DOUBLE, 0 ); UM_OUTPUT( SIG_DOUBLE_COMPLEX, 0 ); // Initialise the callback-functions. Delete the ones you don't use desc->fn_init = spc_init; desc->fn_reconfigure = spc_reconfig; desc->fn_process_data = spc_pdata; desc->fn_finalize = spc_finalize; desc->fn_custom_msg = spc_custom_msg; // And register the module in the SPM. Change the name! cdb_id = swr_cdb_register_spc( &desc, "rnddouble" ); if ( cdb_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 spc_module_exit( void ) { PR_DBG( 4, "Freeing id: %i\n", cdb_id ); if ( swr_cdb_unregister_spc( cdb_id ) < 0 ) { PR_DBG( 0, "Still in use somewhere\n" ); }}module_init( spc_module_init );module_exit( spc_module_exit );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -