Spaces:
Sleeping
Sleeping
Commit ·
36e3e71
1
Parent(s): 4650c5f
fix for upddate
Browse files- lab_tools/spectrogram.py +55 -0
lab_tools/spectrogram.py
CHANGED
|
@@ -54,6 +54,7 @@ def plot_cwt_result(cwt_matrix, time_array, max_frequency, fontsize=12):
|
|
| 54 |
plt.colorbar().set_label("Power", fontsize=fontsize)
|
| 55 |
plt.clim(0, 5)
|
| 56 |
plt.gca().invert_yaxis()
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
def perform_stft(signal_data, sample_rate: int, segment_length: int, overlap=0.5):
|
|
@@ -95,6 +96,8 @@ def plot_stft_spectrogram(amplitude, frequencies, times, max_frequency=None, fon
|
|
| 95 |
if max_frequency:
|
| 96 |
ax.set_ylim([0, max_frequency])
|
| 97 |
|
|
|
|
|
|
|
| 98 |
|
| 99 |
def plot_signal(signal_data, time_data, start_time, end_time, fontsize, window_size=50):
|
| 100 |
smoothed_signal = np.convolve(signal_data, np.ones(window_size) / window_size, mode='valid')
|
|
@@ -104,6 +107,7 @@ def plot_signal(signal_data, time_data, start_time, end_time, fontsize, window_s
|
|
| 104 |
plt.xlim(start_time, end_time)
|
| 105 |
plt.xlabel("Time [sec]", fontsize=fontsize)
|
| 106 |
plt.ylabel("Voltage [uV]", fontsize=fontsize)
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
def calculate_frequency_band_intensity(
|
|
@@ -162,6 +166,7 @@ def plot_frequency_band_intensity(
|
|
| 162 |
plt.xlabel("Time [sec]", fontsize=fontsize)
|
| 163 |
plt.ylabel("Integrated Power", fontsize=fontsize)
|
| 164 |
plt.grid()
|
|
|
|
| 165 |
|
| 166 |
|
| 167 |
def save_all_data_to_csv(
|
|
@@ -199,6 +204,50 @@ def save_all_data_to_csv(
|
|
| 199 |
df.to_csv(output_path, index=False)
|
| 200 |
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
def export_arguments_to_yaml(
|
| 203 |
uploaded_file, analysis_method, sample_rate, max_frequency,
|
| 204 |
signal_column_name, start_time, end_time, filter_type,
|
|
@@ -335,6 +384,9 @@ def generate_spectrogram_and_signal_plot(
|
|
| 335 |
csv_path = os.path.join(output_dir, "band_intensity_data.csv")
|
| 336 |
save_all_data_to_csv(times, frequencies, amplitude, csv_path, method="STFT", integration_method=integration_method)
|
| 337 |
|
|
|
|
|
|
|
|
|
|
| 338 |
else:
|
| 339 |
spectrogram_plot_path = os.path.join(output_dir, "wavelet_spectrogram_plot.png")
|
| 340 |
cwt_matrix = perform_cwt(sample_rate, signal_data, max_frequency)
|
|
@@ -351,6 +403,9 @@ def generate_spectrogram_and_signal_plot(
|
|
| 351 |
csv_path = os.path.join(output_dir, "band_intensity_data.csv")
|
| 352 |
save_all_data_to_csv(time_array, np.arange(max_frequency), cwt_matrix, csv_path, method="CWT", integration_method=integration_method)
|
| 353 |
|
|
|
|
|
|
|
|
|
|
| 354 |
config_yaml_path = os.path.join(output_dir, "lab_tool_spectrogram.yaml")
|
| 355 |
|
| 356 |
export_arguments_to_yaml(
|
|
|
|
| 54 |
plt.colorbar().set_label("Power", fontsize=fontsize)
|
| 55 |
plt.clim(0, 5)
|
| 56 |
plt.gca().invert_yaxis()
|
| 57 |
+
plt.tick_params(axis='both', which='major', labelsize=fontsize)
|
| 58 |
|
| 59 |
|
| 60 |
def perform_stft(signal_data, sample_rate: int, segment_length: int, overlap=0.5):
|
|
|
|
| 96 |
if max_frequency:
|
| 97 |
ax.set_ylim([0, max_frequency])
|
| 98 |
|
| 99 |
+
ax.tick_params(axis='both', which='major', labelsize=fontsize)
|
| 100 |
+
|
| 101 |
|
| 102 |
def plot_signal(signal_data, time_data, start_time, end_time, fontsize, window_size=50):
|
| 103 |
smoothed_signal = np.convolve(signal_data, np.ones(window_size) / window_size, mode='valid')
|
|
|
|
| 107 |
plt.xlim(start_time, end_time)
|
| 108 |
plt.xlabel("Time [sec]", fontsize=fontsize)
|
| 109 |
plt.ylabel("Voltage [uV]", fontsize=fontsize)
|
| 110 |
+
plt.tick_params(axis='both', which='major', labelsize=fontsize)
|
| 111 |
|
| 112 |
|
| 113 |
def calculate_frequency_band_intensity(
|
|
|
|
| 166 |
plt.xlabel("Time [sec]", fontsize=fontsize)
|
| 167 |
plt.ylabel("Integrated Power", fontsize=fontsize)
|
| 168 |
plt.grid()
|
| 169 |
+
plt.tick_params(axis='both', which='major', labelsize=fontsize)
|
| 170 |
|
| 171 |
|
| 172 |
def save_all_data_to_csv(
|
|
|
|
| 204 |
df.to_csv(output_path, index=False)
|
| 205 |
|
| 206 |
|
| 207 |
+
def save_stft_to_csv(frequencies, times, amplitude, output_path):
|
| 208 |
+
"""
|
| 209 |
+
STFTの結果(frequencies, times, amplitude)をCSVに書き出す関数
|
| 210 |
+
|
| 211 |
+
Parameters:
|
| 212 |
+
frequencies (np.ndarray): 周波数データ
|
| 213 |
+
times (np.ndarray): 時間データ
|
| 214 |
+
amplitude (np.ndarray): 振幅データ
|
| 215 |
+
output_path (str): 保存先のCSVファイルパス
|
| 216 |
+
"""
|
| 217 |
+
# 周波数データを列名として使用
|
| 218 |
+
column_names = [f"Freq_{freq:.2f}Hz" for freq in frequencies]
|
| 219 |
+
|
| 220 |
+
# 振幅データをDataFrameに変換
|
| 221 |
+
df_amplitude = pd.DataFrame(amplitude.T, columns=column_names)
|
| 222 |
+
df_amplitude.insert(0, "Time [s]", times) # 時間データを先頭列に挿入
|
| 223 |
+
|
| 224 |
+
# CSVファイルに書き出し
|
| 225 |
+
df_amplitude.to_csv(output_path, index=False)
|
| 226 |
+
print(f"STFT結果をCSVファイルとして保存しました: {output_path}")
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def save_cwt_to_csv(time_array, frequency_array, cwt_matrix, output_path):
|
| 230 |
+
"""
|
| 231 |
+
CWTの結果(time_array, frequency_array, cwt_matrix)をCSVに書き出す関数
|
| 232 |
+
|
| 233 |
+
Parameters:
|
| 234 |
+
time_array (np.ndarray): 時間配列
|
| 235 |
+
frequency_array (np.ndarray): 周波数配列
|
| 236 |
+
cwt_matrix (np.ndarray): CWTの振幅データ (時間×周波数)
|
| 237 |
+
output_path (str): 保存先のCSVファイルパス
|
| 238 |
+
"""
|
| 239 |
+
# 周波数データを列名として使用
|
| 240 |
+
column_names = [f"Freq_{freq:.2f}Hz" for freq in frequency_array]
|
| 241 |
+
|
| 242 |
+
# 振幅データをDataFrameに変換
|
| 243 |
+
df_cwt = pd.DataFrame(cwt_matrix.T, columns=column_names)
|
| 244 |
+
df_cwt.insert(0, "Time [s]", time_array) # 時間データを先頭列に挿入
|
| 245 |
+
|
| 246 |
+
# CSVファイルに書き出し
|
| 247 |
+
df_cwt.to_csv(output_path, index=False)
|
| 248 |
+
print(f"CWT結果をCSVファイルとして保存しました: {output_path}")
|
| 249 |
+
|
| 250 |
+
|
| 251 |
def export_arguments_to_yaml(
|
| 252 |
uploaded_file, analysis_method, sample_rate, max_frequency,
|
| 253 |
signal_column_name, start_time, end_time, filter_type,
|
|
|
|
| 384 |
csv_path = os.path.join(output_dir, "band_intensity_data.csv")
|
| 385 |
save_all_data_to_csv(times, frequencies, amplitude, csv_path, method="STFT", integration_method=integration_method)
|
| 386 |
|
| 387 |
+
spectrogram_raw_data_path = os.path.join(output_dir, "spectrogram_raw_data.csv")
|
| 388 |
+
save_stft_to_csv(frequencies, times, amplitude, spectrogram_raw_data_path)
|
| 389 |
+
|
| 390 |
else:
|
| 391 |
spectrogram_plot_path = os.path.join(output_dir, "wavelet_spectrogram_plot.png")
|
| 392 |
cwt_matrix = perform_cwt(sample_rate, signal_data, max_frequency)
|
|
|
|
| 403 |
csv_path = os.path.join(output_dir, "band_intensity_data.csv")
|
| 404 |
save_all_data_to_csv(time_array, np.arange(max_frequency), cwt_matrix, csv_path, method="CWT", integration_method=integration_method)
|
| 405 |
|
| 406 |
+
spectrogram_raw_data_path = os.path.join(output_dir, "spectrogram_raw_data.csv")
|
| 407 |
+
save_cwt_to_csv(time_array, np.arange(max_frequency), cwt_matrix, spectrogram_raw_data_path)
|
| 408 |
+
|
| 409 |
config_yaml_path = os.path.join(output_dir, "lab_tool_spectrogram.yaml")
|
| 410 |
|
| 411 |
export_arguments_to_yaml(
|