Delivery | Terms and Conditions | Opening Hours

Categories

Fuzzy Logic AI

Fuzzy Logic AI for Guitar generated Music analysis Part -I

Introduction to Fuzzy Logic AI

Fuzzy Logic is the easiest form of Artificial Intelligence implementation, and its strength lies in interpretation and understanding, while the weakness is in learning algorithms.  Dr Lotfi Zadeh developed it in the 1960s and he wanted a computer to see the greys between 0 and 1.

Fuzzy Logic implementations can be of various kinds, and the two most popular ones are listed below:

  1. Mamdani Inference method
  2. Sugeno inference method

The Mamdani Fuzzy Logic way, and we will focus more on Mamdani inference method, includes:

Ebrahim Mamdani
introduced a new method of fuzzy inference systems, which was called ‘Mamdani-Type Fuzzy Inference‘. Mamdani-Type Fuzzy Inference have elements like human instincts

Fuzzy logic
values rather old or somewhat young. The most well-known system is the Mamdani rule-based one. It uses the following rules: Fuzzify all input values into

  1. Fuzzy sets
  2. Fuzzification
  3. Defuzzification
  4. Crisp sets
djj1m
What’s next on the todo list today? 1,iTunes chart for website 2,fuzzy logic natural language mp3 info and artwork… https://t.co/b2GFk7Y7tO

To understand Mamdani Fuzzy Logic inference, implies to know that the input data has memberships in the range of 0-1, all Fuzzy Logic operates between 0 and 1.  A common example that I often give is the one of classifying people by their ages:

  1. Newborns
  2. Infants
  3. Young
  4. Adolescent
  5. Teenage
  6. Middle aged
  7. Old
aj_buu
@BULLETSYMPHONY Toshiba Digital Programmable Rice Cooker, Steamer & Warmer, 3 Cups Uncooked Rice with Fuzzy Logic a… https://t.co/2TFfdpt4JV

I understand memberships to be a graph/table that segregates people into these categories by assigning them weightages between 0 to 1.  Young would have the value of 1 for Infants, Young and Adolescents, while Infants would have a value of 1 only when the age group is 0-6 years.  Drawing a table of this would explain this better:

Ages\Membership

body>
 

Newborns

Infants

Young

Adolescent

Teenage

Adult

Old

0-2

1

0.4

1

0.2

0

0

0

3-5

0.1

1

1

0.4

0

0

0

6-12

0.2

0.4

1

1

0

0

0

13-19

0.1

0.2

1

0.5

1

1

0

20-32

0

0

1

0.2

0.4

1

0

32-52

0

0

0.4

0

0

1

0.3

52-72

0

0

0

0

0

1

1

The membership values as they appear in the table are depictive of Fuzzy Logic, there is a crisp value at 1, but otherwise they are fuzzy.

Prelude to Fuzzy Logic

Before we even get to the stage where we have to work in Fuzzy Logic, there are somethings that must be prepared, that is the output of guitar skills, to be the input to the Fuzzy Logic System.  These include our audio fingerprints that require extraction from the music analysis example:

  1. Frequency and Pitch
  2. Dynamics, Intensity and Loudness
  3. Timbre
  4. Duration (this is being worked out)
  5. Structure
  6. Scale

This was mentioned and detailed out in a previous post.  Go there.

MediaNewZealand
@AceSNZ @ChantelleBakerr @radionz Fuzzy logic. Ardern could be PM again tomorrow. She is still an MP and a member o… https://t.co/cvdOqBBsPK

Fuzzy logic
Fuzzy logic is a form of many-valued logic in which the truth value of variables may be any real number between 0 and 1. It is employed to handle the concept

It is important to understand what these factors are and how they relate to each other, if we note Frequency & Pitch deal with the change in Pitch of the music analysis example, Dynamics, Intensity and Loudness deal with the loudness of sound, Timbre is the Tone of the piece and Structure is the repetitive patterns in music analysis examples that give us some idea about the construction of the music piece.  The first four seem like each other, as they indeed might be, while the Scale, the fifth point must be understood under the aegis of notes, the first four points as below:

kachoo22057315
@jjbpod I enjoy the music samples, but the extent of your review/critique doesn’t extend to music analysis that war… https://t.co/7AnCm8DD4u

Lestatmi5
Music and discourse: A systemic-Functional approach for music analysis in multimodal contexts – Diego L Forte, 2023 https://t.co/4z8x32HYMj

  1. Pitch is measured by Frequency in Hertz(Hz).
  2. All notes in music have a certain frequency, Hertz (Hz), the player has to play for the notes each of which have a frequency and duration. It is easy to imagine, a guitar player will play a single note and keep the finger on the note for a duration. In short, we could say that both the frequency and the duration will determine how the note sounds.  Here, since the duration would be important, I decided to add the duration to audio fingerprint.
  3. Dynamics, Intensity and Loudness in decibels(dB).
  4. The structure impacts the piece played and can be defined as the way the piece has been constructed, verses and chorus etc. all pay a part in how the structure is defined .
  5. Timbre is rather difficult to describe but it is the sound quality or the tone quality of the note being played. There are 7 ways to describe timbre:
    1. Nasal
    2. Rich or thick
    3. Noisy
    4. Distorted
    5. Breathy
    6. Vibrato
    7. Tremolo
Musical analysis
Musical analysis is the study of musical structure in either compositions or performances. According to music theorist Ian Bent, music analysis “is the

The Instruments timbre can be calculated by calculating the fundamental pitch and the overtone.  For instance, when we play the note F, apart from the frequency of F being played, what is also played are some overtone frequencies like, F4, C5, F5, A5 and so on.  My problem here was that while I could get to the frequency of say, note F, there was little data on the overtones, as also, I would have to write a formula for Timbre from this data and then extract it from the most perfect piece and then the music analysis example of the employee or candidate.  Since the quality of the tone would depend on the instrument as well, one Guitar being different from another Guitar, and it is a known fact, the Fender would differ from a Les Paul from Epiphone etc, I decided against using timbre as an audio fingerprint.

Music theory
principles in music”. The musicological approach to theory differs from music analysis “in that it takes as its starting-point not the individual work or performance

  1. Further to the above – v = lambda x Hertz or speed = frequency x wavelength.
  2. Also wavelength = 2 x Length, in our case this is the length of the string of the Guitar from bridge to nut, for the Fender Squier I use, it is 76.5cm or .765m.
  3. These formulas are particularly important since we would be asked to find the frequency of a note given that we know the length of the string and velocity or speed, or sometimes to find the velocity knowing that the wavelength is known, and the frequency of the note being played is known. Either way these formulas would help in extracting and calculating the frequency in Hz which is our basic purpose.

To further enhance this understanding, I will analyse every note on my Guitar of the G major scale and calculate the wavelength for each note, if you remember the G major scale is made up as follows:

G A B C D E F#

Ideally these notes, which are in the first octave, should be played at frequencies for G major:

G3. – 24.500 – 25.957. 297 Hz (as per data made available later here)

A3. – 27.500 – 29.135. 327.5 Hz

B3. – 30.868. 367 Hz

C3. – 16.352 – 17.324. 390 Hz

D3. – 18.354 – 19.445. 435 Hz

E4. – 20.602. 495 Hz

F4#. – 21.827 – 23.125. 550 Hz

G4 – 587 Hz

It comes to my mind, that having written the notes that comprise G major all over the fretboard, click here to see the diagram, it would also make sense to write down the frequencies for all those notes as they would be in the 0th, 1st, 2nd , 3rd, 4th, and sometimes the 5th octave.  This would make for a very interesting project – analysing how on a guitar neck when you play the scales vertically, the octaves change.  As mentioned in guitar theory earlier, scales can be played horizontally, along one string, and vertically on some 3-5 frets on the fretboard.

On using python Librosa with matplotlib, we got D major and B major for both the music analysis example, while the Guitar skill evaluator had played it in G major.  Doubts arise here, but I am sure the guitar was playing G major.  However, as it turns out, this is a solo, not a rhythm piece and further the influence of both, the note B, and the note D is heavy in this solo.

I realize that I must check the perfect solo piece which still has to be recorded.  There’s work and I am happy to play the guitar anytime.

For our information further, the tuning of open strings for an electric guitar (or classical guitar) is E2 (82 Hz), A2 (110 Hz), D3 (147 Hz), G3 (196 Hz), B3 (246.94) and E4 (330 Hz), though the music is notated two octaves higher than it sounds (for readability).

At the end of this section, later on, we will try and develop a widget that will calculate the scale of a music piece.

 

Coding for inputs to Fuzzy Logic Inference system

To my mind the structure is important, as it is the place we rule out all that, that is not required even before we get into the frequencies and decibels.  To decipher the structure I cut upon this unique method:

  1. Analyze and magnify the most perfect piece, at say, location 1000, using numpy library.
  2. Plot a graph using matplotlib library of python.
  3. Analyze and magnify the sample employee music analysis example.
  4. Now locate the employee music analysis example on the most perfect piece.
  5. Any deviations here will be defects in structure.
  6. We can rule out any sample audio that is out by over 20%.

Code and the output for the music analysis sample as below.  We have used python for the code.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import librosa
import IPython.display as ipd

x,sr = librosa.load('RSP-Skill-evaluation-19_01_23_-4.47-PM.wav')
print(type(x))
print('x length: {}' .format(len(x)))
print(x[1])
print(type(sr))
print('sr = {}' .format(sr))
#sr = sample rate = samples per second

print('Sound clip is {} seconds long.' .format( len(x) / sr))

plt.figure( figsize=(14,5))

t = np.array(range(0, len(x))) / sr
plt.plot(t[0:1000],x[0:1000])
#plt.plot(t,x)
plt.xlabel('Time (seconds)')
plt.ylabel('Pressure')
ipd.Audio(x, rate=sr)
plt.show()

Music analysis example with Fuzzy Logic
Fig 1.1 : Structure of music analysis example at 0-1000 ms of CEO candidate.
Fig. 1.1a – Structure analysis of junior management candidate at 0-1000 ms

As becomes obvious, the two structures of music analysis example are different and we could rate them based on the structure analysis of the most perfect piece.

Once the structure has been verified, we delve into the frequency and pitch, dynamics, intensity & loudness in decibels and duration:

  1. Use numpy, matplotlib and Librosa to find the pitch of the music analysis example as well as the perfect piece.
  2. Similarly do for decibels which can be in the same frame as frequency.
  3. Compare with the most perfect piece.
  4. The timbre is the tone, however we have decided to do away with it as an audio fingerprint because of tonal differences based on the guitar you are playing.
import librosa
import matplotlib.pyplot as plt
import librosa.display

x, sr = librosa.load('RSP-Skill-evaluation-19_01_23_-4.47-PM.ogg')
# Spectrogram of frequency
X = librosa.stft(x)
Xdb = librosa.amplitude_to_db(abs(X))
plt.figure(figsize=(15, 3))
librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz')
plt.colorbar()
plt.show()

Fig 1.2 : Code for measuring Loudness of a music analysis example with Librosa

import librosa
import numpy as np
import pandas as pd
import librosa.display
import matplotlib.pyplot as plt
import sys
from aubio import source, pitch

win_s = 4096
hop_s = 512 
samplerate = 22024

s = source("Rashmi.wav", samplerate, hop_s)
samplerate = s.samplerate

tolerance = 0.8

pitch_o = pitch("yin", win_s, hop_s, samplerate)
pitch_o.set_unit("midi")
pitch_o.set_tolerance(tolerance)

pitches = []
confidences = []

total_frames = 0
while True:
    samples, read = s()
    pitch = pitch_o(samples)[0]
    pitches += [pitch]
    confidence = pitch_o.get_confidence()
    confidences += [confidence]
    total_frames += read
    if read < hop_s: break

print("Average frequency = " + str(np.array(pitches).mean()) + " hz")

Fig 1.3 Code for frequency detection of music analysis example (with Aubio not Librosa)

import librosa
import matplotlib.pyplot as plt
import librosa.display


# x, sr = librosa.load('RSP-Skill-evaluation-19_01_23_-4.47-PM.ogg')
x, sr = librosa.load('Rashmi - Skill evaluation - 19_01_23, 4.58 PM.wav')
hop_length = 512
chromagram = librosa.feature.chroma_stft(x, sr=sr, hop_length=hop_length)
fig, ax = plt.subplots(figsize=(15, 3))
img = librosa.display.specshow(chromagram, x_axis='time', y_axis='chroma', hop_length=hop_length, cmap='coolwarm')
fig.colorbar(img, ax=ax)
plt.show()

Fig 1.4 – Detection of Scale using Librosa

5. Duration is the 5th parameter, however, which we have added to our list and implies the duration the note is played for.  Duration can be found out by zooming in on a particular section of the music analysis example and calculating the duration of the B note and the D note and comparing with the most perfect piece, note by note.

Process of Fuzzy Logic

To work with Fuzzy Logic, it is important to define:

  1. Fuzzy Regions
  2. Fuzzy Profiles
  3. Fuzzy Rules
  4. Fuzzification
  5. Defuzzifier

In my next few posts, we will take each of these 5 items listed above. 

 931 total views,  1 views today