Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
TMP-23-105
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
K.Tharmikan
TMP-23-105
Commits
f8cd3909
Commit
f8cd3909
authored
Sep 06, 2023
by
Stelin Dinoshan R R
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
9400f71d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
0 deletions
+37
-0
Base_Frequency.py
Base_Frequency.py
+37
-0
No files found.
Base_Frequency.py
0 → 100644
View file @
f8cd3909
import
numpy
as
np
import
scipy.io.wavfile
as
wavfile
# Define the bases and frequencies for different moods
happy_base
=
440
happy_freqs
=
np
.
array
([
0.5
,
1
,
1.5
,
2
,
2.5
,
3
])
sad_base
=
220
sad_freqs
=
np
.
array
([
1
,
1.25
,
1.5
,
1.75
,
2
,
2.25
])
def
classify_mood
(
base
,
freqs
,
song
):
# Compute the FFT of the song
fft
=
np
.
fft
.
fft
(
song
)
# Compute the magnitudes of the FFT
mag
=
np
.
abs
(
fft
)
# Find the index of the peak frequency
peak_idx
=
np
.
argmax
(
mag
)
# Compute the frequency corresponding to the peak index
peak_freq
=
peak_idx
/
len
(
song
)
# Compute the difference between the peak frequency and the expected frequencies
diffs
=
np
.
abs
(
peak_freq
-
freqs
)
# Find the index of the closest expected frequency
closest_idx
=
np
.
argmin
(
diffs
)
# Compute the difference between the base and the peak frequency
base_diff
=
np
.
abs
(
base
-
peak_freq
)
# If the base difference is less than 0.1, classify as the expected mood, otherwise classify as "unknown"
if
base_diff
<
0.1
:
return
"happy"
if
base
==
happy_base
else
"sad"
else
:
return
"unknown"
# Load the song from a .wav file
fs
,
song
=
wavfile
.
read
(
"Pop2.wav"
)
song
=
song
/
32767.0
# Normalize the song to the range [-1, 1]
# Example usage:
mood
=
classify_mood
(
happy_base
,
happy_freqs
,
song
)
print
(
mood
)
# Output: "happy"
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment