{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Simple Audio Preprocessing & Augmentations\n\n1) Preprocessing\n- Waveform\n- FFT Power Spectrum\n- Power Spectrogram\n- SFTF Spectrogram\n- Log Spectrogram\n- MFCCs\n\n2) Augmentations\n- Add Noise\n- Random Shifting\n- Pitch & Speed\n- Pitch\n- Speed\n- Amplify value\n- Hpss\n- Stretching","metadata":{}},{"cell_type":"code","source":"import pandas as pd \nimport librosa\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ndf = pd.read_csv('/kaggle/input/birdclef-2024/train_metadata.csv')\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:15:56.100276Z","iopub.execute_input":"2024-04-04T10:15:56.101065Z","iopub.status.idle":"2024-04-04T10:15:56.238980Z","shell.execute_reply.started":"2024-04-04T10:15:56.101019Z","shell.execute_reply":"2024-04-04T10:15:56.237869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## df","metadata":{}},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:15:56.241012Z","iopub.execute_input":"2024-04-04T10:15:56.241374Z","iopub.status.idle":"2024-04-04T10:15:56.265970Z","shell.execute_reply.started":"2024-04-04T10:15:56.241343Z","shell.execute_reply":"2024-04-04T10:15:56.264832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Single Audio oog file : 11470","metadata":{}},{"cell_type":"code","source":"ex = df.loc[11470]\npath = '/kaggle/input/birdclef-2024/train_audio/'+ex.filename\nex","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:15:56.267392Z","iopub.execute_input":"2024-04-04T10:15:56.267782Z","iopub.status.idle":"2024-04-04T10:15:56.280665Z","shell.execute_reply.started":"2024-04-04T10:15:56.267752Z","shell.execute_reply":"2024-04-04T10:15:56.279456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Audio\nlibrosa.get_samplerate(path)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:15:56.282029Z","iopub.execute_input":"2024-04-04T10:15:56.282407Z","iopub.status.idle":"2024-04-04T10:15:56.299250Z","shell.execute_reply.started":"2024-04-04T10:15:56.282378Z","shell.execute_reply":"2024-04-04T10:15:56.297912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sampling_rate = 32000\nAudio(path, rate=sampling_rate)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:15:56.302238Z","iopub.execute_input":"2024-04-04T10:15:56.302635Z","iopub.status.idle":"2024-04-04T10:15:56.311930Z","shell.execute_reply.started":"2024-04-04T10:15:56.302584Z","shell.execute_reply":"2024-04-04T10:15:56.310679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Signal & Sampling rate","metadata":{}},{"cell_type":"code","source":"sig, sr = librosa.load(path, sr=sampling_rate, duration=15)\nsig, sr, len(sig) / sampling_rate","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:15:56.313665Z","iopub.execute_input":"2024-04-04T10:15:56.314092Z","iopub.status.idle":"2024-04-04T10:15:56.334034Z","shell.execute_reply.started":"2024-04-04T10:15:56.314058Z","shell.execute_reply":"2024-04-04T10:15:56.333149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Various Plots","metadata":{}},{"cell_type":"code","source":"fig, (ax0, ax1) = plt.subplots(2, 1, figsize=(12, 6))\nax0.plot(sig)\nax0.set_title(\"plot\")\nlibrosa.display.waveshow(sig, sr=sr, ax=ax1, color=\"blue\")\nax1.set_title(\"librosa.display.waveshow\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:15:56.335243Z","iopub.execute_input":"2024-04-04T10:15:56.336274Z","iopub.status.idle":"2024-04-04T10:15:57.258188Z","shell.execute_reply.started":"2024-04-04T10:15:56.336238Z","shell.execute_reply":"2024-04-04T10:15:57.257045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Power Spectrum","metadata":{}},{"cell_type":"code","source":"# Fourier -> Spectrum\n\nfft = np.fft.fft(sig)\n\nmagnitude = np.abs(fft) \n\nf = np.linspace(0,sr,len(magnitude))\n\nleft_spectrum = magnitude[:int(len(magnitude) / 2)]\nleft_f = f[:int(len(magnitude) / 2)]\n\nplt.figure()\nplt.plot(left_f, left_spectrum)\nplt.xlabel(\"Frequency\")\nplt.ylabel(\"Magnitude\")\nplt.title(\"Power spectrum\")","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:15:57.259666Z","iopub.execute_input":"2024-04-04T10:15:57.260817Z","iopub.status.idle":"2024-04-04T10:15:57.686854Z","shell.execute_reply.started":"2024-04-04T10:15:57.260783Z","shell.execute_reply":"2024-04-04T10:15:57.685476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Power Spectrogram","metadata":{}},{"cell_type":"code","source":"S = np.abs(librosa.stft(sig))\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(S,\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:15:57.688190Z","iopub.execute_input":"2024-04-04T10:15:57.688641Z","iopub.status.idle":"2024-04-04T10:15:58.715146Z","shell.execute_reply.started":"2024-04-04T10:15:57.688584Z","shell.execute_reply":"2024-04-04T10:15:58.714009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# STFT Spectrogram","metadata":{}},{"cell_type":"code","source":"# STFT -> spectrogram\n\nhop_length = 256\nn_fft = 1024\n\nhop_length_duration = float(hop_length) / sr\nn_fft_duration = float(n_fft) / sr\n\nstft = librosa.stft(sig, n_fft=n_fft, hop_length=hop_length)\n\nspectrogram = np.abs(stft)\n\nplt.figure()\nlibrosa.display.specshow(spectrogram, sr=sr, hop_length=hop_length)\nplt.xlabel(\"Time\")\nplt.ylabel(\"Frequency\")\nplt.colorbar(format=\"%+2.0f dB\")\nplt.title(\"Spectrogram (dB)\")","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:15:58.716699Z","iopub.execute_input":"2024-04-04T10:15:58.717701Z","iopub.status.idle":"2024-04-04T10:15:59.453536Z","shell.execute_reply.started":"2024-04-04T10:15:58.717654Z","shell.execute_reply":"2024-04-04T10:15:59.452191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Log Spectrogram","metadata":{}},{"cell_type":"markdown","source":"too dark... let's do it differently\nWhat is the conceptual purpose of **librosa.amplitude_to_db?**\nhttps://stackoverflow.com/questions/63347977/what-is-the-conceptual-purpose-of-librosa-amplitude-to-db\n- By converting to decibels (dB) the scale becomes logarithmic. This limits the numerical range, to something like 0-120 dB instead. \n- The intensity of colors when this is plotted corresponds **more closely to what we hear** than if one used a linear scale","metadata":{}},{"cell_type":"code","source":"# STFT -> log spectrogram\n\nhop_length = 256\nn_fft = 1024\n\nhop_length_duration = float(hop_length) / sr\nn_fft_duration = float(n_fft) / sr\n\nstft = librosa.stft(sig, n_fft=n_fft, hop_length=hop_length)\n\nmagnitude = np.abs(stft)\n\nlog_spectrogram = librosa.amplitude_to_db(magnitude)\n\nplt.figure()\nlibrosa.display.specshow(log_spectrogram, sr=sr, hop_length=hop_length)\nplt.xlabel(\"Time\")\nplt.ylabel(\"Frequency\")\nplt.colorbar(format=\"%+2.0f dB\")\nplt.title(\"Log Spectrogram (dB)\")","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:15:59.455430Z","iopub.execute_input":"2024-04-04T10:15:59.455938Z","iopub.status.idle":"2024-04-04T10:16:00.263767Z","shell.execute_reply.started":"2024-04-04T10:15:59.455895Z","shell.execute_reply":"2024-04-04T10:16:00.262774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = librosa.stft(sig.astype('float'),n_fft=n_fft, hop_length=hop_length)\nXdb = librosa.amplitude_to_db(X)\nplt.figure(figsize=(12, 5))\nlibrosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:00.265159Z","iopub.execute_input":"2024-04-04T10:16:00.265801Z","iopub.status.idle":"2024-04-04T10:16:00.973982Z","shell.execute_reply.started":"2024-04-04T10:16:00.265765Z","shell.execute_reply":"2024-04-04T10:16:00.972720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MFCCs","metadata":{}},{"cell_type":"code","source":"MFCCs = librosa.feature.mfcc(y=sig, sr=sr, n_fft=n_fft, hop_length=hop_length, n_mfcc=13)\nplt.figure()\nlibrosa.display.specshow(MFCCs, sr=sr, hop_length=hop_length)\nplt.xlabel('Time')\nplt.ylabel('MFCC Coefficients')\nplt.colorbar()\nplt.title('MFCCs')","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:00.975643Z","iopub.execute_input":"2024-04-04T10:16:00.976062Z","iopub.status.idle":"2024-04-04T10:16:01.384265Z","shell.execute_reply.started":"2024-04-04T10:16:00.976018Z","shell.execute_reply":"2024-04-04T10:16:01.382908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Augmentation","metadata":{}},{"cell_type":"code","source":"def noising(data,noise_factor):\n    \"\"\"\n    Add noise to original data.\n    Adjust noise factor how much you want to add.\n    \"\"\"\n    noise = np.random.randn(len(data))\n    augmented_data = data + noise_factor * noise\n    # Cast back to same data type\n    augmented_data = augmented_data.astype(type(data[0]))\n    return augmented_data\ndef shifting(data, sampling_rate, shift_max, shift_direction):\n    shift = np.random.randint(sampling_rate * shift_max+1)\n    if shift_direction == 'right':\n        shift = -shift\n    elif shift_direction == 'both':\n        direction = np.random.randint(0, 2)\n        if direction == 1:\n            shift = -shift\n    augmented_data = np.roll(data, shift)\n    # Set to silence for heading/ tailing\n    if shift > 0:\n        augmented_data[:shift] = 0\n    else:\n        augmented_data[shift:] = 0\n    return augmented_data\n\ndef change_pitch(data, sampling_rate, pitch_factor):\n    return librosa.effects.pitch_shift(y=data, sr=sampling_rate, n_steps=pitch_factor)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:01.388993Z","iopub.execute_input":"2024-04-04T10:16:01.389362Z","iopub.status.idle":"2024-04-04T10:16:01.399753Z","shell.execute_reply.started":"2024-04-04T10:16:01.389333Z","shell.execute_reply":"2024-04-04T10:16:01.398276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Original MelSpectorgram\n- again **log scale** using power_to_db","metadata":{}},{"cell_type":"code","source":"mfcc_data = librosa.feature.melspectrogram(y=sig,sr=sr,n_fft=512,win_length=400,hop_length=160,n_mels=80)\nmfcc_data = librosa.power_to_db(mfcc_data, ref=np.max)\nprint(mfcc_data.shape)\nplt.pcolor(mfcc_data)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:01.401324Z","iopub.execute_input":"2024-04-04T10:16:01.401771Z","iopub.status.idle":"2024-04-04T10:16:03.720717Z","shell.execute_reply.started":"2024-04-04T10:16:01.401732Z","shell.execute_reply":"2024-04-04T10:16:03.719449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Add Noise","metadata":{}},{"cell_type":"code","source":"\nnoising_data = noising(sig,np.random.uniform(0,0.1))\nAudio(data=noising_data, rate=sr)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:03.722590Z","iopub.execute_input":"2024-04-04T10:16:03.722960Z","iopub.status.idle":"2024-04-04T10:16:03.750582Z","shell.execute_reply.started":"2024-04-04T10:16:03.722931Z","shell.execute_reply":"2024-04-04T10:16:03.749498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc_data = librosa.feature.melspectrogram(y=noising_data,sr=sr,n_fft=512,win_length=400,hop_length=160,n_mels=80)\nmfcc_data = librosa.power_to_db(mfcc_data, ref=np.max)\nprint(mfcc_data.shape)\nplt.pcolor(mfcc_data)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:03.751795Z","iopub.execute_input":"2024-04-04T10:16:03.752877Z","iopub.status.idle":"2024-04-04T10:16:05.951496Z","shell.execute_reply.started":"2024-04-04T10:16:03.752844Z","shell.execute_reply":"2024-04-04T10:16:05.950319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Shifting","metadata":{}},{"cell_type":"code","source":"shifting_data = shifting(sig,sr,0.8,'right')\nAudio(data=shifting_data, rate=sr)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:05.953091Z","iopub.execute_input":"2024-04-04T10:16:05.953581Z","iopub.status.idle":"2024-04-04T10:16:05.974365Z","shell.execute_reply.started":"2024-04-04T10:16:05.953549Z","shell.execute_reply":"2024-04-04T10:16:05.973278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc_data = librosa.feature.melspectrogram(y=shifting_data,sr=sr,n_fft=512,win_length=400,hop_length=160,n_mels=80)\nmfcc_data = librosa.power_to_db(mfcc_data, ref=np.max)\nprint(mfcc_data.shape)\nplt.pcolor(mfcc_data)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:05.975797Z","iopub.execute_input":"2024-04-04T10:16:05.976677Z","iopub.status.idle":"2024-04-04T10:16:08.234156Z","shell.execute_reply.started":"2024-04-04T10:16:05.976642Z","shell.execute_reply":"2024-04-04T10:16:08.232922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Change Pitch & Speed","metadata":{}},{"cell_type":"code","source":"y_pitch_speed = sig.copy()\n# you can change low and high here\nlength_change = np.random.uniform(low=0.8, high = 1)\nspeed_fac = 1.0  / length_change\nprint(\"resample length_change = \",length_change)\ntmp = np.interp(np.arange(0,len(y_pitch_speed),speed_fac),np.arange(0,len(y_pitch_speed)),y_pitch_speed)\nminlen = min(y_pitch_speed.shape[0], tmp.shape[0])\ny_pitch_speed *= 0\ny_pitch_speed[0:minlen] = tmp[0:minlen]\nAudio(y_pitch_speed, rate=sr)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:08.235784Z","iopub.execute_input":"2024-04-04T10:16:08.236219Z","iopub.status.idle":"2024-04-04T10:16:08.267720Z","shell.execute_reply.started":"2024-04-04T10:16:08.236182Z","shell.execute_reply":"2024-04-04T10:16:08.265702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc_data = librosa.feature.melspectrogram(y=y_pitch_speed,sr=sr,n_fft=512,win_length=400,hop_length=160,n_mels=80)\nmfcc_data = librosa.power_to_db(mfcc_data, ref=np.max)\nprint(mfcc_data.shape)\nplt.pcolor(mfcc_data)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:08.270488Z","iopub.execute_input":"2024-04-04T10:16:08.271178Z","iopub.status.idle":"2024-04-04T10:16:10.495090Z","shell.execute_reply.started":"2024-04-04T10:16:08.271111Z","shell.execute_reply":"2024-04-04T10:16:10.493839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Change Pitch","metadata":{}},{"cell_type":"code","source":"pitch_data = change_pitch(sig,sr,np.random.randint(-5,5))\nAudio(data=pitch_data, rate=sr)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:10.496602Z","iopub.execute_input":"2024-04-04T10:16:10.496943Z","iopub.status.idle":"2024-04-04T10:16:10.666415Z","shell.execute_reply.started":"2024-04-04T10:16:10.496916Z","shell.execute_reply":"2024-04-04T10:16:10.665312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc_data = librosa.feature.melspectrogram(y=pitch_data,sr=sr,n_fft=512,win_length=400,hop_length=160,n_mels=80)\nmfcc_data = librosa.power_to_db(mfcc_data, ref=np.max)\nprint(mfcc_data.shape)\nplt.pcolor(mfcc_data)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:10.667865Z","iopub.execute_input":"2024-04-04T10:16:10.668266Z","iopub.status.idle":"2024-04-04T10:16:12.883554Z","shell.execute_reply.started":"2024-04-04T10:16:10.668236Z","shell.execute_reply":"2024-04-04T10:16:12.882286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Change Speed","metadata":{}},{"cell_type":"code","source":"y_speed = sig.copy()\nspeed_change = np.random.uniform(low=0.9,high=1.1)\nprint(\"speed_change = \",speed_change)\ntmp = librosa.effects.time_stretch(y=y_speed.astype('float64'), rate=speed_change)\nminlen = min(y_speed.shape[0], tmp.shape[0])\ny_speed *= 0 \ny_speed[0:minlen] = tmp[0:minlen]\nAudio(y_speed, rate=sr)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:12.884969Z","iopub.execute_input":"2024-04-04T10:16:12.885334Z","iopub.status.idle":"2024-04-04T10:16:13.092845Z","shell.execute_reply.started":"2024-04-04T10:16:12.885298Z","shell.execute_reply":"2024-04-04T10:16:13.091331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc_data = librosa.feature.melspectrogram(y=y_speed,sr=sr,n_fft=512,win_length=400,hop_length=160,n_mels=80)\nmfcc_data = librosa.power_to_db(mfcc_data, ref=np.max)\nprint(mfcc_data.shape)\nplt.pcolor(mfcc_data)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:13.094497Z","iopub.execute_input":"2024-04-04T10:16:13.094903Z","iopub.status.idle":"2024-04-04T10:16:15.313690Z","shell.execute_reply.started":"2024-04-04T10:16:13.094870Z","shell.execute_reply":"2024-04-04T10:16:15.312665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Value Augmentation","metadata":{}},{"cell_type":"code","source":"y_aug = sig.copy()\ndyn_change = np.random.uniform(low=1.5,high=3)\nprint(\"dyn_change = \",dyn_change)\ny_aug = y_aug * dyn_change\nprint(y_aug[:50])\nprint(sig[:50])\nAudio(y_aug, rate=sr)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:15.315202Z","iopub.execute_input":"2024-04-04T10:16:15.315578Z","iopub.status.idle":"2024-04-04T10:16:15.338297Z","shell.execute_reply.started":"2024-04-04T10:16:15.315549Z","shell.execute_reply":"2024-04-04T10:16:15.337140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc_data = librosa.feature.melspectrogram(y=y_aug,sr=sr,n_fft=512,win_length=400,hop_length=160,n_mels=80)\nmfcc_data = librosa.power_to_db(mfcc_data, ref=np.max)\nprint(mfcc_data.shape)\nplt.pcolor(mfcc_data)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:15.339811Z","iopub.execute_input":"2024-04-04T10:16:15.340254Z","iopub.status.idle":"2024-04-04T10:16:17.540093Z","shell.execute_reply.started":"2024-04-04T10:16:15.340214Z","shell.execute_reply":"2024-04-04T10:16:17.539264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Hpss","metadata":{}},{"cell_type":"code","source":"y_hpss = librosa.effects.hpss(sig.astype('float64'))\nprint(y_hpss[1][:10])\nprint(sig[:10])\nAudio(y_hpss[1], rate=sr)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:17.541462Z","iopub.execute_input":"2024-04-04T10:16:17.542106Z","iopub.status.idle":"2024-04-04T10:16:18.319089Z","shell.execute_reply.started":"2024-04-04T10:16:17.542074Z","shell.execute_reply":"2024-04-04T10:16:18.317907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc_data = librosa.feature.melspectrogram(y=y_hpss[1],sr=sr,n_fft=512,win_length=400,hop_length=160,n_mels=80)\nmfcc_data = librosa.power_to_db(mfcc_data, ref=np.max)\nprint(mfcc_data.shape)\nplt.pcolor(mfcc_data)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:18.320539Z","iopub.execute_input":"2024-04-04T10:16:18.321403Z","iopub.status.idle":"2024-04-04T10:16:20.517142Z","shell.execute_reply.started":"2024-04-04T10:16:18.321371Z","shell.execute_reply":"2024-04-04T10:16:20.516076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Stretching","metadata":{}},{"cell_type":"code","source":"input_length = len(sig)\nstreching = sig.copy()\nstreching = librosa.effects.time_stretch(y=streching.astype('float'), rate=1.1)\nif len(streching) > input_length:\n    streching = streching[:input_length]\nelse:\n    streching = np.pad(streching, (0, max(0, input_length - len(streching))), \"constant\")\nAudio(streching, rate=sr)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:20.518333Z","iopub.execute_input":"2024-04-04T10:16:20.518680Z","iopub.status.idle":"2024-04-04T10:16:20.683757Z","shell.execute_reply.started":"2024-04-04T10:16:20.518651Z","shell.execute_reply":"2024-04-04T10:16:20.682497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc_data = librosa.feature.melspectrogram(y=streching,sr=sr,n_fft=512,win_length=400,hop_length=160,n_mels=80)\nmfcc_data = librosa.power_to_db(mfcc_data, ref=np.max)\nprint(mfcc_data.shape)\nplt.pcolor(mfcc_data)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:20.685406Z","iopub.execute_input":"2024-04-04T10:16:20.685906Z","iopub.status.idle":"2024-04-04T10:16:22.890981Z","shell.execute_reply.started":"2024-04-04T10:16:20.685868Z","shell.execute_reply":"2024-04-04T10:16:22.889700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compare Augmentations","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 4*9))\nplt.subplot(9, 1, 1)\nlibrosa.display.waveshow(sig.astype('float'), sr=sr,axis=None,color='blue')\nplt.title('Original')\nplt.subplot(9, 1, 2)\nlibrosa.display.waveshow(noising_data.astype('float'), sr=sr,axis=None,color='blue')\nplt.title('Add Noise')\nplt.subplot(9, 1, 3)\nlibrosa.display.waveshow(shifting_data.astype('float'), sr=sr,axis=None,color='blue')\nplt.title('Random Shifting')\nplt.subplot(9, 1, 4)\nlibrosa.display.waveshow(y_pitch_speed.astype('float'), sr=sr,axis=None,color='blue')\nplt.title('Augmented Pitch & Speed')\nplt.subplot(9, 1, 5)\nlibrosa.display.waveshow(pitch_data, sr=sr,axis=None,color='blue')\nplt.title('Augmented Pitch')\nplt.subplot(9, 1, 6)\nlibrosa.display.waveshow(y_speed, sr=sr,axis=None,color='blue')\nplt.title('Augmented Speed')\nplt.subplot(9, 1, 7)\nlibrosa.display.waveshow(y_aug.astype('float'), sr=sr,axis=None,color='blue')\nplt.title('Amplify value')\nplt.subplot(9, 1, 8)\nlibrosa.display.waveshow(y_hpss[1], sr=sr,axis=None,color='blue')\nplt.title('Apply Hpss')\nplt.subplot(9, 1, 9)\nlibrosa.display.waveshow(streching, sr=sr,axis=None,color='blue')\nplt.title('Stretching')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:16:22.892463Z","iopub.execute_input":"2024-04-04T10:16:22.892918Z","iopub.status.idle":"2024-04-04T10:16:31.512149Z","shell.execute_reply.started":"2024-04-04T10:16:22.892882Z","shell.execute_reply":"2024-04-04T10:16:31.511028Z"},"trusted":true},"execution_count":null,"outputs":[]}]}