{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "Z8R69UJ6cvTF" }, "source": [ "# **INPUT LIBRARY**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "fni2ZayEHNBL" }, "outputs": [], "source": [ "import librosa\n", "import librosa.display\n", "import pandas as pd\n", "import os\n", "import numpy as np\n", "from scipy.fftpack import dct\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.multiclass import OneVsOneClassifier, OneVsRestClassifier\n", "from sklearn.svm import SVC\n", "from sklearn.model_selection import GridSearchCV\n", "from sklearn.svm import LinearSVC\n", "from sklearn import metrics\n", "from joblib import dump, load\n", "from sklearn.preprocessing import StandardScaler, LabelEncoder\n", "from sklearn.metrics import accuracy_score, precision_score, recall_score, classification_report, confusion_matrix, f1_score\n", "# from google.colab import drive\n", "import ctypes\n", "from ctypes import util\n", "import matplotlib.pyplot as plt\n", "from openpyxl import Workbook\n", "from openpyxl.utils.dataframe import dataframe_to_rows\n", "from openpyxl.styles import Border, Side" ] }, { "cell_type": "markdown", "metadata": { "id": "QyDOnMcJgZ48" }, "source": [ "# **PROSES MFCC**" ] }, { "cell_type": "markdown", "metadata": { "id": "ebw2dT3acNyx" }, "source": [ "**Fungis Pre-Emphasis**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "SigcZQ-5Hdz_" }, "outputs": [], "source": [ "# Fungsi pre-emphasis\n", "def pre_emphasis(signal, coefficient=0.97):\n", " return np.append(signal[0], signal[1:] - coefficient * signal[:-1])" ] }, { "cell_type": "markdown", "metadata": { "id": "vWao7svucTno" }, "source": [ "**Fungsi Frame-Blocking**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "PhqB8zmrHhQw" }, "outputs": [], "source": [ "# Fungsi framing\n", "def framing(signal, sample_rate, frame_length=0.025,\n", " frame_step=0.010):\n", " nsamples_signal = len(signal)\n", " nsamples_frame = int(round(frame_length * sample_rate))\n", " nsamples_stride = int(round(frame_step * sample_rate))\n", " n_frames = int(np.ceil((nsamples_signal - nsamples_frame) / nsamples_stride) + 1)\n", " nsamples_padding = ((n_frames - 1) * nsamples_stride + nsamples_frame) - nsamples_signal\n", " z = np.zeros(nsamples_padding)\n", " signal = np.append(signal, z)\n", " frames = np.empty((n_frames, nsamples_frame))\n", " for i in range(n_frames):\n", " left = i * nsamples_stride\n", " right = left + nsamples_frame\n", " frame = signal[left:right]\n", " frames[i] = frame\n", " return frames" ] }, { "cell_type": "markdown", "metadata": { "id": "TSzNwvIdcaY8" }, "source": [ "**Fungsi MFCC**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "urkPLMLFHklQ" }, "outputs": [], "source": [ "# Fungsi untuk mengekstrak fitur MFCC dari file audio\n", "def extract_mfcc(audio_file, sr=44100, frame_length=0.025, frame_step=0.010, nfilt=40, num_ceps=12):\n", " y, _ = librosa.load(audio_file, sr=sr)\n", "\n", " # Normalize\n", " y_norm = librosa.util.normalize(y)\n", "\n", " # Pre-emphasis\n", " y_pre_emphasis = pre_emphasis(y_norm)\n", "\n", " # Framing\n", " frames = framing(y_pre_emphasis, sr, frame_length, frame_step)\n", "\n", " # Hamming window\n", " frames *= np.hanning(len(frames[0]))\n", "\n", " # FFT\n", " NFFT = 512\n", " mag_frames = np.absolute(np.fft.rfft(frames, NFFT))\n", " pow_frames = ((1.0 / NFFT) * ((mag_frames) ** 2))\n", "\n", " # Mel filter bank\n", " low_freq_mel = 0\n", " high_freq_mel = (2595 * np.log10(1 + (sr / 2) / 700))\n", " mel_points = np.linspace(low_freq_mel, high_freq_mel, nfilt + 2)\n", " hz_points = (700 * (10**(mel_points / 2595) - 1))\n", " bin = np.floor((NFFT + 1) * hz_points / sr)\n", "\n", " fbank = np.zeros((nfilt, int(np.floor(NFFT / 2 + 1))))\n", " for m in range(1, nfilt + 1):\n", " f_m_minus = int(bin[m - 1])\n", " f_m = int(bin[m])\n", " f_m_plus = int(bin[m + 1])\n", " for k in range(f_m_minus, f_m):\n", " fbank[m - 1, k] = (k - bin[m - 1]) / (bin[m] - bin[m - 1])\n", " for k in range(f_m, f_m_plus):\n", " fbank[m - 1, k] = (bin[m + 1] - k) / (bin[m + 1] - bin[m])\n", "\n", " filter_banks = np.dot(pow_frames, fbank.T)\n", " filter_banks = np.where(filter_banks == 0, np.finfo(float).eps, filter_banks)\n", " filter_banks = 20 * np.log10(filter_banks)\n", "\n", " # Discrete Cosine Transform (DCT) for MFCC\n", " mfcc = dct(filter_banks, type=2, axis=1, norm='ortho')[:, 1:(num_ceps + 1)]\n", "\n", " return np.mean(mfcc, axis=0)" ] }, { "cell_type": "markdown", "metadata": { "id": "17rFJaT_gGwo" }, "source": [ "**Dataset**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "CKcTFlrEHq0T" }, "outputs": [], "source": [ "# Directory tempat file audio Anda disimpan\n", "dataset = '/content/drive/MyDrive/REVISI SKRIPSI/Dataset Pertama'" ] }, { "cell_type": "code", "source": [ "dataset_kedua = '/content/drive/MyDrive/REVISI SKRIPSI/Dataset Kedua'" ], "metadata": { "id": "rXuoZlCgI73-" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "urbp24shuk3s" }, "source": [ "**Inisialisasi Dataset**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "M5kcsHfqHvL2" }, "outputs": [], "source": [ "data = []\n", "\n", "# Loop melalui setiap direktori label\n", "for label_folder in os.listdir(dataset):\n", " label_dir = os.path.join(dataset, label_folder)\n", " if os.path.isdir(label_dir):\n", " # Loop melalui file-file audio di dalam setiap label\n", " for file in os.listdir(label_dir):\n", " if file.endswith(\".wav\"):\n", " audio_file = os.path.join(label_dir, file)\n", " mfcc_features = extract_mfcc(audio_file)\n", " # Menambahkan ID ke data\n", " features_list = list(mfcc_features) + [label_folder]\n", " data.append(features_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "GKuXVqAyXGa1" }, "outputs": [], "source": [ "# Inisialisasi data\n", "data = []\n", "\n", "# Loop melalui setiap direktori ID\n", "for id_folder in os.listdir(dataset):\n", " id_dir = os.path.join(dataset, id_folder)\n", " if os.path.isdir(id_dir):\n", " # Loop melalui setiap label di dalam direktori ID\n", " for label in os.listdir(id_dir):\n", " label_dir = os.path.join(id_dir, label)\n", " if os.path.isdir(label_dir):\n", " # Loop melalui file-file audio di dalam setiap label\n", " for file in os.listdir(label_dir):\n", " if file.endswith(\".wav\"):\n", " audio_file = os.path.join(label_dir, file)\n", " mfcc_features = extract_mfcc(audio_file)\n", " # Menambahkan ID ke data\n", " data.append(list(mfcc_features) + [id_folder] + [label])" ] }, { "cell_type": "markdown", "metadata": { "id": "FVsE69BGgSZi" }, "source": [ "**Menyimpan Hasil MFCC ke CSV**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "RYqBjtOxXXon" }, "outputs": [], "source": [ "# Mengubah data ke dalam DataFrame\n", "df = pd.DataFrame(data, columns=[f'mfcc_{i}' for i in range(1, 13)] + ['id', 'label'])\n", "\n", "# Menggabungkan kolom ID dan label menjadi satu kolom id_label\n", "df['id_label'] = df['id'] + '_' + df['label']\n", "\n", "# Menyimpan DataFrame yang sudah diubah ke dalam file CSV\n", "df.to_csv('/content/drive/MyDrive/Skripsi/mfcc/backup_4_label.csv', index=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Lgn-kmfFCajq" }, "outputs": [], "source": [ " # Mengubah data ke dalam DataFrame\n", "df = pd.DataFrame(data, columns=[f'mfcc_{i}' for i in range(1, 13)] + ['target'])\n", "\n", "# Menyimpan DataFrame yang sudah diubah ke dalam file CSV\n", "df.to_csv('/content/drive/MyDrive/REVISI SKRIPSI/dataset_pertama.csv', index=False)" ] }, { "cell_type": "code", "source": [ "# Menyimpan DataFrame ke dalam file Excel\n", "wb = Workbook()\n", "ws = wb.active\n", "\n", "# Menulis data ke dalam worksheet\n", "for r in dataframe_to_rows(df, index=True, header=True):\n", " ws.append(r)\n", "\n", "# Menambahkan border di antara setiap sel\n", "for row in ws.iter_rows():\n", " for cell in row:\n", " cell.border = Border(left=Side(style='thin'),\n", " right=Side(style='thin'),\n", " top=Side(style='thin'),\n", " bottom=Side(style='thin'))\n", "\n", "# Menyimpan file Excel\n", "wb.save('/content/drive/MyDrive/Persiapan Sidang/inisialisasi data label.xlsx')" ], "metadata": { "id": "a2W2-nG4rZOu" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "C4VSfME2v0F9" }, "source": [ "# **KLASIFIKASI SVM**" ] }, { "cell_type": "markdown", "metadata": { "id": "9MO1GqtaCxX_" }, "source": [ "## **A. Split Data**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "M9FGECAbxYbg" }, "outputs": [], "source": [ "read_csv = pd.read_csv('/content/drive/MyDrive/REVISI SKRIPSI/dataset_pertama.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 443 }, "id": "yVh_JnDdozez", "outputId": "850babf2-eda9-4f7b-cbcf-9c1c4d81ffe8" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " mfcc_1 mfcc_2 mfcc_3 mfcc_4 mfcc_5 mfcc_6 \\\n", "0 134.007563 -10.597640 7.266465 -3.754714 -13.808333 2.155736 \n", "1 50.750191 -5.598916 32.949004 -22.501260 14.779131 -19.538301 \n", "2 122.293842 -12.814458 12.493625 -10.989077 -15.963840 1.330859 \n", "3 126.803849 -0.053941 8.270297 -6.800313 -13.392560 -0.899367 \n", "4 10.468602 9.117828 -8.683499 -1.466168 24.753077 -21.531051 \n", ".. ... ... ... ... ... ... \n", "145 36.427943 25.707284 8.212668 10.450583 -5.248485 -11.510404 \n", "146 43.954271 6.456048 2.131374 -9.353882 -17.369689 -7.557169 \n", "147 55.782108 26.974924 -5.329280 -1.423440 -9.777956 -8.758294 \n", "148 26.584968 -8.043035 -4.178721 -0.592769 -15.964644 -9.010896 \n", "149 19.589333 9.885163 -20.071891 -13.235444 -15.691194 -10.111671 \n", "\n", " mfcc_7 mfcc_8 mfcc_9 mfcc_10 mfcc_11 mfcc_12 target \n", "0 -12.878377 -4.975601 6.321278 -2.777375 -6.506576 -1.876924 Hilmi \n", "1 7.961897 -11.957439 7.594936 -14.929685 5.006317 -1.295943 Hilmi \n", "2 -17.650124 -3.014083 3.925209 -6.664020 -1.170681 -0.027291 Hilmi \n", "3 -14.874580 0.227556 7.045879 -7.464138 -5.668817 -0.148281 Hilmi \n", "4 2.124193 -6.581121 11.937312 -18.942954 5.763798 0.569468 Hilmi \n", ".. ... ... ... ... ... ... ... \n", "145 -6.744978 -9.246621 -3.982937 -10.705474 -6.795224 -6.532252 Random \n", "146 -15.326618 -16.521912 1.466365 -6.723767 1.931086 2.092220 Random \n", "147 -1.948076 -6.980490 -0.893085 -4.223947 -4.055362 -5.809257 Random \n", "148 -4.953900 -11.460907 -0.344327 -4.228080 -1.677582 -2.752977 Random \n", "149 2.390462 -5.974159 -0.621773 -2.311574 4.188963 -0.232814 Random \n", "\n", "[150 rows x 13 columns]" ], "text/html": [ "\n", "
\n", " | mfcc_1 | \n", "mfcc_2 | \n", "mfcc_3 | \n", "mfcc_4 | \n", "mfcc_5 | \n", "mfcc_6 | \n", "mfcc_7 | \n", "mfcc_8 | \n", "mfcc_9 | \n", "mfcc_10 | \n", "mfcc_11 | \n", "mfcc_12 | \n", "target | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "134.007563 | \n", "-10.597640 | \n", "7.266465 | \n", "-3.754714 | \n", "-13.808333 | \n", "2.155736 | \n", "-12.878377 | \n", "-4.975601 | \n", "6.321278 | \n", "-2.777375 | \n", "-6.506576 | \n", "-1.876924 | \n", "Hilmi | \n", "
1 | \n", "50.750191 | \n", "-5.598916 | \n", "32.949004 | \n", "-22.501260 | \n", "14.779131 | \n", "-19.538301 | \n", "7.961897 | \n", "-11.957439 | \n", "7.594936 | \n", "-14.929685 | \n", "5.006317 | \n", "-1.295943 | \n", "Hilmi | \n", "
2 | \n", "122.293842 | \n", "-12.814458 | \n", "12.493625 | \n", "-10.989077 | \n", "-15.963840 | \n", "1.330859 | \n", "-17.650124 | \n", "-3.014083 | \n", "3.925209 | \n", "-6.664020 | \n", "-1.170681 | \n", "-0.027291 | \n", "Hilmi | \n", "
3 | \n", "126.803849 | \n", "-0.053941 | \n", "8.270297 | \n", "-6.800313 | \n", "-13.392560 | \n", "-0.899367 | \n", "-14.874580 | \n", "0.227556 | \n", "7.045879 | \n", "-7.464138 | \n", "-5.668817 | \n", "-0.148281 | \n", "Hilmi | \n", "
4 | \n", "10.468602 | \n", "9.117828 | \n", "-8.683499 | \n", "-1.466168 | \n", "24.753077 | \n", "-21.531051 | \n", "2.124193 | \n", "-6.581121 | \n", "11.937312 | \n", "-18.942954 | \n", "5.763798 | \n", "0.569468 | \n", "Hilmi | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
145 | \n", "36.427943 | \n", "25.707284 | \n", "8.212668 | \n", "10.450583 | \n", "-5.248485 | \n", "-11.510404 | \n", "-6.744978 | \n", "-9.246621 | \n", "-3.982937 | \n", "-10.705474 | \n", "-6.795224 | \n", "-6.532252 | \n", "Random | \n", "
146 | \n", "43.954271 | \n", "6.456048 | \n", "2.131374 | \n", "-9.353882 | \n", "-17.369689 | \n", "-7.557169 | \n", "-15.326618 | \n", "-16.521912 | \n", "1.466365 | \n", "-6.723767 | \n", "1.931086 | \n", "2.092220 | \n", "Random | \n", "
147 | \n", "55.782108 | \n", "26.974924 | \n", "-5.329280 | \n", "-1.423440 | \n", "-9.777956 | \n", "-8.758294 | \n", "-1.948076 | \n", "-6.980490 | \n", "-0.893085 | \n", "-4.223947 | \n", "-4.055362 | \n", "-5.809257 | \n", "Random | \n", "
148 | \n", "26.584968 | \n", "-8.043035 | \n", "-4.178721 | \n", "-0.592769 | \n", "-15.964644 | \n", "-9.010896 | \n", "-4.953900 | \n", "-11.460907 | \n", "-0.344327 | \n", "-4.228080 | \n", "-1.677582 | \n", "-2.752977 | \n", "Random | \n", "
149 | \n", "19.589333 | \n", "9.885163 | \n", "-20.071891 | \n", "-13.235444 | \n", "-15.691194 | \n", "-10.111671 | \n", "2.390462 | \n", "-5.974159 | \n", "-0.621773 | \n", "-2.311574 | \n", "4.188963 | \n", "-0.232814 | \n", "Random | \n", "
150 rows × 13 columns
\n", "OneVsOneClassifier(estimator=SVC(C=8, gamma=0.001))In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
OneVsOneClassifier(estimator=SVC(C=8, gamma=0.001))
SVC(C=8, gamma=0.001)
SVC(C=8, gamma=0.001)