The extraction of five frequency waves of EEG is correct or not alpha, beta, gamma, theta, delta?

I used one channel EEG devices has powerspec.csv file

Time 0 0.25 0.5 0.75 1 1.25 1.5 1.75 2 … 61.5 61.8 62 62.3 62.5 62.8 63 63.3 63.5 63.8
0 22:50:34 26.40 140.0 52.40 27.30 61.00 12.5 29.1 34.3 6.14 … 3.780000e-13 3.770000e-13 3.540000e-13 3.080000e-13 3.130000e-13 2.970000e-13 2.840000e-13 2.760000e-13 2.700000e-13 2.670000e-13
2481 rows × 257 columns

*Is the below code was python code power spectral feature extraction is correct or not?

import numpy as np
import pandas as pd

Load your preprocessed data

data = pd.read_csv(‘/content/drive/MyDrive/EEG Dataset/SSN_fwdvideoreadings/181112225542.powerspec.csv’)

Reshape the data to (samples, time_steps, channels)

data_values = data.drop(columns=[‘Time’]).values
data_reshaped = np.reshape(data_values, (len(data), 256, -1))

Sampling frequency

fs = 256

Get real amplitudes of FFT (only in positive frequencies)

fft_vals = np.absolute(np.fft.fft(data_reshaped, axis=1))

Get frequencies for amplitudes in Hz

fft_freq = np.fft.fftfreq(data_reshaped.shape[1], 1.0 / fs)

Define EEG bands

eeg_bands = {‘Delta’: (0.5, 4),
‘Theta’: (4.1, 8),
‘Alpha’: (8.1, 12),
‘Beta’: (12.1, 30),
‘Gamma’: (30.1, 120)}

Initialize dictionaries to store aggregated wave data

aggregated_data = {band: for band in eeg_bands.keys()}
time_dict = {}

Take the mean of the FFT amplitude for each EEG band across all time steps

for i in range(len(data)):
timestamp = data[‘Time’][i]
if timestamp not in time_dict:
time_dict[timestamp] = len(time_dict)
time_idx = time_dict[timestamp]
for band, (low, high) in eeg_bands.items():
freq_ix = np.where((fft_freq >= low) & (fft_freq <= high))[0]
aggregated_data[band].append(np.mean(fft_vals[i, freq_ix]))

Create DataFrame

result_df = pd.DataFrame(aggregated_data)

Add the Time column back to the DataFrame

result_df[‘Time’] = data[‘Time’]

Group by Time and take the mean

result_df = result_df.groupby(‘Time’).mean().reset_index()

Reorder columns

result_df = result_df[[‘Time’, ‘Alpha’, ‘Beta’, ‘Delta’, ‘Theta’, ‘Gamma’]]

Save the DataFrame to a CSV file

#result_df.to_csv(‘extracted_waveforms_reduced.csv’, index=False)
result_df

The results also
Time Alpha Beta Delta Theta Gamma
0 22:50:34 307.858865 328.737738 926.303834 491.416773 211.999118
1 22:50:35 349.872691 367.122390 720.928035 462.434233 198.847660
2 22:50:36 650.277401 571.233902 1052.908029 790.054977 250.895087
3 22:50:37 2071.023165 1641.168546 2757.112038 2440.801969 613.760081
4 22:50:38 6812.422723 4970.802084 8072.064257 7597.704078 1043.839759
… … … … … … …
304 22:55:38 135.733638 178.732464 566.884751 245.447899 89.462139
305 22:55:39 157.473915 150.729884 439.677633 200.039832 80.212311
306 22:55:40 410.353346 298.445394 810.049615 466.847896 170.234903
307 22:55:41 909.313430 693.503189 1345.535591 1029.820925 265.058099
308 22:55:42 1264.562631 865.893222 1618.648096 1325.605290 288.059167

309 rows × 6 columns
` Is this code correct or not feature extraction`

Please format your code so that others can read it (see How to correctly format your question - #3).

1 Like