| import numpy as np |
| import pandas as pd |
| import os |
| import matplotlib.pyplot as plt |
|
|
| print("๐ INITIATING FFT TRANSFORM (TIME -> FREQUENCY DOMAIN)...") |
|
|
| |
| possible_paths = ['vG.0.1/real_tokamak_data_v2.csv', 'real_tokamak_data_v2.csv'] |
| df = None |
| for path in possible_paths: |
| if os.path.exists(path): |
| print(f" โ
Found data at: {path}") |
| df = pd.read_csv(path) |
| break |
| if df is None: exit() |
|
|
| df.replace([np.inf, -np.inf], np.nan, inplace=True) |
| df.fillna(0, inplace=True) |
| y = df['label'].values |
|
|
| |
| |
| |
| target_signals = ['n1', 'ip'] |
| fft_features = [] |
| feature_names = [] |
|
|
| for p in target_signals: |
| |
| cols = [c for c in df.columns if c.startswith(p + '_')] |
| cols.sort(key=lambda x: int(x.split('_')[1])) |
| signal_data = df[cols].values |
| |
| |
| |
| |
| fft_vals = np.abs(np.fft.rfft(signal_data, axis=1))[:, 1:] |
| |
| |
| |
| |
| |
| n_freqs = fft_vals.shape[1] |
| indices = np.linspace(0, n_freqs-1, 4, dtype=int) |
| |
| extracted = fft_vals[:, indices] |
| fft_features.append(extracted) |
| |
| for i in indices: |
| feature_names.append(f"{p}_freq_{i}") |
|
|
| |
| X_fft = np.hstack(fft_features) |
|
|
| print(f" FFT Shape: {X_fft.shape} (8 Features representing Plasma Rhythm)") |
| print(f" Features: {feature_names}") |
|
|
| |
| np.savez('golden_qgan_data.npz', |
| X=X_fft, |
| y=y, |
| feature_names=feature_names) |
|
|
| print("\n๐พ Saved Frequency Data to 'golden_qgan_data.npz'.") |
|
|
| |
| |
| idx_0 = np.where(y==0)[0] |
| idx_1 = np.where(y==1)[0] |
|
|
| avg_0 = np.mean(X_fft[idx_0], axis=0) |
| avg_1 = np.mean(X_fft[idx_1], axis=0) |
|
|
| plt.figure(figsize=(10, 6)) |
| plt.plot(avg_0, label='Healthy Spectrum', marker='o') |
| plt.plot(avg_1, label='Disruptive Spectrum', marker='x') |
| plt.title("The 'Sound' of a Plasma: Frequency Domain Analysis") |
| plt.xlabel("Frequency Bins (n1 then ip)") |
| plt.ylabel("Magnitude") |
| plt.legend() |
| plt.grid(True) |
| plt.savefig('fft_spectrum_debug.png') |
| print("๐ธ Saved spectrum plot to 'fft_spectrum_debug.png'") |