

function [OUT] = MEMsumsyn(Outar,tarm_cpl_fft,refm_cpl_fft,tars_cpl_fft,refs_cpl_fft)%#codegen

% target MID match reference side
mid_match_fft = refm_cpl_fft ./ tarm_cpl_fft;
%% ---filter frquency MID---
%  pole = 1/50*ones(50,1);
%  AA=filter(pole,1,mid_match_fft');
yupperm = envelope(mid_match_fft,10,'peak');
ym=yupperm.*(yupperm>0);
MIDF=ym;
Msize=length(ym);
% [MIDF,LOCM]=findpeaks(ym,'MinPeakDistance',5,'MinPeakProminence',0.01);
% check odd or even length 
% if mod(length(MIDF),2)==0
%     MIDF=[ym(1),ym(2),ym(3),MIDF(4:end-3),ym(end-2),ym(end-1),ym(end)];
%     Msize=length(MIDF);
% else
%     MIDF=[ym(1),ym(1),ym(2),ym(3),MIDF(4:end-3),ym(end-2),ym(end-1),ym(end)];
%     Msize=length(MIDF);
% end

% disp('Filtering mid matching FFT...')
mid_match_filter=zeros(1,Msize);
mid_match_filter(1:(Msize/2)+1)=MIDF(1:(Msize/2)+1);
% Remove DC offset,
mid_match_filter(Msize/2+2:Msize)=MIDF(Msize/2:-1:2);
mid_match_filter(1) = 0;
% midMatchingFftFiltered(2) = AA(2);
% midMatchingFftFiltered(fftSize) = mid_match_fft(fftSize);
% disp('Calculating mid FIR for matching EQ...')
% mid_match_filter=mid_match_filter/max(abs(mid_match_filter));
midFirTmp = real(ifft(mid_match_filter));
midFir1 = ifftshift(midFirTmp);
Midb=find(abs(midFir1)>abs(mean(midFir1)-std(midFir1)),1);
gap=(Msize/2)-Midb;
midFir=midFir1((Msize/2)+(-gap:gap+1));
% midFir=midFir/max(abs(midFir));
midFirHann = midFir .* hann(2*gap+2)';


% target side match reference side
sid_match_fft = refs_cpl_fft ./ tars_cpl_fft;
%% ----filter frquency SID---
% pole = 1/256*ones(256,1);
% BB=filter(pole,1,sid_match_fft');
yuppers = envelope(sid_match_fft,10,'peak');
ys=yuppers.*(yuppers>0);
SIDF=ys;
Ssize=length(ys);
% SIDF=findpeaks(ys,'MinPeakDistance',10,'MinPeakProminence',0.01);
% % check odd or even length 
% if mod(length(SIDF),2)==0
%     SIDF=[ys(1),ys(2),ys(3),SIDF(4:end-3),ys(end-2),ys(end-1),ys(end)];
%     Ssize=length(SIDF);
% else
%     SIDF=[ys(1),ys(1),ys(2),ys(3),SIDF(4:end-3),ys(end-2),ys(end-1),ys(end)];
%     Ssize=length(SIDF);
% end

% disp('Filtering side matching FFT...')
sid_match_filter=zeros(1,Ssize);
sid_match_filter(1:Ssize/2+1)=SIDF(1:Ssize/2+1);
% Remove DC offset,
sid_match_filter(Ssize/2+2:Ssize)=SIDF(Ssize/2:-1:2);
sid_match_filter(1) = 0;
% sideMatchingFftFiltered(2) = BB(2);
% sideMatchingFftFiltered(fftSize) = sid_match_fft(fftSize);
% disp('Calculating side FIR for matching EQ...')
% sid_match_filter=sid_match_filter/max(abs(sid_match_filter));
sideFirTemp = real(ifft(sid_match_filter));
sideFir1 = ifftshift(sideFirTemp);
Sidb=find(abs(sideFir1)>abs(mean(sideFir1)-std(sideFir1)),1);
gap=(Ssize/2)-Sidb;
sidFir=sideFir1((Ssize/2)+(-gap:gap+1));
sideFirHann =sidFir .* hann(2*gap+2)';

%% CONVOLUTION two mid and ref
%disp('Convolving target with calculated FIRs...')
outmid = Outar(:, 1)';
outmid2 = Nfconv(outmid, midFirHann);
outsid = Outar(:, 2)';
outsid2 = Nfconv(outsid, sideFirHann);
%disp('Cutting convolution edges and converting MS to LR...')
% outputStage2Mid = outputStage2MidTemp( (fftSize / 2 + 1) : (size(outputStage2MidTemp, 2) - fftSize / 2 + 1) )';
% outputStage2Side = outputStage2SideTemp( (fftSize / 2 + 1) : (size(outputStage2SideTemp, 2) - fftSize / 2 + 1) )';

% outputStage2 for left and right channel
M=size(outmid,2);
OUT=zeros(M,2);
OUT(:, 1) = (outmid2 + outsid2)';
OUT(:, 2) = (outmid2 - outsid2)';

end