📄 trinomiallfsr.java
字号:
// if (powers == null) powers = new TrinomialLFSR[L]; // exist? // see if we haven't pre-computed the contents in an earlier // invocation. do so by checking if value at #0 is this. if (! this.isSameValue(powers[0])) { // pre-compute the table powers[0] = (TrinomialLFSR) Z.clone(); // hash #0 is this for (int i = 1; i < L; i++) { Z.multiply(Z); powers[i] = (TrinomialLFSR) Z.clone(); } } // use it int i; for (i = 0; i < limit; i++) if (n.testBit(i)) Y = multiply(powers[i], Y); Z = powers[i];// } else { Y = trinomialOne(); // multiplicative unity for (int i = 0; i < limit; i++) { if (n.testBit(i)) Y = multiply(Z, Y); Z.multiply(Z); } } load(multiply(Y, Z)); }// Other polynomial methods//........................................................................... /** * Set the LFSR's initial state to a value that corresponds * to the polynomial term of the designated degree. * * @param n Reset the register's contents to all zeroes except * for a <i>1</i> at the index position corresponding to * the term x<font size="-1"><sup>n</sup></font></i>. * @exception IllegalArgumentException If the argument value * is negative or greater than or equal to <code>this</code> * LFSR's trinomial degree. */ public void resetX (int n) { reset(); setX(n); } /** * Set (to one) </code>this</code> LFSR's polynomial term of * the given degree. The other <code>stages</code>, in contrast * to the <code>resetX()</code> method, are unaffected. * * @param n Set (to one) the register position of the term * x<font size="-1"><sup>n</sup></font></i>. * @exception IllegalArgumentException If the argument value * is negative or greater than or equal to <code>this</code> * LFSR's trinomial degree. */ public void setX (int n) { setBit(indexOfX(n)); } /** * Return the register's index relative to the polynomial * term <i>x<font size="-1"><sup>degree</sup></font></i>. * * @param degree The power of the invariate <i>x</i>, for which * the register's index is to be found. * @return The register's index relative to the polynomial term * <i>x<font size="-1"><sup>degree</sup></font></i>. * @exception IllegalArgumentException If the argument value * is negative or greater than or equal to <code>this</code> * LFSR's trinomial degree. */ public int indexOfX (int degree) { if (degree < 0 || degree >= L) throw new IllegalArgumentException(); int index = degree - K; if (index < 0) index += L; return index; } /** * Return the power of the term <i>x<font size="-1"><sup>result</sup> * </font></i> relative to the given register's index. * * @return The power of the invariate <i>x</i> relative to the given * register's index. * @param index The register's index relative to the polynomial term * <i>x<font size="-1"><sup>result</sup></font></i>. * @exception IllegalArgumentException If the argument value * is negative or greater than or equal to <code>this</code> * LFSR's trinomial degree. */ public int degreeAt (int index) { if (index < 0 || index >= L) throw new IllegalArgumentException(); return (index + K) % L; } /** * Return a <code>TrinomialLFSR</code> object whose state is set * to the powers of the polynomial <i>p(x)</i> such that <i>p(x) * = 1</i> in the polynomial <i>Group</i> defined over the trinomial * function of <code>this</code> object. * * @return A <code>TrinomialLFSR</code> object whose state is set * to the powers of the polynomial <i>p(x)</i> such that * <i>p(x) = 1</i> in the polynomial <i>Group</i> defined * over the trinomial function of <code>this</code> object. */ public TrinomialLFSR trinomialOne () { TrinomialLFSR x = (TrinomialLFSR) clone(); x.resetX(0); return x; } /** * Return a <code>TrinomialLFSR</code> object whose state is set * to the powers of the polynomial <i>p(x)</i> such that <i>p(x) * = x</i> in the polynomial <i>Group</i> defined over the trinomial * function of <code>this</code> object. * * @return A <code>TrinomialLFSR</code> object whose state is set * to the powers of the polynomial <i>p(x)</i> such that * <i>p(x) = x</i> in the polynomial <i>Group</i> defined * over the trinomial function of <code>this</code> object. */ public TrinomialLFSR trinomialX () { TrinomialLFSR x = (TrinomialLFSR) clone(); x.resetX(1); return x; }// Accessors//........................................................................... /** * Return the number of <i>elements</i> in this LFSR, which is also * the <i>degree</i> of the trinomial. * * @return The number of <i>elements</i> in this LFSR. */ public int getSize () { return L; } /** * Return the degree/power of the <i>mid-tap</i> element in this LFSR. * * @return The degree/power of the <i>mid-tap</i> element in this LFSR. */ public int getMidTap () { return K; } /** * Return the maximum number of meaningful bits in this LFSR, which * is also the maximum number of bits that can be processed in one * operation without loss of desired output sequence. * * @return The maximum number of meaningful bits in this LFSR. */ public int getSlice () { return slice; }// Test and comparison//........................................................................... /** * Return true if the TrinomialLFSR <i>x</i> has equal characteristics * and contents to this one; false otherwise. * <p> * NOTE: the <code>equals</code> method is not used, because this is * a mutable object (see the requirements for equals in the Java Language * Spec). * * @return true iff x has equal characteristics and contents. */ public boolean isSameValue (TrinomialLFSR x) { if (x == null || x.K != K) return false; return super.isSameValue((BigRegister) x); } /** * Compare this LFSR to the argument, returning -1, 0 or 1 for * less than, equal to, or greater than comparison. * * @return -1, 0, +1 If the contents of this object are * respectively less than, equal to, or greater than * those of the argument. */ public int compareTo (TrinomialLFSR x) { if (L > x.L) return 1; if (L < x.L) return -1; if (K > x.K) return 1; if (K < x.K) return -1; BigRegister ba = this.toBigRegister(); BigRegister bb = x.toBigRegister(); return ba.compareTo(bb); } /** * Return true iff the argument is a polynomial that belongs to * the same <i>Group</i> as <code>this</code>. * * @return true iff the argument is a polynomial that belongs to * the same <i>Group</i> as <code>this</code>. */ public boolean isSameGroup (TrinomialLFSR x) { if (x == null || L != x.L || K != x.K) return false; return true; } // Visualisation and introspection methods//........................................................................... /** * Return the state of <code>this</code> LFSR as a <code>BigRegister * </code> object where now the powers of the polynomial terms are * ordered in ascending succession starting from power 0 at index 0. * * @return The state of <code>this</code> LFSR as a <code>BigRegister * </code> object where now the powers of the polynomial terms * are ordered in ascending succession starting from power 0 * at index 0. */ public BigRegister toBigRegister () { BigRegister result = (BigRegister) clone(); result.rotateLeft(K); return result; } /** * Return a formatted <code>String</code> representation of the binary * contents of <code>this</code>. * * @return A formatted string representation of the binary contents * of <code>this</code>. */ public String toString () { StringBuffer sb = new StringBuffer(8 * L + 64); sb.append("[...\n TrinomialLFSR <").append(L).append(", x"). append(L).append(" + ").append((K == 1) ? "x" : "x" + K). append(" + 1").append(">...\n"). append(" current state is: ").append(super.toString()). append("...]\n"); return sb.toString(); } /** * Return a formatted <code>String</code> representation of the * polynomial form represented by <code>this</code> LFSR's state. * * @return A formatted string representation of the binary contents * of <code>this</code>. */ public String toPolynomial () { StringBuffer sb = new StringBuffer(16); sb.append(' '); int d = L; boolean firstTime = true; while (--d >= 0) { if (testBit(indexOfX(d))) { if (firstTime) firstTime = !firstTime; else sb.append(" + "); if (d != 0) { sb.append('x'); if (d != 1) sb.append(d); } else sb.append('1'); } } if (firstTime) sb.append('0'); return sb.append(' ').toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -