% IIR filter with linear phase response
%
% -----------------------------------------------------------------------------------
%
% (C) 2017, Krzysztof Sozanski, Digital Signal Processing in Power Electronics Control 
% Circuits, second edition, Springer-Verlag, London, 2017. 
% 
% -----------------------------------------------------------------------------------
%
clear all;
fs=352.8e3; % sampling frequency
fb=20e3; % end of band of interest
fsyg=19e3; % signal frequency
ib=6; % number of blocks
N=2048; % length of block
Ns=N*ib; % length of input signal
t=(0:Ns-1)/fs; % time vector
y=zeros(1,Ns); %space for y
% coherent frequency of input signal
fsyg_k=round(fsyg/(fs/N))*fs/N; 
x=sin(2*pi*fsyg_k*t); % input signal
% ------ Filter design ---------------
Fg=2*fb/fs;
[b a]=ellip(8,0.1,63,Fg,'low');
% ----- Causal filtering -------------
y1=filter(b,a,x);
% Non-causal filtering, time reversing 
N_ov=1024; % number of samples in overlap
y3=zeros(1,N+N_ov); %space for y3
for nb=1:ib-1
% time reversing and filtering 
y3=filter(b,a,y1(nb*N+N_ov:-1:(nb-1)*N+1));
% time reversing
y3=y3(N+N_ov:-1:N_ov+1);
% output signal synthesis  
y(1,(nb-1)*N+1:nb*N)=y3;
end