# Help on audio filter with FFT on python

I need to take a .wav audio file that's noisy and filter out some noise. I have to do it using Fourier Transform. After some days researching and experimenting, I finally made a working function, the problem is that it doesn't work as I intend it to. Here is the function I made:

# Audio signal processing
import matplotlib.pyplot as plt
import numpy as np
from scipy.fft import fft, fftfreq, ifft

def AudioSignalProcessing(audio):

# Import the .wav format audio into two variables:
# sampling (int)
# audio signal (numpy array)

# time duration of the audio
length = signal.shape[0] / sampling

# x axis based on the time duration
time = np.linspace(0., length, signal.shape[0])

# show original signal
plt.plot(time, signal)
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.title("Original signal")
plt.show()

# apply Fourier transform and normalize
transform = fft(signal)

# obtain frequencies
xf = fftfreq(transform.size, 1/sampling)

# show transformed signal (frequencies domain)
plt.plot(xf, abs(transform)/np.linalg.norm(transform))
plt.xlabel("Frecuency (Hz)")
plt.ylabel("Amplitude")
plt.title("Frequency domain signal")
plt.show()

# filter the transformed signal to a 40% of its maximum amplitude
threshold = np.amax(transform)*0.4
filtered = np.copy(transform)
filtered[abs(transform) < 0.4 * max(abs(transform))] = 0

# show filtered transformed signal
plt.plot(xf,abs(filtered)/np.linalg.norm(filtered))
plt.xlabel("Frecuency (Hz)")
plt.ylabel("Amplitude")
plt.title("FILTERED time domain signal")
plt.show()

# transform the signal back to the time domain
filtered = ifft(filtered)

# show original signal filtered
plt.plot(time, filtered)
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.title("Filtered signal")
plt.show()

# convert audio signal to .wav format audio

return None

AudioSignalProcessing("audio.wav")


The output plots are these:

When I listen to the filtered audio, it doesn't sound good. The voice that's talking seems like it's in another room.

I've read some related questions here and maybe it's not a code problem but a "theoretical" problem.

• Well you can see from the spectrum that you've removed a lot of high frequency content, so it will sound filtered, too. Can you just lower the threshold? Mar 14, 2022 at 0:44
• You do a lot of damage to your signal and of course it sounds bad. What type of analysis and research have you done that led you to this algorithm? Why do you think this should work ? Mar 14, 2022 at 6:57
• @Hilmar well this class is very vague si they taught me basically that the FFT exists and how to use python, though the rest I had to learn it myself. Anyway, I couldn't go any further for this homework. Mar 15, 2022 at 3:06
• @endolith I did that and I guess I have to adjust it to every audio file I use, as I said one comment above, this should be enough for this homework. Thanks for the answer Mar 15, 2022 at 3:07