

function [Outar,tarm_cpl_fft,tars_cpl_fft] = MEMsumtar(target,referenceMatchingRms,tarfz)%#codegen
% Initial Paremeters
% tarfz=44100;
fftSize=4096;
% convert windows size
timeArea=15;
areaSize = timeArea * tarfz;
tarsiz = size(target, 1);
%% Matching RMS
tarm= (target(:, 1) + target(:, 2)) ./ 2;
tars= (target(:, 1) - target(:, 2)) ./ 2;
% sizes of parts
tarN = fix(tarsiz / areaSize) + 1;
tarPn = fix(tarsiz / tarN);
% get rms of ref
tarm_RMS = zeros(1, tarN);
for i = 1 : 1 : tarN
    tarm_RMS(i) = rms(tarm( (tarPn * (i - 1) + 1) : (tarPn * i) ));
end
% May add array preallocation here in future
% position beyond the average RMS mid
tar_RMSB_P= find(tarm_RMS>=rms(tarm_RMS));
tar_extra=tarm_RMS>=rms(tarm_RMS);
tarm_an = zeros(sum(tar_extra,2), tarPn);
tars_an = zeros(sum(tar_extra,2), tarPn);
for i = 1 : 1 :sum(tar_extra,2)
    tarm_an(i, :) = tarm( (tarPn * (tar_RMSB_P(i) - 1) + 1) : (tarPn * tar_RMSB_P(i)) );
    tars_an(i, :) = tars( (tarPn * (tar_RMSB_P(i) - 1) + 1) : (tarPn * tar_RMSB_P(i)) );
end
tar_an_rms = zeros(1, sum(tar_extra,2));
for i = 1 : 1 : sum(tar_extra,2)
    tar_an_rms(i) = tarm_RMS(tar_RMSB_P(i));
end
% target mid match reference mid
targetMatchingRms = rms(tar_an_rms);
if targetMatchingRms < 0.000001
    targetMatchingRms = 0.000001;
end
rmslearn = referenceMatchingRms / targetMatchingRms;
Outar=zeros(size(tarm,1),2);
Outar(:, 1) = tarm .* rmslearn;  % Left channel of this is mid
Outar(:, 2) = tars .* rmslearn; % Right channel of this is side
tarm_an = tarm_an .* rmslearn;
tars_an = tars_an .* rmslearn;
%% Matching FR
tarm_fft_N = fix(tarPn / fftSize);
tars_fft_N = tarm_fft_N;
%----tar MID channel----
tarm_av_fft = zeros(sum(tar_extra,2),fftSize);
for j = 1 : 1 : sum(tar_extra,2)
    tarm_fft = zeros(tarm_fft_N, fftSize);
    for i = 1 : 1 : tarm_fft_N
        tarm_fft(i, :) = abs(fft(tarm_an(j, (fftSize * (i - 1) + 1) : (fftSize * i) ))/fftSize);
    end
    if size(tarm_fft, 1) == 1
        tarm_av_fft(j, :) = tarm_fft(1,:);
    else
        tarm_av_fft(j, :) = sum(tarm_fft) ./ tarm_fft_N;
    end
end
% average FFT of reference mid channel
if size(tarm_av_fft, 1) == 1
    tarm_cpl_fft = tarm_av_fft;
else
    tarm_cpl_fft = sum(tarm_av_fft) ./ size(tarm_an, 1);
end
% target mid match reference mid
tarm_cpl_fft(tarm_cpl_fft <0.0001) =0.0001;

%----tars SID channel----
tars_av_fft = zeros(size(tars_an, 1), fftSize);
for j = 1 : 1 : size(tars_an, 1)
    tars_fft = zeros(tars_fft_N, fftSize);
    for i = 1 : 1 : tars_fft_N
        tars_fft(i, :) = abs(fft(tars_an(j, (fftSize * (i - 1) + 1) : (fftSize * i) ))/fftSize);
    end
    if size(tars_fft, 1) == 1
        tars_av_fft(j, :) = tars_fft(1,:);
    else
        tars_av_fft(j, :) = sum(tars_fft) ./ tars_fft_N;
    end
end
%disp('Calculating average FFT of target side...')
if size(tars_av_fft, 1) == 1
    tars_cpl_fft = tars_av_fft;
else
    tars_cpl_fft = sum(tars_av_fft) ./ size(tars_an, 1);
end
% disp('Calculating side matching FFT...')
tars_cpl_fft(tars_cpl_fft < 0.0001) = 0.0001;


end