function varargout = resultados_masking(varargin)
% RESULTADOS_MASKING M-file for resultados_masking.fig
%      RESULTADOS_MASKING, by itself, creates a new RESULTADOS_MASKING or raises the existing
%      singleton*.
%
%      H = RESULTADOS_MASKING returns the handle to a new RESULTADOS_MASKING or the handle to
%      the existing singleton*.
%
%      RESULTADOS_MASKING('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in RESULTADOS_MASKING.M with the given input arguments.
%
%      RESULTADOS_MASKING('Property','Value',...) creates a new RESULTADOS_MASKING or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before resultados_masking_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to resultados_masking_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help resultados_masking

% Last Modified by GUIDE v2.5 26-Nov-2006 13:07:44

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @resultados_masking_OpeningFcn, ...
                   'gui_OutputFcn',  @resultados_masking_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before resultados_masking is made visible.
function resultados_masking_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to resultados_masking (see VARARGIN)

% Choose default command line output for resultados_masking
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes resultados_masking wait for user response (see UIRESUME)
% uiwait(handles.figure1);

global maskb y fs n select Xgrid prob medias vars d;

% Graficas de señales y mascaras
tiempo=(1/fs)*[1:length(y(1,:))];
axes(handles.grafica1);
plot(tiempo,y(1,:));axis tight;
axes(handles.grafica2);
plot(tiempo,y(2,:));axis tight;
axes(handles.graficaMask1);
imagesc(squeeze(maskb(:,:,1)));
axis off;
axes(handles.graficaMask2);
imagesc(squeeze(maskb(:,:,2)));
axis off;

% Ahora las graficas de los retardos
[Ni,ax]=hist(select,Xgrid);

aux=(-2:.01:2).';
Laux=length(aux);
faux=zeros(size(aux));
for k=1:n    
  fk_por_Prob(1:Laux,k)=prob(k)*1/(sqrt(2*pi*vars(k)))*exp(-(aux-medias(k)).^2/(2*vars(k)));
  faux=faux+fk_por_Prob(:,k);
end

axes(handles.graficaRetardos1);
bar(ax,Ni/length(select)*100/(max(select)-min(select)));
hold on;
plot(aux,faux,'r','LineWidth',2);

%%%%%%%%%%%%%%%%%%%%%%%%
mediangulos=(180/pi)*asin(medias*344/(fs*d(1)));
varangulos=(180/pi)*asin(vars*344/(fs*d(1)));
% Ahora la grafica de los angulos
aux_ang=(-180:.05:180).';
Laux_ang=length(aux_ang);
faux_ang=zeros(size(aux_ang));
for k=1:n    
  fk_por_Prob_ang(1:Laux_ang,k)=prob(k)*1/(sqrt(2*pi*varangulos(k)))*exp(-(aux_ang-mediangulos(k)).^2/(2*varangulos(k)));
  faux=faux_ang+fk_por_Prob_ang(:,k);
end
axes(handles.graficaRetardos2);
plot(aux_ang,fk_por_Prob_ang,'--');
%%%%%%%%%%%%%%%%%%%%%%%%
%axes(handles.graficaRetardos2);
%plot(aux,fk_por_Prob,'--');

if n==3
    set(handles.grafica3,'Visible','on');
    axes(handles.grafica3);
    plot(tiempo,y(3,:));axis tight;
    set(handles.reproducir_y3,'Enable','on');
    set(handles.specgram_y3,'Enable','on');
    set(handles.text4,'Visible','on');
    set(handles.graficaMask3,'Visible','on');
    axes(handles.graficaMask3);
    imagesc(squeeze(maskb(:,:,3)));
    axis off;
end

% --- Outputs from this function are returned to the command line.
function varargout = resultados_masking_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in reproducir_y2.
function reproducir_y2_Callback(hObject, eventdata, handles)
% hObject    handle to reproducir_y2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global y fs;
wavplay(y(2,:),fs);

% --- Executes on button press in specgram_y2.
function specgram_y2_Callback(hObject, eventdata, handles)
% hObject    handle to specgram_y2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global y;
specgramdemo(y(2,:));

% --- Executes on button press in reproducir_y3.
function reproducir_y3_Callback(hObject, eventdata, handles)
% hObject    handle to reproducir_y3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global y fs;
wavplay(y(3,:),fs);

% --- Executes on button press in specgram_y3.
function specgram_y3_Callback(hObject, eventdata, handles)
% hObject    handle to specgram_y3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global y;
specgramdemo(y(3,:));

% --- Executes on button press in reproducir_y1.
function reproducir_y1_Callback(hObject, eventdata, handles)
% hObject    handle to reproducir_y1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global y fs;
wavplay(y(1,:),fs);

% --- Executes on button press in specgram_y1.
function specgram_y1_Callback(hObject, eventdata, handles)
% hObject    handle to specgram_y1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global y;
specgramdemo(y(1,:));

% --- Executes on button press in salir.
function salir_Callback(hObject, eventdata, handles)
% hObject    handle to salir (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

close(gcbf);
