def test_codec_StackedHighShelf(): filter = HighShelf(48000, 20, 1.5, 2.5, count=5) output = json.dumps(filter.to_json()) assert output == '{"_type": "HighShelf", "fs": 48000, "fc": 20.0, "q": 1.5, "gain": 2.5, "count": 5}' decoded = filter_from_json(json.loads(output)) assert decoded is not None assert isinstance(decoded, HighShelf) assert filter.fs == decoded.fs assert filter.q == decoded.q assert filter.gain == decoded.gain assert filter.freq == decoded.freq assert filter.count == decoded.count assert decoded.get_transfer_function() is not None
def test_codec_signal(): fs = 1000 peak = LowShelf(fs, 30, 1, 10, count=2).get_transfer_function().get_magnitude() avg = LowShelf(fs, 30, 1, 10).get_transfer_function().get_magnitude() median = LowShelf(fs, 30, 1, 10).get_transfer_function().get_magnitude() filt = CompleteFilter() filt.save(HighShelf(fs, 60, 1, 5, count=2)) data = SingleChannelSignalData('test', fs, xy_data=[avg, peak, median], filter=filt, duration_seconds=123456, start_seconds=123, offset=4.2) output = json.dumps(signaldata_to_json(data)) assert output is not None decoded = signaldata_from_json(json.loads(output), None) assert decoded is not None assert isinstance(decoded, SingleChannelSignalData) assert decoded.name == data.name assert decoded.fs == data.fs assert decoded.filter is not None assert type(decoded.filter) is type(data.filter) assert decoded.filter.id != -1 assert decoded.filter.description == data.filter.description assert decoded.filter.filters == data.filter.filters assert decoded.current_unfiltered is not None assert len(decoded.current_unfiltered) == 3 assert decoded.current_unfiltered == data.current_unfiltered assert decoded.duration_hhmmss == data.duration_hhmmss assert decoded.start_hhmmss == data.start_hhmmss assert decoded.end_hhmmss == data.end_hhmmss assert decoded.offset == data.offset
def xml_to_filt(file, fs=1000, unroll=False): ''' Extracts a set of filters from the provided minidsp file ''' from model.iir import PeakingEQ, LowShelf, HighShelf filts = __extract_filters(file) output = [] for filt_tup, count in filts.items(): filt_dict = dict(filt_tup) if filt_dict['type'] == 'SL': for i in range(0, count if unroll is True else 1): filt = LowShelf(fs, float(filt_dict['freq']), float(filt_dict['q']), float(filt_dict['boost']), count=1 if unroll is True else count) output.append(filt) elif filt_dict['type'] == 'SH': for i in range(0, count if unroll is True else 1): filt = HighShelf(fs, float(filt_dict['freq']), float(filt_dict['q']), float(filt_dict['boost']), count=1 if unroll is True else count) output.append(filt) elif filt_dict['type'] == 'PK': for i in range(0, count): filt = PeakingEQ(fs, float(filt_dict['freq']), float(filt_dict['q']), float(filt_dict['boost'])) output.append(filt) else: logger.info(f"Ignoring unknown filter type {filt_dict}") return output
def create_shaping_filter(self): ''' Creates a filter of the specified type. :return: the filter. ''' filt = None if self.filterType.currentText() == 'Low Shelf': filt = LowShelf(self.__signal.fs, self.freq.value(), self.filterQ.value(), self.filterGain.value(), self.filterCount.value()) elif self.filterType.currentText() == 'High Shelf': filt = HighShelf(self.__signal.fs, self.freq.value(), self.filterQ.value(), self.filterGain.value(), self.filterCount.value()) elif self.filterType.currentText() == 'PEQ': filt = PeakingEQ(self.__signal.fs, self.freq.value(), self.filterQ.value(), self.filterGain.value()) elif self.filterType.currentText() == 'Gain': filt = Gain(self.__signal.fs, self.filterGain.value()) elif self.filterType.currentText() == 'Variable Q LPF': filt = SecondOrder_LowPass(self.__signal.fs, self.freq.value(), self.filterQ.value()) elif self.filterType.currentText() == 'Variable Q HPF': filt = SecondOrder_HighPass(self.__signal.fs, self.freq.value(), self.filterQ.value()) if filt is None: raise ValueError( f"Unknown filter type {self.filterType.currentText()}") else: filt.id = self.__selected_id return filt
def __convert_to_filter(filter_params, id): ft = int(filter_params['FilterType']) if SyncHTP1Dialog.__has_filter_type(filter_params) else 0 if ft == 0: return PeakingEQ(HTP1_FS, filter_params['Fc'], filter_params['Q'], filter_params['gaindB'], f_id=id) elif ft == 1: return LowShelf(HTP1_FS, filter_params['Fc'], filter_params['Q'], filter_params['gaindB'], f_id=id) elif ft == 2: return HighShelf(HTP1_FS, filter_params['Fc'], filter_params['Q'], filter_params['gaindB'], f_id=id)
def filter_from_json(o): ''' Converts a dict (parsed from json) to a filter. :param o: the dict. :return: the filter. ''' from model.iir import Passthrough, PeakingEQ, LowShelf, HighShelf, FirstOrder_LowPass, \ FirstOrder_HighPass, SecondOrder_LowPass, SecondOrder_HighPass, AllPass, CompleteFilter, ComplexLowPass, \ FilterType, ComplexHighPass filt = None if '_type' not in o: raise ValueError(f"{o} is not a filter") if o['_type'] == Passthrough.__name__: if 'fs' in o: filt = Passthrough(fs=int(o['fs'])) else: filt = Passthrough() elif o['_type'] == Gain.__name__: filt = Gain(o['fs'], o['gain']) elif o['_type'] == PeakingEQ.__name__: filt = PeakingEQ(o['fs'], o['fc'], o['q'], o['gain']) elif o['_type'] == LowShelf.__name__: filt = LowShelf(o['fs'], o['fc'], o['q'], o['gain'], o['count']) elif o['_type'] == HighShelf.__name__: filt = HighShelf(o['fs'], o['fc'], o['q'], o['gain'], o['count']) elif o['_type'] == FirstOrder_LowPass.__name__: filt = FirstOrder_LowPass(o['fs'], o['fc'], o['q']) elif o['_type'] == FirstOrder_HighPass.__name__: filt = FirstOrder_HighPass(o['fs'], o['fc'], o['q']) elif o['_type'] == SecondOrder_LowPass.__name__: filt = SecondOrder_LowPass(o['fs'], o['fc'], o['q']) elif o['_type'] == SecondOrder_HighPass.__name__: filt = SecondOrder_HighPass(o['fs'], o['fc'], o['q']) elif o['_type'] == AllPass.__name__: filt = AllPass(o['fs'], o['fc'], o['q']) elif o['_type'] == CompleteFilter.__name__: kwargs = {} if 'fs' in o: kwargs['fs'] = o['fs'] filt = CompleteFilter( filters=[filter_from_json(x) for x in o['filters']], description=o['description'], **kwargs) elif o['_type'] == ComplexLowPass.__name__: filt = ComplexLowPass(FilterType(o['filter_type']), o['order'], o['fs'], o['fc']) elif o['_type'] == ComplexHighPass.__name__: filt = ComplexHighPass(FilterType(o['filter_type']), o['order'], o['fs'], o['fc']) if filt is None: raise ValueError(f"{o._type} is an unknown filter type") else: if filt.id == -1: filt.id = uuid4() return filt