{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":91844,"databundleVersionId":11361821,"sourceType":"competition"}],"dockerImageVersionId":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install librosa","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-23T10:29:25.574616Z","iopub.execute_input":"2025-04-23T10:29:25.575525Z","iopub.status.idle":"2025-04-23T10:29:29.573044Z","shell.execute_reply.started":"2025-04-23T10:29:25.575492Z","shell.execute_reply":"2025-04-23T10:29:29.571794Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport glob\n\nimport numpy as np\nimport librosa\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-04-23T10:29:29.574942Z","iopub.execute_input":"2025-04-23T10:29:29.575251Z","iopub.status.idle":"2025-04-23T10:29:29.581124Z","shell.execute_reply.started":"2025-04-23T10:29:29.575226Z","shell.execute_reply":"2025-04-23T10:29:29.579855Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"train_audioの音声ファイルのパスをリスト化  \n音声ファイルのファイル数を確認","metadata":{}},{"cell_type":"code","source":"ogg_files = glob.glob(\"/kaggle/input/birdclef-2025/train_audio/**/*.ogg\", recursive=True)\nprint(f\"音声ファイル数：{len(ogg_files)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-23T10:29:29.582148Z","iopub.execute_input":"2025-04-23T10:29:29.582435Z","iopub.status.idle":"2025-04-23T10:29:45.011354Z","shell.execute_reply.started":"2025-04-23T10:29:29.582407Z","shell.execute_reply":"2025-04-23T10:29:45.010484Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"5秒以上の音声ファイルのファイル数を確認","metadata":{}},{"cell_type":"code","source":"min_duration = 5.0 # 分析対象とする最小の長さ（秒）\n\nfive_sec_plus_ogg_files = []\nfor path in tqdm(ogg_files):\n    try:\n        y, sr = librosa.load(path, sr=None)\n        duration = librosa.get_duration(y=y, sr=sr)\n        if duration >= min_duration:\n            five_sec_plus_ogg_files.append(path)\n    except Exception as e:\n        print(f\"Error loading {path}: {e}\")\nfive_sec_plus_ogg_file_ratio = len(five_sec_plus_ogg_files) / len(ogg_files) * 100\n\nprint(f\"{min_duration}秒以上の音声ファイル数: {len(five_sec_plus_ogg_files)} / {len(ogg_files)} ({five_sec_plus_ogg_file_ratio:.2f}%)\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-23T10:29:45.013442Z","iopub.execute_input":"2025-04-23T10:29:45.014025Z","iopub.status.idle":"2025-04-23T10:50:35.459290Z","shell.execute_reply.started":"2025-04-23T10:29:45.013999Z","shell.execute_reply":"2025-04-23T10:50:35.458232Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"音声ファイルごとに、中央5秒間のRMS / 全体のRMSを算出  \n(中央5秒間の音声レベルを全体の音声レベルと比較）","metadata":{}},{"cell_type":"code","source":"rms_ratios = []\n\nfor path in tqdm(five_sec_plus_ogg_files):\n    try:\n        y, sr = librosa.load(path, sr=None)\n        duration = librosa.get_duration(y=y, sr=sr)\n\n        # 中央5秒を切り出し\n        start = int((duration / 2 - 2.5) * sr)\n        end = int((duration / 2 + 2.5) * sr)\n        y_center = y[start:end]\n\n        # RMS計算\n        rms_total = librosa.feature.rms(y=y)[0]\n        avg_rms_total = np.mean(rms_total)\n\n        rms_center = librosa.feature.rms(y=y_center)[0]\n        avg_rms_center = np.mean(rms_center)\n\n        # 比率を記録\n        if avg_rms_total > 0:\n            ratio = avg_rms_center / avg_rms_total\n            rms_ratios.append(ratio)\n\n    except Exception as e:\n        print(f\"Error with {path}: {e}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-23T10:50:35.460466Z","iopub.execute_input":"2025-04-23T10:50:35.460755Z","iopub.status.idle":"2025-04-23T11:58:21.155853Z","shell.execute_reply.started":"2025-04-23T10:50:35.460708Z","shell.execute_reply":"2025-04-23T11:58:21.154171Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist(rms_ratios, bins=50)\nplt.axvline(x=1.0, color='red', linestyle='--', label=\"Center = Total\")\nplt.xlabel(\"Center RMS / Total RMS\")\nplt.ylabel(\"Number of files\")\nplt.title(\"RMS Ratio: Center 5s vs Entire Clip\")\nplt.grid(True)\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-23T11:58:21.159109Z","iopub.execute_input":"2025-04-23T11:58:21.159605Z","iopub.status.idle":"2025-04-23T11:58:21.537066Z","shell.execute_reply.started":"2025-04-23T11:58:21.159529Z","shell.execute_reply":"2025-04-23T11:58:21.536157Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ratios = np.array(rms_ratios)\nprint(f\"サンプル数: {len(ratios)}\")\nprint(f\"平均: {np.mean(ratios):.3f}\")\nprint(f\"中央値: {np.median(ratios):.3f}\")\nprint(f\"0.8未満の割合: {(np.sum(ratios < 0.8) / len(ratios)) * 100:.1f}%\")\nprint(f\"0.5未満の割合: {(np.sum(ratios < 0.5) / len(ratios)) * 100:.1f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-23T11:58:21.538288Z","iopub.execute_input":"2025-04-23T11:58:21.538613Z","iopub.status.idle":"2025-04-23T11:58:21.548877Z","shell.execute_reply.started":"2025-04-23T11:58:21.538582Z","shell.execute_reply":"2025-04-23T11:58:21.547817Z"}},"outputs":[],"execution_count":null}]}