# comm.PNSequence

Generate a pseudonoise (PN) sequence

## Description

The comm.PNSequence System object™ generates a sequence of pseudorandom binary numbers using a linear-feedback shift register (LFSR). This object implements LFSR using a simple shift register generator (SSRG, or Fibonacci) configuration. Pseudonoise sequences are typically used for pseudorandom scrambling and in direct-sequence spread-spectrum systems.

To generate a PN sequence:

1. Create the comm.PNSequence object and set its properties.

2. Call the object with arguments, as if it were a function.

## Creation

### Description

pnSequence = comm.PNSequence creates a pseudonoise (PN) sequence generator System object. This object generates a sequence of pseudorandom binary numbers using a linear-feedback shift register (LFSR).

pnSequence = comm.PNSequence(Name,Value) sets properties using one or more name-value pairs. Enclose each property name in quotes. For example, 'Mask',1 specifies a one sample offset of the output sequence from the starting point.

## Properties

expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and the release function unlocks them.

If a property is tunable, you can change its value at any time.

Generator polynomial that determines the feedback connections of the shift register, specified as one of these options:

• Character vector or string scalar of a polynomial whose constant term is 1. For more information, see Representation of Polynomials in Communications Toolbox.

• Binary-valued row vector that represents the coefficients of the polynomial in order of descending powers. The length of this vector must be N + 1, where N is the degree of the polynomial. The first and last entries must be 1, indicating the leading term with degree N and a constant term of 1.

• Integer-valued row vector of elements that represent the exponents for the nonzero terms of the polynomial in order of descending powers. The last entry must be 0, indicating a constant term of 1.

Example: 'z^8 + z^2 + 1', [1 0 0 0 0 0 1 0 1], and [8 2 0] represent the same polynomial, p(z) = z8 + z2 + 1.

Data Types: double | char

Source of the initial conditions used for the shift register of the PN sequence, specified as one of these values:

• 'Property' — Specify PN sequence generator initial conditions by using the InitialConditions property.

• 'Input port' — Specify PN sequence generator initial conditions by using the initcond input argument.

Data Types: char

Initial conditions used for the shift register of the PN sequence generator when the simulation starts, specified as a binary-valued scalar or binary-valued vector.

• If you set this property to a scalar, the initial value of all cells in the shift register are the specified scalar value.

• If you set this property to a vector, each element of the vector corresponds to the initial value of the corresponding cell in the shift register. The length of the vector must equal the degree of the generator polynomial specified by the Polynomial property.

Note

For the object to generate a nonzero sequence, at least one element of the initial conditions for the first or second preferred PN sequence generator must be nonzero. Specifically, the initial state of at least one of the shift registers must be nonzero.

#### Dependencies

To enable this property, set InitialConditionsSource to 'Property'.

Data Types: double

Source of the mask that determines the shift of the PN sequence, specified as one of these:

• 'Input port' — Specify the mask by using the maskvec input argument.

Data Types: char

Mask that determines how the PN sequence is shifted from its starting point, specified as an integer scalar or a binary vector.

• When you set this property to an integer scalar, the value is the length of the shift. The object wraps shift values that are negative or greater than the length of the PN sequence.

• When you set this property to a binary vector, its length must equal the degree of the generator polynomial specified by the Polynomial property.

#### Dependencies

To enable this property, set MaskSource to 'Property'.

Enable variable-size outputs, specified as a numeric or logical 0 (false) or 1 (true). To enable variable-size outputs by using the outputsize input argument, set this property to true.The enabled input specifies the output size of the PN sequence. The input value must be less than or equal to the value of the MaximumOutputSize property.

When you set this property to false, the SamplesPerFrame property specifies the number of output samples.

Maximum output frame size, specified as a vector of the form [m 1], where m is a positive integer. The first element of the vector indicates the maximum length of the output frame and the second element of the vector must be 1.

Example: [20 1] specifies a maximum frame output size of 20-by-1.

#### Dependencies

To enable this property, set VariableSizeOutput to true.

Number of samples output per frame, specified as a positive integer. If you set this property to a value of M, then the object outputs M samples of a PN sequence that has a period of N = 2n – 1, where n represents the degree of the generator polynomial that the Polynomial specifies.

If you set the BitPackedOutput property to false, the samples are bits from the PN sequence. If you set the BitPackedOutput property to true, then the output corresponds to SamplesPerFrame groups of bit-packed samples.

Enable the generator reset input, specified as a numeric or logical 0 (false) or 1 (true). To enable the ability to reset the sequence generator using the resetseq input argument, set this property to true. This input resets the states of the PN sequence generator to the initial conditions specified in the InitialConditions property. For more information, see Resetting a Signal.

#### Dependencies

To enable this property, set InitialConditionsSource to 'Property'.

Option to output bit-packed words, specified as false or true. Set this property to true to enable bit-packed outputs.

When BitPackedOutput is true, the object outputs a column vector of length M, which contains most-significant-bit (MSB) first integer representations of bit words of length P. M is the number of samples per frame specified in the SamplesPerFrame property. P is the size of the bit-packed words specified in the NumPackedBits property.

Note

The first bit from the left in the bit-packed word contains the most significant bit for the integer representation.

Number of bits packed into each output data word, specified as an integer in the range [1, 32].

#### Dependencies

To enable this property, set BitPackedOutput to true.

Set this property to true to obtain signed, bit-packed, output words. In this case, a 1 in the most significant bit (sign bit) indicates a negative value. The property indicates negative numbers in a two's complement format.

#### Dependencies

To enable this property, set BitPackedOutput to true.

Output data type, specified as one of these values:

• When BitPackedOutput is set to false, OutputDataType can be 'double', 'logical', or 'Smallest unsigned integer'.

• When BitPackedOutput is set to true, OutputDataType can be 'double' or 'Smallest integer'.

Note

You must have a Fixed-Point Designer™ user license to use this property in 'Smallest unsigned integer' or 'Smallest integer' mode.

#### Dependencies

The valid settings for this property depend on the setting of the BitPackedOutput property.

## Usage

### Description

example

outSequence = pnSequence() outputs a frame of the PN sequence in a column vector based on the configured object.

outSequence = pnSequence(initcond) uses initcond as the initial conditions for the PN sequence of the linear-feedback shift register.

To enable this syntax, set the InitialConditionsSource property to 'Input port'.

outSequence = pnSequence(maskvec) uses the maskvec input to specify the mask vector that determines how the PN sequence is shifted from its starting point.

To enable this syntax, set the MaskSource property to 'Input port'.

outSequence = pnSequence(outputsize) uses outputsize as the output size.

To enable this syntax, set the VariableSizeOutput property to true.

outSequence = pnSequence(resetseq) uses resetseq as the reset signal.

To enable this syntax, set the InitialConditionsSource property to 'Property' and the ResetInputPort property to true.

To enable this syntax, set the InitialConditionsSource property to 'Input port', the ResetInputPort property to false, the MaskSource property to 'Input port', and the VariableSizeOutput property to true.

To enable this syntax, set the InitialConditionsSource property to 'Property', the MaskSource property to 'Input port', the VariableSizeOutput property to true, the ResetInputPort property to true.

### Input Arguments

expand all

Initial conditions used for the shift register when the simulation starts, specified as a binary-valued scalar or binary-valued vector.

• If you set this property to a scalar, the initial value of all cells in the shift register are the specified scalar value.

• If you set this input to a vector, each element of the vector corresponds to the initial value of the corresponding cell in the shift register. The length of the vector must equal the degree of the generator polynomial specified by the Polynomial property.

Note

For the object to generate a nonzero sequence, at least one element of the initial condition for the PN sequence generator must be nonzero. Specifically, the initial state of at least one of the shift registers must be nonzero.

Example: outSequence = pnSequence([1 1 0]) corresponds to possible initial register states for a PN sequence generator specified by a generator polynomial of degree 3.

Data Types: double

Mask that determines how the PN sequence is shifted from its starting point, specified as a binary vector. The length of the vector must equal the degree of the Polynomial property.

Length of output sequence, specified as a nonnegative integer, n, or a vector of the form [n 1], where n is a positive integer. The first element of the vector indicates the length of the output frame and the second element of the vector must be 1.

The scalar or the first element of the row vector must be less than or equal to the first element of the MaximumOutputSize property value.

Reset sequence generator, specified as a scalar or a column vector with length equal to the number of samples per frame specified by the SamplesPerFrame property.

• When you specify this input as a nonzero scalar, the object resets to the specified initial conditions and then generates a new output frame.

• When you specify this input as a column vector, the object resets to the specified initial conditions at each sample in the output frame that aligns with a nonzero value in the reset vector.

### Output Arguments

expand all

PN sequence generated by the object, returned as a column vector.

## Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named obj, use this syntax:

release(obj)

expand all

 step Run System object algorithm release Release resources and allow changes to System object property values and input characteristics reset Reset internal states of System object

## Examples

collapse all

When configuring a PN sequence generator System object™ object, you have options regarding how to express the polynomial and the mask. This figure defines a PN sequence generator with a generator polynomial $\mathit{p}\left(\mathit{z}\right)={\mathit{z}}^{6}+\mathit{z}+1$ and a mask $\mathit{m}\left(\mathit{z}\right)={\mathit{z}}^{5}+{\mathit{z}}^{4}+{\mathit{z}}^{2}+1$. The example shows a few of the formatting options available to define the generator polynomial and the mask when you configure the PN sequence generator in this figure.

You can input the polynomial exponents of z for the nonzero terms of the polynomial in descending order of powers and the mask as a binary vector.

pnseq1 = comm.PNSequence('Polynomial',[6 1 0], ...
'Mask',[1 1 0 1 0 1],'SamplesPerFrame',20)
pnseq1 =
comm.PNSequence with properties:

Polynomial: [6 1 0]
InitialConditionsSource: 'Property'
InitialConditions: [0 0 0 0 0 1]
Mask: [1 1 0 1 0 1]
VariableSizeOutput: false
SamplesPerFrame: 20
ResetInputPort: false
BitPackedOutput: false
OutputDataType: 'double'

You can input the polynomial exponents as a binary-valued row vector that represents the coefficients of the polynomial in order of descending powers.

pnseq2 = comm.PNSequence('Polynomial',[1 0 0 0 0 1 1], ...
'Mask',[1 1 0 1 0 1],'SamplesPerFrame',20)
pnseq2 =
comm.PNSequence with properties:

Polynomial: [1 0 0 0 0 1 1]
InitialConditionsSource: 'Property'
InitialConditions: [0 0 0 0 0 1]
Mask: [1 1 0 1 0 1]
VariableSizeOutput: false
SamplesPerFrame: 20
ResetInputPort: false
BitPackedOutput: false
OutputDataType: 'double'

You can also define the mask as a scalar value using the mask2shift function.

mask2shift ([1 0 0 0 0 1 1],[1 1 0 1 0 1])
ans = 22
pnseq3 = comm.PNSequence('Polynomial',[1 0 0 0 0 1 1], ...
pnseq3 =
comm.PNSequence with properties:

Polynomial: [1 0 0 0 0 1 1]
InitialConditionsSource: 'Property'
InitialConditions: [0 0 0 0 0 1]
VariableSizeOutput: false
SamplesPerFrame: 20
ResetInputPort: false
BitPackedOutput: false
OutputDataType: 'double'

Use each PN sequence object to generate a frame of 20 samples and compare the generated sequences.

out_1 = pnseq1();
out_2 = pnseq2();
out_3 = pnseq3();
isequal(out_1,out_2)
ans = logical
1

isequal(out_1,out_3)
ans = logical
1

Generate a 14-sample frame of a maximal length PN sequence given generator polynomial, ${x}^{3}+{x}^{2}+1$.

Generate PN sequence data by using the comm.PNSequence object. The sequence repeats itself as it contains 14 samples while the maximal sequence length is only 7 samples (${2}^{3}-1$).

pnSequence = comm.PNSequence('Polynomial',[3 2 0], ...
'SamplesPerFrame',14,'InitialConditions',[0 0 1]);
x1 = pnSequence();
[x1(1:7) x1(8:14)]
ans = 7×2

1     1
0     0
0     0
1     1
1     1
1     1
0     0

Create another maximal length sequence based on the generator polynomial, ${x}^{4}+x+1$. As it is a fourth order polynomial, the sequence repeats itself after 15 samples (${2}^{4}-1$).

pnSequence2 = comm.PNSequence('Polynomial','x^4+x+1', ...
'InitialConditions',[0 0 0 1],'SamplesPerFrame',30);
x2 = pnSequence2();
[x2(1:15) x2(16:30)]
ans = 15×2

1     1
0     0
0     0
0     0
1     1
0     0
0     0
1     1
1     1
0     0
⋮

The comm.PNSequence system object implements a linear-feedback shift register (LFSR) using a simple shift register generator (SSRG, or Fibonacci configuration). This configuration differs from the modular shift register generator (MSRG, or Galois configuration) by a phase difference, that can be determined empirically from the system object.

This phase difference can be specified as the Mask parameter for the comm.PNSequence system object to generate the equivalent MSRG configuration output. The block diagram represents the implementation of a 5-bit LFSR in the Galois (MSRG) configuration.

Load the file GaloisLFSR. The file contains the following variables that define the properties and output PN sequence of the 5-bit Galois LFSR:

• polyVec: Generator polynomial

• polySize: Degree of the generator polynomial

• initStates: Initial conditions of the shift register

• pn_msrg: Output PN sequence of maximal length, from the 5-bit Galois LFSR

Generate PN sequence data by using the comm.PNSequence object with the same set of properties used to implement the 5-bit Galois LFSR. Compare this PN sequence with the output of the 5-bit Galois LFSR. The two sequences differ by a phase shift.

pnSequence = comm.PNSequence( ...
'Polynomial',polyVec, ...
'InitialConditions',initStates,...
'SamplesPerFrame',2^polySize-1);
pn = pnSequence();
isequal(pn,pn_msrg)
ans = logical
0

Compute the phase shift between the two configurations. Set the value of the Mask property based on this phase shift.

for i = 1:length(pn)
exp_pn = [pn(i:end);pn(1:(i-1))];
if isequal(exp_pn,pn_msrg)
break
end
end

Generate PN sequence data by using the comm.PNSequence system object with the modified Mask property value. Compare this sequence with the output of the 5-bit Galois LFSR. The two sequences are now equal.

pnSequence_mod = comm.PNSequence( ...
'Polynomial',polyVec, ...
'InitialConditions',initStates,...
'SamplesPerFrame',2^polySize-1);
pn_mod = pnSequence_mod();
isequal(pn_mod,pn_msrg)
ans = logical
1

expand all

## References

[1] Proakis, John G. Digital Communications. 3rd ed. New York: McGraw Hill, 1995.

[2] Lee, J. S., and L. E. Miller. CDMA Systems Engineering Handbook. Boston and London. Artech House, 1998.

[3] Golomb, S.W. Shift Register Sequences. Laguna Hills. Aegean Park Press, 1967.

## Extended Capabilities

### Blocks

Introduced in R2008a