% The Linkwitz-Riley digital crossover
%
% -----------------------------------------------------------------------------------
%
% (C) 2017, Krzysztof Sozanski, Digital Signal Processing in Power Electronics Control 
% Circuits, second edition, Springer-Verlag, London, 2017. 
% 
% -----------------------------------------------------------------------------------
%
%
% Linkwitz-Riley digital crrosover
clear all; close all; grub_lin=2; roz_fon=10; 
fs=44100; N=2^12;
fcr=3000; % crrosing frequency
[bd ad]=butter(2,2*fcr/fs);[bg ag]=butter(2,2*fcr/fs,'high');
input=zeros(1,N); input(1,1)=1; % impulse
out_L=filter(bd,ad,input); out_L=filter(bd,ad,out_L);
out_H=filter(bg,ag,input); out_H=filter(bg,ag,out_H);
out_sum=out_L+out_H;
t=(0:N-1)/fs;
ff=(0:N-1)*fs/N;
figure('Name','Responses of LR crossover','NumberTitle','off');
subplot(211), 
plot(t,out_L,'+',t,out_H,'*',t,out_sum,'o','linewidth',grub_lin);  
grid on; set(gca,'FontSize',[roz_fon],'FontWeight','n'),xlabel('Time [s]'), 
ylabel('Amplitude'), title('(a)'); axis([0 0.0005 -0.3 1.0]);
legend('Low','High','Sum');
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);
axis([0 0.5*fs -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_LR_crossover.pdf','-dpdf');
