Spaces:
Sleeping
Sleeping
Commit ·
9cb91de
1
Parent(s): a0d16e8
update_fft
Browse files- lab_tools/spectrogram.py +24 -5
lab_tools/spectrogram.py
CHANGED
|
@@ -51,18 +51,37 @@ def plot_cwt_result(cwt_matrix, time_array, max_frequency):
|
|
| 51 |
|
| 52 |
|
| 53 |
# 短時間フーリエ変換 (STFT) のスペクトログラムをプロットする関数
|
| 54 |
-
def plot_stft_spectrogram(signal_data, sample_rate, segment_length, max_frequency=None):
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
amplitude = np.abs(stft_result)
|
| 57 |
-
amplitude[amplitude == 0] = np.finfo(float).eps
|
|
|
|
|
|
|
| 58 |
fig, ax = plt.subplots(figsize=(12, 6))
|
| 59 |
spectrogram = ax.pcolormesh(times, frequencies, amplitude, shading="auto", vmin=0, vmax=5)
|
| 60 |
fig.colorbar(spectrogram, ax=ax, orientation="vertical").set_label("Amplitude")
|
| 61 |
ax.set_xlabel("Time [s]")
|
| 62 |
ax.set_ylabel("Frequency [Hz]")
|
|
|
|
|
|
|
| 63 |
if max_frequency:
|
| 64 |
ax.set_ylim([0, max_frequency])
|
| 65 |
-
plt.show()
|
| 66 |
|
| 67 |
|
| 68 |
# 信号を正規化する関数
|
|
@@ -118,7 +137,7 @@ def generate_spectrogram_and_signal_plot(
|
|
| 118 |
# スペクトログラムプロットの保存
|
| 119 |
if analysis_method == "Short-Time Fourier Transform":
|
| 120 |
plt.figure(dpi=200)
|
| 121 |
-
plot_stft_spectrogram(signal_data, sample_rate, segment_length=
|
| 122 |
spectrogram_plot_path = os.path.join(output_dir, "stft_spectrogram_plot.png")
|
| 123 |
plt.savefig(spectrogram_plot_path)
|
| 124 |
else:
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
# 短時間フーリエ変換 (STFT) のスペクトログラムをプロットする関数
|
| 54 |
+
def plot_stft_spectrogram(signal_data, sample_rate, segment_length, overlap=0.5, max_frequency=None):
|
| 55 |
+
"""
|
| 56 |
+
短時間フーリエ変換(STFT)スペクトログラムをプロットします。
|
| 57 |
+
|
| 58 |
+
Parameters:
|
| 59 |
+
signal_data (np.ndarray): 信号データ
|
| 60 |
+
sample_rate (int): サンプリングレート
|
| 61 |
+
segment_length (int): セグメント長(nperseg)
|
| 62 |
+
overlap (float): セグメントのオーバーラップ率 (0.0~1.0)
|
| 63 |
+
max_frequency (float, optional): 表示する最大周波数
|
| 64 |
+
"""
|
| 65 |
+
# オーバーラップするサンプル数を計算
|
| 66 |
+
noverlap = int(segment_length * overlap)
|
| 67 |
+
|
| 68 |
+
# STFTの計算
|
| 69 |
+
frequencies, times, stft_result = signal.stft(
|
| 70 |
+
signal_data, fs=sample_rate, window='hann', nperseg=segment_length, noverlap=noverlap
|
| 71 |
+
)
|
| 72 |
amplitude = np.abs(stft_result)
|
| 73 |
+
amplitude[amplitude == 0] = np.finfo(float).eps # 振幅がゼロの箇所を微小値に置き換え
|
| 74 |
+
|
| 75 |
+
# プロット
|
| 76 |
fig, ax = plt.subplots(figsize=(12, 6))
|
| 77 |
spectrogram = ax.pcolormesh(times, frequencies, amplitude, shading="auto", vmin=0, vmax=5)
|
| 78 |
fig.colorbar(spectrogram, ax=ax, orientation="vertical").set_label("Amplitude")
|
| 79 |
ax.set_xlabel("Time [s]")
|
| 80 |
ax.set_ylabel("Frequency [Hz]")
|
| 81 |
+
|
| 82 |
+
# 最大周波数の設定
|
| 83 |
if max_frequency:
|
| 84 |
ax.set_ylim([0, max_frequency])
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
# 信号を正規化する関数
|
|
|
|
| 137 |
# スペクトログラムプロットの保存
|
| 138 |
if analysis_method == "Short-Time Fourier Transform":
|
| 139 |
plt.figure(dpi=200)
|
| 140 |
+
plot_stft_spectrogram(signal_data, sample_rate=sample_rate, segment_length=4096, overlap=0.99, max_frequency=max_frequency)
|
| 141 |
spectrogram_plot_path = os.path.join(output_dir, "stft_spectrogram_plot.png")
|
| 142 |
plt.savefig(spectrogram_plot_path)
|
| 143 |
else:
|