{"cells":[{"metadata":{"_uuid":"afb4f19034f03a29d7e9ef9e8d104ab76c6a8dd6"},"cell_type":"markdown","source":"# Signal processing with Scipy\nBelow, we will explore different methods for processing and cleaning signal with Scipy. I haven't worked extensively with signal processing, so if there is any expert out there who would have insight or suggestion in improving this work, please let me know!\n\n## About Scipy\n> SciPy (pronounced “Sigh Pie”) is open-source software for mathematics, science, and engineering.\n\n![scipy](https://docs.scipy.org/doc/scipy/reference/_static/img/scipy_org_logo.gif)\n\nScipy is an extremely useful library for scientific and numerical computing in Python. It contains very useful submodules for Optimization, Fast Fourier Transform, Linear Algebra, Matrix Encoding, and Image Processing. In fact, Scikit-learn uses it extensively for maniputating large sparse matrix, and for algorithms such as Ordinary Least Square!\n\nWe will focus on the `signal` processing submodule for this exploration notebook.\n\nThis is a work in progress, so please tune in for more updates!\n\n### References:\n* Loading data (signal and target): https://www.kaggle.com/theoviel/fast-fourier-transform-denoising\n* Read data: https://www.kaggle.com/sohier/reading-the-data-with-python\n\n### Recommendations:\nFFT Kernel (We won't cover this since the following notebook cover it extensively):\n* https://www.kaggle.com/theoviel/fast-fourier-transform-denoising\n\nOfficial Scipy Tutorial on Signal Processing:\n* https://docs.scipy.org/doc/scipy/reference/tutorial/signal.html\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport pyarrow.parquet as pq\nimport matplotlib.pyplot as plt\nfrom scipy import signal\n\nsns.set_style(\"whitegrid\")","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true,"_uuid":"f4809f991fd98876ac4233c633a156d9507cdec5"},"cell_type":"code","source":"import warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"69c64fb0552cabedd92b2cf984f0f188201e4602"},"cell_type":"markdown","source":"# Importing and Preprocessing"},{"metadata":{"trusted":true,"_uuid":"82d327461c6760e54c336b0ce1ba9957d020cb8a"},"cell_type":"code","source":"signals = pq.read_table('../input/train.parquet', columns=[str(i) for i in range(999)]).to_pandas()\nsignals = signals.values.T.reshape((999//3, 3, 800000))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d09cebb30547e5fde915a2b843c3836b38ead729"},"cell_type":"code","source":"train_df = pd.read_csv('../input/metadata_train.csv')\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d1a769181052038291c013cdae6d72296cf8ac4a"},"cell_type":"code","source":"target = train_df['target'][::3]\ntarget.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9c1da49f08cb5de797b15d14a96f15d849a016b2"},"cell_type":"markdown","source":"## 1. Applying Convolutions"},{"metadata":{"trusted":true,"_uuid":"172cc934389909f2892f046758bbdee29d5bc643","scrolled":true},"cell_type":"code","source":"def apply_convolution(sig, window):\n    \"\"\"Apply a simple same-size convolution with a given window size\"\"\"\n    conv = np.repeat([0., 1., 0.], window)\n    filtered = signal.convolve(sig, conv, mode='same') / window\n    return filtered","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"48fcd669860f918752e9131d8d6f149094cbb49e"},"cell_type":"markdown","source":"Here, we take an example with negative target."},{"metadata":{"_uuid":"0ca90b1402f35b7a8496d0d4ca462175c7c9964a"},"cell_type":"markdown","source":"### Negative Target"},{"metadata":{"trusted":true,"_uuid":"f7f72fe3aee2b970c668b5478f98d7a3edb9b96d","_kg_hide-input":false},"cell_type":"code","source":"plt.figure(figsize=(15, 10))\nwindow = 10\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    convolved = apply_convolution(sig, window)\n    plt.plot(convolved, label=f'Phase {phase} Convolved')\n\nplt.legend()\nplt.title(f\"Applying convolutions - Window Size {window}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"823ca5e3a993bc588286cf8b5cc5fe54662718b7"},"cell_type":"code","source":"plt.figure(figsize=(15, 10))\nwindow = 100\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    convolved = apply_convolution(sig, window)\n    plt.plot(convolved, label=f'Phase {phase} Convolved')\n\nplt.legend()\nplt.title(f\"Applying convolutions - Window Size {window}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"f441afb52149eda622200a9189537f77049b96ef"},"cell_type":"code","source":"plt.figure(figsize=(15, 10))\nwindow = 1000\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    convolved = apply_convolution(sig, window)\n    plt.plot(convolved, label=f'Phase {phase} Convolved')\n\nplt.legend()\nplt.title(f\"Applying convolutions - Window Size {window}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fc4118cab32cc5b12f069b6eefd459ec5d1e5e91"},"cell_type":"markdown","source":"Now, let's see a positive target."},{"metadata":{"_uuid":"44937ba8dce3c981a3bd9d966196243157e77aa5"},"cell_type":"markdown","source":"### Positive Target"},{"metadata":{"trusted":true,"_uuid":"f7f72fe3aee2b970c668b5478f98d7a3edb9b96d","_kg_hide-input":true},"cell_type":"code","source":"plt.figure(figsize=(15, 10))\nwindow = 10\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    convolved = apply_convolution(sig, window)\n    plt.plot(convolved, label=f'Phase {phase} Convolved')\n\nplt.legend()\nplt.title(f\"Applying convolutions - Window Size {window}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"59ac9e699d0c21c8a4b8c5474a8b0d4afac4f2b0"},"cell_type":"code","source":"plt.figure(figsize=(15, 10))\nwindow = 100\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    convolved = apply_convolution(sig, window)\n    plt.plot(convolved, label=f'Phase {phase} Convolved')\n\nplt.legend()\nplt.title(f\"Applying convolutions - Window Size {window}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"8c6085fadc4673fa3d8d7a286bd67b3e0c6ba848"},"cell_type":"code","source":"plt.figure(figsize=(15, 10))\nwindow = 1000\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    convolved = apply_convolution(sig, window)\n    plt.plot(convolved, label=f'Phase {phase} Convolved')\n\nplt.legend()\nplt.title(f\"Applying convolutions - Window Size {window}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c9918f57e7be8d6f69559a3e36673dae7ccdd053"},"cell_type":"markdown","source":"## 2. C-Spline"},{"metadata":{"_uuid":"7e532396fb146c0e2300110fc7a23594505ac9b5"},"cell_type":"markdown","source":"### Negative Target"},{"metadata":{"trusted":true,"_uuid":"dd9dfd0c105015745354e679c049424d5ea37522","_kg_hide-input":true},"cell_type":"code","source":"%%time\nsmoothing = 0\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    filtered = signal.cspline1d(sig, smoothing)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Cubic Spline, Smoothing: {smoothing}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"406ea23598735fad1fd2ea1792f4fa2348097786","_kg_hide-input":true},"cell_type":"code","source":"smoothing = 1\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    filtered = signal.cspline1d(sig, smoothing)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Cubic Spline, Smoothing: {smoothing}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8fd97a3b8a00cfe26e1b3c3f9f0a415d068cbfcf","_kg_hide-input":true},"cell_type":"code","source":"smoothing = 10\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    filtered = signal.cspline1d(sig, smoothing)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Cubic Spline, Smoothing: {smoothing}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ed5e0f35d66192dc62107519e913a361c844a26e"},"cell_type":"markdown","source":"### Positive Target"},{"metadata":{"trusted":true,"_uuid":"caa543fc5d18c26d345a2bcd522c68cf94fb289d","_kg_hide-input":true},"cell_type":"code","source":"smoothing = 0\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    filtered = signal.cspline1d(sig, smoothing)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Cubic Spline, Smoothing: {smoothing}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f01b9d81aa64e67955a53d13ff0b51431f1c2f72","_kg_hide-input":true},"cell_type":"code","source":"smoothing = 1\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    filtered = signal.cspline1d(sig, smoothing)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Cubic Spline, Smoothing: {smoothing}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"236d1208db5285ad8647bd1f992551e2dd2c8ba3","_kg_hide-input":true},"cell_type":"code","source":"smoothing = 10\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    filtered = signal.cspline1d(sig, smoothing)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Cubic Spline, Smoothing: {smoothing}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"48e1276f6e4e1883cca510f7b89ee7fb4f097fd9"},"cell_type":"markdown","source":"## 3. Q-Spline\n\nScipy does not support smoothing Q-Spline yet, so we set it to be 0. "},{"metadata":{"_uuid":"18838e8bfc042301d519919d71af0559396e3d12"},"cell_type":"markdown","source":"### Negative Target"},{"metadata":{"trusted":true,"_uuid":"41979ca82c2a4009abfd4dc4fcc45c612f8036d2"},"cell_type":"code","source":"%%time\n# Start with negative target.\nsmoothing = 0\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    filtered = signal.qspline1d(sig, smoothing)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Quadratic Spline, Smoothing: {smoothing}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"da2eb56f63b115b36da2060ed350c39f90563147"},"cell_type":"markdown","source":"### Positive Target"},{"metadata":{"trusted":true,"_uuid":"d1adb9b77d376c5b9bb6e32d1bf4be8541912386","_kg_hide-input":true},"cell_type":"code","source":"smoothing = 0\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    filtered = signal.qspline1d(sig, smoothing)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Quadratic Spline, Smoothing: {smoothing}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ead4bf7a57bc6d0b03748bee2a403f4d1cc57272"},"cell_type":"markdown","source":"## 4. Median Filtering"},{"metadata":{"_uuid":"1d5d3a3e2a815a7c8dc032637075389f55a8e39e"},"cell_type":"markdown","source":"### Negative Example"},{"metadata":{"_kg_hide-input":false,"trusted":true,"_uuid":"6cbad68b4c83e6649e07a59b7347d8ed70b4b8da"},"cell_type":"code","source":"%%time\nkernel_size = 1\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    filtered = signal.medfilt(sig, kernel_size)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Median Filters, Kernel Size: {kernel_size}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"d7a7ab2dc59f502cd806e59546b577d6eba69157"},"cell_type":"code","source":"kernel_size = 11\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    filtered = signal.medfilt(sig, kernel_size)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Median Filters, Kernel Size: {kernel_size}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true,"_uuid":"14e36abcc0a8a993ced4455accd9c98f77b03a7d"},"cell_type":"code","source":"kernel_size = 51\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    filtered = signal.medfilt(sig, kernel_size)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Median Filters, Kernel Size: {kernel_size}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true,"_uuid":"f6ae1dc09c4c113f7836a00170b21f4ae7bdc67e"},"cell_type":"code","source":"kernel_size = 101\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    filtered = signal.medfilt(sig, kernel_size)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Median Filters, Kernel Size: {kernel_size}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a88ab6b2323117973c217d7db4ec2f208ae2fc08"},"cell_type":"markdown","source":"### Positive Example"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"26e7bf70c9ad0e02f13c4d7bd8020be380f13a20"},"cell_type":"code","source":"kernel_size = 1\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    filtered = signal.medfilt(sig, kernel_size)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Median Filters, Kernel Size: {kernel_size}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"6a506e6e91ee6722276754e8a28cfb7cb8de3ed9"},"cell_type":"code","source":"kernel_size = 11\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    filtered = signal.medfilt(sig, kernel_size)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Median Filters, Kernel Size: {kernel_size}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"135b33d6605c1b2944d386a4bbb5244fd646a042"},"cell_type":"code","source":"kernel_size = 51\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    filtered = signal.medfilt(sig, kernel_size)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Median Filters, Kernel Size: {kernel_size}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"aa06514af4543342809e9405cc7206c4badf8891"},"cell_type":"code","source":"kernel_size = 101\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    filtered = signal.medfilt(sig, kernel_size)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying Median Filters, Kernel Size: {kernel_size}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"46fb6f9b56b9ff79eae5664da2f4ba4fdba5e755"},"cell_type":"markdown","source":"## 5. Digital Filters (IIR)\n\n#### Butter Filter Design\n\nThis is what we will be using:\n\n> Butterworth digital and analog filter design.\n> Design an Nth-order digital or analog Butterworth filter and return the filter coefficients.\n\nIn this case the Numerator (b) and denominator (a) polynomials of the IIR filter are returned. The following describes the Wn parameter:\n\n> A scalar or length-2 sequence giving the critical frequencies. For a Butterworth filter, this is the point at which the gain drops to 1/sqrt(2) that of the passband (the “-3 dB point”).\n> For digital filters, Wn are in the same units as fs. By default, fs is 2 half-cycles/sample, so these are normalized from 0 to 1, where 1 is the Nyquist frequency. (Wn is thus in half-cycles / sample.)\n> For analog filters, Wn is an angular frequency (e.g. rad/s).\n\nIs Butterworth good for Time Series? Check out [this paper](https://amstat.tandfonline.com/doi/abs/10.1198/073500101681019909). Here's the abstract:\n\n> Long-term trends and business cycles are usually estimated by applying the Hodrick and Prescott (HP) filter to X-11 seasonally adjusted data. A two-stage procedure is proposed in this article to improve this methodology. The improvement is based on (a) using Butterworth or band-pass filters specifically designed for the problem at hand as an alternative to the HP filter, (b) applying the selected filter to estimated trend cycles instead of to seasonally adjusted series, and (c) using autoregressive integrated moving average models to extend the input series with forecasts and backcasts."},{"metadata":{"_uuid":"c9277b09a19452acb1e6fe6af5ac8cec2e7c6308"},"cell_type":"markdown","source":"### Negative Example"},{"metadata":{"trusted":true,"_uuid":"4445a4959919d496d524c99ee8936d2c21be240f"},"cell_type":"code","source":"%%time\nWn = 0.50\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    \n    b, a = signal.butter(3, Wn)\n    filtered = signal.filtfilt(b, a, sig)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying IIR Filtering with Butterworth, Wn: {Wn}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"5dc94eb75f16173ddd6440b767eba805282dac57"},"cell_type":"code","source":"Wn = 0.05\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    \n    b, a = signal.butter(3, Wn)\n    filtered = signal.filtfilt(b, a, sig)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying IIR Filtering with Butterworth, Wn: {Wn}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"e349c255470239e9335b7108bfce93e7e5b8030d"},"cell_type":"code","source":"Wn = 0.01\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[0, phase, :]\n    \n    b, a = signal.butter(3, Wn)\n    filtered = signal.filtfilt(b, a, sig)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying IIR Filtering with Butterworth, Wn: {Wn}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4cad325e13d1bccf1d33f663aab09e709d3c0ec7"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0d1f16b355ff0a1c4b8c2aca142de2e382058932"},"cell_type":"markdown","source":"### Negative Example"},{"metadata":{"trusted":true,"_kg_hide-input":true,"_uuid":"e81f1b7cb61c42a980ca0be0a62b23ca1d93df65"},"cell_type":"code","source":"%%time\nWn = 0.50\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    \n    b, a = signal.butter(3, Wn)\n    filtered = signal.filtfilt(b, a, sig)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying IIR Filtering with Butterworth, Wn: {Wn}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"e0249642f0c6e0b6ed92eca468922444642ef3e4"},"cell_type":"code","source":"Wn = 0.05\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    \n    b, a = signal.butter(3, Wn)\n    filtered = signal.filtfilt(b, a, sig)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying IIR Filtering with Butterworth, Wn: {Wn}\", size=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"44e3e43ac49d3a6b0334185dd02f36b55df1ee60"},"cell_type":"code","source":"Wn = 0.01\nplt.figure(figsize=(15, 10))\n\nfor phase in range(3):\n    sig = signals[1, phase, :]\n    \n    b, a = signal.butter(3, Wn)\n    filtered = signal.filtfilt(b, a, sig)\n    \n    plt.plot(sig, label=f'Phase {phase} Raw')\n    plt.plot(filtered, label=f'Phase {phase} Filtered')\n\nplt.legend()\nplt.title(f\"Applying IIR Filtering with Butterworth, Wn: {Wn}\", size=15)\nplt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}