FFT data
Koreanstudent
Seoul, Korea
https://github.com/dlalsdnr/R_E_Project
I want to save 8-channel data as CSV files after performing FFT.
Here's My Python code, the raw data file, and the data after FFT and filtering.
from pyOpenBCI import OpenBCICyton
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipy.fftpack import fft
from scipy import signal
import keyboard
Data = [1, 2, 3, 4, 5, 6, 7, 8]
Data_Set_Size = 1
def print_raw(sample):
global Data
Data = np.append(Data, sample.channels_data)
print(len(Data))
Data_Set_Size = Data_Set_Size + 1
def make_data_set(DS): #DS = Data
global Data_1_Y, Data_2_Y, Data_3_Y, Data_4_Y, Data_5_Y, Data_6_Y, Data_7_Y, Data_8_Y, X_Data_Set_Size
DS = DS.reshape(-1, 8)
Sensor_Data = DS
Sensor_Data_1 = Sensor_Data[:, 0]
Sensor_Data_2 = Sensor_Data[:, 1]
Sensor_Data_3 = Sensor_Data[:, 2]
Sensor_Data_4 = Sensor_Data[:, 3]
Sensor_Data_5 = Sensor_Data[:, 4]
Sensor_Data_6 = Sensor_Data[:, 5]
Sensor_Data_7 = Sensor_Data[:, 6]
Sensor_Data_8 = Sensor_Data[:, 7]
Data_1_Y = Sensor_Data_1
Data_2_Y = Sensor_Data_2
Data_3_Y = Sensor_Data_3
Data_4_Y = Sensor_Data_4
Data_5_Y = Sensor_Data_5
Data_6_Y = Sensor_Data_6
Data_7_Y = Sensor_Data_7
Data_8_Y = Sensor_Data_8
return DS
def bandpass(start, stop, data, fs = 200):
bp_Hz = np.array([start, stop])
b, a = signal.butter(5, bp_Hz / (fs / 2.0), btype='bandpass')
return signal.lfilter(b, a, data, axis=0)
def notch(val, data, fs= 200):
notch_freq_Hz = np.array([float(val)])
for freq_Hz in np.nditer(notch_freq_Hz):
bp_stop_Hz = freq_Hz + 3.0 * np.array([-1, 1])
b, a = signal.butter(3, bp_stop_Hz / (fs / 2.0), 'bandstop')
fin = data = signal.lfilter(b, a, data)
return fin
def fft(data, fs):
L = len(data)
freq = np.linspace(0.0, 1.0 / (2.0 * fs **-1), L // 2)
yi = np.fft.fft(data)[1:]
y = yi[range(int(L / 2))]
return freq, abs(y)
fs = 1000
board = OpenBCICyton(port='COM3', daisy=False)
board.start_stream(print_raw)
board.stop_stream()
raw_data = make_data_set(Data)
X_Data_Set_Size = np.arange(Data_Set_Size)
raw_data_print = pd.DataFrame(raw_data)
raw_data_print.to_csv("raw_data.csv", index = False)
channels = []
for i in range(8):
channels.append(raw_data[:, i].astype(np.float64))
t = len(channels[0])/fs
time = np.linspace(0, t, len(channels[0]))
for i in range(len(channels)):
freq, y = fft(channels[i], fs)
notch_channels = []
for i in range(len(channels)):
notch_channels.append(notch(60,channels[i], fs = fs))
band = (15,50)
bandpass_notch_channels = []
for i in range(len(notch_channels)):
bandpass_notch_channels.append(bandpass(band[0],band[1],notch_channels[i], fs = fs))
for i in range(len(bandpass_notch_channels)):
freq, y = fft(bandpass_notch_channels[i], fs)
K = freq, y
filtered_data = pd.DataFrame(K)
filtered_data.to_csv('filtered_data.csv', index=False)
`
I want to save the data that has undergone FFT transformation for each channel, similar to the raw data file.
What should I do?
Comments
'pyOpenBCI' is no longer recommended or supported. That is the reason the repo has the '-archive' suffix. It is deprecated and no longer supported.
https://github.com/openbci-archive/pyOpenBCI
Instead you should be using Brainflow:
https://brainflow.org/
https://brainflow.readthedocs.io/en/stable/Examples.html#python-band-power
https://brainflow.readthedocs.io/en/stable/UserAPI.html#python-api-reference
However, pyOpenBCI continues to work... Is it not possible to use pyOpenBCI as is and save channel-specific FFT data?
Can you just call Python functions that save the FFT data as a CSV ascii file ? I don't understand what the problem is. FFT uses a 'window' (which covers the current sample and past N samples), and tells you for each frequency histogram 'bin', the amplitude at that frequency. But of course is operating over the entire window.
The resulting CSV file will have N bins per line, separated by commas.
If you want to show all 8 channels, you will have in essence a 3D array. Not sure the best way to dump that to CSV.