<jupyter_start><jupyter_text>Note: if plots don't show, there is a recent matplotlib issue that may be related:https://github.com/matplotlib/matplotlib/issues/18396  Filter Out Background NoiseUse soundpy to filter out background noise from audio signals. To see how soundpy implements this, see `soundpy.builtin.filtersignal`.<jupyter_code># to be able to import soundpy from parent directory:
import os
package_dir = '../'
os.chdir(package_dir)<jupyter_output><empty_output><jupyter_text>Let's import soundpy, assuming it is in your working directory:<jupyter_code>import soundpy as sp;
import IPython.display as ipd<jupyter_output><empty_output><jupyter_text>soundpy offers an example audio file. Let's use it and add some white background noise. Speech sample:<jupyter_code># Use function 'string2pathlib' to turn string path into pathlib object 
# This allows flexibility across operating systems
speech = sp.string2pathlib('audiodata/python.wav')<jupyter_output><empty_output><jupyter_text>Hear and see the speech<jupyter_code># set feature_type for visualization: 'stft' (same as 'powspec'), 'fbank', 'mfcc'
feature_type = 'stft'
# For filtering, we will set the sample rate to be quite high:
sr = 48000
s, sr = sp.loadsound(speech, sr=sr)
ipd.Audio(s,rate=sr)
sp.plotsound(s, sr=sr, feature_type='signal', title='Speech (signal)')
sp.plotsound(s, sr=sr, feature_type=feature_type, title='Speech ({})'.format(feature_type.upper()))<jupyter_output><empty_output><jupyter_text>Add noise: 10 SNR Go ahead and play with different SNR levels and see how the filtering handles it.<jupyter_code>snr = 20
s_snr = sp.augment.add_white_noise(s, sr=sr, snr=snr)
ipd.Audio(s_snr,rate=sr)
sp.plotsound(s_snr, sr=sr, feature_type='signal', title='Noisy Speech {} SNR (signal)'.format(snr))
sp.plotsound(s_snr, sr=sr, feature_type=feature_type, title='Noisy Speech {} SNR ({})'.format(snr, feature_type.upper()))<jupyter_output><empty_output><jupyter_text>Wiener Filter<jupyter_code>wf_snr, sr = sp.filtersignal(s_snr,
                               sr=sr,
                               filter_type='wiener', # default filter
                               filter_scale=2, # default = 1
                               duration_noise_ms = 120, # amount of time at beg of signal for noise reference
                               )
ipd.Audio(wf_snr,rate=sr)
sp.plotsound(wf_snr, sr=sr, feature_type='signal', 
               title='Filtered Noisy Speech {} SNR (signal) \nWiener Filter'.format(snr))
sp.plotsound(wf_snr, sr=sr, feature_type=feature_type, 
               title='Filtered Noisy Speech {} SNR ({}) \nWiener Filter'.format(snr, feature_type.upper()))<jupyter_output><empty_output><jupyter_text>Wiener Filter with PostfilterIn this case, **the post filter doesn't improve the signal** (rather makes it worse). But if you have a filtered signal that has a lot of 'musical noise' or artifacts resulting from filtering, this postfilter should reduce those artifacts.<jupyter_code>wf_snr_pf, sr = sp.filtersignal(s_snr,
                                   sr=sr,
                                   filter_type='wiener',
                                   filter_scale=2,
                                   duration_noise_ms = 120,
                                   apply_postfilter = True)
ipd.Audio(wf_snr_pf,rate=sr)
sp.plotsound(wf_snr_pf, sr=sr, feature_type='signal', 
               title='Filtered Noisy Speech {} SNR (signal) \nWiener Filter with Post Filter'.format(snr))
sp.plotsound(wf_snr_pf, sr=sr, feature_type=feature_type, 
               title='Filtered Noisy Speech {} SNR ({}) \nWiener Filter with Post Filter'.format(snr, feature_type.upper()))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10587598158678581, 'fraction_numerical': 0.006769564040075819, 'mean_word_length': 3.7177522349936143, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '43431', 'license': 'RSA-MD', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1154, 'n_words': 303, 'path': 'jupyter_notebooks/filter_out_noise.ipynb', 'repo_name': 'a-n-rose/Python-Sound-Tool', 'size': '3693'}