@@ -78,6 +78,7 @@ def __init__(self, debug=False):
7878 self .reset_timer ()
7979 self .packet_count = 0
8080 self .progress = 0
81+ self ._notch_filter = None
8182
8283 def subscribe (self , callback , topic ):
8384 """Subscribe a function to a topic
@@ -312,13 +313,17 @@ def process(self, packet):
312313 self .last_exg_packet_timestamp = get_local_time ()
313314 missing_timestamps = self .fill_missing_packet (packet )
314315 self ._update_last_time_point (packet , received_time )
315- self .dispatch (topic = TOPICS .raw_ExG , packet = packet )
316316 self .packet_count += 1
317- if self ._is_imp_mode and self . imp_calculator :
317+ if self .is_imp_running () :
318318 packet_imp = self .imp_calculator .measure_imp (
319319 packet = copy .deepcopy (packet ))
320320 if packet_imp is not None :
321321 self .dispatch (topic = TOPICS .imp , packet = packet_imp )
322+ if self ._notch_filter :
323+ self ._notch_filter .apply (packet )
324+ self .dispatch (topic = TOPICS .raw_ExG , packet = packet )
325+ else :
326+ self .dispatch (topic = TOPICS .raw_ExG , packet = packet )
322327 try :
323328 self .apply_filters (packet = packet )
324329 except ValueError as error :
@@ -454,9 +459,10 @@ def imp_initialize(self, notch_freq, calibration=False):
454459 cmd = ZMeasurementEnable ()
455460 if self .configure_device (cmd ):
456461 self .imp_calib_info ['calibration' ] = calibration
462+ self ._add_notch_filter ()
457463 self .imp_calculator = ImpedanceMeasurement (device_info = self .device_info ,
458464 calib_param = self .imp_calib_info ,
459- notch_freq = notch_freq )
465+ notch_freq = self . get_power_line_freq () or notch_freq )
460466 self ._is_imp_mode = True
461467 else :
462468 raise ConnectionError ('Device configuration process failed!' )
@@ -470,6 +476,7 @@ def disable_imp(self):
470476 return True
471477 print ("WARNING: Couldn't disable impedance measurement mode. "
472478 "Please restart your device manually." )
479+ self ._notch_filter = None
473480 return False
474481
475482 def set_marker (self , marker_string , time_lsl = None , name = 'mkr' , soft_marker = True ):
@@ -583,3 +590,22 @@ def fill_missing_packet(self, packet):
583590 timestamps = np .linspace (self ._last_packet_timestamp + sps ,
584591 packet .timestamp , num = missing_samples , endpoint = True )
585592 return timestamps [:- 1 ]
593+
594+ def _add_notch_filter (self ):
595+ self ._notch_filter = ExGFilter (
596+ cutoff_freq = 62.5 ,
597+ filter_type = 'notch_imp' ,
598+ s_rate = 250 ,
599+ n_chan = SettingsManager (self .device_info ['device_name' ]).get_channel_count ()
600+ )
601+
602+ def is_imp_running (self ):
603+ return self ._is_imp_mode and self .imp_calculator
604+
605+ def get_power_line_freq (self ):
606+ match = next (
607+ (item for item in self .filters if item .filter_type == 'notch' ),
608+ None
609+ )
610+
611+ return match .cutoff_freq if match else None
0 commit comments