0
$\begingroup$

I am implementing a project for infant cry detection and the audio set contains background noises. So for preprocessing i need to remove the background noise from the audio.I am not able to get a proper output for the code in jupyter notebook. For this code the output file does not contain anything even the baby cry is erased.

import numpy as np  
import scipy as sp  
from scipy.io.wavfile import read  
from scipy.io.wavfile import write     
from scipy import signal  
import matplotlib.pyplot as plt  
get_ipython().magic('matplotlib inline')  

(Frequency, array) = read('cry.wav')  

len(array)  

plt.plot(array)   
plt.title('Original Signal Spectrum')  
plt.xlabel('Frequency(Hz)')  
plt.ylabel('Amplitude')  

FourierTransformation = sp.fft(array)  

scale = sp.linspace(0, Frequency, len(array))  

plt.stem(scale[0:5000], np.abs(FourierTransformation[0:5000]), 'r')    
plt.title('Signal spectrum after FFT')  
plt.xlabel('Frequency(Hz)')  
plt.ylabel('Amplitude')  


GuassianNoise = np.random.rand(len(FourierTransformation))  


NewSound = GuassianNoise + array  

write("New-Sound-Added-With-Guassian-Noise.wav", Frequency, NewSound)  

b,a = signal.butter(5, 1000/(Frequency/2), btype='highpass')  

filteredSignal = signal.lfilter(b,a,NewSound)  
plt.plot(filteredSignal) # plotting the signal.  
plt.title('Highpass Filter')  
plt.xlabel('Frequency(Hz)')  
plt.ylabel('Amplitude')  


c,d = signal.butter(5, 380/(Frequency/2), btype='lowpass') # ButterWorth low-filter  
newFilteredSignal = signal.lfilter(c,d,filteredSignal) # Applying the filter to the signal  
plt.plot(newFilteredSignal) # plotting the signal.  
plt.title('Lowpass Filter')  
plt.xlabel('Frequency(Hz)')  
plt.ylabel('Amplitude')  

write("New-Filtered-Sound.wav", Frequency, newFilteredSignal)  

Can i know how to debug this code or get any other code.

$\endgroup$
6
  • $\begingroup$ The way this question is posed makes it off topic for this board. You are passing a NewSound variable to lfilter that is not declared anywhere before and furthermore you probably want to be passing array. I'd also check the parameters of butter to make sure that it returns a digital filter. $\endgroup$
    – A_A
    Mar 2, 2019 at 13:00
  • 1
    $\begingroup$ oh, yet another infant cry detection project. These tend to be the rage here, so maybe the existing topics have information that matter to you. $\endgroup$ Mar 2, 2019 at 13:04
  • $\begingroup$ I only want the removal of background @Marcus Muller $\endgroup$
    – Ambruni
    Mar 2, 2019 at 13:06
  • $\begingroup$ I have updated the code please i had missed out on few lines. @A_A $\endgroup$
    – Ambruni
    Mar 2, 2019 at 13:08
  • 2
    $\begingroup$ I know, but your problem description is "I don't get the right output", and that's not really a problem description, so I've referred you to others having the same project, so that you can be inspired on how to investigate the problem :) $\endgroup$ Mar 2, 2019 at 13:08

2 Answers 2

1
$\begingroup$

Since you're adding white noise, the highpass and lowpass filtering will almost not remove the noise in the frequency band where you want to keep your signal, so you will always have some background noise with this highpass and lowpass filtering strategy.

Not sure if this helps, it depends on the signal-to-noise ratio: If you can clearly distinguish the noise from the signal in the spectrum (something similar as in the second figure of the Noisy Signal example in Matlab's documentation of the fft), you could set a threshold and make the spectrum with an amplitude below that threshold equal to zero before taking an inverse Fourier transform to get back to the (denoised) time-domain signal.

By the way, GuassianNoise = np.random.rand(len(FourierTransformation)) produces uniformly distributed noise. To get Gaussian noise, you need randn instead of rand.

$\endgroup$
0
$\begingroup$

try this in the final line.

write("file.wav", Frequency, np.int16(newFilteredSignal/np.max(np.abs(newFilteredSignal)) * 32767))
$\endgroup$

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.