{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Import Libraries\n___","metadata":{}},{"cell_type":"code","source":"import os\nos.environ['CUDA_IS_VISIBLE'] = '0'\nfrom collections import defaultdict # defaultdict(list)\nimport copy # copy.deepcopy(model.state_dict())\nimport sys\nfrom tqdm import tqdm\nimport ctypes\nimport gc\nimport random\nimport pickle\nfrom glob import glob\n\nimport pandas as pd, numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport plotly.graph_objects as go\n\n# Pytorch Imports \nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\nfrom torch.utils.data import Dataset,DataLoader\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nDEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:39.164176Z","iopub.execute_input":"2024-05-27T09:56:39.164685Z","iopub.status.idle":"2024-05-27T09:56:39.174445Z","shell.execute_reply.started":"2024-05-27T09:56:39.164650Z","shell.execute_reply":"2024-05-27T09:56:39.173139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    SEED = 2024\n    VER = 1\n    BASE_PATH = '../input/birdclef-2024/'\n   ","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:39.176248Z","iopub.execute_input":"2024-05-27T09:56:39.176630Z","iopub.status.idle":"2024-05-27T09:56:39.188237Z","shell.execute_reply.started":"2024-05-27T09:56:39.176601Z","shell.execute_reply":"2024-05-27T09:56:39.187042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def clean_memory():\n    ctypes.CDLL('libc.so.6').malloc_trim(0)\n    gc.collect()\n        \nclean_memory()  ","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:39.190380Z","iopub.execute_input":"2024-05-27T09:56:39.190794Z","iopub.status.idle":"2024-05-27T09:56:39.409743Z","shell.execute_reply.started":"2024-05-27T09:56:39.190737Z","shell.execute_reply":"2024-05-27T09:56:39.408272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To proudce simliar result in each run \ndef seed_everything():\n    random.seed(CFG.SEED)\n    os.environ['PYTHONHASHSEED'] = str(CFG.SEED)\n    np.random.seed(CFG.SEED)\n    torch.manual_seed(CFG.SEED)\n    torch.cuda.manual_seed(CFG.SEED)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = True\n    \nseed_everything()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:39.411691Z","iopub.execute_input":"2024-05-27T09:56:39.412192Z","iopub.status.idle":"2024-05-27T09:56:39.432093Z","shell.execute_reply.started":"2024-05-27T09:56:39.412138Z","shell.execute_reply":"2024-05-27T09:56:39.430628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Road and Read Data \n___","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv(CFG.BASE_PATH + 'train_metadata.csv')\n\nprint('Shape of Train: ', df_train.shape)\nprint(display(df_train.head()))","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:39.435559Z","iopub.execute_input":"2024-05-27T09:56:39.436489Z","iopub.status.idle":"2024-05-27T09:56:39.664840Z","shell.execute_reply.started":"2024-05-27T09:56:39.436438Z","shell.execute_reply":"2024-05-27T09:56:39.663705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:39.666295Z","iopub.execute_input":"2024-05-27T09:56:39.666722Z","iopub.status.idle":"2024-05-27T09:56:39.724743Z","shell.execute_reply.started":"2024-05-27T09:56:39.666683Z","shell.execute_reply":"2024-05-27T09:56:39.723194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Numerical & Categorical Columns**","metadata":{}},{"cell_type":"code","source":"categorical_columns = df_train.select_dtypes(include=['object','category']).columns\n\nnumerical_columns = df_train.select_dtypes(exclude = ['object','category']).columns","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:39.726166Z","iopub.execute_input":"2024-05-27T09:56:39.726556Z","iopub.status.idle":"2024-05-27T09:56:39.736819Z","shell.execute_reply.started":"2024-05-27T09:56:39.726519Z","shell.execute_reply":"2024-05-27T09:56:39.735456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[numerical_columns].describe()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:39.738300Z","iopub.execute_input":"2024-05-27T09:56:39.738653Z","iopub.status.idle":"2024-05-27T09:56:39.767517Z","shell.execute_reply.started":"2024-05-27T09:56:39.738622Z","shell.execute_reply":"2024-05-27T09:56:39.766318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = df_train[df_train['secondary_labels'] != '[]'].iloc[:50]\nfig = px.sunburst(tmp, path=['primary_label', 'secondary_labels'] )\nfig.update_layout(\n         title=\"Primary & Secondary Labels \",\n    )\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:39.769566Z","iopub.execute_input":"2024-05-27T09:56:39.770046Z","iopub.status.idle":"2024-05-27T09:56:41.735304Z","shell.execute_reply.started":"2024-05-27T09:56:39.770012Z","shell.execute_reply":"2024-05-27T09:56:41.734026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Longitude & Latitude**","metadata":{}},{"cell_type":"code","source":"tmp = df_train.copy()\n\nfig = px.scatter_mapbox(\n    tmp, \n    lat=\"latitude\", \n    lon=\"longitude\", \n    color=\"primary_label\",\n    zoom=0.1,\n    title='Bird Recordings Loaction'\n)\n\nfig.update_layout(mapbox_style=\"open-street-map\")\nfig.update_layout(margin={\"r\":0,\"t\":30,\"l\":0,\"b\":0})\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:41.739816Z","iopub.execute_input":"2024-05-27T09:56:41.740197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Raw Audio\n___","metadata":{}},{"cell_type":"markdown","source":"If this competition is your first time to approach audo data, \n\nClick [**Working with Audio in Python**](https://www.kaggle.com/code/robikscube/working-with-audio-in-python)","metadata":{}},{"cell_type":"code","source":"import librosa \nimport librosa.display\nimport IPython.display as ipd","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:42.434153Z","iopub.execute_input":"2024-05-27T09:56:42.434585Z","iopub.status.idle":"2024-05-27T09:56:42.440383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Frequency\n- Frequency describes the differences of wave lengths\n\n![](https://uploads-cdn.omnicalculator.com/images/britannica-wave-frequency.jpg)","metadata":{}},{"cell_type":"markdown","source":"#### Intensity(db/power)\n![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSzVQNjeynyt64ahWuR84jIlE_KKbZVHzsdUQ&s)","metadata":{}},{"cell_type":"markdown","source":"#### Reading in Audio Files\nThere are many types of audio files: `mp3`, `wav`, `m4a`, `flac`, `oog`\n\nAnd In this Competitions, audio type is `ogg`.","metadata":{}},{"cell_type":"code","source":"audio_asbfly = glob('/kaggle/input/birdclef-2024/train_audio/asbfly/*')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:42.441977Z","iopub.execute_input":"2024-05-27T09:56:42.442525Z","iopub.status.idle":"2024-05-27T09:56:42.478762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(audio_asbfly[2])","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:42.480270Z","iopub.execute_input":"2024-05-27T09:56:42.480728Z","iopub.status.idle":"2024-05-27T09:56:42.497105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y, sr = librosa.load(audio_asbfly[2])\nprint(f'y: {y[:10]}')\nprint(f'shape y: {y.shape}')\nprint(f'Sample Rate is: {sr}')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:42.498461Z","iopub.execute_input":"2024-05-27T09:56:42.498908Z","iopub.status.idle":"2024-05-27T09:56:49.859305Z","shell.execute_reply.started":"2024-05-27T09:56:42.498869Z","shell.execute_reply":"2024-05-27T09:56:49.857616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nplt.title('Raw Audio Sample')\npd.Series(y).plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:49.860629Z","iopub.execute_input":"2024-05-27T09:56:49.861057Z","iopub.status.idle":"2024-05-27T09:56:50.289758Z","shell.execute_reply.started":"2024-05-27T09:56:49.861026Z","shell.execute_reply":"2024-05-27T09:56:50.288115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nplt.title('Middel 1s in Raw Audio Sample')\npd.Series(y[55566-11113:55566+11113]).plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:50.291301Z","iopub.execute_input":"2024-05-27T09:56:50.291678Z","iopub.status.idle":"2024-05-27T09:56:50.691761Z","shell.execute_reply.started":"2024-05-27T09:56:50.291645Z","shell.execute_reply":"2024-05-27T09:56:50.690502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Down Smapling","metadata":{}},{"cell_type":"code","source":"down_sample = y[::5,]","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:50.693031Z","iopub.execute_input":"2024-05-27T09:56:50.693331Z","iopub.status.idle":"2024-05-27T09:56:50.698671Z","shell.execute_reply.started":"2024-05-27T09:56:50.693304Z","shell.execute_reply":"2024-05-27T09:56:50.697385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nplt.title('DownSampling Raw Audio Sample')\npd.Series(down_sample).plot(color='pink')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:50.699998Z","iopub.execute_input":"2024-05-27T09:56:50.700523Z","iopub.status.idle":"2024-05-27T09:56:51.052090Z","shell.execute_reply.started":"2024-05-27T09:56:50.700466Z","shell.execute_reply":"2024-05-27T09:56:51.050839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Audio Filtering\n___","metadata":{}},{"cell_type":"markdown","source":"#### Butter Filter\nThere are `Low-Pass`, `Band-Pass`, `High-Pass`, `Notch Filtering` in Butter Filter","metadata":{}},{"cell_type":"markdown","source":"**Butter Low Pass(~10Hz)**","metadata":{}},{"cell_type":"code","source":"from scipy.signal import butter, lfilter\n\ndef butter_lowpass_filter(data, cutoff_freq=10, sampling_rate=22050, order=1):\n    nyquist = 0.5 * sampling_rate\n    normal_cutoff = cutoff_freq / nyquist\n    \n    b,a = butter(order, normal_cutoff, btype='low', analog=False)\n    filtered_data = lfilter(b, a, data, axis=0)\n    \n    return filtered_data\n","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:51.053577Z","iopub.execute_input":"2024-05-27T09:56:51.053945Z","iopub.status.idle":"2024-05-27T09:56:51.062319Z","shell.execute_reply.started":"2024-05-27T09:56:51.053914Z","shell.execute_reply":"2024-05-27T09:56:51.061005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nplt.subplot(1,2,1)\nplt.title('Butter Low Pass in Order=2')\npd.Series(butter_lowpass_filter(y, order=2)).plot()\nplt.subplot(1,2,2)\nplt.title('Butter Low Pass in Order=5')\npd.Series(butter_lowpass_filter(y, order=5)).plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:51.063912Z","iopub.execute_input":"2024-05-27T09:56:51.064346Z","iopub.status.idle":"2024-05-27T09:56:51.687158Z","shell.execute_reply.started":"2024-05-27T09:56:51.064307Z","shell.execute_reply":"2024-05-27T09:56:51.686046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### **Butter Band Pass(5Hz~10Hz)**","metadata":{}},{"cell_type":"code","source":"from scipy.signal import butter, lfilter\n\ndef butter_bandpass_filter(data, sampling_rate=22050, order=1):\n    nyquist = 0.5 * sampling_rate\n    low =  5/nyquist\n    high = 10/nyquist\n    \n    b,a = butter(order, [low, high], btype='band', analog=False)\n    filtered_data = lfilter(b, a, data, axis=0)\n    \n    return filtered_data","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:51.689019Z","iopub.execute_input":"2024-05-27T09:56:51.689452Z","iopub.status.idle":"2024-05-27T09:56:51.698080Z","shell.execute_reply.started":"2024-05-27T09:56:51.689412Z","shell.execute_reply":"2024-05-27T09:56:51.696697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nplt.subplot(1,2,1)\nplt.title('Butter band Pass in Order=2')\npd.Series(butter_bandpass_filter(y, order=2)).plot()\nplt.subplot(1,2,2)\nplt.title('Butter band Pass in Order=5')\npd.Series(butter_bandpass_filter(y, order=5)).plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:51.699889Z","iopub.execute_input":"2024-05-27T09:56:51.700228Z","iopub.status.idle":"2024-05-27T09:56:52.536991Z","shell.execute_reply.started":"2024-05-27T09:56:51.700200Z","shell.execute_reply":"2024-05-27T09:56:52.535729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### IIR Filter & FIR filters\nThere are IIR Filters and FIR Filters in Scipy.singal\n\nIf you want to learn more, Click [**Signal Processing with SciPy**](https://warrenweckesser.github.io/papers/weckesser-scipy-linear-filters.pdf)","metadata":{}},{"cell_type":"markdown","source":"**Below Signals are made with FIR filters, and then Unlike IIR filers, Fir filters use taps not order**","metadata":{}},{"cell_type":"code","source":"from scipy.signal import firwin, lfilter\n\ndef fir_lowpass_filter(data, cutoff_freq=10, sampling_rate=22050, numtaps=10):\n    nyquist = 0.5 * sampling_rate\n    normal_cutoff = cutoff_freq / nyquist\n    \n    # Generate the FIR filter coefficients\n    taps = firwin(numtaps, normal_cutoff, pass_zero='lowpass')\n    \n    # Apply the filter to the data\n    filtered_data = lfilter(taps, 1.0, data)\n    \n    return filtered_data","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:52.538358Z","iopub.execute_input":"2024-05-27T09:56:52.538846Z","iopub.status.idle":"2024-05-27T09:56:52.546209Z","shell.execute_reply.started":"2024-05-27T09:56:52.538803Z","shell.execute_reply":"2024-05-27T09:56:52.544867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nplt.subplot(1,2,1)\nplt.title('FIR Low Pass in Tap=5000')\npd.Series(fir_lowpass_filter(y, numtaps=5000)).plot()\nplt.subplot(1,2,2)\nplt.title('FIR Low Pass in Tap=10000')\npd.Series(fir_lowpass_filter(y, numtaps=10000)).plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:52.547802Z","iopub.execute_input":"2024-05-27T09:56:52.548263Z","iopub.status.idle":"2024-05-27T09:56:53.595218Z","shell.execute_reply.started":"2024-05-27T09:56:52.548218Z","shell.execute_reply":"2024-05-27T09:56:53.593818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Gaussian Filter[Smoothing]","metadata":{}},{"cell_type":"code","source":"from scipy.ndimage import gaussian_filter\n\ndef applyed_gaussian_filter(data, sigma=1):\n    \n    #Applying Gaussian Filter\n    filtered_data = gaussian_filter(data, sigma=sigma, order=2, mode='reflect')\n    \n    return filtered_data","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:53.600257Z","iopub.execute_input":"2024-05-27T09:56:53.600626Z","iopub.status.idle":"2024-05-27T09:56:53.607044Z","shell.execute_reply.started":"2024-05-27T09:56:53.600597Z","shell.execute_reply":"2024-05-27T09:56:53.605741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nplt.subplot(1,2,1)\nplt.title('Guassian Filter in sigma=1')\npd.Series(applyed_gaussian_filter(y, sigma=1)).plot()\nplt.subplot(1,2,2)\nplt.title('Gaussian Filter in sigma=2')\npd.Series(applyed_gaussian_filter(y, sigma=2)).plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:53.608306Z","iopub.execute_input":"2024-05-27T09:56:53.608749Z","iopub.status.idle":"2024-05-27T09:56:54.180378Z","shell.execute_reply.started":"2024-05-27T09:56:53.608707Z","shell.execute_reply":"2024-05-27T09:56:54.178976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Auido Denoising with Wavelet Transform\n___","metadata":{}},{"cell_type":"markdown","source":"When Recording Bird songs, There are a lot of noisy from environment.\nwavelet transform helps remove noisy from raw audio.\nThere are a lot of wavelist. ex) `bior1.3`, `db8`, `gaus2`\n\nBut, be careful to hurt audio when denoisying","metadata":{}},{"cell_type":"code","source":"import pywt\nprint(pywt.wavelist())","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:54.181637Z","iopub.execute_input":"2024-05-27T09:56:54.182035Z","iopub.status.idle":"2024-05-27T09:56:54.201615Z","shell.execute_reply.started":"2024-05-27T09:56:54.182004Z","shell.execute_reply":"2024-05-27T09:56:54.200323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def maddest(d, axis=None):\n    return np.mean(np.absolute(d - np.mean(d, axis)), axis)\n\ndef denoise(x, wavelet='haar', level=1):    \n    coeff = pywt.wavedec(x, wavelet, mode=\"per\")\n    sigma = (1/0.6745) * maddest(coeff[-level])\n\n    uthresh = sigma * np.sqrt(2*np.log(len(x)))\n    coeff[1:] = (pywt.threshold(i, value=uthresh, mode='hard') for i in coeff[1:])\n\n    ret=pywt.waverec(coeff, wavelet, mode='per')\n    \n    return ret","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:54.203305Z","iopub.execute_input":"2024-05-27T09:56:54.204192Z","iopub.status.idle":"2024-05-27T09:56:54.214311Z","shell.execute_reply.started":"2024-05-27T09:56:54.204148Z","shell.execute_reply":"2024-05-27T09:56:54.213092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nplt.subplot(1,2,1)\nplt.title('Denoised Audio by bior1.1')\npd.Series(denoise(y, wavelet='bior1.1')).plot(color='red')\nplt.subplot(1,2,2)\nplt.title('Denoised Audio by sym14')\npd.Series(denoise(y, wavelet='sym14')).plot(color='red')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:56:54.216086Z","iopub.execute_input":"2024-05-27T09:56:54.216567Z","iopub.status.idle":"2024-05-27T09:56:54.749484Z","shell.execute_reply.started":"2024-05-27T09:56:54.216525Z","shell.execute_reply":"2024-05-27T09:56:54.748142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create Spectrograms with Librosa\n____","metadata":{}},{"cell_type":"markdown","source":"![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRCsNBr8aegj2WZcXA9XoduSCbuhIXCSuPptA&s)","metadata":{}},{"cell_type":"markdown","source":"I'm gonna use library librosa to create spectrograms.\n\n`Spectrograms of size = 256X256(freqXtime)`\n\nThe main function is \n\n    mel_spec = librsoa.feature.melspectrogram(y=x, sr=200, hop_length=len(x)//256, n_fft=1024, n_mels=128, fmin=0, fmax=20, win_length=128\n    \n* `y` is the input time series signal \n* `sr` is the sampling frequency.\n* `hop_length` produces image with `width=len(x)/hop_length`\n* `n_fft` controls vertical resolution and quality of spectrogram\n* `n_mels` produces image with `height=n_mels`\n* `f_min` is smallest frequency in our spectrogram\n* `f_max` is largest frequency in our spectrogram\n* `win_length` controls hortizonal resolution and quality of spectrogram","metadata":{}},{"cell_type":"code","source":"ipd.Audio(audio_asbfly[104])","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:49:32.521012Z","iopub.execute_input":"2024-05-27T10:49:32.521433Z","iopub.status.idle":"2024-05-27T10:49:32.555302Z","shell.execute_reply.started":"2024-05-27T10:49:32.521400Z","shell.execute_reply":"2024-05-27T10:49:32.553951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x, sr = librosa.load(audio_asbfly[104], sr=None)\nprint(f'y: {x[:10]}')\nprint(f'shape y: {x.shape}')\nprint(f'Sample Rate is: {sr}')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:49:32.834293Z","iopub.execute_input":"2024-05-27T10:49:32.834769Z","iopub.status.idle":"2024-05-27T10:49:33.002004Z","shell.execute_reply.started":"2024-05-27T10:49:32.834715Z","shell.execute_reply":"2024-05-27T10:49:33.000760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nplt.title('Raw Audio Sample')\npd.Series(x).plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:49:38.214770Z","iopub.execute_input":"2024-05-27T10:49:38.215236Z","iopub.status.idle":"2024-05-27T10:49:39.135555Z","shell.execute_reply.started":"2024-05-27T10:49:38.215202Z","shell.execute_reply":"2024-05-27T10:49:39.134189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.stft(x)\n\nfrequencies = librosa.fft_frequencies(sr=sr, n_fft=D.shape[0])","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:49:39.800987Z","iopub.execute_input":"2024-05-27T10:49:39.801389Z","iopub.status.idle":"2024-05-27T10:49:40.006614Z","shell.execute_reply.started":"2024-05-27T10:49:39.801360Z","shell.execute_reply":"2024-05-27T10:49:40.005408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Max Frequency in audio_asbfly[104]: {frequencies.max()}')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:49:40.107414Z","iopub.execute_input":"2024-05-27T10:49:40.107881Z","iopub.status.idle":"2024-05-27T10:49:40.113811Z","shell.execute_reply.started":"2024-05-27T10:49:40.107843Z","shell.execute_reply":"2024-05-27T10:49:40.112699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fill nans\nm = np.nanmean(x)\nx = np.nan_to_num(x, nan=m)\n\n# RAW SPECTROGRAM\nmel_spec = librosa.feature.melspectrogram(y=x, sr=32000, hop_length=len(x)//256, \n                     n_fft=2048, n_mels=256, fmin=frequencies.min(), fmax=frequencies.max(), win_length=256)\n# LOG TRANSFORM\nwidth = (mel_spec.shape[1]//32)*32 # 257 -> 256\nmel_spec_db = librosa.power_to_db(mel_spec, ref=np.max).astype(np.float32)[:,:width]\n\n# STANDARDIZE TO -1 TO 1\nmel_spec_db = (mel_spec_db+40)/40 # -80~0db -> -40~40db","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:53:08.734253Z","iopub.execute_input":"2024-05-27T10:53:08.734694Z","iopub.status.idle":"2024-05-27T10:53:08.798338Z","shell.execute_reply.started":"2024-05-27T10:53:08.734659Z","shell.execute_reply":"2024-05-27T10:53:08.796396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nplt.imshow(mel_spec_db, aspect='auto',origin='lower')\nplt.title('Spectrogram in audio_asbfly[104]')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T11:00:55.633551Z","iopub.execute_input":"2024-05-27T11:00:55.634020Z","iopub.status.idle":"2024-05-27T11:00:56.098077Z","shell.execute_reply.started":"2024-05-27T11:00:55.633976Z","shell.execute_reply":"2024-05-27T11:00:56.096839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}