From b2bf1b0e3153c288e0aaa66acf8adaa6fe8b5403 Mon Sep 17 00:00:00 2001 From: nuluh Date: Thu, 28 Aug 2025 07:10:41 +0700 Subject: [PATCH] feat(notebooks): Add type hints for data lists, create preview function for STFT visualization, and save plot for Sensor B. Introduce test section with AU data processing. --- code/notebooks/stft.ipynb | 82 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 6 deletions(-) diff --git a/code/notebooks/stft.ipynb b/code/notebooks/stft.ipynb index 6e9e48d..145c7e5 100644 --- a/code/notebooks/stft.ipynb +++ b/code/notebooks/stft.ipynb @@ -342,7 +342,7 @@ "import os\n", "import pandas as pd\n", "\n", - "ready_data1a = []\n", + "ready_data1a: list[pd.DataFrame] = []\n", "for file in os.listdir('D:/thesis/data/converted/raw/sensor1'):\n", " ready_data1a.append(pd.read_csv(os.path.join('D:/thesis/data/converted/raw/sensor1', file), skiprows=1))" ] @@ -354,7 +354,7 @@ "outputs": [], "source": [ "# Load the processed data for Sensor 2\n", - "ready_data2a = []\n", + "ready_data2a: list[pd.DataFrame] = []\n", "for file in os.listdir('D:/thesis/data/converted/raw/sensor2'):\n", " ready_data2a.append(pd.read_csv(os.path.join('D:/thesis/data/converted/raw/sensor2', file), skiprows=1))" ] @@ -414,6 +414,33 @@ "import matplotlib.pyplot as plt" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def preview_stft(data: pd.DataFrame, x_num_ticks: int = 6, y_num_ticks: int = 5):\n", + " \"\"\"\n", + " Preview the Short-Time Fourier Transform (STFT) of the given data.\n", + "\n", + " Parameters:\n", + " -------\n", + " data (pd.DataFrame): The STFT data to be visualized.\n", + " x_num_ticks (int): Number of ticks on the x-axis (time frames). Defaults to 6.\n", + " y_num_ticks (int): Number of ticks on the y-axis (frequency bins). Defaults to 5.\n", + " \"\"\"\n", + " plt.figure(dpi=300) # Set figure size and DPI\n", + " plt.pcolormesh(data.transpose(), cmap='jet', vmax=0.03, vmin=0.0)\n", + " # plt.title('STFT Preview')\n", + " plt.colorbar(label='Magnitude')\n", + " plt.xlabel('Segmen Waktu')\n", + " plt.ylabel('Sampel Frekuensi (Hz)')\n", + " plt.xticks(np.linspace(0, len(data)-1, x_num_ticks)) # Set x-ticks at regular intervals\n", + " plt.yticks(np.linspace(0, len(data.columns)-1, y_num_ticks)) # Set y-ticks at regular intervals\n", + " plt.show()" + ] + }, { "cell_type": "code", "execution_count": null, @@ -422,10 +449,7 @@ "source": [ "# colormesh give title x is frequency and y is time and rotate/transpose the data\n", "# Plotting the STFT Data\n", - "plt.figure(dpi=300) # Set figure size and DPI\n", - "plt.pcolormesh(ready_data1a[0].transpose(), cmap='jet', vmax=0.03, vmin=0.0)\n", - "plt.title('STFT of Sensor A Dataset A Label 0 Undamaged')\n", - "plt.show()" + "preview_stft(ready_data1a[0]) # Preview for Sensor 1" ] }, { @@ -437,6 +461,7 @@ "plt.figure(dpi=300) # Set figure size and DPI\n", "plt.pcolormesh(ready_data2a[0].transpose(), cmap='jet', vmax=0.03, vmin=0.0)\n", "plt.title('STFT of Sensor B Dataset A Label 0 Undamaged')\n", + "plt.savefig(\"stft-sensor-b-dataset-a-undamaged.png\", dpi=300)\n", "plt.show()" ] }, @@ -1005,6 +1030,51 @@ "print(\"Accuracy on Dataset B:\", accuracy_score(y, y_pred_svm))\n", "print(classification_report(y, y_pred_svm))" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Test with AU" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "\n", + "file_path = 'D:/thesis/data/dataset_B/zzzBU.TXT'\n", + "df = pd.read_csv(file_path, delim_whitespace=True, skiprows=10, header=0, memory_map=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df1= df.iloc[:, [1]]\n", + "df2 = df.iloc[:, [26]]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from scipy.signal.windows import hann\n", + "from scipy.signal import stft\n", + "\n", + "window = 1024\n", + "hop = 512\n", + "\n", + "stft1 = stft(df1.values.flatten(), window=hann(window), nperseg=window, noverlap=hop, fs=window)\n", + "stft2 = stft(df2.values.flatten(), window=hann(window), nperseg=window, noverlap=hop, fs=window)\n" + ] } ], "metadata": {