{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#%pip install librosa\nimport librosa\nimport soundfile as sf\nimport scipy.signal as signal\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"data_dir = '/kaggle/input/rfcx-species-audio-detection/'\ndf1=pd.read_csv(data_dir+'train_tp.csv')\nshow = df1.iloc[0]\ndf1.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Read the FLAC file associated with the first row of df1. Look at the signal from t_min to t_max."},{"metadata":{"trusted":true},"cell_type":"code","source":"data, samplerate = sf.read(data_dir+'train/'+show.recording_id+'.flac')\ntimes = np.linspace(0,len(data),len(data))/samplerate\nplt.plot(times,data)\nplt.xlim(show.t_min, show.t_max)\nplt.ylim(-.1,.1)\nplt.ylabel('Time (s)')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Look at the spectrogram of the data, using the limits of t and f."},{"metadata":{"trusted":true},"cell_type":"code","source":"Pxx, freqs, bins, im = plt.specgram(data, Fs=samplerate)\n\n# add axis labels\nplt.ylabel('Frequency [Hz]')\nplt.xlabel('Time [sec]')\nplt.xlim(44,46)\nplt.ylim(2000,6000)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We can also play the file and listen for ourselves!"},{"metadata":{"trusted":true},"cell_type":"code","source":"import IPython.display as ipd\nipd.Audio(data,rate=samplerate)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"An alternative approach to plotting spectrograph, based on https://www.kdnuggets.com/2020/02/audio-data-analysis-deep-learning-python-part-1.html"},{"metadata":{"trusted":true},"cell_type":"code","source":"from librosa import display as ld\nX = librosa.stft(data)\nXdb = librosa.amplitude_to_db(abs(X))\nplt.figure(figsize=(14, 5))\nld.specshow(Xdb, sr=samplerate, x_axis='time', y_axis='hz')\nplt.colorbar()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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}