Spaces:
Sleeping
Sleeping
Commit ·
b3240ff
1
Parent(s): 17d5203
add comment
Browse files- lab_tools/spectrogram.py +67 -0
lab_tools/spectrogram.py
CHANGED
|
@@ -378,6 +378,73 @@ def generate_spectrogram_and_signal_plot(
|
|
| 378 |
sample_rate, max_frequency, signal_column_name, start_time, end_time,
|
| 379 |
filter_type, highpass_cutoff, stopband_cutoff, passband_ripple, stopband_attenuation,
|
| 380 |
band_intensity_setting, integration_method, segment_length, overlap, fontsize):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
file_path = uploaded_file.name
|
| 383 |
file_basename = os.path.basename(file_path)
|
|
|
|
| 378 |
sample_rate, max_frequency, signal_column_name, start_time, end_time,
|
| 379 |
filter_type, highpass_cutoff, stopband_cutoff, passband_ripple, stopband_attenuation,
|
| 380 |
band_intensity_setting, integration_method, segment_length, overlap, fontsize):
|
| 381 |
+
"""
|
| 382 |
+
入力パラメータに基づいてスペクトログラムと信号プロットを生成し、結果をファイルに保存する関数。
|
| 383 |
+
|
| 384 |
+
Parameters:
|
| 385 |
+
uploaded_file (File): 信号データを含むアップロードされたファイルオブジェクト。
|
| 386 |
+
analysis_method (str): 使用する解析方法 ("Short-Time Fourier Transform" または "Continuous Wavelet Transform")。
|
| 387 |
+
sample_rate (int): 信号のサンプリングレート (Hz)。
|
| 388 |
+
max_frequency (int): 解析する最大周波数 (Hz)。
|
| 389 |
+
signal_column_name (str): アップロードされたファイル内の信号データが含まれる列の名前。
|
| 390 |
+
start_time (float): 解析を開始する時刻 (秒)。
|
| 391 |
+
end_time (float): 解析を終了する時刻 (秒)。
|
| 392 |
+
filter_type (str): 適用するフィルターの種類 ("High PASS" または "Low PASS")。
|
| 393 |
+
highpass_cutoff (float): ハイパスフィルターのカットオフ周波数 (Hz)。
|
| 394 |
+
stopband_cutoff (float): フィルターのストップバンドカットオフ周波数 (Hz)。
|
| 395 |
+
passband_ripple (float): パスバンドで許容される最大リップル (dB)。
|
| 396 |
+
stopband_attenuation (float): ストップバンドでの最小減衰量 (dB)。
|
| 397 |
+
band_intensity_setting (str): 解析対象の周波数帯域 (例: "Gamma", "Beta", "Alpha")。
|
| 398 |
+
integration_method (str): 使用する数値積分法 ("simps" または "trapz")。
|
| 399 |
+
segment_length (int): STFT用のセグメント長 (サンプル数)。
|
| 400 |
+
overlap (float): STFTセグメント間のオーバーラップ率 (0.0 ~ 1.0)。
|
| 401 |
+
fontsize (int): 生成するプロットのフォントサイズ。
|
| 402 |
+
|
| 403 |
+
Returns:
|
| 404 |
+
tuple: 以下のパスを含むタプル:
|
| 405 |
+
- str: スペクトログラムプロットの保存先パス。
|
| 406 |
+
- str: 周波数帯域強度プロットの保存先パス。
|
| 407 |
+
- str: 信号プロットの保存先パス。
|
| 408 |
+
- str: すべての結果を含むZIPファイルの保存先パス。
|
| 409 |
+
|
| 410 |
+
Raises:
|
| 411 |
+
ValueError: アップロードされたファイルが有効な信号データを含んでいない場合。
|
| 412 |
+
FileNotFoundError: 必要なファイルまたはディレクトリにアクセスできない場合。
|
| 413 |
+
Exception: 処理やプロットの保存中に問題が発生した場合。
|
| 414 |
+
|
| 415 |
+
Notes:
|
| 416 |
+
- フィルタリング、CWTまたはSTFT解析を実行し、ユーザー入力に基づいてプロットを生成します。
|
| 417 |
+
- 中間および最終的な出力は一時ディレクトリに保存され、ダウンロード用にZIP化されます。
|
| 418 |
+
- 信号処理には `labutils` および `filter` モジュールを使用します。
|
| 419 |
+
|
| 420 |
+
Example:
|
| 421 |
+
```python
|
| 422 |
+
# 使用例
|
| 423 |
+
spectrogram_path, band_intensity_path, signal_plot_path, zip_file_path = generate_spectrogram_and_signal_plot(
|
| 424 |
+
uploaded_file=my_file,
|
| 425 |
+
analysis_method="Short-Time Fourier Transform",
|
| 426 |
+
sample_rate=1000,
|
| 427 |
+
max_frequency=50,
|
| 428 |
+
signal_column_name="EEG Signal",
|
| 429 |
+
start_time=0,
|
| 430 |
+
end_time=10,
|
| 431 |
+
filter_type="High PASS",
|
| 432 |
+
highpass_cutoff=0.5,
|
| 433 |
+
stopband_cutoff=0.1,
|
| 434 |
+
passband_ripple=0.5,
|
| 435 |
+
stopband_attenuation=40,
|
| 436 |
+
band_intensity_setting="Alpha",
|
| 437 |
+
integration_method="simps",
|
| 438 |
+
segment_length=256,
|
| 439 |
+
overlap=0.5,
|
| 440 |
+
fontsize=14
|
| 441 |
+
)
|
| 442 |
+
print(f"スペクトログラムの保存先: {spectrogram_path}")
|
| 443 |
+
print(f"周波数帯域強度プロットの保存先: {band_intensity_path}")
|
| 444 |
+
print(f"信号プロットの保存先: {signal_plot_path}")
|
| 445 |
+
print(f"すべての結果が保存されたZIPファイル: {zip_file_path}")
|
| 446 |
+
```
|
| 447 |
+
"""
|
| 448 |
|
| 449 |
file_path = uploaded_file.name
|
| 450 |
file_basename = os.path.basename(file_path)
|