def test_codec_SecondOrderLowPass(): filter = SecondOrder_LowPass(48000, 2000, 1.5) output = json.dumps(filter.to_json()) assert output == '{"_type": "SecondOrder_LowPass", "fs": 48000, "fc": 2000.0, "q": 1.5}' decoded = filter_from_json(json.loads(output)) assert decoded is not None assert isinstance(decoded, SecondOrder_LowPass) assert filter.fs == decoded.fs assert filter.q == decoded.q assert filter.freq == decoded.freq assert decoded.get_transfer_function() is not None
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 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