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();
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++) {
}
+
+
+//######################################################################
+//# 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
//######################################################################
//######################################################################
-//# 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)