Currently, I am trying to get the pitch for this twinkle twinkle little star file. For the most part the notes are correct in their frequencies which we get with the variable index_max. However, for the note of C5, it is returning C6. Frequency for C5 is around 523, while the frequency for C6 is around 1046. The FFT is telling us that the frequency is one octave above the expected result. This actually happens with many other files, and it seems that the lower the note, the more likely there is to be an issue. Any clarification on a better way to ask this question or an answer would be greatly appreciated!
import scipy.io.wavfile as wave
import numpy as np
from frequencyUtil import *
from scipy.fft import fft, ifft
def read_data(scale):
infile = "twinkle.wav"
rate, data = wave.read(infile)
sample_rate = int(rate/scale)
time_frames = [data[i:i + sample_rate] for i in range(0, len(data), sample_rate)]
notes = []
for x in range(len(time_frames)): # for each section, get the FFT
if(type(data[0]) is np.int16): # If not dual channel process like normal
dataZero = np.array(time_frames[x])
else: # if is dual channel get first ele of every list
data = np.array(time_frames[x]) # convert to np array
dataZero = [row[0] for row in data]
frequencies = fft(dataZero) # get the FFT of the wav file
inverse = ifft(np.real(frequencies))
index_max = np.argmax(np.abs(frequencies[0:8800//scale])) # get the index of the max number within music range
#print(abs(frequencies[index_max]))
# filters out the amplitudes that are lower than this value found through testing
# should eventually understand the scale of the fft frequencies
if(abs(frequencies[index_max]) < 4000000/scale):
continue
index_max = index_max*scale
print(index_max)
notes.append(index_max)
return notes```