Peak Alpha Frequency?

Post Reply
NotoriousREV
Posts: 5
Joined: Sat Aug 12, 2023 8:31 am

Peak Alpha Frequency?

Post by NotoriousREV »

Is it possible to use Mind Monitor to measure Peak Alpha Frequency eg “the peak frequency within the alpha range (8–12 Hz) with a 0.5 Hz frequency resolution”? If it’s possible, could you explain how I can extract this data?

Thanks.
User avatar
James
Site Admin
Posts: 1110
Joined: Wed Jan 02, 2013 9:06 pm

Re: Peak Alpha Frequency?

Post by James »

That's something that you can calculate from the recorded RAW EEG data by using an FFT.
The Math for an FFT is here: https://www.rosettacode.org/wiki/Fast_Fourier_transform

To get the data you will need, set the "Recording Interval" in settings from it's default of 1 data point per second to "Constant".
Note that this will make your recording files HUGE! ;-)
NotoriousREV
Posts: 5
Joined: Sat Aug 12, 2023 8:31 am

Re: Peak Alpha Frequency?

Post by NotoriousREV »

Thanks James. I’ll give it a go.
NotoriousREV
Posts: 5
Joined: Sat Aug 12, 2023 8:31 am

Re: Peak Alpha Frequency?

Post by NotoriousREV »

Hi James,

When I do the FFT on the data I’ve captured from the Alpha_x columns, the frequencies displayed are between 0 and 1 Hz. Is there something I need to do to those values to convert them to the actual frequencies? Or does 0 = 8Hz and 1 = 12 Hz?

If it isn’t obvious, I have no idea what I’m doing 🤣

Eg

Code: Select all

Frequency,Magnitude
0.015873015873015872,7.693787725565066
0.031746031746031744,15.307929229099642
0.047619047619047616,6.827003020802145
0.06349206349206349,6.916880805035763
0.07936507936507936,6.02594460167426
0.09523809523809523,3.1312430504094233
0.1111111111111111,6.094026874631805
0.12698412698412698,4.255716096788426
0.14285714285714285,4.83208936877447
User avatar
James
Site Admin
Posts: 1110
Joined: Wed Jan 02, 2013 9:06 pm

Re: Peak Alpha Frequency?

Post by James »

You need to use the RAW EEG, eg. RAW_AF7 is the raw data from the AF7 sensor.
The FFT will then give you frequency bins, the granularity of which will depend on your FFT window. Each bin will corrospond to a frequency range.
NotoriousREV
Posts: 5
Joined: Sat Aug 12, 2023 8:31 am

Re: Peak Alpha Frequency?

Post by NotoriousREV »

I guess I'm just struggling to understand what I'm doing.

I've recorded some sample data (about a minute with a sample rate of 0.5 seconds as I couldn't get the FFT function to work without having a set sample rate).

I extracted the TimeStamp and RAW_TP9 columns into a new csv file.

I ran the following code against it:

Code: Select all

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Load the data from the CSV file
input_file = 'data_RAW_TP9.csv'
df = pd.read_csv(input_file)

# Preprocess the data by filling missing values with zeros
df['RAW_TP9'] = df['RAW_TP9'].fillna(0)

# Create a time array for plotting (assuming a sampling rate of 0.5 seconds)
time = np.arange(len(df)) * 0.5

# Apply FFT to 'RAW_TP9' data
fft_result = np.fft.fft(df['RAW_TP9'])
# Calculate the frequency values
n = len(fft_result)
freq = np.fft.fftfreq(n, d=0.5)  # Relative frequencies
# Extract the magnitude of the FFT result
magnitude = np.abs(fft_result)

# Filter out positive frequencies
positive_freq_mask = freq > 0
positive_freq = freq[positive_freq_mask]
positive_magnitude = magnitude[positive_freq_mask]

# Plot the FFT result for positive frequencies
plt.figure(figsize=(12, 6))
plt.title('FFT for RAW_TP9 (Positive Frequencies)')
plt.plot(positive_freq, positive_magnitude)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.grid()

# Save the graph as a JPG file
plt.savefig('FFT_RAW_TP9.jpg')
plt.close()

# Create a DataFrame for the positive frequency and magnitude data
fft_data = pd.DataFrame({'Frequency (Hz)': positive_freq, 'Magnitude': positive_magnitude})

# Save the data as a new CSV file
output_file = 'output_RAW_TP9.csv'
fft_data.to_csv(output_file, index=False)
I got the following graph:
FFT_RAW_TP9.jpg
FFT_RAW_TP9.jpg (106.53 KiB) Viewed 3892 times
I got the following data:

Code: Select all

Frequency (Hz)	Magnitude
0.014388489208633100	3883.7829057527200
0.02877697841726620	1763.875806532500
0.04316546762589930	2364.7548375790900
0.05755395683453240	907.6062576131200
0.07194244604316550	2669.2160446270400
0.08633093525179860	1248.871647181520
0.10071942446043200	3759.879066465700
0.11510791366906500	1772.5406775104700
0.12949640287769800	2900.538053568630
0.14388489208633100	630.6437474856770
0.15827338129496400	2604.040351348210
0.17266187050359700	904.9073505367600
0.18705035971223000	526.1452294559570
0.20143884892086300	3102.666058392090
0.21582733812949600	533.7863481259360
0.2302158273381300	3965.0983632988300
0.2446043165467630	1481.8775183763900
0.2589928057553960	1077.8259170570900
0.2733812949640290	1009.3464059089800
0.28776978417266200	4131.412146691210
0.302158273381295	3851.888931564770
0.3165467625899280	1843.7622520145600
0.33093525179856100	5030.344442277420
0.34532374100719400	4124.8800392147200
0.3597122302158270	2100.5685677079000
0.37410071942446000	3252.958097644980
0.38848920863309400	1726.4378511252200
0.40287769784172700	5113.858435326000
0.4172661870503600	2251.3356583357300
0.43165467625899300	4425.053235589330
0.4460431654676260	1586.4498150133700
0.460431654676259	2849.2559453142100
0.47482014388489200	2893.008732903440
0.4892086330935250	1614.9546763683000
0.5035971223021580	1846.2720175788300
0.5179856115107910	1765.780963653180
0.5323741007194250	2990.4000684758700
0.5467625899280580	3019.9402003919300
0.5611510791366910	1280.4490461331500
0.5755395683453240	216.0353139727510
0.5899280575539570	2367.487101991650
0.60431654676259	3073.866656329910
0.6187050359712230	4069.594041778200
0.6330935251798560	7772.182198705500
0.6474820143884890	899.2311906069560
0.6618705035971220	1982.365662897000
0.6762589928057550	4448.110015312240
0.6906474820143890	3261.2028389329300
0.7050359712230220	1055.8389715596900
0.7194244604316550	2999.2154585418200
0.7338129496402880	4019.7373480294400
0.7482014388489210	1648.0264095310800
0.762589928057554	3760.209760886390
0.7769784172661870	2981.56771239279
0.7913669064748200	1975.2381301532000
0.8057553956834530	377.6915705888330
0.8201438848920860	3186.086116124280
0.8345323741007200	5073.995335057610
0.8489208633093530	3072.168550462750
0.8633093525179860	1070.9828307775100
0.8776978417266190	218.77773879881100
0.8920863309352520	836.8906042412640
0.9064748201438850	3212.120376041330
0.920863309352518	3784.0798211796000
0.9352517985611510	2440.153846550750
0.9496402877697840	2872.140113724980
0.9640287769784170	3988.246796237670
0.9784172661870500	3886.6931154881700
0.9928057553956840	2509.0556476818800
No matter what I do, the frequencies don't seem to relate in the way I expected.
User avatar
James
Site Admin
Posts: 1110
Joined: Wed Jan 02, 2013 9:06 pm

Re: Peak Alpha Frequency?

Post by James »

Here's TouchDesigner example I did a while ago which does an FFT in python
Mind-Monitor-TouchDesigner-FFT.toe
(56.57 KiB) Downloaded 1182 times
Post Reply