diff --git a/code/notebooks/stft.ipynb b/code/notebooks/stft.ipynb index 145c7e5..ef8418a 100644 --- a/code/notebooks/stft.ipynb +++ b/code/notebooks/stft.ipynb @@ -402,7 +402,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### Preview Plot" + "##### Preview Plot" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "###### Function plot" ] }, { @@ -411,7 +418,8 @@ "metadata": {}, "outputs": [], "source": [ - "import matplotlib.pyplot as plt" + "import matplotlib.pyplot as plt\n", + "from typing import Union" ] }, { @@ -420,7 +428,7 @@ "metadata": {}, "outputs": [], "source": [ - "def preview_stft(data: pd.DataFrame, x_num_ticks: int = 6, y_num_ticks: int = 5):\n", + "def preview_stft(data: Union[pd.DataFrame, list[pd.DataFrame]], x_num_ticks: int = 6, y_num_ticks: int = 5, cols: int=3):\n", " \"\"\"\n", " Preview the Short-Time Fourier Transform (STFT) of the given data.\n", "\n", @@ -430,15 +438,54 @@ " 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()" + " if type(data) == pd.DataFrame:\n", + " plt.figure(dpi=300) # Set figure size and DPI\n", + " plt.pcolormesh(data.transpose(), cmap='jet', vmax=0.03, vmin=0.0, rasterized=True)\n", + " # plt.title('STFT Preview')\n", + " plt.colorbar(label='Magnitude ($m/s^2$)')\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.savefig(\"output_single.svg\", format=\"svg\", dpi=100)\n", + " plt.show()\n", + "\n", + " elif type(data) == list and len(data) > 1:\n", + " # Create a figure and subplots\n", + " fig, axes = plt.subplots(len(data)//cols, cols, figsize=(15, 8), sharex=True, sharey=True)\n", + "\n", + " # Flatten the axes array for easier iteration\n", + " axes = axes.flatten()\n", + "\n", + " # Loop through each subplot and plot the data\n", + " for i in range(len(data)):\n", + " pcm = axes[i].pcolormesh(data[i].transpose(), cmap='jet', vmax=0.03, vmin=0.0, rasterized=True)\n", + " axes[i].set_title(f'd_{i+1}', fontsize=12)\n", + " axes[i].set_xticks(np.linspace(0, len(data[i])-1, 6)) # Set x-ticks at regular intervals\n", + " axes[i].set_yticks(np.linspace(0, len(data[i].columns)-1, 9)) # Set y-ticks at regular intervals\n", + "\n", + " # Add a single color bar for all subplots\n", + " # Use the first `pcolormesh` object (or any valid one) for the color bar\n", + " cbar = fig.colorbar(pcm, ax=axes, orientation='vertical')\n", + " cbar.set_label('Magnitude ($m/s^2$)', fontsize=12)\n", + "\n", + " # Set shared labels\n", + " fig.text(0.5, 0.04, 'Segmen Waktu', ha='center', fontsize=12)\n", + " fig.text(0.04, 0.5, 'Sampel Frekuensi (Hz)', va='center', rotation='vertical', fontsize=12)\n", + " # fig.suptitle('STFT of Sensor A Damage Case (d1--d6) Dataset A', fontsize=16)\n", + "\n", + " # Adjust layout\n", + " # plt.tight_layout(rect=[0.05, 0.05, 1, 1]) # Leave space for shared labels\n", + " plt.subplots_adjust(left=0.1, right=0.75, top=0.9, bottom=0.1, wspace=0.2, hspace=0.2)\n", + " plt.savefig(\"output_multiple.svg\", format=\"svg\", dpi=80)\n", + " plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "###### Sensor A" ] }, { @@ -449,7 +496,14 @@ "source": [ "# colormesh give title x is frequency and y is time and rotate/transpose the data\n", "# Plotting the STFT Data\n", - "preview_stft(ready_data1a[0]) # Preview for Sensor 1" + "preview_stft(ready_data1a[0], y_num_ticks=9) # Preview for Sensor 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "###### Sensor B" ] }, { @@ -458,11 +512,7 @@ "metadata": {}, "outputs": [], "source": [ - "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()" + "preview_stft(ready_data2a[0], y_num_ticks=9) # Preview for Sensor 2" ] }, { @@ -483,7 +533,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### Preview Plot" + "##### Preview Plot" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "###### Sensor A" ] }, { @@ -492,33 +549,14 @@ "metadata": {}, "outputs": [], "source": [ - "import matplotlib.pyplot as plt\n", - "# Create a figure and subplots\n", - "fig, axes = plt.subplots(2, 3, figsize=(15, 8), sharex=True, sharey=True)\n", - "\n", - "# Flatten the axes array for easier iteration\n", - "axes = axes.flatten()\n", - "\n", - "# Loop through each subplot and plot the data\n", - "for i in range(6):\n", - " pcm = axes[i].pcolormesh(ready_data1a[i+1].transpose(), cmap='jet', vmax=0.03, vmin=0.0)\n", - " axes[i].set_title(f'Label {i+1}', fontsize=12)\n", - "\n", - "# Add a single color bar for all subplots\n", - "# Use the first `pcolormesh` object (or any valid one) for the color bar\n", - "cbar = fig.colorbar(pcm, ax=axes, orientation='vertical')\n", - "# cbar.set_label('Magnitude')\n", - "\n", - "# Set shared labels\n", - "fig.text(0.5, 0.04, 'Time Frames', ha='center', fontsize=12)\n", - "fig.text(0.04, 0.5, 'Frequency [Hz]', va='center', rotation='vertical', fontsize=12)\n", - "fig.suptitle('STFT of Sensor A Damage Case (d1--d6) Dataset A', fontsize=16)\n", - "\n", - "# Adjust layout\n", - "# plt.tight_layout(rect=[0.05, 0.05, 1, 1]) # Leave space for shared labels\n", - "plt.subplots_adjust(left=0.1, right=0.75, top=0.9, bottom=0.1, wspace=0.2, hspace=0.2)\n", - "\n", - "plt.show()" + "preview_stft(ready_data1a[1:], cols=3, y_num_ticks=9) # Preview for Sensor 1 Damage Cases (d1--d6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "###### Sensor B" ] }, { @@ -527,33 +565,7 @@ "metadata": {}, "outputs": [], "source": [ - "from cmcrameri import cm\n", - "# Create a figure and subplots\n", - "fig, axes = plt.subplots(2, 3, figsize=(15, 8), sharex=True, sharey=True)\n", - "\n", - "# Flatten the axes array for easier iteration\n", - "axes = axes.flatten()\n", - "\n", - "# Loop through each subplot and plot the data\n", - "for i in range(6):\n", - " pcm = axes[i].pcolormesh(ready_data2a[i+1].transpose(), cmap='jet', vmax=0.03, vmin=0.0)\n", - " axes[i].set_title(f'Label {i+1}', fontsize=12)\n", - "\n", - "# Add a single color bar for all subplots\n", - "# Use the first `pcolormesh` object (or any valid one) for the color bar\n", - "cbar = fig.colorbar(pcm, ax=axes, orientation='vertical')\n", - "# cbar.set_label('Magnitude')\n", - "\n", - "# Set shared labels\n", - "fig.text(0.5, 0.04, 'Time Frames', ha='center', fontsize=12)\n", - "fig.text(0.04, 0.5, 'Frequency [Hz]', va='center', rotation='vertical', fontsize=12)\n", - "fig.suptitle('STFT of Sensor B Damage Case (d1--d6) Dataset A', fontsize=16)\n", - "\n", - "# Adjust layout\n", - "# plt.tight_layout(rect=[0.05, 0.05, 1, 1]) # Leave space for shared labels\n", - "plt.subplots_adjust(left=0.1, right=0.75, top=0.9, bottom=0.1, wspace=0.2, hspace=0.2)\n", - "\n", - "plt.show()" + "preview_stft(ready_data2a[1:], cols=3, y_num_ticks=9) # Preview for Sensor 1 Damage Cases (d1--d6)" ] }, { @@ -1072,8 +1084,57 @@ "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" + "_, _, 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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = pd.DataFrame(np.abs(stft1.T), columns=[f\"Freq_{freq:.2f}\" for freq in np.linspace(0, window/2, stft1.shape[1])]).rename(columns={a.columns[0]: \"00\"})\n", + "b = pd.DataFrame(np.abs(stft2.T), columns=[f\"Freq_{freq:.2f}\" for freq in np.linspace(0, window/2, stft2.shape[1])]).rename(columns={b.columns[0]: \"00\"})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "y = [0]*len(a)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from joblib import load\n", + "\n", + "svm_model_a = load('D:/thesis/models/sensor1/SVM with StandardScaler and PCA.joblib')\n", + "svm_model_b = load('D:/thesis/models/sensor2/SVM with StandardScaler and PCA.joblib')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "y_pred_svm = svm_model_b.predict(b)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "y_pred_svm" ] } ],