Answer To: { "cells": [ { "cell_type": "markdown", "metadata": { "id": "KqfSrMPzw_o-" }, "source": [ "# ECE113...
Joice G answered on Jun 07 2021
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "signal_spectro_hann_overlap.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 274
},
"id": "Ac5ac442hAtG",
"outputId": "5b493581-4577-4a4e-9b14-fe64bcf086e3"
},
"source": [
"import numpy as np\n",
"from scipy import signal\n",
"from scipy.fft import fftshift\n",
"import scipy.io.wavfile as wavfile\n",
"import matplotlib.pyplot as plt\n",
"#import sounddevice as sd\n",
"\n",
"# Spectrogram plot\n",
"def sg_plot (t_range, f_range , y ,dbf=60,fig =None) :\n",
"\n",
" eps = 10.0**(-dbf/20.0)\n",
" # minimum signal\n",
" # find maximum \n",
" y_max = abs(y).max()\n",
" # compute 20*log magnitude, scaled to the max\n",
" y_log = 20.0*np.log10((abs(y)/y_max)*(1-eps)+eps)\n",
"\n",
" # rescale image intensity to 256\n",
" img =256*(y_log+dbf)/dbf - 1\n",
"\n",
" fig = plt.figure(figsize =(16,6))\n",
"\n",
" plt.imshow(np.flipud(64.0*(y_log + dbf )/dbf), extent = t_range + f_range)\n",
" plt.xlabel('Time, s')\n",
" plt.ylabel('Frequency, Hz')\n",
" plt.tight_layout()\n",
" return fig\n",
"\n",
"#Spectrogram with overlapping hanning windows\n",
"def mySpectrogram_hann_overlap(data_s1,fs_s1,m):\n",
" \n",
" zro_padd = 0;\n",
" tot_len = len(data_s1);\n",
" print('The total length is ' +str(tot_len))\n",
" print('The m is ' +str(m))\n",
"\n",
" if ((tot_len % m) != 0 ):\n",
" zro_padd = m-(tot_len % m); \n",
"\n",
" data_s1_fin = np.concatenate([data_s1, np.zeros(zro_padd)])\n",
" col_size = len(data_s1_fin)//m;\n",
"\n",
" print('The column size is ' +str(col_size))\n",
"\n",
" data_s1_fin = np.array(data_s1_fin)\n",
" print(col_size)\n",
" print('The final data length is '+str(len(data_s1_fin)))\n",
"\n",
" XX=[]\n",
" leng = col_size;\n",
" diff = leng-128;\n",
" for mm in range(0,len(data_s1_fin)):\n",
" if leng+(diff*mm) <= len(data_s1_fin):\n",
" x= data_s1_fin[(diff*mm):leng+(diff*mm)]\n",
" else:\n",
" break\n",
" \n",
" XX.append(x)\n",
"\n",
" XXm=np.array(XX).transpose()\n",
" print(XXm.shape)\n",
"\n",
" #Xm = np.reshape(data_s1_fin, (m,col_size), order = 'F' )\n",
" Xmw=XXm * np.hanning(len(XXm))[:,None]\n",
" t_range = [0.0, 1*tot_len]\n",
" f_range = [0.0, fs_s1 / 2.0] \n",
" Xmf = np.fft.fft(Xmw,len(Xmw),axis=0)\n",
" fig=sg_plot(t_range,f_range,Xmf[0:m//2,:])\n",
" #print(Xm.shape)\n",
" print('Hnning overlap Over')\n",
"\n",
"#Spectrogram with hanning windows\n",
"def mySpectrogram_hann(data_s1,fs_s1,m):\n",
" \n",
" zro_padd = 0;\n",
" tot_len = len(data_s1);\n",
" print('The total length is ' +str(tot_len))\n",
" print('The m is ' +str(m))\n",
"\n",
" if ((tot_len % m) != 0 ):\n",
" zro_padd = m-(tot_len % m); \n",
"\n",
" data_s1_fin = np.concatenate([data_s1, np.zeros(zro_padd)])\n",
" col_size = len(data_s1_fin)//m;\n",
"\n",
" print('The column size is ' +str(col_size))\n",
"\n",
" data_s1_fin = np.array(data_s1_fin)\n",
" print(col_size)\n",
" print('The final data length is '+str(len(data_s1_fin)))\n",
"\n",
" \n",
" Xm = np.reshape(data_s1_fin, (m,col_size), order = 'F' )\n",
" Xmw=Xm * np.hanning(len(Xm))[:,None]\n",
" t_range = [0.0, 1*tot_len]\n",
" f_range = [0.0, fs_s1 / 2.0] \n",
" Xmf = np.fft.fft(Xmw,len(Xmw),axis=0)\n",
" fig=sg_plot(t_range,f_range,Xmf[0:m//2,:])\n",
" #print(Xm.shape)\n",
" print('Hanning Over')\n",
"\n",
"#Spectrogram with rectangular windows\n",
"def mySpectrogram(data_s1,fs_s1,m):\n",
" \n",
" zro_padd = 0;\n",
" tot_len = len(data_s1);\n",
" print('The total length is ' +str(tot_len))\n",
" print('The m is ' +str(m))\n",
"\n",
" if ((tot_len % m) != 0 ):\n",
" zro_padd = m-(tot_len % m); \n",
"\n",
" data_s1_fin = np.concatenate([data_s1, np.zeros(zro_padd)])\n",
" col_size = len(data_s1_fin)//m;\n",
"\n",
" print('The column size is ' +str(col_size))\n",
"\n",
" data_s1_fin = np.array(data_s1_fin)\n",
" print(col_size)\n",
" print('The final data length is '+str(len(data_s1_fin)))\n",
"\n",
" \n",
" Xm = np.reshape(data_s1_fin, (m,col_size), order = 'F' )\n",
" Xmw=Xm;\n",
" t_range = [0.0, 1*tot_len]\n",
" f_range = [0.0, fs_s1 / 2.0] \n",
" Xmf = np.fft.fft(Xmw,len(Xmw),axis=0)\n",
" fig=sg_plot(t_range,f_range,Xmf[0:m//2,:])\n",
" #print(Xm.shape)\n",
" print('Rectangular Over')\n",
"\n",
"\n",
"# Reading the wave files\n",
"###Change the path while using from your machine\n",
"#(fs, data) = wavfile.read('/content/drive/MyDrive/python_works/spectrogram_asssign/s1.wav')\n",
"(fs, data) = wavfile.read('/content/drive/MyDrive/python_works/spectrogram_asssign/s2.wav')\n",
"#(fs, data) = wavfile.read('/content/drive/MyDrive/python_works/spectrogram_asssign/s3.wav')\n",
"#(fs, data) = wavfile.read('/content/drive/MyDrive/python_works/spectrogram_asssign/s4.wav')\n",
"#(fs, data) = wavfile.read('/content/drive/MyDrive/python_works/spectrogram_asssign/s5.wav')\n",
"\n",
"m_block = 256;\n",
"#mySpectrogram(data,fs, m_block)\n",
"#mySpectrogram_hann(data,fs, m_block)\n",
"mySpectrogram_hann_overlap(data,fs, m_block)"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"The total length is 199587\n",
"The m is 256\n",
"The column size is 780\n",
"780\n",
"The final data length is 199680\n",
"(780, 306)\n",
"Hnning overlap Over\n"
],
"name": "stdout"
},
{
"output_type": "display_data",
"data": {
"image/png":...