|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "8b85da6f", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import numpy as np\n", |
| 11 | + "import pynumdiff\n", |
| 12 | + "\n", |
| 13 | + "from pynumdiff.utils import simulate, evaluate\n" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "markdown", |
| 18 | + "id": "7a77ab05", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "# Generate testing data\n", |
| 22 | + "\n", |
| 23 | + "Here we amplify the usual signal to get outside the -pi to pi bound." |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "id": "e351d4d1", |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "noise_type = 'normal' # noise is generated using np.random, e.g. 'normal', 'uniform', 'poisson'\n", |
| 34 | + "noise_parameters = [0, 0.5] # compatible with np.random functions \n", |
| 35 | + "random_seed = 1\n", |
| 36 | + "\n", |
| 37 | + "dt = 0.01 # step size and series length in terms of independent variable\n", |
| 38 | + "duration = 4\n", |
| 39 | + "\n", |
| 40 | + "x, x_truth, dxdt_truth = simulate.lorenz_x(duration=duration, dt=dt, outliers=False,\n", |
| 41 | + " noise_type=noise_type, noise_parameters=noise_parameters)\n", |
| 42 | + "\n", |
| 43 | + "# amplify signal\n", |
| 44 | + "gain = 4\n", |
| 45 | + "x_truth *= gain\n", |
| 46 | + "dxdt_truth *= gain\n", |
| 47 | + "\n", |
| 48 | + "# add noise\n", |
| 49 | + "x = simulate._add_noise(x_truth, random_seed, noise_type, noise_parameters)\n", |
| 50 | + "\n", |
| 51 | + "# wrap to [-pi, pi]\n", |
| 52 | + "x = (x + np.pi) % (2*np.pi) - np.pi\n", |
| 53 | + "x_truth = (x_truth + np.pi) % (2*np.pi) - np.pi\n" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "markdown", |
| 58 | + "id": "a4ae1732", |
| 59 | + "metadata": {}, |
| 60 | + "source": [ |
| 61 | + "# Naive numerical differentiation (without considering wrapping)" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": null, |
| 67 | + "id": "cfd04e33", |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [], |
| 70 | + "source": [ |
| 71 | + "x_hat, dxdt_hat = pynumdiff.kalman_smooth.rtsdiff(x, dt, 1, 5, True, axis=0, circular=False)\n", |
| 72 | + "x_hat_wrapped = (x_hat + np.pi) % (2*np.pi) - np.pi\n", |
| 73 | + "\n", |
| 74 | + "evaluate.plot(x, dt, x_hat_wrapped, dxdt_hat, x_truth, dxdt_truth)\n" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "markdown", |
| 79 | + "id": "e9a43fff", |
| 80 | + "metadata": {}, |
| 81 | + "source": [ |
| 82 | + "# Now with circular=True\n" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": null, |
| 88 | + "id": "17083a61", |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "x_hat, dxdt_hat = pynumdiff.kalman_smooth.rtsdiff(x, dt, 1, 3, True, axis=0, circular=True)\n", |
| 93 | + "x_hat_wrapped = (x_hat + np.pi) % (2*np.pi) - np.pi\n", |
| 94 | + "\n", |
| 95 | + "evaluate.plot(x, dt, x_hat_wrapped, dxdt_hat, x_truth, dxdt_truth)\n" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "markdown", |
| 100 | + "id": "a49062c2", |
| 101 | + "metadata": {}, |
| 102 | + "source": [ |
| 103 | + "# Test multidimensional" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": null, |
| 109 | + "id": "baf0a4a0", |
| 110 | + "metadata": {}, |
| 111 | + "outputs": [], |
| 112 | + "source": [ |
| 113 | + "v, v_truth, dvdt_truth = simulate.triangle(duration=duration, dt=dt, outliers=False,\n", |
| 114 | + " noise_type=noise_type, noise_parameters=noise_parameters)\n" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": null, |
| 120 | + "id": "9dc4418f", |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [], |
| 123 | + "source": [ |
| 124 | + "X = np.vstack((x, v)).T\n", |
| 125 | + "print('Shape:', X.shape)\n" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": null, |
| 131 | + "id": "67ffcb8c", |
| 132 | + "metadata": {}, |
| 133 | + "outputs": [], |
| 134 | + "source": [ |
| 135 | + "# Differentiate circular and non-circular dimensions separately\n", |
| 136 | + "x_hat_col, dxdt_hat_col = pynumdiff.kalman_smooth.rtsdiff(X[:,0], dt, 1, 3, True, circular=True)\n", |
| 137 | + "v_hat_col, dvdt_hat_col = pynumdiff.kalman_smooth.rtsdiff(X[:,1], dt, 1, 3, True)\n", |
| 138 | + "\n", |
| 139 | + "x_hat_wrapped = (x_hat_col + np.pi) % (2*np.pi) - np.pi\n", |
| 140 | + "evaluate.plot(x, dt, x_hat_wrapped, dxdt_hat_col, x_truth, dxdt_truth)\n" |
| 141 | + ] |
| 142 | + } |
| 143 | + ], |
| 144 | + "metadata": { |
| 145 | + "kernelspec": { |
| 146 | + "display_name": "Python 3 (ipykernel)", |
| 147 | + "language": "python", |
| 148 | + "name": "python3" |
| 149 | + }, |
| 150 | + "language_info": { |
| 151 | + "codemirror_mode": { |
| 152 | + "name": "ipython", |
| 153 | + "version": 3 |
| 154 | + }, |
| 155 | + "file_extension": ".py", |
| 156 | + "mimetype": "text/x-python", |
| 157 | + "name": "python", |
| 158 | + "nbconvert_exporter": "python", |
| 159 | + "pygments_lexer": "ipython3", |
| 160 | + "version": "3.13.3" |
| 161 | + } |
| 162 | + }, |
| 163 | + "nbformat": 4, |
| 164 | + "nbformat_minor": 5 |
| 165 | +} |
0 commit comments