{ "cells": [ { "cell_type": "markdown", "id": "4e79abf9-61fd-4366-a378-94042a2ad09b", "metadata": {}, "source": [ "# RF Exercise" ] }, { "cell_type": "markdown", "id": "98a71e06-14df-4093-bc81-cd075778da29", "metadata": {}, "source": [ "You'll need the following `import`s for the tasks below." ] }, { "cell_type": "code", "execution_count": null, "id": "af0b284b-41ad-4cc4-aded-a8516a03904a", "metadata": { "tags": [] }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "from IPython.display import display\n", "from ipywidgets import interact, fixed\n", "\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "4647a59c-047b-46e0-aa5c-ba615b5e36ed", "metadata": {}, "source": [ "## Task 1 - FFT\n", "We start by creating the sum of at least two sinus frequences and plot them." ] }, { "cell_type": "markdown", "id": "406b5f2c-b697-46f0-94a7-f39ccf958c49", "metadata": {}, "source": [ "Start by creating the sinus curves. Adjust the frequencies as you like from the sample code below." ] }, { "cell_type": "code", "execution_count": null, "id": "4ce28b67-f975-4eae-9779-c04c16fd4d54", "metadata": {}, "outputs": [], "source": [ "f1 = 0.15 # Hz\n", "f2 = 0.5 # Hz\n", "t = np.linspace(0, 10, 150)\n", "s = np.sin(f1*2*np.pi*t) + np.sin(f2*2*np.pi*t)\n", "plt.plot(t, s)" ] }, { "cell_type": "markdown", "id": "b8ed5179-4791-4188-b836-513e811b33ee", "metadata": {}, "source": [ "Now it's up to you to use fft to transform the sinus from the time domain to the\n", "frequency domain and plot again. Can you see the frequencies you've created?" ] }, { "cell_type": "markdown", "id": "51a71a83-068b-412b-acce-ab127deba9a7", "metadata": {}, "source": [ "## Task 2 - IQ Sampling" ] }, { "cell_type": "markdown", "id": "f4bd2838-c906-4df9-8978-94e77056e33b", "metadata": {}, "source": [ "The function below takes the amplitude I of the in-phase component, a frequency and a number of samples. It plots a graphic representation of the in-phase component with the given parameters." ] }, { "cell_type": "code", "execution_count": null, "id": "d99659ec-68a9-4014-b536-af7e38384f4b", "metadata": {}, "outputs": [], "source": [ "def iq_plot(I, frequency=0.2, samples=150):\n", " t = np.linspace(0, 10, samples)\n", " i_s = I * np.cos(2 * np.pi * frequency * t)\n", " plt.plot(t, i_s, 'r-', label=\"I * cos(2 $\\pi$ f t)\")\n", " plt.legend()\n", " plt.xlabel('time')\n", " plt.ylim(-3, 3)" ] }, { "cell_type": "markdown", "id": "7102f906-3151-4f1a-87b4-5aa9fc07ad1f", "metadata": {}, "source": [ "To allow efficient experiments with the function above, the `interact` function allows calling the function repeatedly with the inputs being adjusted by UI controls. In our case, `I` can take any value between -2 and 2 in steps of 0.02. There won't be controls for the frequency or samples, since both are set to fixed." ] }, { "cell_type": "code", "execution_count": null, "id": "bffd4505-1ab3-45e6-bdea-67c98e6eb50a", "metadata": {}, "outputs": [], "source": [ "interact(iq_plot, I=(-2, 2, 0.02), frequency=fixed(0.2), samples=fixed(150))" ] }, { "cell_type": "markdown", "id": "3a09f852-8bb0-42ca-a3af-327bccbc601a", "metadata": {}, "source": [ "It is your task to extend the `iq_plot` function to also plot the quadrature component and the sum of the two components. Give functions a proper label so they are easy to identify by the given legend. Also, name the y axis \"amplitude\".\n", "Finally, add a second slider to the `interact` output that allows to adjust the quadrature component as well." ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 5 }