% ======================================================================
%> @brief Brief description of the segSNR function
%>
%> This function provides segmental SNR calculations in time domain.
%> Following equations are used to compute SNR in time domain.
%> \latexonly 
%> \begin{equation}\label{SNRseg}
%> \mathrm{SNRseg}=\frac{10}{M}\sum_{m=0}^{M-1}\mathrm{log}_{10}\frac{\sum_{n=Nm}^{Nm+N-1}x^2(n)}{\sum_{n=Nm}^{Nm+N-1}\Big(x(n)-\widehat{x}(n)\Big)^2},
%> \end{equation} 
%>
%> \begin{equation}\label{SNRsegR}
%> \mathrm{SNRseg_{R}}=\frac{10}{M}\sum_{m=0}^{M-1}\mathrm{log}_{10}\left(1+\frac{\sum_{n=Nm}^{Nm+N-1}x^2(n)}{\sum_{n=Nm}^{Nm+N-1}\Big(x(n)-\widehat{x}(n)\Big)^2}\right),
%> \end{equation}
%> \endlatexonly
%> @param rx Contains signal x reshaped into matrix where one row is one
%>  frame.
%> @param ry Same as rx but this contains signal y
%> @retval SNRseg
%> @retval SNRsegR
% =====================================================================

function [ SNRseg,SNRsegR ] = SegSNR( rx, ry )
%UNTITLED5 Summary of this function goes here
%   Detailed explanation goes here
dim=size(rx);
b(1,dim(1))=0;
c=b;d=b;e=b;
for i=1:dim(1);
b(i)=sum ((rx(i,:)).^2);
c(i)=sum ((rx(i,:)-ry(i,:)).^2);
d(i)=log10(b/c);
    if  d(i)<-10^6;
        d(i)=-10^6;
    else d(i)=d(i);  
    end 
e(i)=log10(1+(b/c));
end
SNRseg=(10/dim(1))*sum(d);
SNRsegR=(10/dim(1))*sum(e);
fid = fopen('SNRseg.txt', 'a+');
fseek(fid,0,'eof');
fprintf(fid, ' %f ',SNRseg);
fclose(fid);
fid = fopen('SNRsegR.txt', 'a+');
fseek(fid,0,'eof');
fprintf(fid, ' %f ',SNRsegR);
fclose(fid);
end

