% The strictly complementary (SC) digital crossover
%
% -----------------------------------------------------------------------------------
%
% (C) 2017, Krzysztof Sozanski, Digital Signal Processing in Power Electronics Control 
% Circuits, second edition, Springer-Verlag, London, 2017. 
% 
% -----------------------------------------------------------------------------------
%
%
clear all; close all;
grub_lin=2; roz_fon=10;
N=2^11; fs=44100; 
fg=10; fz=6000; Rp=0.0005; Rz=0.0005;
[n,Wn,beta,typ] = kaiserord([fg fz],[1 0],[Rp Rz],fs)
if rem(n,2)~=0
   n=n+1;  %Filter order should be even 
end
bd = fir1(n, Wn, typ, kaiser(n+1,beta), 'noscale');
ad=1; ag=1;
[bg,ag]=stricomp(bd,ad);
input=zeros(1,N); input(1,1)=1;
out_L=filter(bd,ad,input); out_H=filter(bg,ag,input);
out_sum=out_L+out_H;
tt=(0:N-1)/fs; ff=(0:N-1)*fs/N;
figure('Name','Responses of SC crossover','NumberTitle','off');
subplot(211), 
plot(tt,out_L,'+',tt,out_H,'*',tt,out_sum,'o','linewidth',grub_lin);  
grid on; set(gca,'FontSize',[roz_fon],'FontWeight','n'),xlabel('Time [s]'), 
ylabel('Amplitude'), title('(a)'); legend('Low','High','Sum');
set(gca,'ylim',[-0.2 1.0]);set(gca,'xlim',[0 n/fs]),
subplot(212), 
plot(ff,20*log10(abs(fft(out_L))+eps),ff,20*log10(abs(fft(out_H))+eps), ...
    ff,20*log10(abs(fft(out_sum))+eps),'linewidth',grub_lin); 
set(gca,'xlim',[0 0.5*fs]);set(gca,'ylim',[-100 10]);
set(gca,'FontSize',[roz_fon],'FontWeight','n'), xlabel('Frequency [Hz]'); 
ylabel('Magnitude [dB]');title('(b)'); grid on;
legend('Low','High','Sum');
print('Freq_SC_crossover.pdf','-dpdf');

