BrainFlow【降采样】
降采样 (Downsample_Data)
一开始在一些脑机接口的参考书中没找到,有也是简单略过,接着就去问了问度娘:
- 采样率的单位是Hz,指的是一秒内要记录多少个数据点的意思,比如原始数据的采样率是1000Hz的话,就是代表一秒内有1000个数据点。
- 在后期分析的时候,我们有时候会想要降低采样率,比如降到500Hz或者250Hz。这样做最大的好处其实就是减小数据量,以提高计算速度,没有其他的作用了。所以这一步并不是必须的。
- 但降低采样率需要注意两点,一是降低采样率要在滤波之后。因为降低采样率会使我们丢失高频信息,使高频信息变得扭曲,所以最好在保留了我们感兴趣波段之后再去降低采样率,这样可以保证信号最大程度不会失真。二是有一个采样定理,理论上来说,我们的采样率必须是我们想要分析的波段的两倍,比如我们想要分析60Hz的波,那数据采样率为120Hz就足够了。实际上,建议采样率最好在分析波段的三到四倍。
代码块
1 import time 2 import numpy as np 3 import brainflow 4 from brainflow.board_shim import BoardShim, BrainFlowInputParams, LogLevels, BoardIds 5 from brainflow.data_filter import DataFilter, FilterTypes, AggOperations 6 7 def main (): 8 BoardShim.enable_dev_board_logger () 9 10 # use synthetic board for demo 11 #GET PARAMS 12 params = BrainFlowInputParams () 13 board = BoardShim (BoardIds.SYNTHETIC_BOARD.value, params) 14 board.prepare_session () 15 board.start_stream () 16 BoardShim.log_message (LogLevels.LEVEL_INFO.value, 'start sleeping in the main thread') 17 time.sleep (10) 18 data = board.get_current_board_data (20) # get 20 latest data points dont remove them from internal buffer 19 board.stop_stream () 20 board.release_session () 21 22 eeg_channels = BoardShim.get_eeg_channels (BoardIds.SYNTHETIC_BOARD.value) 23 # demo for downsampling, it just aggregates data 24 #DataFilter to perform signal processing, it calls methods from underlying DataHandler library 25 for count, channel in enumerate (eeg_channels): 26 print ('Original data for channel %d:' % channel) 27 print (data[channel]) 28 if count == 0: 29 downsampled_data = DataFilter.perform_downsampling (data[channel], 3, AggOperations.MEDIAN.value) 30 elif count == 1: 31 downsampled_data = DataFilter.perform_downsampling (data[channel], 2, AggOperations.MEAN.value) 32 else: 33 downsampled_data = DataFilter.perform_downsampling (data[channel], 2, AggOperations.EACH.value) 34 print ('Downsampled data for channel %d:' % channel) 35 print (downsampled_data) 36 37 38 if __name__ == "__main__": 39 main ()
相关函数
(1) classmethod perform_downsampling(data, period, operation)
perform data downsampling, it doesnt apply lowpass filter for you, it just aggregates several data points
- Parameters
-
-
data (1d numpy array) – initial data
-
period (int) – downsampling period
-
operation (int) – int value from AggOperation enum
-
- Returns
-
downsampled data
- Return type
-
1d numpy array
(2)class brainflow_data_filter.AggOperations
Bases: enum.Enum
Enum to store all supported aggregation operations
· MEAN
= 0
· MEDIAN
= 1
· EACH
= 2