FFT in Python
in Software
Hi all. I have been hacking with my OpenBCI board for some time. I'm hoping to move away from the Processing GUI to work with the data more directly, and I want to be sure that I understand Python's FFT functions correctly.
What I did is I took the sample data from one of the "relaxation.txt" data sets in the \OpenBCI_Processing-master\OpenBCI_GUI\data\EEG_Data folder, and imported it into a NumPy array like so:
with open('/path/relaxation.txt') as f:
import numpy as np
data = []
for line in f:
row = line.replace("[","").replace("]","").replace("\n","").split(", ");
data.append([float(n) for n in row])
Then I used np.fft.fft and np.fft.fftfreq to make another NumPy array like so:
transformed = []
for channel in range(8):
signal = np.array([row[channel+1] for row in data], dtype=float)
fourier = np.fft.fft(signal)
n = signal.size
freq = np.fft.fftfreq(n, d=0.004)
transformed.append(fourier)
Finally, I output it to CSV like so:
with open('/path/relaxation.csv','wb') as f:
import csv
writer = csv.writer(f)
writer.writerow(freq)
for channel in transformed:
writer.writerow(channel.astype(float).tolist())
So, I think what I accomplished is this:
- Each column has a header that I think may represent a frequency band, separated by about 0.002 from the value of the column to the left.
- Each column has eight rows beneath the header, each representing the amplitude for that frequency on one of the eight electrodes.
However, there are some thing I don't understand:
- Why do the values vary so widely across electrodes?
- Half the output is redundant, simply a negative mirror of the other half. Is this always the case with FFT output?
Any thoughts on whether my approach seems correct? Thanks!
What I did is I took the sample data from one of the "relaxation.txt" data sets in the \OpenBCI_Processing-master\OpenBCI_GUI\data\EEG_Data folder, and imported it into a NumPy array like so:
with open('/path/relaxation.txt') as f:
import numpy as np
data = []
for line in f:
row = line.replace("[","").replace("]","").replace("\n","").split(", ");
data.append([float(n) for n in row])
Then I used np.fft.fft and np.fft.fftfreq to make another NumPy array like so:
transformed = []
for channel in range(8):
signal = np.array([row[channel+1] for row in data], dtype=float)
fourier = np.fft.fft(signal)
n = signal.size
freq = np.fft.fftfreq(n, d=0.004)
transformed.append(fourier)
Finally, I output it to CSV like so:
with open('/path/relaxation.csv','wb') as f:
import csv
writer = csv.writer(f)
writer.writerow(freq)
for channel in transformed:
writer.writerow(channel.astype(float).tolist())
So, I think what I accomplished is this:
- Each column has a header that I think may represent a frequency band, separated by about 0.002 from the value of the column to the left.
- Each column has eight rows beneath the header, each representing the amplitude for that frequency on one of the eight electrodes.
However, there are some thing I don't understand:
- Why do the values vary so widely across electrodes?
- Half the output is redundant, simply a negative mirror of the other half. Is this always the case with FFT output?
Any thoughts on whether my approach seems correct? Thanks!
Comments