{"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":"## Import Libraraies","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport torch\nimport torchaudio\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport plotly.express as px\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\nimport sklearn\nimport warnings\nimport seaborn as sns\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:23.718058Z","iopub.execute_input":"2023-10-28T16:09:23.718884Z","iopub.status.idle":"2023-10-28T16:09:23.728575Z","shell.execute_reply.started":"2023-10-28T16:09:23.718840Z","shell.execute_reply":"2023-10-28T16:09:23.727543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Take a look at data","metadata":{}},{"cell_type":"code","source":"train_csv=pd.read_csv('../input/birdclef-2021/train_metadata.csv')\ntrain_csv.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:23.730168Z","iopub.execute_input":"2023-10-28T16:09:23.730469Z","iopub.status.idle":"2023-10-28T16:09:24.139193Z","shell.execute_reply.started":"2023-10-28T16:09:23.730443Z","shell.execute_reply":"2023-10-28T16:09:24.137912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '../input/birdclef-2021/train_short_audio'\ntrain_csv['full_path'] = base_dir+ '/' + train_csv['primary_label'] + '/' + train_csv['filename']\nastfly = train_csv[train_csv['primary_label'] == \"astfly\"].sample(1, random_state = 33)['full_path'].values[0]\ncasvir = train_csv[train_csv['primary_label'] == 'casvir'].sample(1, random_state = 33)['full_path'].values[0]\nsubfly = train_csv[train_csv['primary_label'] == \"subfly\"].sample(1, random_state = 33)['full_path'].values[0]\nwilfly = train_csv[train_csv['primary_label'] == 'wilfly'].sample(1, random_state = 33)['full_path'].values[0]\nverdin = train_csv[train_csv['primary_label'] == 'verdin'].sample(1, random_state = 33)['full_path'].values[0]\nsolsan = train_csv[train_csv['primary_label'] == 'solsan'].sample(1, random_state = 33)['full_path'].values[0]\nbirds= [\"astfly\", \"casvir\", \"subfly\", \"wilfly\", \"verdin\",'solsan']","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:24.141434Z","iopub.execute_input":"2023-10-28T16:09:24.141780Z","iopub.status.idle":"2023-10-28T16:09:24.265948Z","shell.execute_reply.started":"2023-10-28T16:09:24.141750Z","shell.execute_reply":"2023-10-28T16:09:24.264908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Play same audios","metadata":{}},{"cell_type":"code","source":"ipd.Audio(astfly)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:24.267482Z","iopub.execute_input":"2023-10-28T16:09:24.267831Z","iopub.status.idle":"2023-10-28T16:09:24.279314Z","shell.execute_reply.started":"2023-10-28T16:09:24.267788Z","shell.execute_reply":"2023-10-28T16:09:24.278187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(subfly)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:24.282558Z","iopub.execute_input":"2023-10-28T16:09:24.282930Z","iopub.status.idle":"2023-10-28T16:09:24.316149Z","shell.execute_reply.started":"2023-10-28T16:09:24.282901Z","shell.execute_reply":"2023-10-28T16:09:24.314644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(solsan)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:24.317641Z","iopub.execute_input":"2023-10-28T16:09:24.318515Z","iopub.status.idle":"2023-10-28T16:09:24.368135Z","shell.execute_reply.started":"2023-10-28T16:09:24.318482Z","shell.execute_reply":"2023-10-28T16:09:24.366248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(casvir)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:31:34.144225Z","iopub.execute_input":"2023-10-28T16:31:34.144629Z","iopub.status.idle":"2023-10-28T16:31:34.204246Z","shell.execute_reply.started":"2023-10-28T16:31:34.144595Z","shell.execute_reply":"2023-10-28T16:31:34.203128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading and Visualizing an audio file","metadata":{}},{"cell_type":"code","source":"y, sr = librosa.load(subfly)\nprint('y:', y, '\\n')\nprint('y shape:', np.shape(y), '\\n')\nprint('Sample Rate (KHz):', sr, '\\n')\nprint('Check Len of Audio:', np.shape(y)[0]/sr)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:24.369774Z","iopub.execute_input":"2023-10-28T16:09:24.370207Z","iopub.status.idle":"2023-10-28T16:09:24.588137Z","shell.execute_reply.started":"2023-10-28T16:09:24.370170Z","shell.execute_reply":"2023-10-28T16:09:24.587000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## TRIMMING THE LEADING AND TRAILING SILENCE","metadata":{}},{"cell_type":"code","source":"audio_file, _ = librosa.effects.trim(y)\nprint('Audio File:', audio_file, '\\n')\nprint('Audio File shape:', np.shape(audio_file))","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:24.589631Z","iopub.execute_input":"2023-10-28T16:09:24.589982Z","iopub.status.idle":"2023-10-28T16:09:24.776623Z","shell.execute_reply.started":"2023-10-28T16:09:24.589952Z","shell.execute_reply":"2023-10-28T16:09:24.775387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_astfly, sr_astfly = librosa.load(astfly)\naudio_astfly, _ = librosa.effects.trim(y_astfly)\n\ny_casvir, sr_casvir = librosa.load(casvir)\naudio_casvir, _ = librosa.effects.trim(y_casvir)\n\ny_subfly, sr_subfly = librosa.load(subfly)\naudio_subfly, _ = librosa.effects.trim(y_subfly)\n\ny_wilfly, sr_wilfly = librosa.load(wilfly)\naudio_wilfly, _ = librosa.effects.trim(y_wilfly)\n\ny_verdin, sr_verdin = librosa.load(verdin)\naudio_verdin, _ = librosa.effects.trim(y_verdin)\n\ny_solsan, sr_solsan = librosa.load(solsan)\naudio_solsan, _ = librosa.effects.trim(y_solsan)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:24.777953Z","iopub.execute_input":"2023-10-28T16:09:24.778261Z","iopub.status.idle":"2023-10-28T16:09:27.148147Z","shell.execute_reply.started":"2023-10-28T16:09:24.778234Z","shell.execute_reply":"2023-10-28T16:09:27.146874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(2,1, figsize= (16,10))\n\nlibrosa.display.waveshow(audio_solsan, sr=sr_solsan, ax = axes[0])\naxes[0].set_xlabel('Time (s)')\naxes[0].set_ylabel('Amplitude')\naxes[0].set_title('Trimmed Audio Waveform')\n\n\nlibrosa.display.waveshow(y_solsan, sr=sr_solsan, ax = axes[1])\naxes[1].set_xlabel('Time (s)')\naxes[1].set_ylabel('Amplitude')\naxes[1].set_title('Non Trimmed Audio Waveform')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:27.149453Z","iopub.execute_input":"2023-10-28T16:09:27.149783Z","iopub.status.idle":"2023-10-28T16:09:28.365296Z","shell.execute_reply.started":"2023-10-28T16:09:27.149754Z","shell.execute_reply":"2023-10-28T16:09:28.364065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1> 1. Time Domain Features</h1>","metadata":{}},{"cell_type":"markdown","source":"## Waveform Visualization","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(6, figsize = (16, 12))\nfig.suptitle('Sound Waves', fontsize=16)\n\nlibrosa.display.waveshow(y = audio_astfly, sr = sr_astfly, color = \"#A300F9\", ax=ax[0])\nlibrosa.display.waveshow(y = audio_casvir, sr = sr_casvir, color = \"#4300FF\", ax=ax[1])\nlibrosa.display.waveshow(y = audio_subfly, sr = sr_subfly, color = \"#009DFF\", ax=ax[2])\nlibrosa.display.waveshow(y = audio_wilfly, sr = sr_wilfly, color = \"#00FFB0\", ax=ax[3])\nlibrosa.display.waveshow(y = audio_verdin, sr = sr_verdin, color = \"#D9FF00\", ax=ax[4])\nlibrosa.display.waveshow(y = audio_solsan, sr = sr_solsan, color = \"r\", ax=ax[5]);\n\nfor i, name in zip(range(6), birds):\n    ax[i].set_ylabel(name, fontsize=13)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:28.369037Z","iopub.execute_input":"2023-10-28T16:09:28.369593Z","iopub.status.idle":"2023-10-28T16:09:33.643789Z","shell.execute_reply.started":"2023-10-28T16:09:28.369551Z","shell.execute_reply":"2023-10-28T16:09:33.642854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Spectrogram Visualization","metadata":{}},{"cell_type":"code","source":"n_fft=2048\nhop_length=512\n\n# Short-time Fourier transform (STFT)\nD_astfly = np.abs(librosa.stft(audio_astfly, n_fft = n_fft, hop_length = hop_length))\n# Convert an amplitude spectrogram to Decibels-scaled spectrogram.\nDB_astfly = librosa.amplitude_to_db(D_astfly, ref = np.max)\n\n# === PLOT ===\nfig, ax = plt.subplots(1, 1, figsize=(10, 8))\nfig.suptitle('Log Frequency Spectrogram', fontsize=16)\n# fig.delaxes(ax[1, 2])\nimg=librosa.display.specshow(DB_astfly, sr = sr_astfly, hop_length = hop_length, x_axis = 'time', \n                         y_axis = 'log', cmap = 'cool', ax=ax)\nax.set_title('ASTFLY', fontsize=13) \nplt.colorbar(img,ax=ax)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:33.645135Z","iopub.execute_input":"2023-10-28T16:09:33.645432Z","iopub.status.idle":"2023-10-28T16:09:34.501265Z","shell.execute_reply.started":"2023-10-28T16:09:33.645406Z","shell.execute_reply":"2023-10-28T16:09:34.500052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# RMSE","metadata":{}},{"cell_type":"code","source":"S, phase = librosa.magphase(librosa.stft(audio_astfly))\nS_db=librosa.amplitude_to_db(S, ref=np.max)\nrms = librosa.feature.rms(S=S)\nfig, ax = plt.subplots(nrows=2, sharex=True,figsize = (16, 6))\ntimes = librosa.times_like(rms)\nax[0].semilogy(times, rms[0], label='RMS Energy')\nax[0].set(xticks=[])\nax[0].legend()\nax[0].label_outer()\nlibrosa.display.specshow(S_db,\n                         y_axis='log', x_axis='time', ax=ax[1])\nax[1].set(title='log Power spectrogram')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:09:34.502938Z","iopub.execute_input":"2023-10-28T16:09:34.503441Z","iopub.status.idle":"2023-10-28T16:09:35.678533Z","shell.execute_reply.started":"2023-10-28T16:09:34.503403Z","shell.execute_reply":"2023-10-28T16:09:35.677430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mel Spectrogram","metadata":{}},{"cell_type":"code","source":"# Create the Mel Spectrograms\nS_astfly = librosa.feature.melspectrogram(y = audio_astfly, sr=sr_astfly)\nS_DB_astfly = librosa.amplitude_to_db(S_astfly, ref=np.max)\n\n# === PLOT ====\nfig, ax = plt.subplots(1, 1, figsize=(10, 6))\nfig.suptitle('Mel Spectrogram', fontsize=16)\n\n# Display the Mel spectrogram\nimg = librosa.display.specshow(S_DB_astfly, sr=sr_astfly, hop_length=hop_length, x_axis='time', \n                               y_axis='log', cmap='cool', ax=ax)\n\nax.set_title('ASTFLY', fontsize=13)\nplt.colorbar(img, ax=ax)\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:13:33.983155Z","iopub.execute_input":"2023-10-28T16:13:33.983556Z","iopub.status.idle":"2023-10-28T16:13:34.621013Z","shell.execute_reply.started":"2023-10-28T16:13:33.983525Z","shell.execute_reply":"2023-10-28T16:13:34.619812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Zero Crossing Rate (ZCR)\n\nThe ZCR of an audio signal is defined as the rate at which the signal changes sign. ZCR is an efficient and simple way to detecting whether a speech frame is voice, unvoiced, or silent. It is expected that unvoiced segments produce higher ZCRs than for voice segments, and ideally ZCRs equal to zero for silence segments","metadata":{}},{"cell_type":"code","source":"# Total zero_crossings in our 1 song\nzero_astfly = librosa.zero_crossings(audio_astfly, pad=False)\nzero_casvir = librosa.zero_crossings(audio_casvir, pad=False)\nzero_wilfly = librosa.zero_crossings(audio_wilfly, pad=False)\nzero_subfly = librosa.zero_crossings(audio_subfly, pad=False)\nzero_verdin = librosa.zero_crossings(audio_verdin, pad=False)\nzero_solsan = librosa.zero_crossings(audio_solsan, pad=False)\nzero_birds_list = [zero_astfly, zero_casvir, zero_wilfly, zero_subfly, zero_verdin,zero_solsan]\n\nfor bird, name in zip(zero_birds_list, birds):\n    print(\"{} change rate is {:,}\".format(name, sum(bird)))","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:15:23.116019Z","iopub.execute_input":"2023-10-28T16:15:23.116486Z","iopub.status.idle":"2023-10-28T16:15:26.729132Z","shell.execute_reply.started":"2023-10-28T16:15:23.116450Z","shell.execute_reply":"2023-10-28T16:15:26.728336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Separation of Harmonic & Percussive Signals","metadata":{}},{"cell_type":"code","source":"y_harm_casvir, y_perc_casvir = librosa.effects.hpss(audio_casvir)\nD_casvir = np.abs(librosa.stft(audio_casvir, n_fft = n_fft, hop_length = hop_length))\nDB_casvir = librosa.amplitude_to_db(D_casvir, ref = np.max)\nplt.figure(figsize = (16, 6))\nplt.plot(y_perc_casvir, color = '#FFB100')\nplt.plot(y_harm_casvir, color = '#A300F9')\nplt.legend((\"Perceptrual\", \"Harmonics\"))\nplt.title(\"Harmonics + Percussive : Casvir Bird\", fontsize=16);\n\n\nH, P = librosa.decompose.hpss(librosa.stft(audio_casvir))    \nplt.figure(figsize=(16, 6))\nplt.subplot(3, 1, 1)\nlibrosa.display.specshow(DB_casvir, y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Full power spectrogram: Harmonic + Percussive')\n\n# harmonic spectrogram will show more horizontal/pitch-dependent changes\nplt.subplot(3, 1, 2)\nlibrosa.display.specshow(librosa.amplitude_to_db(np.abs(H), ref=np.max), y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Harmonic power spectrogram')\nplt.subplot(3, 1, 3)\n\n# percussive spectrogram will show more vertical/time-dependent changes\nlibrosa.display.specshow(librosa.amplitude_to_db(np.abs(P), ref=np.max), y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Percussive power spectrogram')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:30:28.415900Z","iopub.execute_input":"2023-10-28T16:30:28.416312Z","iopub.status.idle":"2023-10-28T16:31:24.326638Z","shell.execute_reply.started":"2023-10-28T16:30:28.416282Z","shell.execute_reply":"2023-10-28T16:31:24.321968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Beat Extraction","metadata":{}},{"cell_type":"code","source":"tempo, beat_frames = librosa.beat.beat_track(y=y_harm_casvir, sr=sr_casvir)\nprint('Detected Tempo: '+ str(tempo) + ' beats/min')\nbeat_times = librosa.frames_to_time(beat_frames, sr=sr)\nbeat_time_diff = np.ediff1d(beat_times)\nbeat_nums = np.arange(1, np.size(beat_times))\nfig, ax = plt.subplots()\nfig.set_size_inches(20, 5)\nax.set_ylabel(\"Time difference (s)\")\nax.set_xlabel(\"Beats\")\ng = sns.barplot(x= beat_nums,y = beat_time_diff, palette=\"rocket\",ax=ax)\ng = g.set(xticklabels=[])","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:38:59.539520Z","iopub.execute_input":"2023-10-28T16:38:59.539956Z","iopub.status.idle":"2023-10-28T16:39:05.818702Z","shell.execute_reply.started":"2023-10-28T16:38:59.539923Z","shell.execute_reply":"2023-10-28T16:39:05.817464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create Tempo BPM variable\ntempo_astfly, _ = librosa.beat.beat_track(y = y_astfly, sr = sr_astfly)\ntempo_casvir, _ = librosa.beat.beat_track(y= y_casvir, sr = sr_casvir)\ntempo_wilfly, _ = librosa.beat.beat_track(y= y_wilfly, sr = sr_wilfly)\ntempo_subfly, _ = librosa.beat.beat_track(y= y_subfly, sr = sr_subfly)\ntempo_verdin, _ = librosa.beat.beat_track(y= y_verdin, sr = sr_verdin)\ntempo_solsan, _ = librosa.beat.beat_track(y=y_solsan, sr = sr_solsan)\ndata = pd.DataFrame({\"Type\": birds , \n                     \"BPM\": [tempo_astfly, tempo_casvir, tempo_wilfly, tempo_subfly, tempo_verdin,tempo_solsan] })\n\n\n# Plot\nplt.figure(figsize = (16, 6))\nax = sns.barplot(y = data[\"BPM\"], x = data[\"Type\"], palette=\"rocket\")\nplt.ylabel(\"BPM\", fontsize=14)\nplt.yticks(fontsize=13)\nplt.xticks(fontsize=13)\nplt.xlabel(\"\")\nplt.title(\"BPM for 6 Different Bird Species\", fontsize=16);","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:39:51.387063Z","iopub.execute_input":"2023-10-28T16:39:51.387455Z","iopub.status.idle":"2023-10-28T16:39:57.909273Z","shell.execute_reply.started":"2023-10-28T16:39:51.387425Z","shell.execute_reply":"2023-10-28T16:39:57.907935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1> 2. Frequency Domain Features </h1>","metadata":{}},{"cell_type":"markdown","source":"# Chromagram\n\nA chromagram is a representation of the energy distribution of different musical pitches (notes) in an audio signal. It provides information about the harmonic content of a piece of music by mapping the energy of different pitch classes over time.\n\n* Chromagrams are often visualized as heatmaps, with time on the x-axis, pitch classes on the y-axis, and color intensity representing the strength of each pitch class.","metadata":{}},{"cell_type":"code","source":"chroma=librosa.feature.chroma_stft(y=audio_casvir, sr=sr_casvir)\nfig, ax = plt.subplots(1,figsize = (10, 5))\nimg = librosa.display.specshow(chroma, y_axis='chroma', x_axis='time', ax=ax)\nfig.colorbar(img, ax=ax)\nax.set(title='Chromagram')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:46:49.990309Z","iopub.execute_input":"2023-10-28T16:46:49.990880Z","iopub.status.idle":"2023-10-28T16:46:52.054498Z","shell.execute_reply.started":"2023-10-28T16:46:49.990836Z","shell.execute_reply":"2023-10-28T16:46:52.053324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#using an energy(magnitude) spectrum\nS = np.abs(librosa.stft(audio_casvir))\nchroma = librosa.feature.chroma_stft(S=S, sr=sr_casvir)#applying the logarithmic fourier transform\nfig, ax = plt.subplots(1,figsize = (10, 5))\nimg = librosa.display.specshow(chroma, y_axis='chroma', x_axis='time', ax=ax)\nfig.colorbar(img, ax=ax)\nax.set(title='Chromagram')","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:50:05.604714Z","iopub.execute_input":"2023-10-28T16:50:05.605188Z","iopub.status.idle":"2023-10-28T16:50:07.700262Z","shell.execute_reply.started":"2023-10-28T16:50:05.605156Z","shell.execute_reply":"2023-10-28T16:50:07.698905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Constant Q-transform (CQT)\n\nThe constant-Q transform transforms a data series to the frequency domain. It is related to the Fourier transform.\nIn general, the transform is well suited to musical data and proves useful where frequencies span several octaves.It is more useful in the identification of instruments","metadata":{}},{"cell_type":"code","source":"chroma_stft = librosa.feature.chroma_stft(y=audio_casvir, sr=sr_casvir)\nchroma_cq = librosa.feature.chroma_cqt(y=audio_casvir, sr=sr_casvir)\nfig, ax = plt.subplots(nrows=2, sharex=True, sharey=True,figsize = (10, 9))\nlibrosa.display.specshow(chroma_stft, y_axis='chroma', x_axis='time', ax=ax[0])\nax[0].set(title='chroma_stft')\nax[0].label_outer()\nimg = librosa.display.specshow(chroma_cq, y_axis='chroma', x_axis='time', ax=ax[1])\nax[1].set(title='chroma_cqt')\nfig.colorbar(img, ax=ax)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:59:13.353336Z","iopub.execute_input":"2023-10-28T16:59:13.353754Z","iopub.status.idle":"2023-10-28T16:59:18.615738Z","shell.execute_reply.started":"2023-10-28T16:59:13.353722Z","shell.execute_reply":"2023-10-28T16:59:18.614516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Chroma Energy distribution Normalized Statistics (CENS)\n\nAnother chroma-based feature is chroma energy distribution normalized statistics (CENS) which is typically used to identify similarity between different interpretations of the music given.CENS are typically implemented for audio matching and similarity tasks.","metadata":{}},{"cell_type":"code","source":"chroma_stft = librosa.feature.chroma_stft(y=audio_casvir, sr=sr_casvir)\nchroma_cens = librosa.feature.chroma_cens(y=audio_casvir, sr=sr_casvir)\n\nfig, ax = plt.subplots(nrows=2, sharex=True, sharey=True,figsize = (10, 9))\nlibrosa.display.specshow(chroma_stft, y_axis='chroma', x_axis='time', ax=ax[0])\nax[0].set(title='chroma_stft')\nax[0].label_outer()\n\nimg = librosa.display.specshow(chroma_cens, y_axis='chroma', x_axis='time', ax=ax[1])\nax[1].set(title='chroma_cens')\nfig.colorbar(img, ax=ax)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T17:04:43.827883Z","iopub.execute_input":"2023-10-28T17:04:43.828699Z","iopub.status.idle":"2023-10-28T17:04:49.087417Z","shell.execute_reply.started":"2023-10-28T17:04:43.828655Z","shell.execute_reply":"2023-10-28T17:04:49.086217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1> 3. Spectrum Related Features </h1>","metadata":{}},{"cell_type":"markdown","source":"# SPECTRAL CENTROIDS \n\nThe spectral centroid is a measure to characterize the \"center of mass\" of a given spectrum.The spectral centroid is calculated as the weighted means of the frequencies present in a given signal, determined using a Fourier transform, with the frequency magnitudes as the weights.","metadata":{}},{"cell_type":"code","source":"# Calculate the Spectral Centroids\nspectral_centroids = librosa.feature.spectral_centroid(y = audio_casvir, sr=sr_casvir)[0]\n\n# Shape is a vector\nprint('Centroids:', spectral_centroids, '\\n')\nprint('Shape of Spectral Centroids:', spectral_centroids.shape, '\\n')\n\n# Computing the time variable for visualization\nframes = range(len(spectral_centroids))\n\n# Converts frame counts to time (seconds)\nt = librosa.frames_to_time(frames)\n\nprint('frames:', frames, '\\n')\nprint('t:', t)\n\n# Function that normalizes the Sound Data\ndef normalize(x, axis=0):\n    return sklearn.preprocessing.minmax_scale(x, axis=axis)\n#Plotting the Spectral Centroid along the waveform\nplt.figure(figsize = (16, 6))\nlibrosa.display.waveshow(audio_casvir, sr=sr_casvir, alpha=0.4, color = '#A300F9', lw=3)\nplt.plot(t, normalize(spectral_centroids), color='#FFB100', lw=2)\nplt.legend([\"Spectral Centroid\", \"Wave\"])\nplt.title(\"Spectral Centroid: Casvir Bird\", fontsize=16);","metadata":{"execution":{"iopub.status.busy":"2023-10-28T17:11:07.543365Z","iopub.execute_input":"2023-10-28T17:11:07.543770Z","iopub.status.idle":"2023-10-28T17:11:08.894873Z","shell.execute_reply.started":"2023-10-28T17:11:07.543737Z","shell.execute_reply":"2023-10-28T17:11:08.893864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SPECTRAL CONTRAST\n\nThe difference between spectral peaks and spectral valleys will reflect the spectral contrast distribution.\nSpectral peaks correspond to harmonic components and Spectral valleys correspond to non-harmonic components or noise in a music piece.It considers the spectral peak and valley in each sub-band separately.","metadata":{}},{"cell_type":"code","source":"contrast = librosa.feature.spectral_contrast(y=y_harm_casvir,sr=sr_casvir)\nplt.figure(figsize=(15,5))\nlibrosa.display.specshow(contrast, x_axis='time')\nplt.colorbar()\nplt.ylabel('Frequency bands')\nplt.title('Spectral contrast')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T17:16:35.271555Z","iopub.execute_input":"2023-10-28T17:16:35.271989Z","iopub.status.idle":"2023-10-28T17:16:36.530590Z","shell.execute_reply.started":"2023-10-28T17:16:35.271956Z","shell.execute_reply":"2023-10-28T17:16:36.529181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SPECTRAL ROLLOFF\n\nSpectral rolloff point is defined as the Nth percentile frequency of the power spectral distribution, where is usually 85% or 95%. The rolloff point is the frequency below which the N% of the magnitude distribution is concentrated. In other words,the rolloff frequency is defined as the frequency under which the cutoff of the total energy of the spectrum is contained, eg. 85%. It can be used to distinguish between harmonic and noisy sounds","metadata":{}},{"cell_type":"code","source":"# Spectral RollOff Vector\nspectral_rolloff = librosa.feature.spectral_rolloff(y=audio_astfly, sr=sr_astfly)[0]\n\n# Computing the time variable for visualization\nframes = range(len(spectral_rolloff))\n# Converts frame counts to time (seconds)\nt = librosa.frames_to_time(frames)\n\n# The plot\nplt.figure(figsize = (16, 6))\nlibrosa.display.waveshow(audio_astfly, sr=sr_astfly, alpha=0.4, color = '#A300F9', lw=3)\nplt.plot(t, normalize(spectral_rolloff), color='#FFB100', lw=3)\nplt.legend([\"Spectral Rolloff\", \"Wave\"])\nplt.title(\"Spectral Rolloff: Astfly Bird\", fontsize=16);","metadata":{"execution":{"iopub.status.busy":"2023-10-28T17:22:59.661425Z","iopub.execute_input":"2023-10-28T17:22:59.661837Z","iopub.status.idle":"2023-10-28T17:23:00.434775Z","shell.execute_reply.started":"2023-10-28T17:22:59.661784Z","shell.execute_reply":"2023-10-28T17:23:00.433956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mel-frequency cepstral coefficients (MFCCs)","metadata":{}},{"cell_type":"code","source":"mfcc=librosa.feature.mfcc(y=audio_astfly, sr=sr_astfly)\nfig, ax = plt.subplots(1,figsize = (12, 6))\nimg = librosa.display.specshow(mfcc, x_axis='time', ax=ax)\nprint(mfcc.shape)\nfig.colorbar(img, ax=ax)\nax.set(title='MFCC')","metadata":{"execution":{"iopub.status.busy":"2023-10-28T17:23:37.401988Z","iopub.execute_input":"2023-10-28T17:23:37.402500Z","iopub.status.idle":"2023-10-28T17:23:37.898993Z","shell.execute_reply.started":"2023-10-28T17:23:37.402458Z","shell.execute_reply":"2023-10-28T17:23:37.897958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}