{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to SciPy\n", "Tutorial at EuroSciPy 2019, Bilbao\n", "## 2. Signal analysis – Rocking motion of a TGV" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy import fftpack, signal\n", "%matplotlib notebook\n", "import matplotlib.pyplot as plt\n", "from matplotlib import cm\n", "from mpl_toolkits.mplot3d import Axes3D" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Importing the data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = np.genfromtxt('data/TGV_data.csv.bz2', delimiter=',', names=True)\n", "time = data['Time_s']\n", "omega_y = data['Gyroscope_y_rads']\n", "_ = plt.plot(time, omega_y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Time spacing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before analyzing the data any further, let us take a look at the time elapsed between subsequent measurements." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "time_intervals = time[1:]-time[:-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "_ = plt.hist(time_intervals, bins=100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will ignore these small differences and assume the data to be equally spaced in time with the mean time difference between subsequent data points." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "delta_t = np.mean(time_intervals)\n", "delta_t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Spectral analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fourier transformation of a discrete real signal with `rfft`:\n", "$$y_j = \\sum_{k=0}^{n-1} x_k \\exp\\left(-\\text{i}\\frac{2\\pi jk}{n}\\right)$$\n", "With exception of $y_0$ and, for even $n$, $y_{n/2}$ all $y_j$ are complex.\n", "`rfft` returns an array $y_0, \\text{Re}(y_1), \\text{Im}(y_1),\\ldots$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fft_y = fftpack.rfft(omega_y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "_ = plt.plot(fft_y[::2])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "_ = plt.plot(fft_y[1::2])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "freqs = fftpack.rfftfreq(omega_y.shape[0], delta_t)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "_ = plt.plot(freqs[::2], fft_y[::2])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "_ = plt.plot(freqs[1::2], fft_y[1::2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "