diff --git a/mlair/data_handler/data_handler_with_filter.py b/mlair/data_handler/data_handler_with_filter.py index e7101468117492c9ac0a351ee7d1618563719013..b9b90d440074a1ee16db84bd7269326e4981957f 100644 --- a/mlair/data_handler/data_handler_with_filter.py +++ b/mlair/data_handler/data_handler_with_filter.py @@ -117,15 +117,40 @@ class DataHandlerFirFilterSingleStation(DataHandlerFilterSingleStation): filter_add_unfiltered=DEFAULT_ADD_UNFILTERED, **kwargs): # self._check_sampling(**kwargs) # self.original_data = None # ToDo: implement here something to store unfiltered data - self.filter_cutoff_period = (lambda x: [x] if isinstance(x, tuple) else to_list(x))(filter_cutoff_period) + self.fs = self._get_fs(**kwargs) + self.filter_cutoff_period, removed_index = self._prepare_filter_cutoff_period(filter_cutoff_period, self.fs) self.filter_cutoff_freq = self._period_to_freq(self.filter_cutoff_period) - assert len(self.filter_cutoff_period) == len(filter_order) - self.filter_order = filter_order + assert len(self.filter_cutoff_period) == (len(filter_order) - len(removed_index)) + self.filter_order = self._prepare_filter_order(filter_order, removed_index, self.fs) self.filter_window_type = filter_window_type self._add_unfiltered = filter_add_unfiltered - self.fs = self._get_fs(**kwargs) super().__init__(*args, **kwargs) + @staticmethod + def _prepare_filter_order(filter_order, removed_index, fs): + order = [] + for i, o in enumerate(filter_order): + if i not in removed_index: + fo = int(o * fs) + fo = fo + 1 if fo % 2 == 0 else fo + order.append(fo) + return order + + @staticmethod + def _prepare_filter_cutoff_period(filter_cutoff_period, fs): + """Frequency must be smaller than the sampling frequency fs. Otherwise remove given cutoff period pair.""" + cutoff_tmp = (lambda x: [x] if isinstance(x, tuple) else to_list(x))(filter_cutoff_period) + cutoff = [] + removed = [] + for i, (low, high) in enumerate(cutoff_tmp): + low = low if (low is None or low > 2. / fs) else None + high = high if (high is None or high > 2. / fs) else None + if any([low, high]): + cutoff.append((low, high)) + else: + removed.append(i) + return cutoff, removed + @staticmethod def _period_to_freq(cutoff_p): return list(map(lambda x: (1. / x[0] if x[0] is not None else None, 1. / x[1] if x[1] is not None else None),