I generate a simple sine wave with a frequency of 100 and calculate an FFT to check that the obtained frequency is correct.
Then I calculate melspectrogram
but do not understand what its output means? where do I see the frequency 100 in this output? Why is the yellow bar located in the 25th area?
# In[4]:
import numpy as np
import matplotlib.pyplot as plt
import scipy.fft
import librosa
def generate_sine_wave(freq, sample_rate, duration)-> tuple[np.ndarray, np.ndarray]:
x = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
frequencies = x * freq
# 2pi because np.sin takes radians
y = np.sin(2 * np.pi * frequencies)
return x, y
sample_rate = 1024
freq = 100
x, y = generate_sine_wave(freq, sample_rate, 2)
plt.figure(figsize=(10, 4))
plt.plot(x, y)
plt.grid(True)
fft = scipy.fft.fft(y)
fft = fft[0 : len(fft) // 2]
fft = np.abs(fft)
xs = np.linspace(0, sample_rate // 2, len(fft))
plt.figure(figsize=(15, 4))
plt.plot(xs, fft)
plt.grid(True)
melsp = librosa.feature.melspectrogram(sr=sample_rate, y=y)
melsp = melsp.T
plt.matshow(melsp)
plt.title('melspectrogram')
max = np.max(melsp)
print('melsp.shape =', melsp.shape)
print('melsp max =', max)
If I change the frequency to 200, melspectrogram
it gives me this:
Why is the yellow bar in the 50 area?