Changeset 5742

Show
Ignore:
Timestamp:
11/14/07 19:00:38 (6 years ago)
Author:
jgaeddert
Message:

finalizing modulator initialization

Location:
SigProc/branches/SigProc-c
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • SigProc/branches/SigProc-c/Makefile.am

    r5722 r5742  
    3737        tests/dot_product_testsuite.h                   \ 
    3838        tests/pack_testsuite.h                          \ 
    39         tests/gray_codec_testsuite.h 
     39        tests/gray_codec_testsuite.h                    \ 
     40        tests/modem_energy_scaling_testsuite.h 
    4041#       tests/modem_testsuite.h 
    4142 
  • SigProc/branches/SigProc-c/src/modem.c

    r5741 r5742  
    3030} 
    3131 
    32 void modulator_calculate_levels(modulation_scheme_t *ms, float *ref_i, float *ref_q) 
    33 { 
    34     switch (ms->scheme) { 
     32void modulator_calculate_levels(modulation_scheme ms, unsigned int m, float *ref_i, float *ref_q) 
     33{ 
     34    switch (ms) { 
    3535     
    36     case PSK: 
    37         modulator_calculate_levels_psk(ms, ref_i, ref_q); 
    38         return; 
    39     case QAM: 
    40         modulator_calculate_levels_rqam(ms, ref_i, ref_q); 
    41         return; 
    42     case PAM: 
    43         modulator_calculate_levels_pam(ms, ref_i, ref_q); 
     36    case MOD_PSK: 
     37        modulator_calculate_levels_psk(ms, m, ref_i, ref_q); 
     38        break; 
     39    case MOD_QAM: 
     40        modulator_calculate_levels_rqam(ms, m, ref_i, ref_q); 
     41        break; 
     42    case MOD_PAM: 
     43        modulator_calculate_levels_pam(ms, m, ref_i, ref_q); 
     44        break; 
    4445    default: 
    4546        perror("ERROR: modulator_calculate_levels, unknown/unsupported modulation scheme\n"); 
    46         return; 
    47     } 
    48 } 
    49  
    50 void modulator_calculate_levels_psk(modulation_scheme_t *ms, float *ref_i, float *ref_q) 
     47        break; 
     48    } 
     49    modulator_scale_levels(ms, m, ref_i, ref_q); 
     50     
     51} 
     52 
     53void modulator_calculate_levels_psk(modulation_scheme ms, unsigned int m, float *ref_i, float *ref_q) 
    5154{ 
    5255    unsigned int i; 
     56    unsigned int M = 1<<m; 
    5357    float t; 
    54     float two_pi_inv_M = 2*M_PI/ms->M; 
    55     for (i=0; i<ms->M; i++) { 
     58    float two_pi_inv_M = 2*M_PI/M; 
     59    for (i=0; i<M; i++) { 
    5660        t = i*two_pi_inv_M; 
    5761        ref_i[i] = cosf(t); 
     
    6064} 
    6165 
    62 void modulator_calculate_levels_pam(modulation_scheme_t *ms, float *ref_i, float *ref_q) 
    63 { 
    64 } 
    65  
    66 void modulator_calculate_levels_rqam(modulation_scheme_t *ms, float *ref_i, float *ref_q) 
    67 { 
     66void modulator_calculate_levels_pam(modulation_scheme ms, unsigned int m, float *ref_i, float *ref_q) 
     67{ 
     68} 
     69 
     70void modulator_calculate_levels_rqam(modulation_scheme ms, unsigned int m, float *ref_i, float *ref_q) 
     71{ 
     72    int M = 1<<m; 
    6873    int i, I; 
    6974    int q, Q; 
    70     if ((ms->m % 2)==0) { 
     75    if ( (m%2)==0 ) { 
    7176        // square QAM 
    72         I = 1<<(ms->m/2); 
     77        I = 1<<(m/2); 
    7378        Q = I; 
    7479    } else { 
    7580        // rectangular QAM 
    76         I = 1<<((ms->m+1)/2); 
    77         Q = 1<<((ms->m-1)/2); 
    78     } 
    79     assert(I*Q==ms->M); 
     81        I = 1<<((m+1)/2); 
     82        Q = 1<<((m-1)/2); 
     83    } 
     84    assert(I*Q==M); 
    8085 
    8186    unsigned int n=0; 
     
    8994} 
    9095 
    91 float modulator_get_scaling_factor(modulation_scheme_t * ms) 
    92 { 
    93     switch (ms->scheme) { 
     96float modulator_get_scaling_factor(modulation_scheme _ms, unsigned int _M) 
     97{ 
     98    printf("modulator_get_scaling_factor invoked with ms=%d\n", _ms); 
     99    switch (_ms) { 
    94100 
    95101    // PSK 
    96     case PSK: 
     102    case MOD_PSK: 
    97103        return PSK_ALPHA; 
    98104 
    99105    // QAM 
    100     case QAM: 
    101         switch (ms->M) { 
     106    case MOD_QAM: 
     107        switch (_M) { 
    102108        case 4:   return QAM4_ALPHA; 
    103109        //case 8:   return RQAM8_ALPHA; 
     
    108114        case 256: return QAM256_ALPHA; break; 
    109115        default: 
    110             perror("ERROR! Unsupported QAM level\n"); 
     116            perror("ERROR! modulator_get_scaling_factor unsupported QAM level\n"); 
    111117            break; 
    112118        } 
     
    114120 
    115121    // PAM 
    116     case PAM: 
    117         switch (ms->M) { 
     122    case MOD_PAM: 
     123        switch (_M) { 
    118124        case 4:   return PAM4_ALPHA; 
    119125        case 8:   return PAM8_ALPHA; 
    120126        case 16:  return PAM16_ALPHA; 
    121127        default: 
    122             perror("ERROR! Unsupported QAM level\n"); 
     128            perror("ERROR! modulator_get_scaling_factor, unsupported PAM level\n"); 
    123129            break; 
    124130        } 
     
    126132 
    127133    default: 
    128         perror("ERROR! Unknown/unsupported modulation scheme\n"); 
     134        perror("ERROR! modulator_get_scaling_factor, unknown/unsupported modulation scheme\n"); 
    129135        break; 
    130136 
     
    133139} 
    134140 
    135 void modulator_scale_levels(modulation_scheme_t *ms, float *ref_i, float *ref_q) 
    136 { 
    137     float alpha = modulator_get_scaling_factor(ms); 
     141void modulator_scale_levels(modulation_scheme ms, unsigned int m, float *ref_i, float *ref_q) 
     142{ 
     143    unsigned int M = 1<<m; 
     144    float alpha = modulator_get_scaling_factor(ms, M); 
     145    printf("\nalpha = %f\n", alpha); 
    138146 
    139147    unsigned int i; 
    140     for (i=0; i<ms->M; i++) { 
     148    for (i=0; i<M; i++) { 
    141149        ref_i[i] *= alpha; 
    142150        ref_q[i] *= alpha; 
     
    148156    modulator * mod; 
    149157    mod = (modulator*) malloc( sizeof(modulator) ); 
    150     mod->levels_i = (int*) calloc( _M, sizeof(int) ); 
    151     mod->levels_q = (int*) calloc( _M, sizeof(int) ); 
     158    mod->levels_i = (float*) calloc( _M, sizeof(float) ); 
     159    mod->levels_q = (float*) calloc( _M, sizeof(float) ); 
    152160 
    153161    return mod; 
    154162} 
    155163 
    156 void modulator_init(modulator *_m, modulation_scheme_t mod, int _e) 
    157 { 
    158  
     164void free_modulator(modulator * mod) 
     165{ 
     166    free(mod->levels_i); 
     167    free(mod->levels_q); 
     168    free(mod); 
     169} 
     170 
     171void modulator_init(modulator *_m, modulation_scheme mod, unsigned int M, int _e) 
     172{ 
     173    if (M<2) { 
     174        perror("ERROR: modulator_init, modulator must have at least 2 symbols\n"); 
     175        return; 
     176    } 
     177 
     178    _m->M = M; 
     179    unsigned int bits_per_symbol=0; 
     180    while ( !(M & 0x01) ) { 
     181        M >>= 1; 
     182        bits_per_symbol++; 
     183    } 
    159184    _m->ms = mod; 
     185    _m->m = bits_per_symbol; 
     186 
     187    modulator_calculate_levels(_m->ms, _m->m, _m->levels_i, _m->levels_q); 
     188 
     189    printf("modulator_init creating new scheme:\n"); 
     190    printf("  m = %d\n", _m->m); 
     191    printf("  M = %d\n", _m->M); 
     192    unsigned int i; 
     193    for (i=0; i<_m->M; i++) 
     194        printf("  i(%d)=%f; q(%d)=%f;\n", i+1, _m->levels_i[i], i+1, _m->levels_q[i]); 
    160195} 
    161196 
  • SigProc/branches/SigProc-c/src/sigprocc.h

    r5741 r5742  
    284284// 
    285285//----------------------------------------------------------------------------- 
    286  
    287 /* 
    288 enum modulation_scheme { 
    289     UNKNOWN,                                // Unknown modulation scheme 
    290     BPSK,   QPSK,   PSK8,   PSK16, PSK32,   // Phase shift keying 
    291     DBPSK,  DQPSK,  DPSK8,  DPSK16, DSPK32, // Differential PSK 
    292     PAM4,   PAM8,   PAM16,  PAM32,          // Pulse amplitude modulation 
    293     BFSK,   FSK4,   FSK8,   FSK16,          // Frequency shift keying 
    294     QAM4, QAM8, QAM16,  QAM32,  QAM64,  QAM128, QAM256  // Quadrature amplitude modulation 
    295     }; 
    296     */ 
    297  
    298 struct modulation_scheme { 
    299     enum mod_scheme { 
    300         UNKNOWN, 
    301         PSK, 
    302         DPSK, 
    303         PAM, 
    304         FSK, 
    305         QAM 
    306     } scheme; 
    307     unsigned int m; 
    308     unsigned int M; 
    309 }; 
    310  
    311 typedef struct modulation_scheme modulation_scheme_t; 
     286typedef enum { 
     287    MOD_UNKNOWN, 
     288    MOD_PSK, 
     289    MOD_DPSK, 
     290    MOD_PAM, 
     291    MOD_FSK, 
     292    MOD_QAM 
     293} modulation_scheme; 
     294 
     295//typedef struct modulation_scheme modulation_scheme_t; 
    312296 
    313297unsigned int gray(unsigned int symbol_in); 
    314298 
    315 void modulator_calculate_levels(modulation_scheme_t*, float*, float*); 
    316 void modulator_calculate_levels_psk(modulation_scheme_t*, float*, float*); 
    317 void modulator_calculate_levels_pam(modulation_scheme_t*, float*, float*); 
    318 void modulator_calculate_levels_rqam(modulation_scheme_t*, float*, float*); 
    319 void modulator_scale_levels(modulation_scheme_t*, float*, float*); 
    320  
    321 float modulator_get_scaling_factor(modulation_scheme_t*); 
     299void modulator_calculate_levels(modulation_scheme, unsigned int, float*, float*); 
     300void modulator_calculate_levels_psk(modulation_scheme, unsigned int, float*, float*); 
     301void modulator_calculate_levels_pam(modulation_scheme, unsigned int, float*, float*); 
     302void modulator_calculate_levels_rqam(modulation_scheme, unsigned int, float*, float*); 
     303void modulator_scale_levels(modulation_scheme, unsigned int, float*, float*); 
     304 
     305float modulator_get_scaling_factor(modulation_scheme, unsigned int); 
    322306 
    323307// PSK 
     
    348332 
    349333typedef struct { 
    350     modulation_scheme_t ms; 
     334    modulation_scheme ms; 
     335    unsigned int m; 
     336    unsigned int M; 
    351337    int energy; 
    352     int * levels_i; 
    353     int * levels_q; 
    354  
    355     float * ref_i; 
    356     float * ref_q; 
     338    float * levels_i; 
     339    float * levels_q; 
     340 
     341    int state_i; 
     342    int state_q; 
    357343} modulator; 
    358344 
    359345modulator* modulator_create(unsigned int _M); 
    360 void modulator_init(modulator * _m, modulation_scheme_t mod, int _e); 
    361  
     346void modulator_init(modulator*, modulation_scheme, unsigned int, int); 
    362347void modulate(modulator *_m, short symbol_in, short *I_out, short *Q_out); 
    363348void free_modulator(modulator * _ms);