{"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":"code","source":"import os\nimport numpy as np\nimport scipy\nfrom scipy.io import wavfile\nimport scipy.fftpack as fft\nfrom scipy.signal import get_window\nimport IPython.display as ipd\nimport matplotlib.pyplot as plt\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:07:59.040786Z","iopub.execute_input":"2023-03-29T06:07:59.041290Z","iopub.status.idle":"2023-03-29T06:08:00.085445Z","shell.execute_reply.started":"2023-03-29T06:07:59.041239Z","shell.execute_reply":"2023-03-29T06:08:00.084391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = '/kaggle/input/urbansound8k/fold10/102857-5-0-0.wav'\nipd.Audio(PATH)","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:08:00.087975Z","iopub.execute_input":"2023-03-29T06:08:00.088878Z","iopub.status.idle":"2023-03-29T06:08:00.139938Z","shell.execute_reply.started":"2023-03-29T06:08:00.088820Z","shell.execute_reply":"2023-03-29T06:08:00.138929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn\nimport librosa\nimport librosa.display\n\nsamples, sample_rate = librosa.load(PATH, sr=None)\nprint(\"Sample rate: {0}Hz\".format(sample_ rate))\nprint(\"Audio duration: {0}s\".format(len(samples) / sample_rate))","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:08:00.141346Z","iopub.execute_input":"2023-03-29T06:08:00.141770Z","iopub.status.idle":"2023-03-29T06:08:12.956919Z","shell.execute_reply.started":"2023-03-29T06:08:00.141729Z","shell.execute_reply":"2023-03-29T06:08:12.955488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa.display\nimport matplotlib.pyplot as plt\n\n# x-axis has been converted to time using our sample rate. \n# matplotlib plt.plot(y), would output the same figure, but with sample \n# number on the x-axis instead of seconds\nplt.figure(figsize=(14, 5))\nlibrosa.display.waveshow(samples, sr=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:08:12.960021Z","iopub.execute_input":"2023-03-29T06:08:12.961293Z","iopub.status.idle":"2023-03-29T06:08:13.790794Z","shell.execute_reply.started":"2023-03-29T06:08:12.961234Z","shell.execute_reply":"2023-03-29T06:08:13.789768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:31:56.910454Z","iopub.execute_input":"2023-03-29T06:31:56.910869Z","iopub.status.idle":"2023-03-29T06:31:56.920797Z","shell.execute_reply.started":"2023-03-29T06:31:56.910833Z","shell.execute_reply":"2023-03-29T06:31:56.919218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def normalize_audio(audio):\n#     audio = audio / np.max(np.abs(audio))\n#     return audio","metadata":{"execution":{"iopub.status.busy":"2023-03-29T04:34:49.018574Z","iopub.execute_input":"2023-03-29T04:34:49.019001Z","iopub.status.idle":"2023-03-29T04:34:49.025680Z","shell.execute_reply.started":"2023-03-29T04:34:49.018948Z","shell.execute_reply":"2023-03-29T04:34:49.023729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# audio = normalize_audio(audio)\n# plt.figure(figsize=(15,4))\n# plt.plot(np.linspace(0, len(audio) / sample_rate, num=len(audio)), audio)\n# plt.grid(True)","metadata":{"execution":{"iopub.status.busy":"2023-03-29T04:34:50.714021Z","iopub.execute_input":"2023-03-29T04:34:50.714421Z","iopub.status.idle":"2023-03-29T04:34:50.719530Z","shell.execute_reply.started":"2023-03-29T04:34:50.714389Z","shell.execute_reply":"2023-03-29T04:34:50.718117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def frame_audio(samples, FFT_size=2048, hop_size=10, sample_rate=44100):\n    # hop_size in ms\n    \n    audio = np.pad(samples, int(FFT_size / 2), mode='reflect')\n    frame_len = np.round(sample_rate * hop_size / 1000).astype(int)\n    frame_num = int((len(samples) - FFT_size) / frame_len) + 1\n    frames = np.zeros((frame_num,FFT_size))\n    \n    for n in range(frame_num):\n        frames[n] = audio[n*frame_len:n*frame_len+FFT_size]\n    \n    return frames","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:46:37.408326Z","iopub.execute_input":"2023-03-29T06:46:37.408766Z","iopub.status.idle":"2023-03-29T06:46:37.418989Z","shell.execute_reply.started":"2023-03-29T06:46:37.408728Z","shell.execute_reply":"2023-03-29T06:46:37.416922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hop_size = 15 #ms\nFFT_size = 2048\n\naudio_framed = frame_audio(samples, FFT_size=FFT_size, hop_size=hop_size, sample_rate=sample_rate)\nprint(\"Framed audio shape: {0}\".format(audio_framed.shape))","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:46:40.153576Z","iopub.execute_input":"2023-03-29T06:46:40.155214Z","iopub.status.idle":"2023-03-29T06:46:40.167290Z","shell.execute_reply.started":"2023-03-29T06:46:40.155130Z","shell.execute_reply":"2023-03-29T06:46:40.165399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_framed","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:46:42.874468Z","iopub.execute_input":"2023-03-29T06:46:42.875154Z","iopub.status.idle":"2023-03-29T06:46:42.883784Z","shell.execute_reply.started":"2023-03-29T06:46:42.875102Z","shell.execute_reply":"2023-03-29T06:46:42.882022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"window = get_window(\"hann\", FFT_size, fftbins=True)\nplt.figure(figsize=(15,4))\nplt.plot(window)\nplt.grid(True)","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:46:54.561486Z","iopub.execute_input":"2023-03-29T06:46:54.562176Z","iopub.status.idle":"2023-03-29T06:46:54.788710Z","shell.execute_reply.started":"2023-03-29T06:46:54.562133Z","shell.execute_reply":"2023-03-29T06:46:54.787060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_win = audio_framed * window\n\nind = 0\nplt.figure(figsize=(15,6))\nplt.subplot(2, 1, 1)\nplt.plot(audio_framed[ind])\nplt.title('Original Frame')\nplt.grid(True)\nplt.subplot(2, 1, 2)\nplt.plot(audio_win[ind])\nplt.title('Frame After Windowing')\nplt.grid(True)","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:46:56.320516Z","iopub.execute_input":"2023-03-29T06:46:56.320953Z","iopub.status.idle":"2023-03-29T06:46:56.726651Z","shell.execute_reply.started":"2023-03-29T06:46:56.320915Z","shell.execute_reply":"2023-03-29T06:46:56.725104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_winT = np.transpose(audio_win)\n\naudio_fft = np.empty((int(1 + FFT_size // 2), audio_winT.shape[1]), dtype=np.complex64, order='F')\n\nfor n in range(audio_fft.shape[1]):\n    audio_fft[:, n] = fft.fft(audio_winT[:, n], axis=0)[:audio_fft.shape[0]]\n\naudio_fft = np.transpose(audio_fft)","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:05:29.099357Z","iopub.execute_input":"2023-03-29T07:05:29.099792Z","iopub.status.idle":"2023-03-29T07:05:29.125867Z","shell.execute_reply.started":"2023-03-29T07:05:29.099755Z","shell.execute_reply":"2023-03-29T07:05:29.124786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_power = np.square(np.abs(audio_fft))\nprint(audio_power.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:05:36.577084Z","iopub.execute_input":"2023-03-29T07:05:36.578558Z","iopub.status.idle":"2023-03-29T07:05:36.587178Z","shell.execute_reply.started":"2023-03-29T07:05:36.578482Z","shell.execute_reply":"2023-03-29T07:05:36.585369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_min = 0\nfreq_high = sample_rate / 2\nmel_filter_num = 10\n\nprint(\"Minimum frequency: {0}\".format(freq_min))\nprint(\"Maximum frequency: {0}\".format(freq_high))","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:05:56.120116Z","iopub.execute_input":"2023-03-29T07:05:56.121119Z","iopub.status.idle":"2023-03-29T07:05:56.128340Z","shell.execute_reply.started":"2023-03-29T07:05:56.121070Z","shell.execute_reply":"2023-03-29T07:05:56.126860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def freq_to_mel(freq):\n    return 2595.0 * np.log10(1.0 + freq / 700.0)\n\ndef mel_to_freq(mels):\n    return 700.0 * (10.0**(mels / 2595.0) - 1.0)","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:16:44.913103Z","iopub.execute_input":"2023-03-29T07:16:44.913536Z","iopub.status.idle":"2023-03-29T07:16:44.920293Z","shell.execute_reply.started":"2023-03-29T07:16:44.913498Z","shell.execute_reply":"2023-03-29T07:16:44.918720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_filter_points(fmin, fmax, mel_filter_num, FFT_size, sample_rate=44100):\n    fmin_mel = freq_to_mel(fmin)\n    fmax_mel = freq_to_mel(fmax)\n    \n    print(\"MEL min: {0}\".format(fmin_mel))\n    print(\"MEL max: {0}\".format(fmax_mel))\n    \n    mels = np.linspace(fmin_mel, fmax_mel, num=mel_filter_num+2)\n    freqs = mel_to_freq(mels)\n    \n    return np.floor((FFT_size + 1) / sample_rate * freqs).astype(int), freqs","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:16:45.590151Z","iopub.execute_input":"2023-03-29T07:16:45.590892Z","iopub.status.idle":"2023-03-29T07:16:45.599259Z","shell.execute_reply.started":"2023-03-29T07:16:45.590836Z","shell.execute_reply":"2023-03-29T07:16:45.597899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filter_points, mel_freqs = get_filter_points(freq_min, freq_high, mel_filter_num, FFT_size, sample_rate=44100)\nfilter_points","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:16:46.184549Z","iopub.execute_input":"2023-03-29T07:16:46.185777Z","iopub.status.idle":"2023-03-29T07:16:46.195630Z","shell.execute_reply.started":"2023-03-29T07:16:46.185726Z","shell.execute_reply":"2023-03-29T07:16:46.194080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_filters(filter_points, FFT_size):\n    filters = np.zeros((len(filter_points)-2,int(FFT_size/2+1)))\n    \n    for n in range(len(filter_points)-2):\n        filters[n, filter_points[n] : filter_points[n + 1]] = np.linspace(0, 1, filter_points[n + 1] - filter_points[n])\n        filters[n, filter_points[n + 1] : filter_points[n + 2]] = np.linspace(1, 0, filter_points[n + 2] - filter_points[n + 1])\n    \n    return filters","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:17:14.712110Z","iopub.execute_input":"2023-03-29T07:17:14.712578Z","iopub.status.idle":"2023-03-29T07:17:14.721570Z","shell.execute_reply.started":"2023-03-29T07:17:14.712539Z","shell.execute_reply":"2023-03-29T07:17:14.719962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filters = get_filters(filter_points, FFT_size)\n\nplt.figure(figsize=(15,4))\nfor n in range(filters.shape[0]):\n    plt.plot(filters[n])","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:17:21.305208Z","iopub.execute_input":"2023-03-29T07:17:21.306435Z","iopub.status.idle":"2023-03-29T07:17:21.518858Z","shell.execute_reply.started":"2023-03-29T07:17:21.306364Z","shell.execute_reply":"2023-03-29T07:17:21.517524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# taken from the librosa library\nenorm = 2.0 / (mel_freqs[2:mel_filter_num+2] - mel_freqs[:mel_filter_num])\nfilters *= enorm[:, np.newaxis]","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:17:42.287469Z","iopub.execute_input":"2023-03-29T07:17:42.287896Z","iopub.status.idle":"2023-03-29T07:17:42.294233Z","shell.execute_reply.started":"2023-03-29T07:17:42.287851Z","shell.execute_reply":"2023-03-29T07:17:42.292635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,4))\nfor n in range(filters.shape[0]):\n    plt.plot(filters[n])","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:17:48.441542Z","iopub.execute_input":"2023-03-29T07:17:48.441976Z","iopub.status.idle":"2023-03-29T07:17:48.697362Z","shell.execute_reply.started":"2023-03-29T07:17:48.441936Z","shell.execute_reply":"2023-03-29T07:17:48.696095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_filtered = np.dot(filters, np.transpose(audio_power))\naudio_log = 40.0 * np.log10(audio_filtered)\naudio_log.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:18:23.690754Z","iopub.execute_input":"2023-03-29T07:18:23.691206Z","iopub.status.idle":"2023-03-29T07:18:23.703945Z","shell.execute_reply.started":"2023-03-29T07:18:23.691167Z","shell.execute_reply":"2023-03-29T07:18:23.702063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dct(dct_filter_num, filter_len):\n    basis = np.empty((dct_filter_num,filter_len))\n    basis[0, :] = 1.0 / np.sqrt(filter_len)\n    \n    samples = np.arange(1, 2 * filter_len, 2) * np.pi / (2.0 * filter_len)\n\n    for i in range(1, dct_filter_num):\n        basis[i, :] = np.cos(i * samples) * np.sqrt(2.0 / filter_len)\n        \n    return basis","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:21:49.257744Z","iopub.execute_input":"2023-03-29T07:21:49.258190Z","iopub.status.idle":"2023-03-29T07:21:49.266723Z","shell.execute_reply.started":"2023-03-29T07:21:49.258150Z","shell.execute_reply":"2023-03-29T07:21:49.265314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dct_filter_num = 40\n\ndct_filters = dct(dct_filter_num, mel_filter_num)\n\ncepstral_coefficents = np.dot(dct_filters, audio_log)\ncepstral_coefficents.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:21:54.606693Z","iopub.execute_input":"2023-03-29T07:21:54.607119Z","iopub.status.idle":"2023-03-29T07:21:54.617019Z","shell.execute_reply.started":"2023-03-29T07:21:54.607080Z","shell.execute_reply":"2023-03-29T07:21:54.615666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cepstral_coefficents[:, 0]","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:22:07.664338Z","iopub.execute_input":"2023-03-29T07:22:07.664749Z","iopub.status.idle":"2023-03-29T07:22:07.674789Z","shell.execute_reply.started":"2023-03-29T07:22:07.664714Z","shell.execute_reply":"2023-03-29T07:22:07.673516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nplt.plot(np.linspace(0, len(samples) / sample_rate, num=len(samples)), samples)\nplt.imshow(cepstral_coefficents, aspect='auto', origin='lower');","metadata":{"execution":{"iopub.status.busy":"2023-03-29T07:22:55.402245Z","iopub.execute_input":"2023-03-29T07:22:55.402681Z","iopub.status.idle":"2023-03-29T07:22:55.790149Z","shell.execute_reply.started":"2023-03-29T07:22:55.402646Z","shell.execute_reply":"2023-03-29T07:22:55.788111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}