{"cells":[{"metadata":{},"cell_type":"markdown","source":"ライブラリーのインポート\n\n* matplotlib  image　モジュール\n* matplotlib  AnnotationBbox\n\n* librosa","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import sklearn\nimport pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport matplotlib.image as mpimg\nfrom matplotlib.offsetbox import AnnotationBbox, OffsetImage\n\n# Map 1 library\nimport plotly.express as px\n\nimport descartes\nimport geopandas as gpd\n\n#Librossa Libraries\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\n\ntrain = pd.read_csv(\"../input/birdsong-recognition/train.csv\")\ntest = pd.read_csv(\"../input/birdsong-recognition/test.csv\")\n\n#Create some time features\ntrain['year'] = train['date'].apply(lambda x: x.split('-')[0])#2013\ntrain['month'] = train['date'].apply(lambda x: x.split('-')[1])#5\ntrain['day_of_month'] = train['date'].apply(lambda x: x.split('-')[2])#25\n\nprint(\"There are {:,} unique bird species in the dataset.\".format(len(train['species'].unique())))#unique()で重複を避けて、要素の個数を返す\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ipd","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head(20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(train['type'].unique()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['type'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"データの特徴\nデータ総計 21735\ncolumn（目的変数）　35\n\nデータの欠損値","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"test.info()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> **何年のデータが多いか表示**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"bird = mpimg.imread('../input/bird-samples/orange bird.jpg')\nimagebox = OffsetImage(bird, zoom=0.5)\nxy = (0.5, 0.7)\nab = AnnotationBbox(imagebox, xy, frameon=False, pad=1, xybox=(6.5, 2000))\n\nplt.figure(figsize=(16, 6))\nax = sns.countplot(train['year'], palette=\"hls\")#ヒストグラムを作成\nax.add_artist(ab)\n\nplt.title(\"Year of the Audio Files Registration\", fontsize=16)\nplt.xticks(rotation=90, fontsize=13)\nplt.yticks(fontsize=13)\nplt.ylabel(\"Frequency\", fontsize=14)\nplt.xlabel(\"\");","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"何月のデータが多いか表示","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"bird = mpimg.imread(\"../input/birdsamples/wasi.jpeg\")\nimagebox = OffsetImage(bird,zoom=0.7)\nxy = (0.5,0.7)\nab = AnnotationBbox(imagebox,xy,frameon = False,pad=1,xybox = (11,3000))\n\nplt.figure(figsize=(16,6))\nax = sns.countplot(train['month'], palette=\"hls\")\nax.add_artist(ab)\n\nplt.title(\"Month of the Audio Files Registration\", fontsize=16)\nplt.xticks(fontsize=13)\nplt.yticks(fontsize=13)\nplt.ylabel(\"Frequency\", fontsize=14)\nplt.xlabel(\"\");","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#音程（ピッチ）の頻度を表示\nbird = mpimg.imread(\"../input/birdsamples2/white_bird.jpg\")\nimagebox = OffsetImage(bird,zoom = 0.2)\nxy = (0.5,0.7)\nab = AnnotationBbox(imagebox, xy, frameon=False, pad=1, xybox=(3.9,8600))\n\nplt.figure(figsize=(16,6))\nax = sns.countplot(train['pitch'],palette='hls',order = train['pitch'].value_counts().index)\nax.add_artist(ab)\n\nplt.title(\"Pitch(quality of sound - how high/low was the tone)\",fontsize=16)\nplt.xticks(fontsize=20)\nplt.yticks(fontsize=20)\nplt.ylabel(\"Frecuency\",fontsize=18)\nplt.xlabel(\"\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"たくさんあるcolumnのtypeを整形する。(約１２００個）","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"adjusted_type = train['type'].apply(lambda x : x.split(',')).reset_index().explode('type')#reste_indexインデックスを振り直す\nadjusted_type = adjusted_type['type'].apply(lambda x : x.strip().lower()).reset_index()#stirp()空白文字・指定した文字を削除 / lower()すべての文字を小文字に変換\nadjusted_type['type'] = adjusted_type['type'].replace('calls', 'call')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"top_15 = list(adjusted_type['type'].value_counts().head(15).reset_index()['index'])\ndata = adjusted_type[adjusted_type['type'].isin(top_15)] #isin()は列の要素が引数に渡したリストの要素に含まれているかを選び抽出","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n#表示\nbird = mpimg.imread('../input/sample3/blue_bird.jpg')\nimagebox = OffsetImage(bird, zoom=0.43)\nxy = (0.5, 0.7)\nab = AnnotationBbox(imagebox, xy, frameon=False, pad=1, xybox=(12.4, 5700))\n\nplt.figure(figsize=(16, 6))\nax = sns.countplot(data['type'], palette=\"hls\", order = data['type'].value_counts().index)\nax.add_artist(ab)\n\nplt.title(\"Top 15 Song Types\", fontsize=16)\nplt.ylabel(\"Frequency\", fontsize=14)\nplt.yticks(fontsize=13)\nplt.xticks(rotation=45, fontsize=13)\nplt.xlabel(\"\");","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"鳥がどこに住んでいるか調べて表示","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"top_15 = list(train['elevation'].value_counts().head(15).reset_index()['index'])\ndata = train[train['elevation'].isin(top_15)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#plot\nbird = mpimg.imread('../input/sample3/blue_bird.jpg')\nimagebox = OffsetImage(bird, zoom=0.43)\nxy = (0.5, 0.7)\nab = AnnotationBbox(imagebox, xy, frameon=False, pad=1, xybox=(12.4, 1450))\n\nplt.figure(figsize=(16, 6))\nax = sns.countplot(data['elevation'], palette=\"hls\", order = data['elevation'].value_counts().index)\nax.add_artist(ab)\n\nplt.title(\"Top 15 Elevation Types\", fontsize=16)\nplt.ylabel(\"Frequency\", fontsize=14)\nplt.yticks(fontsize=13)\nplt.xticks(rotation=45, fontsize=13)\nplt.xlabel(\"\");","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 鳥の姿が見られた中での音声かどうか？","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train[train.bird_seen == \"no\"].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Cretate data\ndata = train['bird_seen'].value_counts().reset_index()\ndata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Plot\nbird = mpimg.imread('../input/sample4/cute_o_bird.jpg')\nimagebox = OffsetImage(bird, zoom=0.22)\nxy = (0.5, 0.7)\nab = AnnotationBbox(imagebox, xy, frameon=False, pad=1, xybox=(15300, 0.95))\n\nplt.figure(figsize=(16,6))\nax = sns.barplot(x = 'bird_seen' ,y = 'index' ,data = data ,palette=\"hls\")\nax.add_artist(ab)\nplt.title(\"Song was heard,but was Bird Seen?\",fontsize=16)\nplt.xlabel(\"\")\nplt.ylabel(\"Frecuency\",fontsize=16)\nplt.xticks( rotation= 45,fontsize=16)\nplt.yticks()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# top 15 countries with most recordings","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train.country","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#top 15 most common elevations\ntop_15 = list(train['country'].value_counts().head(15).reset_index()['index'])\ndata = train[train['country'].isin(top_15)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#PLOT\nbird = mpimg.imread(\"../input/example5/chick.jpg\")\nimagebox = OffsetImage(bird, zoom=0.3)\nxy = (0.5, 0.7)\nab = AnnotationBbox(imagebox, xy, frameon=False, pad=1, xybox=(12.2, 7000))\n\n#plot\nplt.figure(figsize=(16, 6))\nax = sns.countplot(data['country'], palette='hls', order = data['country'].value_counts().index)\nax.add_artist(ab)\n\nplt.title(\"Top 15 Countries with most Recordings\", fontsize=16)\nplt.ylabel(\"Frequency\", fontsize=14)\nplt.yticks(fontsize=13)\nplt.xticks(rotation=45, fontsize=13)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# MAP VIEW","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Import gapminde data, where we hace counrty and iso ALPHA cpdes\n#df = px.data.gapminder().query(\"year==2007\")[[\"country\",\"iso_alpha\"]]\n# Merge table together\n#data = pd.merge(left= train , right=df,how = 'inner',on=\"country\")\n# Group by county adn count how many spicies can be found in each\n#data = data.groupby(by=[\"country\",\"iso_alpha\"]).count()[\"species\"].reset_index()\n#fig = px.choropleth( data , locations=\"iso_alpha\", color = \"species\" , hover_name = 'country',#\n                    #color_continuous_scale = px.colors.sequential.Teal,title = \"World Map:Recordings per Country\")\n#fig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# AUDIO FILE 🔉","execution_count":null},{"metadata":{},"cell_type":"markdown","source":" Description¶\n1. **train_audio**: short recording (majority in mp3 format) of INDIVIDUAL birds.\n2. **test_audio**: recordings took in 3 locations:\n\nSite 1 and Site 2: recordings 10 mins long (mp3) that have labeled a bird every 5 seconds. This is meant to mimic the real life scenario, when you would usually have more than 1 bird (or no bird) singing.\nSite 3: recordings labeled at file level (because it is especially hard to have coders trained to label these kind of files)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train.duration.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#データの準備 \ntrain[\"duration_interval\"] = \">500\"\ntrain.loc[train[\"duration\"] <= 100,'duration_intercal'] = \"<= 100\"\ntrain.loc[(train['duration'] > 100) & (train['duration'] <= 200),'duration_interval'] ='100~200'\ntrain.loc[(train['duration'] > 200) & (train['duration'] <= 300),'duration_interval'] ='200~300'\ntrain.loc[(train['duration'] > 300) & (train['duration'] <= 400),'duration_interval'] ='300~400'\ntrain.loc[(train['duration'] > 400) & (train['duration'] <= 500),'duration_interval'] ='400~500'\n\n#データを読み込み\nbird = mpimg.imread('../input/sample5/samplebird.jpg')\nimagebox = OffsetImage(bird, zoom=0.2)\nxy = (0.5, 0.7)\nab = AnnotationBbox(imagebox, xy, frameon=False, pad=1, xybox=(3, 10000))\n#図を表示\nplt.figure(figsize=(16,6))\nax = sns.countplot(train['duration_interval'],palette ='hls')\nax.add_artist(ab)\n\n# 図を調整\nplt.title(\"Distribution of Recordings Duration\",fontsize=16)\nplt.ylabel(\"Distribution of Recording Duration\")\nplt.xlabel(\"Freucuency\")\nplt.yticks(fontsize=13)\nplt.xticks(rotation=45,fontsize=13)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 音声データの種類を表示","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train[\"file_type\"].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bird = mpimg.imread('../input/sample6/superbird.jpg')\nimagebox = OffsetImage(bird, zoom=0.1)\nxy = (0.5, 0.5)\nab = AnnotationBbox(imagebox, xy, frameon=False, pad=1, xybox=(2.7, 7000))\n\nplt.figure(figsize=(16,6))\nax = sns.countplot(train['file_type'],palette = 'hls', order = train[\"file_type\"].value_counts().index)\nax.add_artist(ab)\n\nplt.title(\"Recording File Types\", fontsize=16)\nplt.ylabel(\"Frequency\", fontsize=14)\nplt.yticks(fontsize=13)\nplt.xticks(rotation=45, fontsize=13)\nplt.xlabel(\"\");","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 音声を聞く","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"base_dir = \"../input/birdsong-recognition/train_audio/\"\n\ntrain[\"full_path\"] =  base_dir + train['ebird_code'] + '/' + train['filename'] #音声ファイルのパスに繋がるカラムを作成\n\n# 音声のサンプルを抽出する\namered = train[train['ebird_code'] == \"amered\"].sample(1,random_state = 33)['full_path'].values[0]#sample ランダムで１つ抽出\ncangoo = train[train['ebird_code'] == \"cangoo\"].sample(1,random_state = 33)['full_path'].values[0]\nhaiwoo = train[train['ebird_code'] == \"haiwoo\"].sample(1,random_state = 33)['full_path'].values[0]\npingro = train[train['ebird_code'] == \"pingro\"].sample(1,random_state = 33)['full_path'].values[0]\nvesspa = train[train['ebird_code'] == \"vesspa\"].sample(1,random_state = 33)['full_path'].values[0]\n\n\nbird_sample_list = [\"amered\",\"cangoo\",\"haiwoo\",\"pingro\",\"vesspa\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ipd.Audio(amered)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ipd.Audio(cangoo)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ipd.Audio(haiwoo)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ipd.Audio(pingro)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ipd.Audio(vesspa)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* 音声の特徴量を抽出する\n* 前提　音のデータを取り込むことをサンプリング(標本化)という\n* \n1. 音（Sound)とは　「変化する空気圧力の内部の振動の連続」\n2. サンプルレート(Sample Rate)　どれくらいの細かさで音を取り込むのかを示した歩合　数値が高いほど細かく取り込む\n（例　サンプリングレート48kHz　1秒間に48000回に分割してデータを取る）","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Imprting 1 file\ny,sr = librosa.load(vesspa)\n\nprint('y:',y)\nprint('y shape:',np.shape(y))\nprint('Sample Rate(KHz):',sr)\n\n#Vertify length of the audio\nprint(\"Check Len of Audio:\",661794/sr)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#音を整形する\n# yの音の無音部分を切り取る（trim) 　　　　　　　※　_ その変数を使ってませんという表示\naudio_file,_ = librosa.effects.trim(y)\n\n#結果を出す\nprint('Audio File:', audio_file )\nprint('Audio File shape:',np.shape(audio_file))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Importing the 5 files\ny_amered,sr_amered = librosa.load(amered)\naudio_amered,_  = librosa.effects.trim(y_amered)\n\ny_cangoo,sr_cangoo = librosa.load(cangoo)\naudio_cangoo,_  = librosa.effects.trim(y_cangoo)\n\ny_haiwoo,sr_haiwoo = librosa.load(haiwoo)\naudio_haiwoo,_  = librosa.effects.trim(y_haiwoo)\n\ny_pingro,sr_pingro = librosa.load(pingro)\naudio_pingro,_  = librosa.effects.trim(y_pingro)\n\ny_vesspa,sr_vesspa = librosa.load(vesspa)\naudio_vesspa,_  = librosa.effects.trim(y_vesspa)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Sound Waves🌊 (2D Representation)","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"*  音声データの長さはデータにより異なる\n1. 固定長の短時間波形に切り出した上で処理を行う\n2. 短時間波形に対し離散フーリエ変換を適用することで周波数分析を行う","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"fig,ax = plt.subplots(5,figsize = (16,9))\nfig.suptitle(\"Sound Waves\",fontsize = 16)\n\nlibrosa.display.waveplot( y = audio_amered, sr = sr_amered, color = \"#A300F9\", ax = ax[0])\nlibrosa.display.waveplot( y = audio_cangoo, sr = sr_cangoo, color = \"#4300FF\", ax = ax[1])\nlibrosa.display.waveplot( y = audio_haiwoo, sr = sr_haiwoo, color = \"#009DFF\", ax = ax[2])\nlibrosa.display.waveplot( y = audio_pingro, sr = sr_pingro, color = \"#00FFB0\", ax = ax[3])\nlibrosa.display.waveplot( y = audio_vesspa, sr = sr_vesspa, color = \"#D9FF00\", ax = ax[4])\n\nfor i, name in zip(range(5),bird_sample_list):\n    ax[i].set_ylabel(name, fontsize =13)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# フーリエ変換♫","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# FFT(高速フーリエ変換)の画面サイズ FFT 技術を使うと、時間信号をリアルタイムに周波数分析が可能となる。\n# STFT(短時間フーリエ変換)は、統計的特性が時間により変化する非定常信号の分析に使われる信号処理手法。\n# オーディオ CD 上のデータは、複数のフレームに分割されています。\nn_fft = 2048 # フレームのサイズ\nhop_length = 512 #audio_frameの数\n\n#Short_time Fourier transfrom(STFT) フ-リエ変換！！\nD_amered = np.abs(librosa.stft(audio_amered , n_fft = n_fft, hop_length = hop_length))\nD_cangoo = np.abs(librosa.stft(audio_cangoo , n_fft = n_fft, hop_length = hop_length))\nD_haiwoo = np.abs(librosa.stft(audio_haiwoo , n_fft = n_fft, hop_length = hop_length))\nD_pingro = np.abs(librosa.stft(audio_pingro , n_fft = n_fft, hop_length = hop_length))\nD_vesspa = np.abs(librosa.stft(audio_vesspa , n_fft = n_fft, hop_length = hop_length))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Shape of D object:\", np.shape(D_amered))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"周波数（スペクトラム）を表示する","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# スペクトラムの振幅をデシベルのスケールに調整する。\nDB_amered = librosa.amplitude_to_db(D_amered, ref = np.max)\nDB_cangoo = librosa.amplitude_to_db(D_cangoo, ref = np.max)\nDB_haiwoo = librosa.amplitude_to_db(D_haiwoo, ref = np.max)\nDB_pingro = librosa.amplitude_to_db(D_pingro, ref = np.max)\nDB_vesspa = librosa.amplitude_to_db(D_vesspa, ref = np.max)\n\n# PLOT\nfig,ax = plt.subplots(2,3,figsize=(16,9))\nfig.suptitle(\"Spectrogram\", fontsize = 16)\nfig.delaxes(ax[1,2])# del + axes  axesを取り除く\n\nlibrosa.display.specshow(DB_amered, sr = sr_amered, hop_length = hop_length, x_axis = 'time', y_axis = 'log', cmap = 'cool',ax = ax[0,0])\nlibrosa.display.specshow(DB_cangoo, sr = sr_cangoo, hop_length = hop_length, x_axis = 'time', y_axis = 'log', cmap = 'cool',ax = ax[0,1])\nlibrosa.display.specshow(DB_haiwoo, sr = sr_haiwoo, hop_length = hop_length, x_axis = 'time', y_axis = 'log', cmap = 'cool',ax = ax[0,2])\nlibrosa.display.specshow(DB_pingro, sr = sr_pingro, hop_length = hop_length, x_axis = 'time', y_axis = 'log', cmap = 'cool',ax = ax[1,0])\nlibrosa.display.specshow(DB_vesspa, sr = sr_vesspa, hop_length = hop_length, x_axis = 'time', y_axis = 'log', cmap = 'cool',ax = ax[1,1])\n\nfor i, name in zip(range(0,2*3), bird_sample_list):\n    x = i // 3\n    y = i % 3\n    ax[x,y].set_title(name,fontsize=13)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"メル周波数（人間の耳に近い）に変換する","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"bird_sample_list","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create the Mel Spectograms\nS_amered = librosa.feature.melspectrogram(y_amered, sr = sr_amered)\nS_DB_amered = librosa.amplitude_to_db(S_amered, ref = np.max)\n\nS_cangoo = librosa.feature.melspectrogram(y_cangoo, sr = sr_cangoo)\nS_DB_cangoo= librosa.amplitude_to_db(S_cangoo, ref = np.max)\n\nS_haiwoo = librosa.feature.melspectrogram(y_haiwoo, sr = sr_haiwoo)\nS_DB_haiwoo = librosa.amplitude_to_db(S_haiwoo, ref = np.max)\n\nS_pingro = librosa.feature.melspectrogram(y_pingro, sr = sr_pingro)\nS_DB_pingro = librosa.amplitude_to_db(S_pingro, ref = np.max)\n\nS_vesspa = librosa.feature.melspectrogram(y_vesspa, sr = sr_vesspa)\nS_DB_vesspa = librosa.amplitude_to_db(S_amered, ref = np.max)\n\n# PLOT\nfig ,ax = plt.subplots(2,3,figsize = (16,9))\nfig.suptitle(\"Mel Spectogram\",fontsize = 16 )\nfig.delaxes(ax[1,2])\n\nlibrosa.display.specshow(S_DB_amered , sr = sr_amered , hop_length = hop_length , x_axis = 'time', y_axis = 'log' , cmap = 'rainbow',ax = ax[0,0])\nlibrosa.display.specshow(S_DB_cangoo , sr = sr_cangoo , hop_length = hop_length , x_axis = 'time', y_axis = 'log' , cmap = 'rainbow',ax = ax[0,1])\nlibrosa.display.specshow(S_DB_haiwoo , sr = sr_haiwoo , hop_length = hop_length , x_axis = 'time', y_axis = 'log' , cmap = 'rainbow',ax = ax[0,2])\nlibrosa.display.specshow(S_DB_pingro , sr = sr_pingro , hop_length = hop_length , x_axis = 'time', y_axis = 'log' , cmap = 'rainbow',ax = ax[1,0])\nlibrosa.display.specshow(S_DB_vesspa , sr = sr_vesspa , hop_length = hop_length , x_axis = 'time', y_axis = 'log' , cmap = 'rainbow',ax = ax[1,1])\n\n\n# titleをつける\nfor i,name in zip(range(0,2*3), bird_sample_list):\n    x = i // 3\n    y = i % 3\n    ax[x,y].set_title(name,fontsize=13)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Zero Crossing Rate\n* 信号が正の値と負の値をどれだけ入れ替わっているか。","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# + α　Zero crossing Rate　を実装(参考)\ndef zcr(data):\n    count = 0\n    for i in range(len(data)-1):\n        if fata[i]*data[i+1] < 0:\n            count += 1\n    zcr = count/(len(data))\n    return zcr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Total Zero_crossing in our 1song\nzero_amered = librosa.zero_crossings(audio_amered, pad = False)\nzero_cangoo = librosa.zero_crossings(audio_cangoo, pad = False)\nzero_haiwoo = librosa.zero_crossings(audio_haiwoo, pad = False)\nzero_pingro = librosa.zero_crossings(audio_pingro, pad = False)\nzero_vesspa = librosa.zero_crossings(audio_vesspa, pad = False)\n\nzero_birds_list = [zero_amered,zero_cangoo,zero_haiwoo,zero_pingro,zero_vesspa]\n\nfor bird,name in zip(zero_birds_list,bird_sample_list):\n    print(\"{} change rate is {:,}\".format(name,sum(bird)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"zero_amered","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* * Harmonics and Perceptual\n* ハーモニー部分(音色）とパーカッション部分（リズム・曲調）の分離","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"y_harm_haiwoo, y_perc_haiwoo = librosa.effects.hpss(audio_haiwoo)\n\nplt.figure(figsize = (16,9))\nplt.plot(y_perc_haiwoo,color=\"#FFB100\")\nplt.plot(y_harm_haiwoo,color = \"#A300F9\")\nplt.legend((\"Perceptrual\",\"Harmonis\"))\nplt.title(\"Harmonics and Perceptrual : Haiwoo Bird\",fontsize=16)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"音の中心を示す　Spectral Centroid(スペクトルの重心がどこにあるかを示す  知覚的には、音の明るさの印象との強いつながりがある)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"spectral_centroids = librosa.feature.spectral_centroid(audio_cangoo, sr = sr)[0]\n\nprint('Centroids:',spectral_centroids)\nprint(\"Shape of Spectral Centoids:\",spectral_centroids.shape)\n\n# 時間の変化を表す\nframes = range(len(spectral_centroids))\n\n#frame count を time(second) に変換する\nt = librosa.frames_to_time(frames)\n\nprint(\"frames:\",frames)\nprint(\"t:\",t)\n\n# 音のデータを標準化する関数\ndef normalize( x, axis=0):\n    return sklearn.preprocessing.minmax_scale(x,axis = axis)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# spectral centroidの音波を図示する\nplt.figure(figsize=(16,9))\nlibrosa.display.waveplot(audio_cangoo, sr = sr ,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: Cangoo Bird\",fontsize = 16)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Chroma Frecuencies(彩度の頻度図） 12種類の音の特徴を表す\n* １つ高い「ド」までの音程が１オクターブである。 ドレミファソラシドと数えると８番目の音階（８度上の音階）→oct（８） という接頭辞を持つ単語として octave と呼ばれるようになった。\n* 半音単位に換算すると、黒鍵５個＋白鍵７個（ドレミファソラシ）＝半音１２個分である","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"hop_length = 5000\n\n#chromagram vesspa\nchromagram = librosa.feature.chroma_stft(audio_vesspa, sr = sr_vesspa, hop_length = hop_length)\nprint(\"Chromagram Vesspa shape:\",chromagram.shape)\n\nplt.figure(figsize=(16,6))\nlibrosa.display.specshow(chromagram, x_axis = 'time', y_axis = \"chroma\" ,hop_length = hop_length ,cmap = 'twilight')\nplt.title(\"Chromagram:Vesspa\",fontsize=16)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"chromagram","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Tempo BPM(beats per minute)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tempo_amered,_ = librosa.beat.beat_track(y_amered, sr = sr_amered)\ntempo_cangoo,_ = librosa.beat.beat_track(y_cangoo, sr = sr_cangoo)\ntempo_haiwoo,_ = librosa.beat.beat_track(y_haiwoo, sr = sr_haiwoo)\ntempo_pingro,_ = librosa.beat.beat_track(y_pingro, sr = sr_pingro)\ntempo_vesspa,_ = librosa.beat.beat_track(y_vesspa, sr = sr_vesspa)\n\ndata = pd.DataFrame({\"Type\":bird_sample_list,\"BPM\":[tempo_amered,tempo_cangoo,tempo_haiwoo,tempo_pingro,tempo_vesspa]})\ndata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bird = mpimg.imread(\"../input/sample8/two_bird.jpg\")\nimagebox = OffsetImage(bird,zoom = 0.2)\nxy = (0.5,0.7)\nab = AnnotationBbox(imagebox , xy , frameon = False ,pad = 1 , xybox=(0.3,158))\n\nplt.figure(figsize = (16,6))\nax = sns.barplot(y = data[\"BPM\"] , x = data[\"Type\"] , palette = \"hls\")\nax.add_artist(ab)\n\nplt.ylabel(\"BPM\",)\nplt.yticks(fontsize=13)\nplt.xticks(fontsize=13)\nplt.xlabel(\"\")\nplt.title(\"BPM for 5 Different Bird Species\",fontsize=16)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Spectral RollOff Vector Rolooff\nspectral_rolloff = librosa.feature.spectral_rolloff(audio_amered, sr=sr_amered)[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.waveplot(audio_amered, sr=sr_amered, 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: Amered Bird\", fontsize=16);","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}