{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Helper Code - 01","metadata":{}},{"cell_type":"code","source":"# Python version:\nimport sys\nimport warnings\n\npython_version = '3.10.4'\n\nif sys.version_info <= (3, 10):\n    msg = f\"Following code was tested on {python_version}\"\n    warnings.warn(msg)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:50:53.776675Z","iopub.execute_input":"2023-01-18T04:50:53.777115Z","iopub.status.idle":"2023-01-18T04:50:53.783942Z","shell.execute_reply.started":"2023-01-18T04:50:53.777081Z","shell.execute_reply":"2023-01-18T04:50:53.782588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Working with the Data","metadata":{}},{"cell_type":"markdown","source":"Data Type: [.wav](https://en.wikipedia.org/wiki/WAV) audio [file_format].(https://docs.fileformat.com/audio/wav/)<br>\nThere are multiple ways to work with audio file either using specialized external audio processing libraries or standard python (and matplotlib) libraries. Some guidelines given below:","metadata":{}},{"cell_type":"code","source":"# sample .wav audio file\naudio_file_1 = '/kaggle/input/datafestintegration2023/train/train/0/01.wav' # For an A call\naudio_file_0 = '/kaggle/input/datafestintegration2023/train/train/1/01.wav' # For a Non A call","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:50:53.785567Z","iopub.execute_input":"2023-01-18T04:50:53.786116Z","iopub.status.idle":"2023-01-18T04:50:53.798996Z","shell.execute_reply.started":"2023-01-18T04:50:53.786083Z","shell.execute_reply":"2023-01-18T04:50:53.797572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. Using Librosa Library\n[librosa](https://librosa.org/doc/latest/index.html) [[src](https://github.com/librosa/librosa)]","metadata":{}},{"cell_type":"markdown","source":"#### - Loading Data","metadata":{}},{"cell_type":"code","source":"import librosa","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:50:53.802187Z","iopub.execute_input":"2023-01-18T04:50:53.803039Z","iopub.status.idle":"2023-01-18T04:50:55.723717Z","shell.execute_reply.started":"2023-01-18T04:50:53.802988Z","shell.execute_reply":"2023-01-18T04:50:55.722597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the audio as a waveform 'y', Store the sampling rate as 'sr'\ny, sr = librosa.load(audio_file_1)\nz, s_r = librosa.load(audio_file_0)\nprint('A call:', len(y))\nprint('Non A call', len(z))","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:50:55.725516Z","iopub.execute_input":"2023-01-18T04:50:55.726579Z","iopub.status.idle":"2023-01-18T04:50:59.291235Z","shell.execute_reply.started":"2023-01-18T04:50:55.726522Z","shell.execute_reply":"2023-01-18T04:50:59.289763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### - Visualizing Data","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.ticker as ticker\nimport librosa.display","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:50:59.294640Z","iopub.execute_input":"2023-01-18T04:50:59.295103Z","iopub.status.idle":"2023-01-18T04:50:59.302909Z","shell.execute_reply.started":"2023-01-18T04:50:59.295064Z","shell.execute_reply":"2023-01-18T04:50:59.301639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Wave visualization for the audio files\nplt.figure(figsize=(14, 5))\nlibrosa.display.waveshow(y, sr=sr)\nplt.title('A Call Waveform')","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:50:59.304765Z","iopub.execute_input":"2023-01-18T04:50:59.305260Z","iopub.status.idle":"2023-01-18T04:50:59.803090Z","shell.execute_reply.started":"2023-01-18T04:50:59.305211Z","shell.execute_reply":"2023-01-18T04:50:59.801903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(14, 5))\nlibrosa.display.waveshow(z, sr=s_r)\nplt.title('Non A Call Waveform')","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:50:59.804643Z","iopub.execute_input":"2023-01-18T04:50:59.805679Z","iopub.status.idle":"2023-01-18T04:51:00.275149Z","shell.execute_reply.started":"2023-01-18T04:50:59.805642Z","shell.execute_reply":"2023-01-18T04:51:00.273768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize Data\n# librosa short time fourier transform gives signal in the time-frequency domain\nY = librosa.stft(y)\nYdb = librosa.amplitude_to_db(abs(Y))\nplt.figure(figsize=(14, 5))\nplt.ylim([0, 100])\nlibrosa.display.specshow(Ydb, sr=sr, x_axis='time', y_axis='hz')\nplt.title('Spectrogram for A call')","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:00.276925Z","iopub.execute_input":"2023-01-18T04:51:00.277423Z","iopub.status.idle":"2023-01-18T04:51:01.948592Z","shell.execute_reply.started":"2023-01-18T04:51:00.277358Z","shell.execute_reply":"2023-01-18T04:51:01.947351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Z = librosa.stft(z)\nZdb = librosa.amplitude_to_db(abs(Z))\nplt.figure(figsize=(14, 5))\nplt.ylim([0, 100])\nlibrosa.display.specshow(Zdb, sr=s_r, x_axis='time', y_axis='hz')\nplt.title('Spectrogram for Non A call')","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:01.950013Z","iopub.execute_input":"2023-01-18T04:51:01.950386Z","iopub.status.idle":"2023-01-18T04:51:03.998195Z","shell.execute_reply.started":"2023-01-18T04:51:01.950353Z","shell.execute_reply":"2023-01-18T04:51:03.996906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### - Hearing Data","metadata":{}},{"cell_type":"code","source":"from IPython.display import Audio","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:04.000059Z","iopub.execute_input":"2023-01-18T04:51:04.000555Z","iopub.status.idle":"2023-01-18T04:51:04.007207Z","shell.execute_reply.started":"2023-01-18T04:51:04.000507Z","shell.execute_reply":"2023-01-18T04:51:04.005601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(y, rate=sr) # A call audio file","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:04.011872Z","iopub.execute_input":"2023-01-18T04:51:04.012360Z","iopub.status.idle":"2023-01-18T04:51:04.067207Z","shell.execute_reply.started":"2023-01-18T04:51:04.012322Z","shell.execute_reply":"2023-01-18T04:51:04.065568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(z, rate=s_r) # Non A call audio file","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:04.068940Z","iopub.execute_input":"2023-01-18T04:51:04.069430Z","iopub.status.idle":"2023-01-18T04:51:04.130061Z","shell.execute_reply.started":"2023-01-18T04:51:04.069394Z","shell.execute_reply":"2023-01-18T04:51:04.128889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Using standard libraries ","metadata":{}},{"cell_type":"markdown","source":"#### - Loading Data","metadata":{}},{"cell_type":"markdown","source":"1. Using the [wave](https://docs.python.org/3/library/wave.html) python standard library built for WAV sound format","metadata":{}},{"cell_type":"code","source":"import wave\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:04.132100Z","iopub.execute_input":"2023-01-18T04:51:04.133197Z","iopub.status.idle":"2023-01-18T04:51:04.138908Z","shell.execute_reply.started":"2023-01-18T04:51:04.133130Z","shell.execute_reply":"2023-01-18T04:51:04.137786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read data\naudio_file = '/kaggle/input/datafestintegration2023/train/train/1/01.wav'\nw = wave.open(audio_file, mode='r')\n\n# obtain details of data\nprint('Audio parameters:', w.getparams())\nnchannels, sampwidth, framerate, nframes, comptype, compname = w.getparams()\n\n# read all frames of audio as byte object\nframes = w.readframes(-1)\n\n# get np array from byte object, sampwidth is 2B from previous print output\naudio_signal = np.frombuffer(buffer=frames, dtype='int16')\nprint('Audio signal:', audio_signal[:10])\n\n# len of signal and number of audio frames should now match\nassert len(audio_signal) == nframes\n\nw.close()","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:04.140311Z","iopub.execute_input":"2023-01-18T04:51:04.141360Z","iopub.status.idle":"2023-01-18T04:51:04.156776Z","shell.execute_reply.started":"2023-01-18T04:51:04.141305Z","shell.execute_reply":"2023-01-18T04:51:04.155345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### - Visualizing Data","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.ticker as ticker","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:04.158181Z","iopub.execute_input":"2023-01-18T04:51:04.158657Z","iopub.status.idle":"2023-01-18T04:51:04.167963Z","shell.execute_reply.started":"2023-01-18T04:51:04.158615Z","shell.execute_reply":"2023-01-18T04:51:04.166560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# compute time sequence from nframes and framerate\ntime = np.linspace(start=0, stop=nframes/framerate, num=nframes)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:04.170201Z","iopub.execute_input":"2023-01-18T04:51:04.171421Z","iopub.status.idle":"2023-01-18T04:51:04.179809Z","shell.execute_reply.started":"2023-01-18T04:51:04.171376Z","shell.execute_reply":"2023-01-18T04:51:04.178666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(14, 5))\nplt.title('Waveform for a A call')\n# Scale frequency to kilohertz \nplt.plot(time, audio_signal/1000)\nplt.minorticks_on()\nplt.xlabel('Time [in seconds]')\nplt.ylabel('Frequency [in kilohertz]')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:04.181210Z","iopub.execute_input":"2023-01-18T04:51:04.181642Z","iopub.status.idle":"2023-01-18T04:51:04.574814Z","shell.execute_reply.started":"2023-01-18T04:51:04.181605Z","shell.execute_reply":"2023-01-18T04:51:04.573433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Using [matplotlib.pyplot.specgram](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.specgram.html)","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(14, 5))\n\"\"\"Fs is sampling frequency and cmap is colormap (certain colormaps give better \n   visualization of what you are looking for)\"\"\"\nplt.specgram(audio_signal, Fs=framerate, cmap='Spectral')\nplt.title('Spectrogram for A Call')\nplt.ylim([0, 100])\nplt.xlabel('Time [in seconds]')\nplt.ylabel('Frequency [in hertz]')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:04.576297Z","iopub.execute_input":"2023-01-18T04:51:04.576706Z","iopub.status.idle":"2023-01-18T04:51:04.849451Z","shell.execute_reply.started":"2023-01-18T04:51:04.576668Z","shell.execute_reply":"2023-01-18T04:51:04.848255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### - Hearing Data","metadata":{}},{"cell_type":"code","source":"from IPython.display import Audio","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:04.850937Z","iopub.execute_input":"2023-01-18T04:51:04.851280Z","iopub.status.idle":"2023-01-18T04:51:04.857101Z","shell.execute_reply.started":"2023-01-18T04:51:04.851248Z","shell.execute_reply":"2023-01-18T04:51:04.855706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(audio_file)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:04.859085Z","iopub.execute_input":"2023-01-18T04:51:04.859595Z","iopub.status.idle":"2023-01-18T04:51:04.877991Z","shell.execute_reply.started":"2023-01-18T04:51:04.859548Z","shell.execute_reply":"2023-01-18T04:51:04.876333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Repeat the same for the Non A Call","metadata":{}},{"cell_type":"code","source":"# read data\naudio_file = '/kaggle/input/datafestintegration2023/train/train/0/01.wav'\nw = wave.open(audio_file, mode='r')\n\n# obtain details of data\nprint('Audio parameters:', w.getparams())\nnchannels, sampwidth, framerate, nframes, comptype, compname = w.getparams()\n\n# read all frames of audio as byte object\nframes = w.readframes(-1)\n\n# get np array from byte object, sampwidth is 2B from previous print output\naudio_signal = np.frombuffer(buffer=frames, dtype='int16')\nprint('Audio signal:', audio_signal[:10])\n\n# len of signal and number of audio frames should now match\nassert len(audio_signal) == nframes\n\nw.close()\n\n# compute time sequence from nframes and framerate\ntime = np.linspace(start=0, stop=nframes/framerate, num=nframes)\n\nplt.figure(figsize=(14, 5))\nplt.title('Waveform for Non A Call')\n# Scale frequency to kilohertz \nplt.plot(time, audio_signal/1000)\nplt.minorticks_on()\nplt.xlabel('Time [in seconds]')\nplt.ylabel('Frequency [in kilohertz]')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:04.879191Z","iopub.execute_input":"2023-01-18T04:51:04.879546Z","iopub.status.idle":"2023-01-18T04:51:05.282383Z","shell.execute_reply.started":"2023-01-18T04:51:04.879515Z","shell.execute_reply":"2023-01-18T04:51:05.280875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(14, 5))\n\"\"\"Fs is sampling frequency and cmap is colormap (certain colormaps give better \n   visualization of what you are looking for)\"\"\"\nplt.specgram(audio_signal, Fs=framerate, cmap='Spectral')\nplt.title('Spectrogram for Non A Call')\nplt.ylim([0, 100])\nplt.xlabel('Time [in seconds]')\nplt.ylabel('Frequency [in hertz]')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:05.284142Z","iopub.execute_input":"2023-01-18T04:51:05.284566Z","iopub.status.idle":"2023-01-18T04:51:05.609137Z","shell.execute_reply.started":"2023-01-18T04:51:05.284527Z","shell.execute_reply":"2023-01-18T04:51:05.607614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(audio_file)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:05.611396Z","iopub.execute_input":"2023-01-18T04:51:05.611920Z","iopub.status.idle":"2023-01-18T04:51:05.626678Z","shell.execute_reply.started":"2023-01-18T04:51:05.611873Z","shell.execute_reply":"2023-01-18T04:51:05.625275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This helper code mainly dealt with working with audio data and creating some visualizations/transformations of the data from .wav to audio signals, waveform and spectrograms.\n\nTo use these techniques you have to combine the domain knowledge about A calls to build your model. Check out the Overview tab of [DataFest@Integration2022](https://www.kaggle.com/competitions/datafestintegration2023/overview) for important links and information regarding A calls.\n\nAlso keep an eye on the Overview Tab for further Hints and Helper codes. For any further queries, feel free to reach us. All the Best!","metadata":{}},{"cell_type":"code","source":"# Notebook Credit: Sharad from the DataFest Team","metadata":{"execution":{"iopub.status.busy":"2023-01-18T04:51:05.628809Z","iopub.execute_input":"2023-01-18T04:51:05.629234Z","iopub.status.idle":"2023-01-18T04:51:05.634712Z","shell.execute_reply.started":"2023-01-18T04:51:05.629199Z","shell.execute_reply":"2023-01-18T04:51:05.633706Z"},"trusted":true},"execution_count":null,"outputs":[]}]}