I have pitch detection function written here but it need numpy
at least, I think you should change it a bit. It does not rely on that library much. Just for faster results.
Here there is the code, this function as you can see does not shift the window with the size of it, instead it shifts window with some overlap. you should adjust these particular codes.
there is some usage of numpy
module which I could have changed it easily but I leave it to you.
There are many rules in signal processing and I have implemented some. for example if the energy of a frame is not enough it doesn't have pitch and it is shown by sending -1 instead of a pitch.
import numpy as np
def pitch_detection(self, frame_matrix, frame_number, lag_vector, frequency):
np.seterr(divide='ignore', invalid='ignore')
pitch_freq_vector = []
for frame in range(frame_number):
ccf = []
frame_expand_1 = frame_matrix[frame-1, :]
frame_expand_2 = frame_matrix[frame-2, :]
temp_corr_1 = frame_matrix[frame, :]
temp_corr_2 = np.append(frame_expand_1[256:], temp_corr_1, axis=0)
temp_corr_2 = np.append(frame_expand_2[192:256], temp_corr_2, axis=0)
len_tc2 = len(temp_corr_2)
for lag in lag_vector: #pitch is the highest correlation in lag vector
ccf.append(np.sum(temp_corr_1*temp_corr_2[len_tc2-lag-512:len_tc2-lag]))
max_index, max_value = max(enumerate(ccf), key=operator.itemgetter(1))
if max(ccf) > 0.3*np.sum(np.power(temp_corr_1, 2)): #if more than 30 detect pitch
pitch_freq_vector.append(max_index)
else:
pitch_freq_vector.append(-1)
return pitch_freq_vector