% ======================================================================
%> @brief Brief description of the fwSNRseg function
%>
%> This function provides segmental SNR calculations in frequency domain.
%> Following equation is used to compute SNR in frequency domain.
%> \latexonly
%> \begin{equation}
%> \mathrm{fwSNRseg}=\frac{10}{M}\sum_{m=0}^{M-1}\frac{\sum_{j=1}^{K}B_j\ \mathrm{log}_{10}\Bigg[\frac{F^2(m,j)}{\big(F(m,j)-\widehat{F}(m,j)\big)^2}\Bigg]}{\sum_{m=0}^{M-1}B_j},
%> \end{equation}
%> \endlatexonly
%> @param CF Contains center filter frequencies (Hz)
%> @param CFW Contains corresponding articulation index weights used for
%>  computing weighted spectral distance measure.
%> @param rx Contains signal x reshaped into matrix where one row is one
%>  frame.
%> @param ry Same as rx but this contains signal y
%> @param FS Contains sampling frequency
%> @param l Contains number of samples per frame.
%> @retval fwSNRseg
% =====================================================================


function [ fwSNRseg ] = FreqSNR( CF, CFW, rx, ry, FS, l )
%UNTITLED6 Summary of this function goes here
%   Detailed explanation goes here
CFF=1:(FS/l):FS;
ftrx=abs(fft(rx));
ftry=abs(fft(ry));
dim=size(rx);
%wrx=0;
seg(dim(1))=0;
for m=1:dim(1);
    wrx=0;
    for j=1:length(CF)-1;
    w=(CFF>=CF(j) & CFF<CF(j+1));
    %wrx=wrx+(CFW(j)*log10(((w.*ftrx(m,:)).^2)/(w.*ftrx(m,:)-w.*ftry(m,:)).^2)); 
    wrx(j)=(CFW(j)*log10(((w.*ftrx(m,:)).^2)/(w.*ftrx(m,:)-w.*ftry(m,:)).^2));
        if   wrx(j)<-1000000;
             wrx(j)=-1000000;
        else wrx(j)=wrx(j);
        end    
        if   wrx(j)>1000000;
             wrx(j)=1000000;
        else wrx(j)=wrx(j);    
        end 
        if   wrx(j)==0;
             wrx(j)=0.0000001;
        else wrx(j)=wrx(j);    
        end         
    end
    seg(m)=sum(wrx)/sum(CFW);
end
fwSNRseg=(10/dim(1))*sum(seg);

fid = fopen('fwSNRseg.txt', 'a+');
fseek(fid,0,'eof');
fprintf(fid, '%f ',fwSNRseg);
fclose(fid);
end

