\n", "\n", "

" ] }, { "cell_type": "markdown", "id": "83f2c1be-aa58-4084-a963-1d94aa48d236", "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Applications of Pseudo-Random Binary Sequences\n", "\n", "PN sequences are widely used to\n", "\n", "* generate test, measurement and calibration signals\n", "* generate training signals in communication systems\n", "* scramble and descramble data in communication systems" ] }, { "cell_type": "markdown", "id": "7f2c4098-4131-4ce4-aeca-43ce0f008f8b", "metadata": { "tags": [ "remove-nb-cell" ] }, "source": [ "### As a signal for test, measurement, and calibration\n", "The first application is to use a PN sequence to estimate an impulse response of an unknown subsystem, e.g. the cascade of a source, empty chamber, and receiver in a biomedical instrument. Once the impulse response is known, calibration of the system can proceed by means of a linear time-invariant filter in the transmitter (known as a predistorter) or receiver (known as an equalizer) to compensate for the distortion in the subsystem.\n", "\n", "### As a training signal\n", "As a training signal, the PN sequence would be the digital data to be transmitted over the unknown communication channel. The receiver knows the bits that had been transmitted, and can use that knowledge to adapt receiver settings to improve communication performance (signal quality). The receiver can also estimate the impulse response of the communication channel if needed.\n", "\n", "### Data scrambler/descrabler\n", "\n", "The primary application of PN sequences in lab #4 is for data scrambling and descrambling. As a data scrambling sequence, pseudonoise can (1) eliminate strong DC components (long strongs of zeros and ones) in the baseband signal and (2) disperse the power spectrum to mitigate the effects of certain channel impairments.\n", "\n", "1. **Eliminating DC components**\n", "\n", " During a long string of zeros or ones, the primary frequency component in the signal would be at DC. For baseband transmission, this would not get passed by some communication channels (e.g. voiceband and acoustic channels). Some automatic gain control algorithms will also fail in the presence of a strong DC components. For bandpass transmission, the DC component is upconverted to sit at the carrier frequency. Strong components at this frequency will hinder the clock recovery and symbol synchronization subsystems.\n", "\n", "2. **Mitigating channel impairments**\n", "\n", " If a baseband signal has most of its power concentrated at specific frequencies, scrambling with a PN sequence can help disperse the spectrum to mitigate certain channel impairments. For example, a common impairment caused by nonlinearity or time-variance is intermodulation, or cross-modulation. The effect of intermodulation is greatly reduced by dispersing the power spectrum and eliminating signal components concentrated at specific frequencies." ] }, { "cell_type": "markdown", "id": "933cb6df-8906-4f0f-b50c-1b6947c869a3", "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Linear-feedback shift register (LFSR)" ] }, { "cell_type": "markdown", "id": "3ed0b6f6-1109-401a-a81d-aeba40b1b7fd", "metadata": { "tags": [ "remove-nb-cell" ] }, "source": [ "In Lab #4, we will use a a Fibonacci LFSR to (1) generate PN sequences, (2) scramble data, and (3) descramble data. This structure shares many similarities to the tapped delay line for filtering. Like the tapped delay line, the Fibonacci LFSR maintains a delay line of $m$ elements $s[n-1], s[n-2], \\ldots, s[n-m]$. Unlike the tapped delay line for LTI filters, the values are restricted to be binary (zero or one). Instead of real-valued coefficients, the LFSR has a set of taps $h_1, h_2, \\ldots, h_m$ which also have a binary state (connected or disconnected). Since all values are binary, additions are performed modulo two (equivalent to an exclusive or)." ] }, { "cell_type": "markdown", "id": "faf5c67e-5681-41c7-ad69-16fc48bc3657", "metadata": { "tags": [ "remove-nb-cell" ] }, "source": [ "![](../img/Fibonacci_LFSR.svg)" ] }, { "cell_type": "markdown", "id": "1de93d0b-57e4-412b-876a-353d7e8b7d9f", "metadata": { "tags": [ "remove-cell" ] }, "source": [ "\n", "\n", "

" ] }, { "cell_type": "markdown", "id": "66faeff9-0087-4280-a658-74b4ce8788e3", "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "### LFSR for PN sequence generation\n", "\n", "The procedure to produce a PN sequence using the LFSR consists of three steps:\n", "\n", "1. Set the initial values $s[-1], s[-2], \\ldots, s[-m]$ so that at least one is nonzero.\n", "\n", "2. Compute the addition modulo 2, equivalent to a series of exclusive or ($\\oplus$) operations, of the connected taps in the shift register:\n", "\n", " $$ s[n] = \\left( \\sum_{k=1}^{m}{h_k s[n-k]} \\right) \\text{ mod } 2 = \\bigoplus_{k=1}^{m}{h_k s[n-k]}$$\n", "\n", "3. Shift each value along the delay line (i.e. increment n), then repeat starting from step two." ] }, { "cell_type": "markdown", "id": "b88e3cef-4804-424d-af9e-b888d9e52b58", "metadata": { "tags": [ "remove-nb-cell" ] }, "source": [ "With an appropriate choice of connected taps, the LFSR state will cycle through each of the $2^m - 1$ possible (nonzero) combinations of $m$ bits. In this case, we call the system a maximal-length LFSR, and $s[n]$ is a $(2^m -1)$-periodic PN sequence.\n", "\n", "Determining mathematically if a combination of taps produces a maximal length sequence is difficult. However, for engineering applications, we can use configurations which are known to provide this property. For example, the following configuration results in a maximal length PN sequence with period $2^3 -1 = 7$." ] }, { "cell_type": "markdown", "id": "03c35689-4dd5-4065-a5d0-586c641ef9a1", "metadata": { "tags": [ "remove-nb-cell" ] }, "source": [ "![](../img/example_LFSR.svg)" ] }, { "cell_type": "markdown", "id": "003af24b-eef0-4566-865a-f757b490392f", "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [ "remove-cell" ] }, "source": [ "\n", "\n", "

" ] }, { "cell_type": "markdown", "id": "b1ec420a-e0a2-495b-a956-9a2e19674660", "metadata": { "tags": [ "remove-nb-cell" ] }, "source": [ "The first period of the this LFSR is listed below." ] }, { "cell_type": "markdown", "id": "6bde7f37-4760-4633-bf5c-b9db310f4b6e", "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [ "remove-nb-cell" ] }, "source": [ "\n", "\n", "

" ] }, { "cell_type": "markdown", "id": "6b041717-cd21-4f16-a169-be7609ccd6b2", "metadata": { "tags": [ "remove-nb-cell" ] }, "source": [ "The auto-correlation for this PN sequence is equal to one at integer multiples of the period $N$ and is equal to $\\frac{-1}{N}$ for all other offsets. This behavior is the same for *all* maximal-length PN sequences, as long as the following two conditions are met:\n", "\n", "1. The values of the binary PN sequence are mapped to $\\left\\{ -1,1 \\right\\}$. We avoid representing the sequence with $\\left\\{ 0,1 \\right\\}$ because this would introduce a DC component.\n", "\n", "2. The auto-correlation is taken of the infinite-length, periodically extended sequence. The auto-correlation of just one period can also be computed, but its properties will be different." ] }, { "cell_type": "markdown", "id": "0ea15df7-3c69-4f28-9f8e-83ca2c52f26c", "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "### Data scrambling\n", "\n", "Suppose we have a binary data sequence $x[n]$ that we want to transmit. We can scramble the data by adding (modulo two) a scrambling sequence $s[n]$.\n", "\n", "$$x_s[n] = ( x[n] + s[n] ) \\text{ mod 2} = x[n] \\oplus s[n]$$" ] }, { "cell_type": "markdown", "id": "80fbbf03-f038-456e-96ac-6f8ed6d188fe", "metadata": { "tags": [ "remove-nb-cell" ] }, "source": [ "When $s[n]$ is a PN sequence, the scrambler will (1) eliminate strong DC components (long strongs of zeros and ones) in the baseband signal and (2) disperse the power spectrum to mitigate the effects of certain channel impairments. \n" ] }, { "cell_type": "markdown", "id": "c1b4267a-e612-4c4b-b02c-011b07a021fc", "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "We can 'undo' the scrambler (i.e. descramble the data) using the same process:\n", "\n", "$$x_d[n] = ( x_s[n] + s[n] ) \\text{ mod 2} = x_s[n] \\oplus s[n]$$\n", "\n", "If the same scrambling sequence $s[n]$ is used for the scrambler and descrambler, then $x_d[n]=x[n].$" ] }, { "cell_type": "markdown", "id": "89221921-0f14-419d-85c2-e4496712ad8e", "metadata": { "tags": [ "remove-nb-cell" ] }, "source": [ "By initializing two two LFSRs (one at the transmitter and one at the receiever) with the same initial conditions and connected taps $h_k$, this process can be made extremely efficient.\n", "\n", "## Implementation of LFSR in C\n", "\n", "In lab #4, you will implement the LFSR in C. To prepare, it is useful to review some topics in C programming.\n", "\n", "### Bits, words, and arrays\n", "\n", "Although it would be convenient to think of our generated PN sequence as an 'array of bits' there is no datatype in C that would allow this, since the smallest addressable unit of data in C is a byte (8 bits).\n", "\n", "Typically, we refer to the unit of addressable data in an array as a 'word'. For example, if we have an array of ```int16_t```s, then we would say that $1 \\text{ word} = 16 \\text{ bits}$. If the array is composed of ```float32_t```s, then $1 \\text{ word} = 32 \\text{ bits}$, and so on.\n", "\n", "If we need to address individual bits in a long sequence of data in C, we can instead create an array of conveniently sized words and use bitwise operations to access individual bits in each word.\n", "\n", "### Review of bitwise operations in C\n", "\n", "There are six bitwise operations in c: *and* `&`, *or* `|`, *xor* `^`, *left shift* `<<`, *right shift* `>>`, and *not*`~`.\n", "\n", "Most of these behave as you would expect, but the right shift `>>` is an exception. The behavior of the right shift ```>>``` operation depends on if the datatype is signed or unsigned, and varies depending on the compiler used. This is because there are two possible behaviors for the right shift operation\n", "\n", "1. In a logical right shift, the most significant bit receives a value of zero. This is the behavior that occurs when operating on an unsigned datatype.\n", "\n", "2. In an arithmetic right shift, the most significant bit (which is the sign bit for signed datatypes) is unchanged.\n", "\n", "*Takeaway: use unsigned datatypes when implementing the LFSR in C to ensure that all shifts are logical shifts.*\n", "\n", "For left shift operations, the least significant bit always receives a value of zero in C, regardless of the data type.\n", "\n", "### Addressing individual bits\n", "\n", "There are many ways to use the bitwise operations above to perform addressing of individual bits. The easiest way to demonstrate is with an example. Suppose we have a variable `x` in C with the datatype `uint32_t` and we want to perform the following operation:\n", "\n", "*Example operation: Set the 6th least significant bit equal to the xor of the 4th and 2nd least significant bits.* \n", "\n", "Let's outline one approach:\n", "\n", "1. Create expressions corresponding to the 4th and 2nd least significant bits shifted to the least significant position:\n", "\n", " `x>>3` and `x>>1`\n", "\n", "2. Calculate the bitwise XOR of these expressions:\n", "\n", " ```\n", " (x>>3) ^ (x>>1)\n", " ```\n", "\n", "3. Mask out all other bits\n", "\n", " ```\n", " ( (x >> 3) ^ (x >> 1) ) & 1\n", " ```\n", "\n", "4. Shift the expression to the desired position\n", "\n", " ```\n", " ( ( (x >> 3) ^ (x >> 1) ) & 1 ) << 5\n", " ```\n", "\n", "5. Clear the bit to prepare for assignment\n", "\n", " ```\n", " x &= ~(1<<5);\n", " ```\n", "\n", "6. Assign the result back to `x`\n", "\n", " ```\n", " x |= ( ( (x >> 3) ^ (x >> 1) ) & 1 ) << 5\n", " ```\n", "\n", "At this point, the code has become very difficult to read! Although whitespace does not change the behavior, it can make the code somewhat more readable.\n", "\n", "```\n", "x &= ~(1<<5);\n", "x |= (\n", " ( \n", " (x >> 3) ^ (x >> 1) \n", " ) & 1\n", " ) << 5\n", "```\n", "\n", "There are many other ways that this operation can be completed in C, so it can be helpful to try multiple approaches until you find a style you are comfortable with." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.2" } }, "nbformat": 4, "nbformat_minor": 5 }