Start Commit
start implementation of class based simulation of a IM/DD communication system. Mostly based on Move-It but cleaned up and with focus on direct detection, however I try to keep the versatility of move-it alive.
This commit is contained in:
139
Classes/Filter.m
Normal file
139
Classes/Filter.m
Normal file
@@ -0,0 +1,139 @@
|
||||
classdef Filter
|
||||
%FILTER Summary of this class goes here
|
||||
% Detailed explanation goes here
|
||||
|
||||
properties
|
||||
H
|
||||
filterType
|
||||
f_cutoff
|
||||
|
||||
|
||||
signal_length
|
||||
filtdegree
|
||||
passband_ripple
|
||||
stopband_ripple
|
||||
fdac
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = Filter(options)
|
||||
%FILTER Construct an instance of this class
|
||||
% Detailed explanation goes here
|
||||
arguments
|
||||
options.filterType = 1;
|
||||
options.f_cutoff = 0;
|
||||
options.fdac = 0;
|
||||
options.filtdegree = 3;
|
||||
options.passband_ripple = 0.5;
|
||||
options.stopband_ripple = 0.5;
|
||||
end
|
||||
|
||||
|
||||
obj.filterType = options.filterType;
|
||||
|
||||
|
||||
obj.f_cutoff = options.f_cutoff;
|
||||
obj.filtdegree = options.filtdegree;
|
||||
obj.passband_ripple = options.passband_ripple;
|
||||
obj.stopband_ripple = options.stopband_ripple;
|
||||
obj.fdac = options.fdac;
|
||||
|
||||
end
|
||||
|
||||
function yout = process(obj,xin)
|
||||
|
||||
obj.signal_length = length(xin);
|
||||
|
||||
obj.H = 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 = buildFilter(obj,filterType)
|
||||
|
||||
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.fdac);
|
||||
|
||||
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.fdac);
|
||||
[B ,A] = zp2tf(Z ,P ,K);
|
||||
|
||||
case 3
|
||||
|
||||
% Butterworth filter
|
||||
if obj.lowpass == 1 %lowpass
|
||||
[B, A] = butter(obj.filtdegree, obj.f_cutoff/(obj.fdac/2),'low');
|
||||
else % highpass
|
||||
[B, A] = butter(obj.filtdegree, obj.f_cutoff/(obj.fdac/2),'high');
|
||||
end
|
||||
|
||||
case 4
|
||||
|
||||
% Chebyshev 1 filter
|
||||
|
||||
[B, A] = cheby1(obj.filtdegree,rp, obj.f_cutoff/(obj.fdac/2));
|
||||
|
||||
case 5
|
||||
|
||||
% Chebyshev 2 filter
|
||||
|
||||
[B, A] = cheby2(obj.filtdegree,rs, obj.f_cutoff/(obj.fdac/2));
|
||||
|
||||
case 6
|
||||
|
||||
% Elliptic filter
|
||||
|
||||
[B, A] = ellip(obj.filtdegree,rp,rs,obj.f_cutoff/(obj.fdac/2));
|
||||
|
||||
case 7
|
||||
|
||||
% Hamming filter
|
||||
|
||||
g=(obj.filtdegree-1)/2;
|
||||
wc=obj.f_cutoff/(obj.fdac/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.fdac);
|
||||
A=1;
|
||||
|
||||
case 9
|
||||
|
||||
% Sinc filter
|
||||
|
||||
g=(obj.filtdegree-1)/2;
|
||||
wc=obj.f_cutoff/(obj.fdac/2);
|
||||
B = wc*sinc(wc*(-g:g));
|
||||
A=1;
|
||||
|
||||
end
|
||||
|
||||
H = freqz(B, A, obj.signal_length,'whole');
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user