0% found this document useful (0 votes)
2 views33 pages

ML Tutorial Step by Step

This tutorial provides a comprehensive guide on using machine learning to classify rattle sounds, detailing the process from sound generation to model training and evaluation. It covers the extraction of acoustic features, normalization, and the implementation of k-fold cross-validation for KNN and SVM models. The document includes step-by-step instructions for using MATLAB, including code walkthroughs and visualizations of the data and model performance.

Uploaded by

tajv23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views33 pages

ML Tutorial Step by Step

This tutorial provides a comprehensive guide on using machine learning to classify rattle sounds, detailing the process from sound generation to model training and evaluation. It covers the extraction of acoustic features, normalization, and the implementation of k-fold cross-validation for KNN and SVM models. The document includes step-by-step instructions for using MATLAB, including code walkthroughs and visualizations of the data and model performance.

Uploaded by

tajv23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

Rattle Sound Identification


using Machine Learning

Tutorial

Prof. Mohammad Fard


Mr. Parin Sanpetchnarong

2026
2
Contact Information

Prof. Mohammad Fard

location: Building 251, level 03, room 23.


email: [Link]@[Link]
Phone number: 0399256044
Consult Time: 09:00-11:00, Friday

Parin Sanpetchnarong
email: s3918509@[Link]
3
Rattle Sound Classification
 This tutorial demonstrates how can we use Machine Learning (ML) to classify
rattle sound

Features Train/Test Testing Data


Extraction Split
Raw Sound
Signal Acoustic
Features Training
Data

ML Models ML Models
Training Testing
Initial Model Trained Model Evaluation
(Untrained) Results

 There are 8 classes of rattle sound, generated with variation in rattling


material and setup

A B C D

E F G H
4
Rattle Sound Generation
Eight classes of rattle sounds were generated using different combinations of
beam length, material, and damping.

High Damping A
Metal on
Metal
Low Damping B
Short Beam
Length
High Damping C
Metal on
Plastic
Low Damping D
Rattle
High Damping E
Metal on
Metal
Low Damping F
Long Beam
Length
High Damping G
Metal on
Plastic
Low Damping H
7
7

Setup
8
Open MATLAB

1. Select the working directory.


Select the folder of the .zip file
extracted

2. Open the MATLAB script,


SoundClassification.m
9
Open MATLAB

2.1 (Optional) If the script is undocked,


you can dock it into the main window

3. Try run the whole code.


 If no error, follow the next step.
 If error show up, ask for help.
10
Installing the required packages
Install these packages
• Signal Processing Toolbox
• Audio Toolbox
• Statistics and Machine Learning Toolbox

1. On the top menu bar, go to Home >


Adds-Ons > Get Add-Ons

2. Add-On Explorer
will pop up. Search
for a package.

3. Click on Install button,


and wait until the installation
finishes
11
11

MATLAB Basic
12
MATLAB Interface

Path to Working Directory

Command Prompt Workspace

Working
Directory

Command Window

Details Command
History
13
Basic Keyboard Shortcuts
Try out these Keyboard Shortcuts for your future convenience!

Shortcuts Description
CTRL + Enter Run a section
CTRL + Shift + Enter Run a section and advance
F9 Run Selected Code: Highlight (select) the code you want to run,
then press F9 to execute just that part in the Command
Window.
CTRL + R, CTRL + T Comment/Uncomment code
CTRL + F Search text in the code
CTRL + Z, CTRL + Y Undo/Redo
14
14

MATLAB Code
Walkthrough
15
Getting started

In this tutorial, it is highly recommended to …


 Run the code section by section
 Read the guidelines given in the code
 Try to understand what each section does
 Ask question when needed during the tutorial session

Clear things up!


 Run the first section (Restart the code) to clear up Command
Window, variables in Workspace, and all the plots
16
1. Import Dataset
 Run this section to import the audio dataset folders the audioDataStore
object.
Define the dataset path. This is the relative path to the working directory

Load the dataset as


audioDataStore object.

The labels of each audio files


come from folder names.

Note: These lines does not have semi-colon (;) at the end to let the
output show up in the Command Window

 Check out the output!


17
1.1 Display the dataset summary
 Run this section to display the summary of the loaded dataset.
Display the table that count files in each class.

 Check out the output in the Command Window!

Dataset samples distribution


among classes
18
1.2 Extract Class Labels and Filenames
 Run this section to get the variables labels and filename, which are used in
the latter section.
labels is the list of classes in your
dataset

numClasses is the number of classes in


your dataset

 Check out the output in the


Command Window!

#Classes in the dataset


(based on #folders)
19
2. Plotting Sound Waveform and Spectrogram
 Run this section to plot the sound waveform and spectrogram of each sound
classes
For-loop through the number of classes
idx is a list of file positions (indices) that belong to the current
class.
randomIdx is one randomly chosen index from that list. Using
this index, we get the audio file’s filename and label to show in
the plot title

 audioread read audio file from the file path. This


returns sound signal vector and the sampling rate (fs)
 Sound signal are normalized to -1,1 by dividing by the
maximum amplitude.
 timeVector is time vector corresponding to each data
point in the sound signal

nfft: number of FFT points (controls frequency resolution).


window: apply a Hamming window to reduce edge effects.
overlap: 75% overlap between windows for smoother results.
stft(): computes the Short-Time Fourier Transform to show how frequencies change
over time.
spectrogramPower: the strength (magnitude) of frequencies over time.
20
2. Plotting Sound Waveform and Spectrogram
 This part of code plot the waveform and spectrogram of the sound random
from each class
 Top plot: shows the sound waveform
(amplitude vs. time).
 Bottom plot: shows the STFT spectrogram
(frequency content vs. time).
 The plot title includes the sound’s label
and filename.

Expected Output
23
3. Features Extraction
 Run this section to extracts acoustic features from every audio files in the dataset.
 Each sound is converted into a small set of numbers (features) that represent its
main characteristics for machine learning.
Adjust Number of Coefficients for MFCC Hint: Use CTRL + F to search for
features (ADJUST THIS!) in the code. These
marks show where you can change
variables easily for your assignment.

Adjust Power Threshold, defining the


minimum energy of the analysis frame

win_length & overlap_length: Define how each sound is split


into small overlapping frames for analysis.

Loop through each file and extract features:


24
3. Features Extraction (Vectorised Pitch and MFCC)

The features extraction have the following steps

1. Load audio and normalise the signal

2. Framing: Segmenting signal into small parts, then apply window function

3. Calculate energy, Pitch and MFCC from each frame

4. Discard the frame with energy below the threshold

5. Vectorisation: Apply mean for MFCC features and median for Pitch features,
aggregating framewise features into a single vector
25
3. Features Extraction

Framing: Segmenting signal into small parts

Window Length

Overlap Length
Windowing: Apply window function to each frame

Frame 1

Frame 2

Frame 3
26
3. Features Extraction
Calculate energy, Pitch and MFCCs from each frame

1. MFCCs
MFCC n

MFCC 2
Frame 1
MFCC 1

logE
Time Frame

2. Energy: sum square of signal


𝑁𝑁−1
2
𝐸𝐸 𝑚𝑚 = � 𝑥𝑥𝑚𝑚 𝑛𝑛 � 𝑤𝑤[𝑛𝑛]
𝐸𝐸 𝑚𝑚 : energy of frame 𝑚𝑚
𝑛𝑛=0
𝑥𝑥𝑚𝑚 𝑛𝑛 : signal in frame 𝑚𝑚
𝑤𝑤[𝑛𝑛]: window function
3. Pitch
use MATLAB's pitch()
function to estimate it per frame
27
3. Features Extraction: Energy Threshold
• Most of the recording is quiet, only the two "click" (the mechanical
impacts) carry useful information

• Frames with energy below the threshold (silence, background noise)


are discarded

Remaining frames
28
3. Features Extraction: Vectorisation
So far, each audio file produced many frames features. However, machine learning
classifiers (SVM or KNN), requires 1D feature vector for each audio file.

Vectorisation process combine all the high-energy frames into a single vector:
• Median pitch (pitch estimates are sensitive to outliers)
• Mean MFCC values (MFCCs are generally more stable)

Frame 1 Frame 2 … Frame n

Mean MFCC n MFCC n MFCC n MFCC n


Mean … … …
Resulting Mean MFCC 2 MFCC 2 MFCC 2 MFCC 2
features
vector Mean MFCC 1 MFCC 1 MFCC 1 MFCC 1

Mean logE loge loge loge


Median
Median Pitch Pitch Pitch Pitch

Result: one row = [pitch, MFCC₁, MFCC₂, ...]. A single feature vector
representing the whole audio file
29
3. Features Extraction
Create the features table

Display first few rows


Output

 These raw features have very different ranges (some large negative, some small positive).
 Machine learning models can get confused when features have different scales. Features
with larger values can dominate, causing poor learning and inaccurate predictions.
30
3.1 Features Normalisation
 Run this section to normalise the features extracted from the previous section
 In this tutorial, we apply Z-score normalisation on each features (column)
𝑋𝑋 − 𝜇𝜇
Z= ;
𝜎𝜎
𝑍𝑍 is normalised features matrix, 𝑋𝑋 is raw features matrix, 𝜇𝜇 and 𝜎𝜎 are mean and
standard deviation of each feature (column).
Calculate Mean and Standard deviation
for each feature (column)

Rename the variables


 X for features
 Y for labels
Output
31
4. Plot the extracted MFCC
 Run this section to plot MFCCs features of each class

Reuse the settings from previous section

Expected Output
32
5. Visualising Data Separation using t-SNE
 Run this section to plot t-SNE of the extracted features

Select number of dimension. You can also


plot 3D t-SNE!
 You can do this by change it to three and
adjust the plotting code

Expected Output
t-SNE is a technique that reduces high-dimensional data (like your
MFCC features, which may be 12 (more or less) per sound frame into
just 2D or 3D (here is 2D), and plot it as shown here. The 2 dimensions
are statistically identified by t-SNE clustering.

Simple analogy of t-SNE: Think of t-SNE like taking a group


photo:Each person is originally standing in a big 3D room (high-
dimensional MFCC space).t-SNE brings them into 2D (like a flat photo)
but tries to keep friends (similar sounds) standing near each other, and
people from different groups far apart.
6. Training and Validation: Cross-validation 33

In this tutorial, we evaluate the machine learning (ML) models through k-fold cross-validation.
K_fold=5 means the data is divided into 5 equal parts with 4 for Training and 1 for Validation (80% Training &
20% Validation). K_fold=10 means 9 for Training and 1 for Validation (90% Training & 10% Validation). ML
program will repeat a few times (K_Fold times) to ensure each part of the data are tested and validated.

Number of fold for cross-validation

Train Test
Dataset
Fold 1 1 2 3 … k Accuracy 1
Fold 2 1 2 3 … k Accuracy
Accuracy22
Mean
Fold 3 1 2 3 … k Accuracy
Accuracy31
Accuracy
… 1 2 3 … k Accuracy
… 1

Fold n 1 2 3 … k Accuracyk1
Accuracy

 The dataset is split into k equal parts (folds)


 In each round, one fold is used for ML testing, and the remaining for ML training
 This process is repeated k times so that each fold is used as the test set once
 Model performance (e.g., accuracy) is averaged across all rounds for a fair
evaluation
34
6.1 KNN (k-Nearest Neighbours) Machine Learning Model:
Training and Validation
 Run this section to perform cross validation on the KNN model (your codes have
KNN and SVM sections)
Set KNN model number of neighbours!

Check resultsTable_KNN variable for full


classification results

Display misclassification files

Display cross-validated accuracy


35
Confusion Matrix

This part of code plot confusion


matrix

Confusion Matrix for KNN (10-fold Cross-Validation)

k_neigh = 5

A 26 6 81.2% 18.8%

B 2 30 93.8% 6.2%

C 29 3 90.6% 9.4%

D 3 29 90.6% 9.4%

E 32 100.0%

F 32 100.0%
True Class

G 24 8 75.0% 25.0%

H 4 28 87.5% 12.5%

Precision of
each class 92.9% 83.3% 90.6% 90.6% 100.0% 100.0% 85.7% 77.8%

7.1% 16.7% 9.4% 9.4% 14.3% 22.2%


Recall of each class

A B C D E F G H

Predicted Class
36
6.2 SVM (Support Vector Machine) Machine Learning Model:
Training and Validation
 Run this section to perform cross validation on the SVM model
Set SVM model box constraint!

Other part of the code is similar to the


previous section for KNN model
Check resultsTable_SVM variable for full
classification results
37
7. Testing the model with unseen data in the separate
folder (Optional) *You need to uncomment this section by selecting the code then
CTRL+T before running it

 Run this section to test the model with the unseen data from the folder
Rattle_DB_test
 This section is optional, but will be useful for the main assignment, in case you want
to test the model with unseen sound

Unseen data folder directory


 Test data folder will have the same
structure as the training one

The rest of the code consists of


 7.1 Import dataset  7.5 Test on KNN model
 7.2 Display Dataset Summary  7.6 Test on SVM model
 7.3 Features Extraction
 7.4 Features Normalisation

You might also like