{"cells":[{"metadata":{},"cell_type":"markdown","source":"<h1><center> Time-domain, Frequecny domain, and Time-frequency visualization + Band-pass filtering</center></h1>\n"},{"metadata":{},"cell_type":"markdown","source":"<center><img src=\"https://images.unsplash.com/photo-1591533862564-6cb20cd82ec9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80\" width=\"85%\"></center>"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport soundfile as sf\nimport matplotlib.pyplot as plt\nplt.rcParams.update({'font.size': 18})\nplt.style.use('seaborn')\nimport IPython.display as ipd\nfrom scipy.fft import fft\nfrom librosa.display import specshow\nimport librosa","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df_fp  = pd.read_csv('../input/rfcx-species-audio-detection/train_fp.csv')\ndf_tp  = pd.read_csv('../input/rfcx-species-audio-detection/train_tp.csv')\ndf_sample = pd.read_csv('../input/rfcx-species-audio-detection/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Time domain, Fourier, and Spectrogram"},{"metadata":{},"cell_type":"markdown","source":"## Species 11"},{"metadata":{},"cell_type":"markdown","source":"Let's visualize the time domain, frequency domain and time-frequency for one sample and mark the important parts. The vertical and horizontal lines show in what time and frequency the species's song was detected. "},{"metadata":{"trusted":true},"cell_type":"code","source":"example = df_tp[df_tp['species_id']==11].sample(n=1)\nX, fsamp = sf.read('../input/rfcx-species-audio-detection/train/'+str(example.recording_id.values[0])+'.flac')\nN = X.shape[0]\nt = np.linspace(0,len(X),len(X))/fsamp\nX_u = abs(fft(X))\nX_u = X_u[:int(N/2)]\nf_u=np.arange(0,N,1)*fsamp/N \nf_u = f_u[:int(N/2)] \nXstft = librosa.stft(X)\nXstft_db = librosa.amplitude_to_db(abs(Xstft))\nfig, (ax1, ax2, ax3) = plt.subplots(figsize=(10,15),nrows=3, ncols=1)\n\ntmin = int(np.floor(example['t_min'].values))\ntmax = int(np.ceil(example['t_max'].values))\nfmin = int(np.floor(example['f_min'].values))\nfmax = int(np.ceil(example['f_max'].values))\n\nax1.plot(t,X,'navy')\nax1.axvline(x=example['t_min'].values,c='r', linestyle='--')\nax1.axvline(x=example['t_max'].values,c='r', linestyle='--')\nax1.set_xlabel('Time (s)')\nax1.set_ylabel('Amplitude (-)')\nax1.set(title='Time domain')\n\nax2.plot(f_u, X_u,'darkred')\nax2.axvline(x=example['f_min'].values,c='b', linestyle='-.')\nax2.axvline(x=example['f_max'].values,c='b', linestyle='-.')\nax2.set_xlabel('Frequency (Hz)')\nax2.set_ylabel('Amplitude (-)')\nax2.set(title='Fourier transform')\nax2.legend()\n\nimg = specshow(Xstft_db, sr=fsamp, x_axis='time', y_axis='linear')\nax3.axvline(x=tmin, c='r', linestyle='--')\nax3.axvline(x=tmax, c='r', linestyle='--')\nax3.axhline(y=fmin, c='b', linestyle='-.')\nax3.axhline(y=fmax, c='b', linestyle='-.')\nclb = fig.colorbar(img, ax=ax3, format=\"%+2.f dB\")\nclb.ax.set_title('Amplitude (db)')\nax3.set(title='Spectogram')\nax3.set_ylabel('Frequency (Hz)')\nax3.set_xlabel('Time (s)')\nplt.subplots_adjust(hspace=0.5)\nplt.show()\n\ndisplay(example)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Full audio"},{"metadata":{"trusted":true},"cell_type":"code","source":"ipd.Audio(X,rate=fsamp)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Clipped Audio (species's song)"},{"metadata":{"trusted":true},"cell_type":"code","source":"ipd.Audio(X[int(tmin*fsamp):int(tmax*fsamp)], rate=fsamp)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Band-pass Butterworth filter\n\nThe min and max frequencies are given in the data. So, let's filter them out to listen to the species beautiful voice. "},{"metadata":{"trusted":true},"cell_type":"code","source":"from scipy.signal import butter,lfilter\n\ndef butter_bandpass(lowcut, highcut, fs, order=5):\n    nyq = 0.5 * fs\n    low = lowcut / nyq\n    high = highcut / nyq\n    b, a = butter(order, [low, high], btype='band')\n    return b, a\n\ndef butter_bandpass_filter(data, lowcut, highcut, fs, order=5):\n    b, a = butter_bandpass(lowcut, highcut, fs, order=order)\n    y = lfilter(b, a, data)\n    return y\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Applying the filter\nX_filt = butter_bandpass_filter(X, fmin, fmax, fsamp, order=9)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_filt_u = abs(fft(X_filt))[:int(N/2)]\n\nXstft_filt = librosa.stft(X_filt)\nXstft_db_filt = librosa.amplitude_to_db(abs(Xstft_filt))\n\nfig, (ax1, ax2, ax3) = plt.subplots(figsize=(10,15),nrows=3, ncols=1)\nax1.plot(t,X_filt,'navy')\nax1.axvline(x=example['t_min'].values,c='r', linestyle='--')\nax1.axvline(x=example['t_max'].values,c='r', linestyle='--')\nax1.set_xlabel('Time (s)')\nax1.set_ylabel('Amplitude (-)')\nax1.set(title='Time domain')\n\nax2.plot(f_u, X_filt_u,'darkred')\nax2.axvline(x=example['f_min'].values,c='b', linestyle='-.')\nax2.axvline(x=example['f_max'].values,c='b', linestyle='-.')\nax2.set_xlabel('Frequency (Hz)')\nax2.set_ylabel('Amplitude (-)')\nax2.set(title='Fourier transform')\nax2.legend()\n\nimg = specshow(Xstft_db_filt, sr=fsamp, x_axis='time', y_axis='linear')\nax3.axvline(x=example['t_min'].values,c='r', linestyle='--')\nax3.axvline(x=example['t_max'].values,c='r', linestyle='--')\nax3.axhline(y=example['f_min'].values,c='b', linestyle='-.')\nax3.axhline(y=example['f_max'].values,c='b', linestyle='-.')\nclb = fig.colorbar(img, ax=ax3, format=\"%+2.f dB\")\nclb.ax.set_title('Amplitude (db)')\nax3.set(title='Sample Spectogram')\nax3.set_ylabel('Frequency (Hz)')\nax3.set_xlabel('Time (s)')\nplt.subplots_adjust(hspace=0.5)\nplt.show()\n\ndisplay(example)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Full audio (filtered)"},{"metadata":{"trusted":true},"cell_type":"code","source":"ipd.Audio(X_filt, rate=fsamp)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Clipped audio (filtered species's song)"},{"metadata":{"trusted":true},"cell_type":"code","source":"ipd.Audio(X_filt[int(tmin*fsamp):int(tmax*fsamp)], rate=fsamp)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<font size=\"6\"> Please upvote if you liked this.<br>\n<font size=\"6\"> **WORK IN PROGRESS...** <br>"}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}