I am trying to write an harmonic filter.
Here is the code (python):
from scipy.io import wavfileimport numpy as npwave_file_path = 'in.wav'num_harmonics = 10base_frequency = 50sample_rate, data = wavfile.read(wave_file_path)# after this - sample_rate = 10,000 & len(data) = 10,000frequencies = np.fft.fftfreq(len(data), 1.0 / sample_rate)spectrum = np.fft.fft(data, len(data))base_index = int(base_frequency * len(data) / sample_rate)# the above - base_index = 50 # Remove harmonicsfor harmonics in range(2, num_harmonics + 1):# is this the right multiply? harmonics * base_index? it maximum=500, whenever the number of spectrum elements is much bigger (10,000) harmonic_index = int(harmonics * base_index) spectrum[harmonic_index] = 0# Inverse Fourier Transform to get filtered signalfiltered_data = np.round(np.fft.ifft(np.fft.fftshift(spectrum)).real).astype(np.int16)output_file_path = 'out.wav'wavfile.write(output_file_path, len(filtered_data), filtered_data)
seems that this doesn't running the filter properly, since the output has not smooth edges.
The accurate solution with smooth edges should be as this:smooth image
As I can see, the edges are not smooth for harmonic filter, and it seems that the algorithm is wrong.Nonetheless, I check and found that len(data) is as same as sample_rate
What may be wrong? How can I do harmonic filter based on input wav file?
This are some tries - I need to make the output signal (desired) as the screenshot, from the original audio signal.wave examples - input -> output
I tried to change the parameters, check whether the base_index may be wrong.
Also, try chat-gpt and use google, but didn't find any solution for this.