{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Filter Out Background Noise\n\n\nFilter out background noise from noisy speech signals. \n\nTo see how soundpy implements this, see `soundpy.builtin.filtersignal`.\n\nAs a general note for filtering, the Wiener Filter is the default filter for soundpy. It seems to filter signals more consequently than the Band Spectral Subtraction Filter.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Let's import soundpy, and ipd for playing audio data\nimport soundpy as sp\nimport IPython.display as ipd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define the noisy and clean speech audio files.\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nNote: these files are available in the soundpy repo.\nDesignate path relevant for accessing audiodata\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sp_dir = '../../../'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Noise sample:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "noise = '{}audiodata/background_samples/traffic.wav'.format(sp_dir)\nnoise = sp.string2pathlib(noise)\nspeech = '{}audiodata/python.wav'.format(sp_dir)\nspeech = sp.utils.string2pathlib(speech)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For filtering, we will set the sample rate to be quite high:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sr = 48000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create noisy speech signal as SNR 10\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "noisy, snr_measured = sp.dsp.add_backgroundsound(\n speech, \n noise, \n sr = sr, \n snr = 10, \n total_len_sec = 2, \n pad_mainsound_sec = 0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hear and see the noisy speech \n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ipd.Audio(noisy,rate=sr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sp.plotsound(noisy, sr=sr, feature_type='signal', \n title = 'Noisy Speech', subprocess=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hear and see the clean speech \n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "s, sr = sp.loadsound(speech, sr=sr)\nipd.Audio(s,rate=sr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sp.plotsound(s, sr=sr, feature_type='signal', \n title = 'Clean Speech', subprocess=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Filter the noisy speech\n^^^^^^^^^^^^^^^^^^^^^^^\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wiener Filter \n~~~~~~~~~~~~~\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's filter with a Wiener filter:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "noisy_wf, sr = sp.filtersignal(noisy,\n sr = sr,\n filter_type = 'wiener') # default" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ipd.Audio(noisy_wf,rate=sr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sp.plotsound(noisy_wf, sr = sr, feature_type = 'signal', \n title = 'Noisy Speech: Wiener Filter', \n subprocess=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wiener Filter with Postfilter\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's filter with a Wiener filter and postfilter\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "noisy_wfpf, sr = sp.filtersignal(noisy,\n sr = sr,\n filter_type = 'wiener',\n apply_postfilter = True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ipd.Audio(noisy_wfpf,rate=sr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sp.plotsound(noisy_wfpf, sr=sr, feature_type = 'signal', \n title = 'Noisy Speech: Wiener Filter with Postfilter', \n subprocess=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Band Spectral Subtraction\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's filter using band spectral subtraction\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "noisy_bs, sr = sp.filtersignal(noisy,\n sr = sr,\n filter_type = 'bandspec')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ipd.Audio(noisy_bs,rate=sr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sp.plotsound(noisy_bs, sr = sr, feature_type = 'signal', \n title = 'Noisy Speech: Band Spectral Subtraction', \n subprocess=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Band Spectral Subtraction with Postfilter\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, let's filter using band spectral subtraction with a postfilter\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "noisy_bspf, sr = sp.filtersignal(noisy,\n sr = sr,\n filter_type = 'bandspec', \n apply_postfilter = True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ipd.Audio(noisy_bspf,rate=sr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sp.plotsound(noisy_bspf, sr = sr, feature_type = 'signal', \n title = 'Noisy Speech: Band Spectral Subtraction with Postfilter', \n subprocess=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Filter: increase the scale\n^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's filter with a Wiener filter:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "filter_scale = 5\nnoisy_wf, sr = sp.filtersignal(noisy,\n sr=sr,\n filter_type = 'wiener',\n filter_scale = filter_scale)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wiener Filter\n~~~~~~~~~~~~~\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ipd.Audio(noisy_wf,rate=sr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sp.plotsound(noisy_wf, sr = sr, feature_type = 'signal', \n title = 'Noisy Speech: Wiener Filter Scale {}'.format(filter_scale), \n subprocess=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wiener Filter with Postfilter\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's filter with a Wiener filter and postfilter\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "noisy_wfpf, sr = sp.filtersignal(noisy,\n sr = sr,\n filter_type = 'wiener',\n apply_postfilter = True,\n filter_scale = filter_scale)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ipd.Audio(noisy_wfpf,rate = sr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sp.plotsound(noisy_wfpf, sr = sr, feature_type = 'signal', \n title = 'Noisy Speech: Wiener Filter with Postfilter Scale {}'.format(filter_scale),\n subprocess=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 0 }