mc


%% Make test AOs

nsecs = 10000;
fs    = 10;

pl = plist('nsecs', nsecs, 'fs', fs, 'tsfcn', 'sin(2*pi*7.433*t) + randn(size(t))');

a1 = ao(pl);
a1.setName('a1');

%% Make two filters

lp = miir(plist('type', 'lowpass', 'fs', fs, 'fc', 0.1, 'order', 5));
hp = miir(plist('type', 'highpass', 'fs', fs, 'fc', 1, 'order', 2));

%% Serial

a1lp = filter(a1, lp);
a1lphp = filter(a1lp, hp);

a1f = filter(a1, [lp hp], plist('bank', 'serial'));

iplot(a1lphp.psd, a1f.psd, plist('LineStyles', {'', '--'}))

%% Parallel

a1lp = filter(a1, lp);
a1lphp = a1lp + filter(a1, hp);

a1f = filter(a1, [lp hp]);

iplot(a1lphp.psd, a1f.psd, plist('LineStyles', {'', '--'}))

%% Test filter history serial

nsecs = 100;
fs    = 10;

pl = plist('nsecs', nsecs, 'fs', fs, 'tsfcn', 'sin(2*pi*0.433*t)');

a1 = ao(pl);
a1.setName('a1');

segs = split(a1, plist('split_type', 'chunks', 'n', 2));

filts = [lp hp];

% full
[a1lps, filts] = filter(a1, filts, plist('bank', 'serial'));

% segement 1
[a1lps1, filts] = filter(segs(1), filts, plist('bank', 'serial'));

% segement 2
a1lps2 = filter(segs(2), filts, plist('bank', 'serial'));

iplot(a1lps1, a1lps2, a1lps, plist('Xranges', [49 51], 'markers', {'o', 'o', ''}))

%% Test filter history parallel

nsecs = 100;
fs    = 10;

pl = plist('nsecs', nsecs, 'fs', fs, 'tsfcn', 'sin(2*pi*0.433*t)');

a1 = ao(pl);
a1.setName('a1');

segs = split(a1, plist('split_type', 'chunks', 'n', 2));

filts = [lp hp];

% full
[a1lps, filts] = filter(a1, filts);

% segement 1
[a1lps1, filts] = filter(segs(1), filts);

% segement 2
a1lps2 = filter(segs(2), filts);

iplot(a1lps1, a1lps2, a1lps, plist('Xranges', [49 51], 'markers', {'o', 'o', ''}))

%% Test Serial with different units

a1.setYunits('m');

% This should output N
iunits1 = unit('m');
ounits1 = unit('V');
iunits2 = unit('V');
ounits2 = unit('N');

lp = miir(plist('type', 'lowpass', 'fs', fs, 'fc', 0.1, 'order', 5,'iunits', iunits1, 'ounits', ounits1));
hp = miir(plist('type', 'highpass', 'fs', fs, 'fc', 1, 'order', 2,'iunits', iunits2, 'ounits', ounits2));

a1f = filter(a1, [lp hp], plist('bank', 'serial'));

%% Test Parallel with different units

% This must not to be allowed
iunits1 = unit('m');
ounits1 = unit('V');
iunits2 = unit('m');
ounits2 = unit('V');

lp = miir(plist('type', 'lowpass', 'fs', fs, 'fc', 0.1, 'order', 5,'iunits', iunits1, 'ounits', ounits1));
hp = miir(plist('type', 'highpass', 'fs', fs, 'fc', 1, 'order', 2,'iunits', iunits2, 'ounits', ounits2));

a1f = filter(a1, [lp hp], plist('bank', 'parallel'));


