function stateInn = sodaCalcInnovations(sodaPar,kalmanGain,perturbedObs,measOperator,...
        mStatesOutEnsemble,stateInn,iIter)
    
% for qq = 1:States.N,
%    Inn(tt,1:States.n,qq) = (Kg(:,:,tt)*(D(qq,1:States.n)'-(H*A(tt,1:States.n,qq))'))';
% end;

% Inn(iIter,:,:) = kalmanGain*perturbedObs(iIter,:)'-...
%     measOperator*mStatesOutEnsemble


nMembers = sodaPar.nEnsembleMembers;
nStatesKF = sodaPar.nStatesKF;

KG = shiftdim(kalmanGain(iIter+1,1:nStatesKF,1:nStatesKF),1);

for iMember = 1:nMembers
    stateInn(iIter+1,1:nStatesKF,iMember) = (KG*...
        (perturbedObs(iIter+1,1:nStatesKF,iMember)'-...
        (measOperator*mStatesOutEnsemble(iIter+1,1:nStatesKF,iMember))'))';
end
