2
$\begingroup$

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
from scipy.io.wavfile import read, write
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)
    
    sampling, signal = read(audio)
    
    # 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
    # write(audio.replace(".wav", " filtrado.wav"), sampling, filtrada.astype(signal.dtype))
    
    return None

AudioSignalProcessing("audio.wav")

The output plots are these:

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.

$\endgroup$
4
  • $\begingroup$ 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? $\endgroup$
    – endolith
    Mar 14, 2022 at 0:44
  • 2
    $\begingroup$ 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 ? $\endgroup$
    – Hilmar
    Mar 14, 2022 at 6:57
  • $\begingroup$ @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. $\endgroup$
    – Isaac
    Mar 15, 2022 at 3:06
  • $\begingroup$ @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 $\endgroup$
    – Isaac
    Mar 15, 2022 at 3:07

1 Answer 1

1
$\begingroup$

I've heard that some sounds in most languages use some very high and low pitched frequencies, so it would make sense it doesn't sound complete after those frequencies are removed. Getting rid of the noise without compromising heavily is probably more complicated than cropping the frequencies.

$\endgroup$
1
  • $\begingroup$ Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. $\endgroup$
    – ZaellixA
    Sep 26 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.