244
//######################################################################
245
//# Automatically generate an FSM specification describing the
246
//# the trellis for a CPM with h=K/P (relatively prime),
247
//# alphabet size M, and frequency pulse duration L symbols
249
//# This FSM is based on the paper by B. Rimoldi
250
//# "A decomposition approach to CPM", IEEE Trans. Info Theory, March 1988
251
//# See also my own notes at http://www.eecs.umich.edu/~anastas/docs/cpm.pdf
252
//######################################################################
253
fsm::fsm(int P, int M, int L)
256
d_S=(int)(pow(1.0*M,1.0*L-1)+0.5)*P;
257
d_O=(int)(pow(1.0*M,1.0*L)+0.5)*P;
259
d_NS.resize(d_I*d_S);
260
d_OS.resize(d_I*d_S);
262
for(int s=0;s<d_S;s++) {
263
for(int i=0;i<d_I;i++) {
266
int ns1= (i*(int)(pow(1.0*M,1.0*(L-1))+0.5)+s1)/M;
271
d_NS[s*d_I+i] = ns1*P+nv;
272
d_OS[s*d_I+i] = i*d_S+s;
289
//######################################################################
290
//# Automatically generate an FSM specification describing the
291
//# the joint trellis of fsm1 and fsm2
292
//######################################################################
293
fsm::fsm(const fsm &FSM1, const fsm &FSM2)
295
d_I=FSM1.I()*FSM2.I();
296
d_S=FSM1.S()*FSM2.S();
297
d_O=FSM1.O()*FSM2.O();
299
d_NS.resize(d_I*d_S);
300
d_OS.resize(d_I*d_S);
302
for(int s=0;s<d_S;s++) {
303
for(int i=0;i<d_I;i++) {
308
d_NS[s*d_I+i] = FSM1.NS()[s1 * FSM1.I() + i1] * FSM2.S() + FSM2.NS()[s2 * FSM2.I() + i2];
309
d_OS[s*d_I+i] = FSM1.OS()[s1 * FSM1.I() + i1] * FSM2.O() + FSM2.OS()[s2 * FSM2.I() + i2];
320
//######################################################################
321
//# Generate a new FSM representing n stages through the original FSM
323
//######################################################################
324
fsm::fsm(const fsm &FSM, int n)
326
d_I=(int) (pow(1.0*FSM.I(),1.0*n)+0.5);
328
d_O=(int) (pow(1.0*FSM.O(),1.0*n)+0.5);
330
d_NS.resize(d_I*d_S);
331
d_OS.resize(d_I*d_S);
333
for(int s=0;s<d_S;s++ ) {
334
for(int i=0;i<d_I;i++ ) {
335
std::vector<int> ii(n);
336
dec2base(i,FSM.I(),ii);
337
std::vector<int> oo(n);
339
for(int k=0;k<n;k++) {
340
oo[k]=FSM.OS()[ns*FSM.I()+ii[k]];
341
ns=FSM.NS()[ns*FSM.I()+ii[k]];
344
d_OS[s*d_I+i]=base2dec(oo,FSM.O());
239
360
//######################################################################
240
361
//# generate the PS and PI tables for later use
241
362
//######################################################################
242
363
void fsm::generate_PS_PI()
244
d_PS.resize(d_I*d_S);
245
d_PI.resize(d_I*d_S);
247
368
for(int i=0;i<d_S;i++) {
369
d_PS[i].resize(d_I*d_S); // max possible size
370
d_PI[i].resize(d_I*d_S);
249
372
for(int ii=0;ii<d_S;ii++) for(int jj=0;jj<d_I;jj++) {
250
373
if(d_NS[ii*d_I+jj]!=i) continue;
444
//######################################################################
445
//# generate trellis representation of FSM as an SVG file
446
//######################################################################
447
void fsm::write_trellis_svg( std::string filename ,int number_stages)
449
std::ofstream trellis_fname (filename.c_str());
450
if (!trellis_fname) {std::cout << "file not found " << std::endl ; exit(-1);}
451
const int TRELLIS_Y_OFFSET = 30;
452
const int TRELLIS_X_OFFSET = 20;
453
const int STAGE_LABEL_Y_OFFSET = 25;
454
const int STAGE_LABEL_X_OFFSET = 20;
455
const int STATE_LABEL_Y_OFFSET = 30;
456
const int STATE_LABEL_X_OFFSET = 5;
457
const int STAGE_STATE_OFFSETS = 10;
458
// std::cout << "################## BEGIN SVG TRELLIS PIC #####################" << std::endl;
459
trellis_fname << "<svg viewBox = \"0 0 200 200\" version = \"1.1\">" << std::endl;
461
for( int stage_num = 0;stage_num < number_stages;stage_num ++){
463
for ( int state_num = 0;state_num < d_S ; state_num ++ ) {
464
trellis_fname << "<circle cx = \"" << stage_num * STAGE_STATE_OFFSETS + TRELLIS_X_OFFSET <<
465
"\" cy = \"" << state_num * STAGE_STATE_OFFSETS + TRELLIS_Y_OFFSET << "\" r = \"1\"/>" << std::endl;
467
if(stage_num != number_stages-1){
468
for( int branch_num = 0;branch_num < d_I; branch_num++){
469
trellis_fname << "<line x1 =\"" << STAGE_STATE_OFFSETS * stage_num+ TRELLIS_X_OFFSET << "\" ";
470
trellis_fname << "y1 =\"" << state_num * STAGE_STATE_OFFSETS + TRELLIS_Y_OFFSET<< "\" ";
471
trellis_fname << "x2 =\"" << STAGE_STATE_OFFSETS *stage_num + STAGE_STATE_OFFSETS+ TRELLIS_X_OFFSET << "\" ";
472
trellis_fname << "y2 =\"" << d_NS[d_I * state_num + branch_num] * STAGE_STATE_OFFSETS + TRELLIS_Y_OFFSET << "\" ";
473
trellis_fname << " stroke-dasharray = \"3," << branch_num << "\" ";
474
trellis_fname << " stroke = \"black\" stroke-width = \"0.3\"/>" << std::endl;
480
trellis_fname << "<g font-size = \"4\" font= \"times\" fill = \"black\">" << std::endl;
481
for( int stage_num = 0;stage_num < number_stages ;stage_num ++){
482
trellis_fname << "<text x = \"" << stage_num * STAGE_STATE_OFFSETS + STAGE_LABEL_X_OFFSET <<
483
"\" y = \"" << STAGE_LABEL_Y_OFFSET << "\" >" << std::endl;
484
trellis_fname << stage_num << std::endl;
485
trellis_fname << "</text>" << std::endl;
487
trellis_fname << "</g>" << std::endl;
490
trellis_fname << "<g font-size = \"4\" font= \"times\" fill = \"black\">" << std::endl;
491
for( int state_num = 0;state_num < d_S ; state_num ++){
492
trellis_fname << "<text y = \"" << state_num * STAGE_STATE_OFFSETS + STATE_LABEL_Y_OFFSET <<
493
"\" x = \"" << STATE_LABEL_X_OFFSET << "\" >" << std::endl;
494
trellis_fname << state_num << std::endl;
495
trellis_fname << "</text>" << std::endl;
497
trellis_fname << "</g>" << std::endl;
500
trellis_fname << "</svg>" << std::endl;
501
// std::cout << "################## END SVG TRELLIS PIC ##################### " << std::endl;
502
trellis_fname.close();
510
//######################################################################
511
//# Write trellis specification to a text file,
512
//# in the same format used when reading FSM files
513
//######################################################################
514
void fsm::write_fsm_txt(std::string filename)
516
std::ofstream trellis_fname (filename.c_str());
517
if (!trellis_fname) {std::cout << "file not found " << std::endl ; exit(-1);}
518
trellis_fname << d_I << ' ' << d_S << ' ' << d_O << std::endl;
519
trellis_fname << std::endl;
520
for(int i=0;i<d_S;i++) {
521
for(int j=0;j<d_I;j++) trellis_fname << d_NS[i*d_I+j] << ' ';
522
trellis_fname << std::endl;
524
trellis_fname << std::endl;
525
for(int i=0;i<d_S;i++) {
526
for(int j=0;j<d_I;j++) trellis_fname << d_OS[i*d_I+j] << ' ';
527
trellis_fname << std::endl;
529
trellis_fname << std::endl;
530
trellis_fname.close();