Files
imdd_silas/Classes/Filter.m
Silas Oettinghaus 3fd78db417 WIP mac
2023-05-25 08:57:20 +02:00

176 lines
5.0 KiB
Matlab

classdef Filter
%FILTER Summary of this class goes here
% Detailed explanation goes here
properties
H
filterType
f_cutoff
lowpass
signal_length
filtdegree
passband_ripple
stopband_ripple
fsamp
w
end
methods
function obj = Filter(options)
%FILTER Construct an instance of this class
% Detailed explanation goes here
arguments
options.filterType filtertypes = filtertypes.bessel_inp ;
options.f_cutoff = 0;
options.fsamp = 0;
options.filtdegree = 3;
options.passband_ripple = 0.5;
options.stopband_ripple = 0.5;
options.lowpass = 1;
end
fn = fieldnames(options);
for n = 1:numel(fn)
obj.(fn{n}) = options.(fn{n});
end
end
function signalclass_out = process(obj,signalclass_in)
% actual processing of the signal
signalclass_in.signal = obj.process_(signalclass_in.signal);
% append to logbook
filterdesc = [num2str(obj.filtdegree),'. order ',char(obj.filterType),' filter with f_cutoff at ', num2str(obj.f_cutoff*1e-9), ' GHz.'];
signalclass_in = signalclass_in.logbookentry(filterdesc);
% write to output
signalclass_out = signalclass_in;
end
function yout = process_(obj,xin)
obj.signal_length = length(xin);
[obj.H,obj.w] = obj.buildFilter(obj.filterType);
yout = obj.applyFilter(xin);
end
function y_filtered = applyFilter(obj,xin)
y_filtered = ifft(obj.H.*fft(xin));
end
function [H,w] = buildFilter(obj,filterType)
w = [];
rp = obj.passband_ripple; %passband ripple
rs = obj.stopband_ripple; %stopband ripple
switch filterType
case 1
% Bessel filter, impulse invariant transformed
[B, A] = besself(obj.filtdegree, 2*pi*obj.f_cutoff);
[B ,A] = impinvar(B,A,obj.fsamp);
case 2
% Bessel filter, impulse bilinear transformed
[Z, P, K] = besself(obj.filtdegree, 2*pi*obj.f_cutoff);
[Z ,P, K] = bilinear(Z,P,K,obj.fsamp);
[B ,A] = zp2tf(Z ,P ,K);
case 3
% Butterworth filter
if obj.lowpass == 1 %lowpass
[B, A] = butter(obj.filtdegree, obj.f_cutoff/(obj.fsamp/2),'low');
else % highpass
[B, A] = butter(obj.filtdegree, obj.f_cutoff/(obj.fsamp/2),'high');
end
case 4
% Chebyshev 1 filter
[B, A] = cheby1(obj.filtdegree,rp, obj.f_cutoff/(obj.fsamp/2));
case 5
% Chebyshev 2 filter
[B, A] = cheby2(obj.filtdegree,rs, obj.f_cutoff/(obj.fsamp/2));
case 6
% Elliptic filter
[B, A] = ellip(obj.filtdegree,rp,rs,obj.f_cutoff/(obj.fsamp/2));
case 7
% Hamming filter
g=(obj.filtdegree-1)/2;
wc=obj.f_cutoff/(obj.fsamp/2);
B = wc*sinc(wc*(-g:g)).*hamming(obj.filtdegree)';
A=1;
case 8
% Raised Cosine filter
B = firrcos(obj.filtdegree,obj.f_cutoff,para.df,obj.fsamp);
A=1;
case 9
% Sinc filter
g=(obj.filtdegree-1)/2;
wc=obj.f_cutoff/(obj.fsamp/2);
B = wc*sinc(wc*(-g:g));
A=1;
case 10
% Gaussian Filter
%check if order ist multiple of 1/2
blocklen=obj.signal_length;
faxis=linspace(-obj.fsamp/2,obj.fsamp/2,blocklen+1)';%generates arow vector faxis of blocklen+1 points linearly spaced between and including -para.fs/2 and para.fs/2
faxis=ifftshift(faxis(1:end-1));
H=exp(-((faxis)/(obj.f_cutoff*2)).^(2*obj.filtdegree)*log(2)*2^(2*obj.filtdegree-1));
end
% Build Filter from coefficients
if filterType ~= 10
[H,w] = freqz(B, A, obj.signal_length,'whole');
end
end
function show(obj)
obj.signal_length = 1024;
[H,w] = obj.buildFilter(obj.filterType);
plot(w,20*log10(abs(H)))
%freqz(H);
end
end
end