{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# EEG signal denosing using Wavelet transform","metadata":{}},{"cell_type":"markdown","source":"\n\n> Analyzing Electroencephalogram (EEG) signal is a challenge due to the various artifacts used by Electromyogram, eye blink and Electrooculogram. The present de-noising techniques that are based on the frequency selective filtering suffers from a substantial loss of the EEG data. Noise removal using wavelet has the characteristic of preserving signal uniqueness even if noise is going to be minimized. To remove noise from EEG signal, this research employed discrete wavelet transform. Root mean square difference has been used to find the usefulness of the noise elimination. In this research, four different discrete wavelet functions have been used to remove noise from the Electroencephalogram signal gotten from two different types of patients (healthy and epileptic) to show the effectiveness of DWT on EEG noise removal. The result shows that the WF orthogonal meyer is the best one for noise elimination from the EEG signal of epileptic subjects and the WF Daubechies 8 (db8) is the best one for noise elimination from the EEG signal on healthy subjects. \n\nMamun, M., Al-Kadi, M., & Marufuzzaman, M. (2013). Effectiveness of Wavelet Denoising on Electroencephalogram Signals. Journal of Applied Research and Technology, 11(1). https://doi.org/10.1016/S1665-6423(13)71524-4\n\nFor more details, pleas visit [Effectiveness of Wavelet Denoising on Electroencephalogram Signals](https://www.sciencedirect.com/science/article/pii/S1665642313715244)","metadata":{}},{"cell_type":"markdown","source":"### Please Upvote if you Find this Useful :)","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport pywt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-14T08:39:55.230525Z","iopub.execute_input":"2024-01-14T08:39:55.231044Z","iopub.status.idle":"2024-01-14T08:39:56.204607Z","shell.execute_reply.started":"2024-01-14T08:39:55.231001Z","shell.execute_reply":"2024-01-14T08:39:56.203463Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from this notebook: \"Harmful Brain Activity Plotting eeg/spec(https://www.kaggle.com/code/ybbbat/harmful-brain-activity-plotting-eeg-spec)\"\ndef plot_eeg(df, moving_avg=1):\n    fig, axs = plt.subplots(20, 1, figsize=(30, 15), sharex=True)\n    for i, ax in enumerate(axs):\n        ax.plot(df.iloc[:,i], color=\"black\")\n        ax.set_ylabel(df.columns[i], rotation=0)\n        ax.set_yticklabels([])\n        ax.set_yticks([])\n        ax.set_xticks([])\n        ax.spines[[\"top\", \"bottom\", \"left\", \"right\"]].set_visible(False)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T09:02:59.690374Z","iopub.execute_input":"2024-01-14T09:02:59.690869Z","iopub.status.idle":"2024-01-14T09:02:59.699101Z","shell.execute_reply.started":"2024-01-14T09:02:59.690831Z","shell.execute_reply":"2024-01-14T09:02:59.697556Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Denoising function","metadata":{}},{"cell_type":"code","source":"def maddest(d, axis=None):\n    return np.mean(np.absolute(d - np.mean(d, axis)), axis)\n\ndef denoise(x, wavelet='haar', level=1):\n    ret = {key:[] for key in x.columns}\n    \n    for pos in x.columns:\n        coeff = pywt.wavedec(x[pos], wavelet, mode=\"per\")\n        sigma = (1/0.6745) * maddest(coeff[-level])\n\n        uthresh = sigma * np.sqrt(2*np.log(len(x)))\n        coeff[1:] = (pywt.threshold(i, value=uthresh, mode='hard') for i in coeff[1:])\n\n        ret[pos]=pywt.waverec(coeff, wavelet, mode='per')\n    \n    return pd.DataFrame(ret)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T09:07:07.160528Z","iopub.execute_input":"2024-01-14T09:07:07.160973Z","iopub.status.idle":"2024-01-14T09:07:07.171611Z","shell.execute_reply.started":"2024-01-14T09:07:07.160935Z","shell.execute_reply":"2024-01-14T09:07:07.170446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# List of mother Function you can use for wavelet transform","metadata":{}},{"cell_type":"code","source":"print(\"The mother function you can use\")\nprint(pywt.wavelist())","metadata":{"execution":{"iopub.status.busy":"2024-01-14T09:22:25.737915Z","iopub.execute_input":"2024-01-14T09:22:25.738324Z","iopub.status.idle":"2024-01-14T09:22:25.744726Z","shell.execute_reply.started":"2024-01-14T09:22:25.738276Z","shell.execute_reply":"2024-01-14T09:22:25.743534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get eeg signal\ndf = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/train.csv\")\nrow = df.sample(n=1, random_state=60).iloc[0,]","metadata":{"execution":{"iopub.status.busy":"2024-01-14T09:08:18.577027Z","iopub.execute_input":"2024-01-14T09:08:18.577512Z","iopub.status.idle":"2024-01-14T09:08:18.755289Z","shell.execute_reply.started":"2024-01-14T09:08:18.577470Z","shell.execute_reply":"2024-01-14T09:08:18.754077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Befor Denoising","metadata":{}},{"cell_type":"code","source":"eeg = pd.read_parquet(\"/kaggle/input/hms-harmful-brain-activity-classification/train_eegs/\"+str(row[\"eeg_id\"])+\".parquet\")\neeg = eeg[int(row[\"eeg_label_offset_seconds\"])*200:int(row[\"eeg_label_offset_seconds\"])*200+50*200]\nplot_eeg(eeg)\nplt.savefig(\"bef.png\")","metadata":{"execution":{"iopub.status.busy":"2024-01-14T09:38:38.491348Z","iopub.execute_input":"2024-01-14T09:38:38.491768Z","iopub.status.idle":"2024-01-14T09:38:41.700023Z","shell.execute_reply.started":"2024-01-14T09:38:38.491733Z","shell.execute_reply":"2024-01-14T09:38:41.698547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# After Denoising","metadata":{}},{"cell_type":"code","source":"eeg_denoised = denoise(eeg, wavelet=\"db8\")\nplot_eeg(eeg_denoised)\nplt.savefig(\"aft.png\")","metadata":{"execution":{"iopub.status.busy":"2024-01-14T09:38:48.399547Z","iopub.execute_input":"2024-01-14T09:38:48.399989Z","iopub.status.idle":"2024-01-14T09:38:50.831338Z","shell.execute_reply.started":"2024-01-14T09:38:48.399948Z","shell.execute_reply":"2024-01-14T09:38:50.830168Z"},"trusted":true},"execution_count":null,"outputs":[]}]}