# Mixer math ```{contents} Contents :class: this-will-duplicate-information-and-it-is-still-useful-here ``` ## Up-conversion ![iq_mixer](../images/iq_mixer_symbol.plain.svg){align=center w=256px} At the I and Q ports of the up-conversion mixer we have signals $I(t)$ and $Q(t)$ at frequency $\omega_S$: ```{math} I(t) &= X_I \cos(\omega_S t) - Y_I \sin(\omega_S t) \\ &= A_I \cos(\omega_S t + \phi_I) \\ Q(t) &= X_Q \cos(\omega_S t) - Y_Q \sin(\omega_S t) \\ &= A_Q \cos(\omega_S t + \phi_Q) ``` where the following identities hold: ```{math} :label: eye_apxy \begin{cases} A &= \sqrt{X^2 + Y^2} \\ \phi &= \operatorname{atan2}(Y, X) \end{cases} \qquad \begin{cases} X &= A \cos{\phi} \\ Y &= A \sin{\phi} \end{cases} ``` At the R port of the mixer, assuming a carrier frequency $\omega_N$ and zero phase, we get: ```{math} R(t) = I(t) \cos(\omega_N t) - Q(t) \sin(\omega_N t) ``` where the minus sign is a convention. Expanding and rearranging, we get: ```{math} :label: main_rf R(t) &= R_L(t) + R_U(t) \\ R_L(t) &= \frac{1}{2}(X_I + Y_Q) \cos[(\omega_N -\omega_S)t] -\frac{1}{2}(X_Q - Y_I) \sin[(\omega_N -\omega_S)t] \\ R_U(t) &= \frac{1}{2}(X_I - Y_Q) \cos[(\omega_N +\omega_S)t] -\frac{1}{2}(X_Q + Y_I) \sin[(\omega_N +\omega_S)t] ``` where $R_L$ and $R_U$ are the lower and upper sidebands, respectively. ### Zero intermediate frequency When $\omega_S = 0$, at the I and Q ports of the mixer we get: ```{math} I(t) &= X_I = A_I \cos{\phi_I} \\ Q(t) &= X_Q = A_Q \cos{\phi_Q} ``` At the R port: ```{math} R(t) &= X_I \cos(\omega_N t) -X_Q \sin(\omega_N t) \\ &= A_I \cos{\phi_I} \cos(\omega_N t) -A_Q \cos{\phi_Q} \sin(\omega_N t) ``` #### Special case: same amplitude, same phase Choosing $A_I = A_Q = A$ and $\phi_I = \phi_Q = \phi$ we get: ```{math} R(t) &= A \cos{\phi} \cos(\omega_N t) -A \cos{\phi} \sin(\omega_N t) \\ &= \sqrt{2} A \cos{\phi} \cos(\omega_N t + \pi / 4 ) ``` and a total amplitude $|R| = \sqrt{2}A|\cos{\phi}|$. Note that the total phase is $\pi/4$, independent of $\phi$ (for $\phi \in [-\frac{\pi}{2},+\frac{\pi}{2}]$, otherwise there's a sign change). Some notable cases: | $\phi$ | $\|R\| \; / \; \sqrt{2}A$ | | :----: | :-------------------------------: | | $0$ | $1$ | | $\pi/6$ | $\sqrt{3} / 2$ | | $\pi/4$ | $1 / \sqrt{2}$ | | $\pi/3$ | $1 / 2$ | | $\pi/2$ | $0$ | #### Special case: same amplitude, $\pi/2$ phase difference Choosing $A_I = A_Q = A$, $\phi_I = \phi$ and $\phi_Q = \phi \mp \pi/2$ we get: ```{math} R(t) &= A \cos{\phi} \cos(\omega_N t) \mp A \sin{\phi} \sin(\omega_N t) \\ &= A \cos(\omega_N \pm \phi) ``` and a total phase $\pm\phi$, and amplitude $|R| = A$ independent of $\phi$. Note the inversion from $\mp$ to $\pm$. ### Single-sideband modulation (SSB) :::{admonition} Quick reference :class: tip To perform single-sideband modulation, set {math}`\phi_I` to whatever you want and: - {math}`\phi_Q = \phi_I + \pi/2` for lower sideband (LSB); - {math}`\phi_Q = \phi_I - \pi/2` for upper sideband (USB). ::: #### Lower sideband (LSB) To get $R_U=0$ in Eq. {eq}`main_rf`, we want ```{math} :label: lsb_sol \begin{cases} X_I - Y_Q &= 0 \\ X_Q + Y_I &= 0 \end{cases} ``` Then $I(t)$ and $Q(t)$ should have the same amplitude ```{math} A_I^2 = X_I^2 + Y_I^2 = Y_Q^2 + (-X_Q)^2 = A_Q^2 ``` let's call the amplitude $A$. We can rewrite Eq. {eq}`lsb_sol` as ```{math} \begin{cases} \cos{\phi_I} &= +\sin{\phi_Q} \\ \cos{\phi_Q} &= -\sin{\phi_I} \end{cases} ``` which is solved by $\phi_Q = \phi_I + \pi/2$. Substituting into $R_L(t)$ from Eq. {eq}`main_rf` we get: ```{math} R_L(t) &= X_I \cos[(\omega_N -\omega_S)t] + Y_I \sin[(\omega_N -\omega_S)t] \\ &= A \cos{\phi} \cos[(\omega_N -\omega_S)t] + A \sin{\phi} \sin[(\omega_N -\omega_S)t] \\ &= A \cos[(\omega_N -\omega_S)t - \phi ] ``` #### Upper sideband (USB) Analogous to the previous section. To get $R_L=0$ in Eq. {eq}`main_rf`, we want ```{math} :label: usb_sol \begin{cases} X_I + Y_Q &= 0 \\ X_Q - Y_I &= 0 \end{cases} ``` Then $I(t)$ and $Q(t)$ should have the same amplitude ```{math} A_I^2 = X_I^2 + Y_I^2 = (-Y_Q)^2 + X_Q^2 = A_Q^2 ``` let's call the amplitude $A$. We can rewrite Eq. {eq}`usb_sol` as ```{math} \begin{cases} \cos{\phi_I} &= -\sin{\phi_Q} \\ \cos{\phi_Q} &= +\sin{\phi_I} \end{cases} ``` which is solved by $\phi_Q = \phi_I - \pi/2$. Substituting into $R_U(t)$ from Eq. {eq}`main_rf` we get: ```{math} R_U(t) &= X_I \cos[(\omega_N +\omega_S)t] -Y_I \sin[(\omega_N +\omega_S)t] \\ &= A \cos{\phi} \cos[(\omega_N +\omega_S)t] -A \sin{\phi} \sin[(\omega_N +\omega_S)t] \\ &= A \cos[(\omega_N +\omega_S)t + \phi ] ``` ## Down-conversion ### General case At the R port of the mixer, we have an incoming signal $R(t)$ at frequency $\omega_R$: ```{math} R(t) &= X_R \cos(\omega_R t) - Y_R \sin(\omega_R t) \\ &= A_R \cos(\omega_R t + \phi_R) ``` where the identities of Eq. {eq}`eye_apxy` hold. At the I and Q ports of the down-conversion mixer we get the signals $I(t)$ and $Q(t)$, assuming a carrier frequency $\omega_N$ and zero phase: ```{math} I(t) &= +R(t) \cos(\omega_N t) \\ Q(t) &= -R(t) \sin(\omega_N t) ``` where the minus sign is a convention. Expanding and rearranging, we get: ```{math} 2I(t) &= X_R \cos[(\omega_R -\omega_N)t] - Y_R \sin[(\omega_R -\omega_N)t] \\ &= A_R \cos[(\omega_R -\omega_N)t + \phi_R] \\ 2Q(t) &= Y_R \cos[(\omega_R -\omega_N)t] + X_R \sin[(\omega_R -\omega_N)t] \\ &= A_R \cos[(\omega_R -\omega_N)t + \phi_R - \frac{\pi}{2}] ``` where we have omitted high-frequency components at frequency $\omega_R+\omega_N$. See the section [](#spurs-in-down-conversion) below for a complete analysis of those spurious components. ### Zero intermediate frequency In the special case when the incoming RF signal and down-conversion carrier have the same frequency $\omega_R = \omega_N$, the I and Q ports of the mixer output the two quadratures of the original baseband signal: ```{math} I(t) &= \frac{1}{2} X_R = \frac{1}{2} A_R \cos{\phi_R} \\ Q(t) &= \frac{1}{2} Y_R = \frac{1}{2} A_R \sin{\phi_R} ``` ### Single-sideband modulation (SSB) The incoming RF signal $R(t)$ could contain a lower $R_L$ and an upper $R_U$ sideband: ```{math} R(t) &= R_L(t) + R_U(t) \\ R_L(t) &= X_L \cos[(\omega_N -\omega_S)t] -Y_L \sin[(\omega_N -\omega_S)t] \\ R_U(t) &= X_U \cos[(\omega_N +\omega_S)t] -Y_U \sin[(\omega_N +\omega_S)t] ``` where the sidebands are offset by $\omega_S$ from the central carrier at frequency $\omega_N$. Typically we have that $\omega_S \ll \omega_N$. After demodulation in the IQ mixer with a carrier at frequency $\omega_N$, we get at the I and Q ports: ```{math} :label: ssb_down 2I(t) &= (X_L + X_U) \cos(\omega_S t) - (Y_U - Y_L) \sin(\omega_S t) \\ 2Q(t) &= (Y_L + Y_U) \cos(\omega_S t) - (X_L - X_U) \sin(\omega_S t) ``` where we have neglected high-frequency terms at $2\omega_N \pm \omega_S$. Eq. {eq}`ssb_down` can be summarized in terms of the X and Y quadratures of the measured signals at the I and Q ports: ```{math} :label: ssb_down_quad \begin{cases} X_I &= \frac{1}{2} (X_L + X_U) \\ Y_I &= \frac{1}{2} (Y_U - Y_L) \\ X_Q &= \frac{1}{2} (Y_L + Y_U) \\ Y_Q &= \frac{1}{2} (X_L - X_U) \end{cases} ``` Finally, we can invert Eq. {eq}`ssb_down_quad` to obtain the X and Y quadratures of the incoming RF signal: ```{math} :label: ssb_down_quad_inv \begin{cases} X_L &= X_I + Y_Q \\ Y_L &= X_Q - Y_I \\ X_U &= X_I - Y_Q \\ Y_U &= X_Q + Y_I \end{cases} ``` The utility function {func}`.utils.untwist_downconversion` uses the relations in Eq. {eq}`ssb_down_quad_inv` to calculate the original upper and lower sidebands from the measured I and Q signals. ## Spurs in down-conversion We described in the previous section [](#down-conversion) how the down-conversion process generates low-frequency components at frequency $\omega_R - \omega_N$, where $\omega_R$ is the frequency of the incoming RF signal and $\omega_N$ is the frequency of the down-converting carrier generator. These components are output from the I and Q ports, and we'll call them $I_{diff}(t)$ and $Q_{diff}(t)$. However, the down-conversion process generates also high-frequency components at frequency $\omega_R + \omega_N$, which we'll call $I_{sum}(t)$ and $Q_{sum}(t)$ and can be viewed as spurious signals. Overall, we have that $I(t) = I_{diff} + I_{sum}$ and $Q(t) = Q_{diff} + Q_{sum}$. In the [previous section](#down-conversion) we neglected the high-frequency components, so we had assumed $I(t) \approx I_{diff}$ and $Q(t) \approx Q_{diff}$. Typically, we select a carrier frequency very close to the signal frequency, i.e. $\omega_R-\omega_N \ll \omega_R+\omega_N$, so $I_{diff}$ and $I_{sum}$ are very far apart in frequency space. In external, analog frequency down-conversion with IQ mixers and local oscillators the terms $I_{sum}$ and $Q_{sum}$ at frequency $\omega_R+\omega_N$ fall outside the analog bandwidth of the I and Q ports of the mixer and are heavily attenuated, so are usually negligible. In Presto, however, the frequency down-conversion happens in the digital domain and therefore the terms at $\omega_R+\omega_N$ are not attenuated: $I_{sum}$ and $Q_{sum}$ are still present in the data, and are possibly aliased down to a lower frequency. In this section, we will rewrite the [](#down-conversion) equations without neglecting the high-frequency spurious terms. ### General case At the R port of the mixer, we have an incoming signal $R(t)$ at frequency $\omega_R$: ```{math} R(t) &= X_R \cos(\omega_R t) - Y_R \sin(\omega_R t) \\ &= A_R \cos(\omega_R t + \phi_R) ``` At the I and Q ports of the down-conversion mixer we get the signals $I(t)$ and $Q(t)$, assuming a carrier frequency $\omega_N$ and zero phase: ```{math} I(t) &= +R(t) \cos(\omega_N t) \\ Q(t) &= -R(t) \sin(\omega_N t) ``` where the minus sign is a convention. Expanding and rearranging, we get: ```{math} I(t) &= I_{diff}(t) + I_{sum}(t) \\ 2I_{diff}(t) &= X_R \cos[(\omega_R -\omega_N)t] - Y_R \sin[(\omega_R -\omega_N)t] \\ &= A_R \cos[(\omega_R -\omega_N)t + \phi_R] \\ 2I_{sum}(t) &= X_R \cos[(\omega_R +\omega_N)t] - Y_R \sin[(\omega_R +\omega_N)t] \\ &= A_R \cos[(\omega_R +\omega_N)t + \phi_R] ``` Similarly, for $Q(t)$ we get: ```{math} Q(t) &= Q_{diff}(t) + Q_{sum}(t) \\ 2Q_{diff}(t) &= Y_R \cos[(\omega_R -\omega_N)t] + X_R \sin[(\omega_R -\omega_N)t] \\ &= A_R \cos[(\omega_R -\omega_N)t + \phi_R - \frac{\pi}{2}] \\ 2Q_{sum}(t) &= -Y_R \cos[(\omega_R +\omega_N)t] - X_R \sin[(\omega_R +\omega_N)t] \\ &= A_R \cos[(\omega_R +\omega_N)t + \phi_R + \frac{\pi}{2}] ``` ### Zero intermediate frequency When $\omega_R = \omega_N$, at the I and Q ports of the mixer we get: ```{math} I(t) &= I_{diff}(t) + I_{sum}(t) \\ 2I_{diff}(t) &= X_R = A_R \cos(\phi_R) \\ 2I_{sum}(t) &= X_R \cos(2 \omega_R t) - Y_R \sin(2 \omega_R t) \\ &= A_R \cos(2 \omega_R t + \phi_R) ``` ```{math} Q(t) &= Q_{diff}(t) + Q_{sum}(t) \\ 2Q_{diff}(t) &= Y_R = A_R \sin(\phi_R) \\ 2Q_{sum}(t) &= Y_R \cos(2 \omega_R t) - X_R \sin(2 \omega_R t) \\ &= A_R \cos(2 \omega_R t + \phi_R + \frac{\pi}{2}) ``` ### Single-sideband modulation (SSB) The incoming RF signal $R(t)$ could contain a lower $R_L$ and an upper $R_U$ sideband: ```{math} R(t) &= R_L(t) + R_U(t) \\ R_L(t) &= X_L \cos[(\omega_N -\omega_S)t] -Y_L \sin[(\omega_N -\omega_S)t] \\ R_U(t) &= X_U \cos[(\omega_N +\omega_S)t] -Y_U \sin[(\omega_N +\omega_S)t] ``` After demodulation in the IQ mixer with a carrier at frequency $\omega_N$, we get at the I and Q ports: ```{math} I(t) &= I_{diff}(t) + I_{sum}(t) \\ 2I_{diff}(t) &= (X_L + X_U) \cos(\omega_S t) - (Y_U - Y_L) \sin(\omega_S t) \\ 2I_{sum}(t) &= X_L \cos[(2\omega_N-\omega_S) t] - Y_L \sin[(2\omega_N-\omega_S) t] \\ &+ X_U \cos[(2\omega_N+\omega_S) t] - Y_U \sin[(2\omega_N+\omega_S) t] ``` ```{math} Q(t) &= Q_{diff}(t) + Q_{sum}(t) \\ 2Q_{diff}(t) &= (Y_L + Y_U) \cos(\omega_S t) - (X_L - X_U) \sin(\omega_S t) \\ 2Q_{sum}(t) &= -Y_L \cos[(2\omega_N-\omega_S) t] - X_L \sin[(2\omega_N-\omega_S) t] \\ &+ -Y_U \cos[(2\omega_N+\omega_S) t] - X_U \sin[(2\omega_N+\omega_S) t] ```