{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# This is a Jupyter Notebook!\n", "Jupyter Notebooks are essentially interactive documents where you can have text (using the Markdown language) and Python code. It is a very powerful tool for analyzing, displaying, and presenting data. In this basic tutorial we will learn some very basic concepts about Python programming language. For more details, we refer to the official site [www.python.org](www.python.org)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Markdown basics\n", "By double-clicking this cell you will learn the basic syntaxt of Markdown.\n", "\n", "## This is a level 2 heading\n", "### This is a level 3 heading\n", "This is some plain text that forms a paragraph.\n", "Add emphasis via **bold** and __bold__, or *italic* and _italic_.\n", "Paragraphs must be separated by an empty line.\n", "\n", "* This is the first item in a list.\n", "* Item 2.\n", "\n", "You can also include hyperlinks [to a website](http://cis.jhu.edu/~bbejar/bmds/)\n", "\n", "Inline code uses single backticks: `foo()`, and code blocks use triple backticks:\n", "```\n", "# My first Python program\n", "print(\"Hello World!\")\n", "```\n", "\n", "You can display math equations using LaTeX:\n", "\n", "$$f(x) = \\sin(x)$$\n", "\n", "Adding an image is easy: ![JHU BME](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAV4AAACQCAMAAAB3YPNYAAAAolBMVEX///8JLHQAAGcAAGIAIG8AGm0AAGWgqMAAC2kAH2/5+vwAAGB6haiFj68ACGkAJXEAEGrj5u4AFWzv8fWWnbdbaJSvtcr19/nc4OkAKHIcNnjFytkAAFrp6/HQ1eG0us06TIRzf6TM0N1MXpBodZ6YoLuMlrSyucy+w9NkcpxAU4kQM3mnrsUrQ4AdOXtvfKMAAFRVZJMwR4MlPn0+U4tIW4+yRpiKAAAOvUlEQVR4nO2ba5eiOBCGY+QmckcFmgYEhcb2wqrj//9rW5WggNoze85O92zvyfuh5ZKQ8JBUKpU0IUJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJC/wnpbvGmmuv5+yGSLYUqlhwd3udNpWZFov/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YG4E/Ar8maOBshW/uaXdqc+K8T0N7Qf3lnJiV5JVe8rHGa0m1oI0nQ7v9xue2t2KjA8VbMIE9PkG7OPY7pH4n5hWcF8CrLny2jvln4XwyQIscmXo/ZYiO+/QLc4Aqsr84GFWaVY6D+gw2TESYzEGU8Zvvp4CMx3zdkb6rgV2rNxg4DzAkk3bBq7NDWBFv8Wu5fWCLjxlwBbml8Nu5dsI2jYf2BUSr4PWGdeF0MNp5l7DPsDZblD622VCUxftpxwvW6MSuotLpeo5/rNnll4kGxzgviZiQwGyMh5XptlsbKh4O5oZoqMtyt19iSfaOpzE1B1A16dlWTu5ClqaoNtyJZ0xiVcUzJouF79Pg4sFubVWMmpGhwEbNam357LzQafF5J0hyrAk9JmzU8Wc83SWLA00nd8JVW1hvIgj3oO//HnJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkND/Q38DKgg9koss9REAAAAASUVORK5CYII=)\n", "\n", "Further details can be found in this guide [Markdown Guide](https://www.markdownguide.org/basic-syntax/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python basics\n", "\n", "We now give some basic examples on how to use and manipulate variables in Python.\n", "\n", "### Numbers and arithmetic operations\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Comments in Python start with #\n", "\n", "\"\"\"\n", "\n", "Block comments use triple quotation marks.\n", "And can contain line-breaks\n", "\n", "\"\"\"\n", "\n", "# variables can be defined\n", "a = 2 # this is a float\n", "b = 3\n", "\n", "# we can now do some operations on those\n", "c = a + b\n", "print(\"The value of a + b is %f\"%(c))\n", "\n", "# we can cast types, sometimes we will work with integers\n", "c = int(a+b)\n", "print(\"The value of a + b is %d\"%(c))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists\n", "\n", "Lists are very powerful objects that can contain any other Python object, even of different type even though we will typically use lists to contain objects of the same type. You can easily iterate over the elements of a list using a *for* loop." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Lists are defined using brackets\n", "mylist = [] # this is an empty list\n", "\n", "# a list of numbers and strings\n", "mylist = [1,2,3,'five']\n", "\n", "print(\"This is a list of %d elements\\n\"%(len(mylist)))\n", "\n", "# iterate over a list\n", "for item in mylist[:]:\n", " # here item already contains an idexed element\n", " print(item)\n", "\n", "# leave a blank line\n", "print('')\n", "\n", "# add a new element to the list\n", "mylist.append('six')\n", "\n", "# iterate using range\n", "for ii in range(len(mylist)):\n", " # here ii is just a counter, we need to access the list to get its value\n", " print(mylist[ii])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 1.** Create a sequence (list) of numbers corresponding to sampled values of the function $\\sin(2\\pi t)$ over one period, and with a sampling frequency $f_s = 8$ Hz. In other words, create a sequence:\n", "\n", "$$x_n = \\sin(2\\pi n T),\\quad n=0,1,\\ldots,\\lfloor f_s\\rfloor,\\quad T = \\frac{1}{f_s},$$\n", "\n", "where $\\lfloor x \\rfloor$ denotes the largest integer smaller than $x$. Print the sequence of values. For evaluating math functions you can use the `math` module of Python. You will need to import the corresponding module before invoking the `sin()` function as shown in the example below:\n", "```\n", "import math\n", "\n", "x = math.sin(math.pi/2)\n", "\n", "print(x)\n", "\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# import math module to call sin() function\n", "import math\n", "\n", "# evaluate and print sin(pi/2)\n", "print(math.sin(math.pi/2))\n", "\n", "# code for Exercise 1 starts here\n", "# ...\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Modules and basic plotting\n", "Many libraries and added functionalities are available in Python through \"modules\". A module contains a library of objects and functions with specialized purpose. For instance, the `numpy` module allows you to do vector-oriented numerical manipulations similar to MATLAB. Another module that is relevant for this class is the scikit-learn `sklearn` library which implements many machine learning methods for data analysis, clustering, and classification. In order to be able to use a module you should use the `import` command:\n", "\n", "```\n", "# this command imports all numpy library and it will be referred to as np\n", "import numpy as np\n", "\n", "# create an 10x1 dimensional array of zeros\n", "zero_array = np.zeros((10,1))\n", "\n", "# the command below import the svm object classifier from sklearn\n", "from sklearn import svm\n", "\n", "```\n", "You should search the documentation for specific modules in order to see how to use it.\n", "\n", "Data visualization is crucial for any data analysis task. In Python we have the `matplotlib` library to produce nice plots. The `seaborn` module is yet another module built on top of `matplotlib` that produces nice plots with an easy interface." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# import modules\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "# this line is needed to display the plots in the notebook\n", "%matplotlib inline\n", "\n", "# create a sinusoidal signal\n", "N = 64\n", "t = np.arange(N)\n", "x = np.sin(2*np.pi*t/N)\n", "\n", "# display signal\n", "plt.plot(t/N,x)\n", "plt.legend([r'$\\sin(2\\pi t)$'])\n", "plt.xlabel('time [s]')\n", "plt.ylabel('amplitude [V]')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions\n", "Functions in Python are defined using the keyword `def`. Below, you have an example on how to define a function that computes the average of two numbers and returns the result of averaging.\n", "```\n", "def mean(a,b):\n", " mval = (a + b)/2\n", " return mval\n", "```\n", "Please, keep in mind indentation. Python is very strict about indentation and your code won't compile if not properly indented.\n", "\n", "You can now call your function:\n", "\n", "```\n", "mean_value = mean(1,2)\n", "\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 2.** Create two sequences $x_n$ and $y_n$ by sampling $\\sin(2\\pi t)$ and $\\cos(4\\pi t)$ with sampling rate $f_s = 64$ Hz, and store them in two separate lists. Write a function `add_sequence(x,y)` that returns the addition of the two lists. Plot the result of the addition in a graph adding the corresponding labels for the axes.\n", "\n", "Repeat the addition task before using vector array operations with the `numpy` module." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# implement Exercise 2 here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Digital signal processing\n", "A digital signal can be thought as a function defined over a discrete (but possibly infinite) set. For instance, we can obtain a digital signal by recording (sampling) a continuous-time signal at regular sampling intervals. Digital signal processing is a discipline at the foundations of Data Science that deals with the representation, information extraction (estimation), and reconstruction of digital signals. A very powerful tool to the analysis and manipulation of discrete signals is to use linear shift-invariant operators also called filters (see background notes on digital signal processing in the manual). We will get familiar to basic filtering operations using the `numpy` module.\n", "\n", "### Filtering and convolution\n", "The input-output relationship of a linear shift-invariant filter is determined by the __convolution__ of the input signal $x_n$ with the impulse response of the filter $h_n$:\n", "\n", "$$y_n = x_n\\ast h_n = \\sum_{k\\in\\mathbb{Z}} h_k x_{n-k} = \\sum_{k\\in\\mathbb{Z}} x_k h_{n-k}$$\n", "\n", "### Discrete-time Fourier Transform (DTFT)\n", "Similarly to continuous signals, we can also define the Fourier transform for discrete signals. For a sequence $x_n$, its DTFT $X(e^{j\\omega})$ is a continuous and $2\\pi$ periodic signal given by:\n", "\n", "$$X(e^{j\\omega}) = \\sum_{n\\in\\mathbb{Z}}x_n e^{-j\\omega n}$$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 3.** Using the definition of the DTFT show the property that convolution in time is equivalent to multiplication in the frequency domain. In other words, show that for $y_n = x_n\\ast h_n$, its DTFT $Y(e^{j\\omega}) = X(e^{j\\omega})H(e^{j\\omega})$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Discrete Fourier Transform (DFT)\n", "In practice we will be working with finite length (periodic) sequences. For a sequence of $N$ samples of length, we define its DFT as:\n", "\n", "$$X_k = \\sum_{n=0}^{N-1} x_n e^{-j2\\pi\\frac{nk}{N}},\\quad k=0,\\ldots,N-1.$$\n", "\n", "Similarly, its inverse operation IDFT is computed as:\n", "\n", "$$x_n = \\sum_{k=0}^{N-1} X_k e^{j2\\pi\\frac{nk}{N}},\\quad n=0,\\ldots,N-1.$$\n", "\n", "We can compute the DFT of a seuquence and its inverse using the Fast Fourier Transform (FFT) algorithm, which is an efficient method for computing the DFT. We can compute the (I)DFT using `numpy` as illustrated in the example below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# import modules\n", "import numpy as np\n", "\n", "# length of the signal\n", "N = 64\n", "\n", "# create an empy signal\n", "x = np.zeros(N)\n", "# fill-in with ones the first 10 entries\n", "x[0:10] = 1\n", "\n", "# display the sequence\n", "plt.figure()\n", "plt.stem(x)\n", "plt.xlabel('n')\n", "plt.ylabel('amplitude')\n", "\n", "# compute the FFT\n", "X = np.fft.fft(x)\n", "\n", "# display real and imaginary parts\n", "plt.figure()\n", "plt.plot(np.real(X))\n", "plt.plot(np.imag(X))\n", "plt.xlabel('k')\n", "plt.ylabel('amplitude')\n", "plt.legend(['real','imag'])\n", "\n", "# retrieve the signal back (take the real part to remove numerical errors)\n", "x = np.real(np.fft.ifft(X))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 4.** Generate two box sequences as defined below:\n", "\n", "$$x_n = \\begin{cases}1 &0\\leq n\\leq 10\\\\0 &\\textrm{else}\\end{cases},\\quad h_n = \\begin{cases}1 &0\\leq n\\leq 20\\\\0 &\\textrm{else}\\end{cases},\\quad n = 0,\\ldots,63.$$\n", "\n", "Compute $y_n = x_n \\ast h_n$ using `numpy.convolve` and plot (`plt.stem`) the resulting sequence $y_n$. Compute the DFT $Y_k$ and plot its **magnitude** over the frequency range $[-\\pi,\\pi]$. _(Hint. Use `numpy.fft.fftshift`)_. Label the horizontal and vertical axes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# implement Exercise 4 in this cell\n", "# ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Random signal generation\n", "In many situations it is of practical importance to generate signals at random (e.g., testing different noise conditions, simulating different realizations of the same experiment, etc). We can use the random submodule of numpy for generating random signals with different distributions [numpy.random](https://docs.scipy.org/doc/numpy-1.16.1/reference/routines.random.html). Below you have some examples on how to generate numbers following different distributions:\n", "```\n", "# import the sub-module\n", "import numpy.random as rnd\n", "\n", "# array of 10 random numbers from the interval [0,1)\n", "x = rnd.rand(10)\n", "\n", "# array of 10 random numbers from a standard Gaussian distribution\n", "x = rnd.randn(10)\n", "\n", "# array of 10 random integers from -2 to 3\n", "x = rnd.randint(-2,3,10)\n", "\n", "\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 5.** For this exercise, generate the sequence defined by the following finite difference equation:\n", "\n", "$$ x_n = \\alpha x_{n-1} + \\beta x_{n-2} + \\epsilon_{n},\\quad x_0=x_1=0,\\quad n=0,1,\\ldots,127, $$\n", "\n", "where the driving noise sequence $\\epsilon_{n}$ consists of independent and identically distributed random numbers from a standard, normal distribution `numpy.random.randn`. For the coefficients, generate three random pairs of $(\\alpha,\\beta)$ where $\\alpha,\\beta \\sim \\mathcal{U}(-1,1)$ (uniformly distributed between -1 and 1). Plot the three time-series on the same figure. Include a title, axis labels, and a legend that lists the generated $( \\alpha,\\beta)$ values of each time-series." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# fix random number generator seed (reproducibility)\n", "np.random.seed(10)\n", "\n", "# complete Exercise 5 in this cell\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 6.** In this exercise you will perform a basic denoising operation by filtering a noisy signal with a low-pass filter. You are provided with a function `get_noisy_signal()` that returns a signal corrupted with noise. Let a Gaussian filter $g_n$ be defined as:\n", "\n", "$$g_n = \\frac{1}{K}e^{-(n/\\sigma)^2},\\quad -10\\leq n\\leq10,$$\n", "\n", "where $K$ is a constant such that the weights of the filter add up to 1, and where $\\sigma$ is a parameter. Filter the signal for three different values of $\\sigma=1,5,10$. Observe the trade-off between denoising and sharpness of the signal. Compare the original signal with its denoised versions by plotting them in the same figure. Add labels and legends as appropriate. Why do we loose sharp transitions when filtering with a wider kernel?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# fix random number generator seed (reproducibility)\n", "np.random.seed(10)\n", "\n", "# stepwise function with noise\n", "def get_noisy_signal(s=0.2):\n", " \"\"\"\n", " This function generates a signal with a varying amount of noise. The noise\n", " variance is controlled via the input parameter s [s=1 default].\n", " \"\"\"\n", " \n", " # generate signal\n", " N = 128\n", " t = np.arange(N)\n", " x = np.sin(2*np.pi*t/N)\n", " x[80:105] -= 1\n", " \n", " # generate noise\n", " n = s*np.random.randn(128)\n", " \n", " # return noisy signal\n", " return x+n\n", "\n", "# complete Exercise 6 starting here\n", "# ..." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }