DSPRelated.com
Forums

Java code for generating m-sequences

Started by SudanBoy February 5, 2013
>................
>And, of course, no one is going to write you the code for free.
here is my java code, please verify if it works properly or not. I know it is very simple programming algorithm but hey it works. package WorkSpace; public class mainSpace { public static void main(String args[]){ int poly_deg = 5; // you can only put 6 and 7. int seq_length = mSeqlength.seq_length(poly_deg); System.out.println("The Sequences length is: "+seq_length); // Initialising the shift registers int [] init_state = new int[poly_deg]; init_state[poly_deg-1] = 1; // Generating the two m-sequences int [] seqA = mSeqGenA.PNseq(poly_deg, seq_length, init_state); int [] seqB = mSeqGenB.PNseq(poly_deg, seq_length, init_state); System.out.println(""); // Generating the Gold Code int [] gold = goldCodeGen.gold(seq_length, seqA, seqB); } } // calculating the sequences length public class mSeqlength { public static int seq_length (int deg){ int length = (int)(Math.pow(2,deg) - 1); System.out.println(""); return length; } } // generating the first m-sequence (make sure to XOR the right taps) public class mSeqGenA { public static int []PNseq (int poly_deg, int seq_length, int []taps_seed){ int i,j; int xor1, xor2; int [] PNseq = new int[seq_length]; int [] temp = new int[poly_deg]; temp = taps_seed; System.out.println("the first m-sequence is: "); if (seq_length<32){ for (i=0; i<seq_length; i++){ xor2 = taps_seed[3] ^ taps_seed[poly_deg-1]; xor2 = xor2 ^ taps_seed[2]; xor1 = xor2 ^ taps_seed[1]; //shifting the taps for (j=(poly_deg-1); j>0; j--){ temp[j]= taps_seed[j-1]; } j=0; temp[j] = xor1; PNseq[i] = taps_seed[poly_deg-1]; taps_seed = temp; } for (i=seq_length; i>0; i--){ System.out.print(PNseq[i-1]);} System.out.println(""); } else if(seq_length>32){ if (seq_length<64){ for (i=0; i<seq_length; i++){ xor2 = taps_seed[4] ^ taps_seed[poly_deg-1]; xor2 = xor2 ^ taps_seed[1]; xor1 = xor2 ^ taps_seed[0]; //shifting the sequence for (j=(poly_deg-1); j>0; j--){ temp[j]= taps_seed[j-1]; } j=0; temp[j] = xor1; PNseq[i] = taps_seed[poly_deg-1]; taps_seed = temp; } for (i=0; i<seq_length; i++){ System.out.print(PNseq[i]);} System.out.println(""); } else if (seq_length>64){ for (i=0; i<seq_length; i++){ xor2 = taps_seed[5] ^ taps_seed[poly_deg-1]; xor2 = xor2 ^ taps_seed[4]; xor1 = xor2 ^ taps_seed[3]; //shifting the sequence for (j=(poly_deg-1); j>0; j--){ temp[j]= taps_seed[j-1]; } j=0; temp[j] = xor1; PNseq[i] = taps_seed[poly_deg-1]; taps_seed = temp; } for (i=0; i<seq_length; i++){ System.out.print(PNseq[i]);} System.out.println(""); } } return PNseq; } } // generating the second m-sequence (make sure to XOR the right taps) public class mSeqGenB { public static int [] PNseq (int poly_deg, int seq_length, int []taps_seed){ int i, j; int xor1; int [] PNseq = new int[seq_length]; int [] temp = new int[poly_deg]; temp = taps_seed; System.out.println("the second m-sequence is: "); if (seq_length<=32){ for (i=0; i<seq_length; i++){ xor1 = taps_seed[2] ^ taps_seed[poly_deg-1]; //shifting the taps for (j=(poly_deg-1); j>0; j--){ temp[j]= taps_seed[j-1]; } j=0; temp[j] = xor1; PNseq[i] = taps_seed[poly_deg-1]; taps_seed = temp; } for (i=seq_length; i>0; i--){ System.out.print(PNseq[i-1]);} System.out.println(""); } else if (seq_length>31){ if (seq_length<=64){ for (i=0; i<seq_length; i++){ xor1 = taps_seed[0] ^ taps_seed[poly_deg-1]; for (j=(poly_deg-1); j>0; j--){ temp[j]= taps_seed[j-1]; } j=0; temp[j] = xor1; PNseq[i] = taps_seed[poly_deg-1]; taps_seed = temp; } for (i=0; i<seq_length; i++){ System.out.print(PNseq[i]);} System.out.println(""); } else if (seq_length>64){ for (i=0; i<seq_length; i++){ xor1 = taps_seed[3] ^ taps_seed[poly_deg-1]; for (j=(poly_deg-1); j>0; j--){ temp[j]= taps_seed[j-1]; } j=0; temp[j] = xor1; PNseq[i] = taps_seed[poly_deg-1]; taps_seed = temp; } for (i=0; i<seq_length; i++){ System.out.print(PNseq[i]);} System.out.println(""); } } return PNseq; } } // where gold codes are generated public class goldCodeGen { public static int [] gold (int seq_length, int []seqA, int []seqB) { int i, j, d; int []gold_code = new int[seq_length]; for (d=0; d<seq_length; d++){ int []temp = new int[seq_length]; // generating the gold code for (i=0; i<seq_length; i++){ gold_code[i] = seqA[i] ^ seqB[i];} // showing the gold code for (i=seq_length; i>0; i--){ System.out.print(gold_code[i-1]); } //checking if the gold sequence is balanced int zeros=0, ones=0; for (i=0; i<seq_length; i++){ if (gold_code[i]==0){ zeros= zeros+1;} else {ones= ones+1;} } if ((zeros==ones) || (zeros==(ones-1))) {System.out.println(" 'Balanced'");} else { System.out.println(" 'NOT balanced'"); } //shifting the second m-sequence for (j=(seq_length-1); j>0; j--){ temp[j] = seqB[j-1]; } j=0; temp[j] = seqB[(seq_length-1)]; seqB = temp; } return gold_code; } } as I said above it's simple and straight forward .. please check if it generate the correct m-sequence and gold codes
On Thu, 14 Feb 2013 05:00:17 -0600, SudanBoy wrote:

>>On Tue, 05 Feb 2013 13:04:48 -0600, SudanBoy wrote: >> >>don't think you're going to get any takers. >> >>I'm pretty sure that the "m-sequence" in your title means the output of >>an LFSR. If this is the case, then actually generating the sequence >>should be pretty straight forward. My inclination is to think that if >>you cannot generate the sequence, then you cannot use it -- so it's a >>very good exercise to keep working at this until you can generate the >>sequence. > > thanks for your advice (and for the steps), now I am actually generating > the PN sequence (i.e: the m-sequence or the LFSR output). > >>And, of course, no one is going to write you the code for free. > > I wasn't looking for the code for the sake of copying it, I was just > looking for the algorithm. BTW, I don't mind pasting my code if that > will help other engineers as well. > >>Alternately, if you can point to the specific area where you're having >>problems then let us know and we'll help with just those areas. But >>keep > >>in mind that it really helps if you already have a firm grasp of how to >>generate these sequences. > > as I mentioned in my main post, I want to generate the gold codes. My > real problem now is how to verify the gold sequences that I got from the > two LFSR. I mean calculating the cross-correlation between the > sequences. > > anyhow, thank you for your help, I really appreciate the steps you > mentioned above which helped me generating the m-sequences.
Verifying cross-correlation should be easy. Take the two samples that you want to cross-correlate, and do a bitwise XOR (I'm assuming you have these stored as bit streams, or have a way of pretending that they are). Now count the ones. If the number of ones is exactly 1/2 the number of bits, then the cross-correlation is zero for those two samples. If the result is all ones then you have a correlation coefficient of -1; if the result is all zeros then you have a correlation coefficient of +1. -- My liberal friends think I'm a conservative kook. My conservative friends think I'm a liberal kook. Why am I not happy that they have found common ground? Tim Wescott, Communications, Control, Circuits & Software http://www.wescottdesign.com
On 2/14/13 12:47 PM, Tim Wescott wrote:
> On Thu, 14 Feb 2013 05:00:17 -0600, SudanBoy wrote: > >>> On Tue, 05 Feb 2013 13:04:48 -0600, SudanBoy wrote: >>> >>> don't think you're going to get any takers. >>> >>> I'm pretty sure that the "m-sequence" in your title means the output of >>> an LFSR. If this is the case, then actually generating the sequence >>> should be pretty straight forward. My inclination is to think that if >>> you cannot generate the sequence, then you cannot use it -- so it's a >>> very good exercise to keep working at this until you can generate the >>> sequence. >> >> thanks for your advice (and for the steps), now I am actually generating >> the PN sequence (i.e: the m-sequence or the LFSR output). >> >>> And, of course, no one is going to write you the code for free. >> >> I wasn't looking for the code for the sake of copying it, I was just >> looking for the algorithm. BTW, I don't mind pasting my code if that >> will help other engineers as well. >> >>> Alternately, if you can point to the specific area where you're having >>> problems then let us know and we'll help with just those areas. But >>> keep >> >>> in mind that it really helps if you already have a firm grasp of how to >>> generate these sequences. >> >> as I mentioned in my main post, I want to generate the gold codes. My >> real problem now is how to verify the gold sequences that I got from the >> two LFSR. I mean calculating the cross-correlation between the >> sequences. >> >> anyhow, thank you for your help, I really appreciate the steps you >> mentioned above which helped me generating the m-sequences. > > Verifying cross-correlation should be easy. Take the two samples that > you want to cross-correlate, and do a bitwise XOR (I'm assuming you have > these stored as bit streams, or have a way of pretending that they are). > Now count the ones. If the number of ones is exactly 1/2 the number of > bits, then the cross-correlation is zero for those two samples. If the > result is all ones then you have a correlation coefficient of -1; if the > result is all zeros then you have a correlation coefficient of +1.
remember that the shift register state of all 0s cannot be in the sequence (otherwise you will get nothing but zeros coming out). that means, for a "maximum length sequence" (another name for this "m-sequence", "PN sequence", "LFSR sequence" or whatever), there will always be exactly one more 1 than there are 0s. if the sequence is long, that difference will seem rather small. -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
Now, after verifying the sequences as gold-codes sequences (using the
cross-correlation function), I can say confidently that we have a Java code
for the m-sequences generator and the gold code generator .. 

thank you all
> >Now, after verifying the sequences as gold-codes sequences (using the >cross-correlation function), I can say confidently that we have a Java
code
>for the m-sequences generator and the gold code generator .. > >thank you all >
one thing about the cross correlation. I used the Matlab function: "xcorr". Applying the cross correlation on a single gold-code sequence (i.e.: Auto-correlation) will give you the "common-known" graphs of the cross correlation with many small peaks (having the three values of the gold-code cross-correlation values) and one high peak. however, for better graph that shows only the exact three values that verify the gold-codes sequences, circular-shifting the second sequence while keeping the first reference sequence fixed will generate a clear graphs.
hi 
please help me quickly 
i need LFSR program in java