Adding file operations result checking.
[debian/gnuradio] / gr-trellis / src / lib / fsm.cc
index a27bc87daca7c3fd663559b56bf49475bf2b8745..5950b56b9d2ae612fad5597e66bf4e3d1707eab0 100644 (file)
@@ -6,7 +6,7 @@
  *
  * GNU Radio 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, or (at your option)
+ * the Free Software Foundation; either version 3, or (at your option)
  * any later version.
  *
  * GNU Radio is distributed in the hope that it will be useful,
@@ -26,6 +26,7 @@
 #include <fstream>
 #include <stdexcept>
 #include <cmath>
+#include <stdlib.h>
 #include "base.h"
 #include "fsm.h"
 
@@ -85,16 +86,30 @@ fsm::fsm(const char *name)
   if((fsmfile=fopen(name,"r"))==NULL) 
     throw std::runtime_error ("fsm::fsm(const char *name): file open error\n");
     //printf("file open error in fsm()\n");
+
+  if(fscanf(fsmfile,"%d %d %d\n",&d_I,&d_S,&d_O) == EOF) {
+    if(ferror(fsmfile) != 0)
+      throw std::runtime_error ("fsm::fsm(const char *name): file read error\n");
+  }
   
-  fscanf(fsmfile,"%d %d %d\n",&d_I,&d_S,&d_O);
   d_NS.resize(d_I*d_S);
   d_OS.resize(d_I*d_S);
 
   for(int i=0;i<d_S;i++) {
-    for(int j=0;j<d_I;j++) fscanf(fsmfile,"%d",&(d_NS[i*d_I+j]));
+    for(int j=0;j<d_I;j++) {
+      if(fscanf(fsmfile,"%d",&(d_NS[i*d_I+j])) == EOF) {
+       if(ferror(fsmfile) != 0)
+         throw std::runtime_error ("fsm::fsm(const char *name): file read error\n");
+      }
+    }
   }
   for(int i=0;i<d_S;i++) {
-    for(int j=0;j<d_I;j++) fscanf(fsmfile,"%d",&(d_OS[i*d_I+j]));
+    for(int j=0;j<d_I;j++) {
+      if(fscanf(fsmfile,"%d",&(d_OS[i*d_I+j])) == EOF) {
+       if(ferror(fsmfile) != 0)
+         throw std::runtime_error ("fsm::fsm(const char *name): file read error\n");
+      }
+    }
   }
  
   generate_PS_PI();
@@ -169,7 +184,7 @@ fsm::fsm(int k, int n, const std::vector<int> &G)
 
 
   for(int s=0;s<d_S;s++) {
-    dec2bases(s,bases_x,sx); // split s into k values, each representing on of the k shift registers
+    dec2bases(s,bases_x,sx); // split s into k values, each representing one of the k shift registers
 //printf("state = %d \nstates = ",s);
 //for(int j=0;j<sx.size();j++) printf("%d ",sx[j]); printf("\n");
     for(int i=0;i<d_I;i++) {
@@ -238,6 +253,124 @@ fsm::fsm(int mod_size, int ch_length)
 }
 
 
+
+
+//######################################################################
+//# Automatically generate an FSM specification describing the 
+//# the trellis for a CPM with h=K/P (relatively prime), 
+//# alphabet size M, and frequency pulse duration L symbols
+//#
+//# This FSM is based on the paper by B. Rimoldi
+//# "A decomposition approach to CPM", IEEE Trans. Info Theory, March 1988
+//# See also my own notes at http://www.eecs.umich.edu/~anastas/docs/cpm.pdf
+//######################################################################
+fsm::fsm(int P, int M, int L)
+{
+  d_I=M;
+  d_S=(int)(pow(1.0*M,1.0*L-1)+0.5)*P;
+  d_O=(int)(pow(1.0*M,1.0*L)+0.5)*P;
+
+  d_NS.resize(d_I*d_S);
+  d_OS.resize(d_I*d_S);
+  int nv;
+  for(int s=0;s<d_S;s++) {
+    for(int i=0;i<d_I;i++) {
+      int s1=s/P;
+      int v=s%P;
+      int ns1= (i*(int)(pow(1.0*M,1.0*(L-1))+0.5)+s1)/M;
+      if (L==1)
+        nv=(i+v)%P;
+      else
+        nv=(s1%M+v)%P;
+      d_NS[s*d_I+i] = ns1*P+nv;
+      d_OS[s*d_I+i] = i*d_S+s;
+    }
+  }
+
+  generate_PS_PI();
+  generate_TM();
+}
+
+
+
+
+
+
+
+
+
+
+//######################################################################
+//# Automatically generate an FSM specification describing the 
+//# the joint trellis of fsm1 and fsm2
+//######################################################################
+fsm::fsm(const fsm &FSM1, const fsm &FSM2)
+{
+  d_I=FSM1.I()*FSM2.I();
+  d_S=FSM1.S()*FSM2.S();
+  d_O=FSM1.O()*FSM2.O();
+
+  d_NS.resize(d_I*d_S);
+  d_OS.resize(d_I*d_S);
+
+  for(int s=0;s<d_S;s++) {
+    for(int i=0;i<d_I;i++) {
+      int s1=s/FSM2.S();
+      int s2=s%FSM2.S();
+      int i1=i/FSM2.I();
+      int i2=i%FSM2.I();
+      d_NS[s*d_I+i] = FSM1.NS()[s1 * FSM1.I() + i1] * FSM2.S() + FSM2.NS()[s2 * FSM2.I() + i2];
+      d_OS[s*d_I+i] = FSM1.OS()[s1 * FSM1.I() + i1] * FSM2.O() + FSM2.OS()[s2 * FSM2.I() + i2];
+    }
+  }
+
+  generate_PS_PI();
+  generate_TM();
+}
+
+
+
+
+//######################################################################
+//# Generate a new FSM representing n stages through the original FSM
+//# AKA radix-n FSM
+//######################################################################
+fsm::fsm(const fsm &FSM, int n)
+{
+  d_I=(int) (pow(1.0*FSM.I(),1.0*n)+0.5);
+  d_S=FSM.S();
+  d_O=(int) (pow(1.0*FSM.O(),1.0*n)+0.5);
+
+  d_NS.resize(d_I*d_S);
+  d_OS.resize(d_I*d_S);
+
+  for(int s=0;s<d_S;s++ ) {
+    for(int i=0;i<d_I;i++ ) {
+      std::vector<int> ii(n);
+      dec2base(i,FSM.I(),ii);
+      std::vector<int> oo(n);
+      int ns=s;
+      for(int k=0;k<n;k++) {
+        oo[k]=FSM.OS()[ns*FSM.I()+ii[k]];
+        ns=FSM.NS()[ns*FSM.I()+ii[k]];
+      }
+      d_NS[s*d_I+i]=ns;
+      d_OS[s*d_I+i]=base2dec(oo,FSM.O());
+    }
+  }
+
+  generate_PS_PI();
+  generate_TM();
+}
+
+
+
+
+
+
+
+
+
 //######################################################################
 //# generate the PS and PI tables for later use
 //######################################################################
@@ -389,7 +522,7 @@ void fsm::write_trellis_svg( std::string filename ,int number_stages)
 
 
 //######################################################################
-//# Write trellis specification to a text files,
+//# Write trellis specification to a text file,
 //# in the same format used when reading FSM files
 //######################################################################
 void fsm::write_fsm_txt(std::string filename)