Commit f8cd3909 authored by Stelin Dinoshan R R's avatar Stelin Dinoshan R R

Upload New File

parent 9400f71d
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"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment