function varargout = parametros_simulacion(varargin)
% PARAMETROS_SIMULACION M-file for parametros_simulacion.fig
%      PARAMETROS_SIMULACION, by itself, creates a new PARAMETROS_SIMULACION or raises the existing
%      singleton*.
%
%      H = PARAMETROS_SIMULACION returns the handle to a new PARAMETROS_SIMULACION or the handle to
%      the existing singleton*.
%
%      PARAMETROS_SIMULACION('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PARAMETROS_SIMULACION.M with the given input arguments.
%
%      PARAMETROS_SIMULACION('Property','Value',...) creates a new PARAMETROS_SIMULACION or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before parametros_simulacion_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to parametros_simulacion_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 parametros_simulacion

% Last Modified by GUIDE v2.5 12-Dec-2006 12:28:25

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @parametros_simulacion_OpeningFcn, ...
                   'gui_OutputFcn',  @parametros_simulacion_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 parametros_simulacion is made visible.
function parametros_simulacion_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 parametros_simulacion (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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

global n fs N NFFT w theta1 theta2 theta3;
NFFT=1024;
w=1;
N=2*floor(30e-3*fs/2);
if n==3
    set(handles.radioAnemuller,'Enable','off');
    set(handles.slider_theta3,'Enable','on');
end
theta1=0;
theta2=0;
theta3=0;


% --- Outputs from this function are returned to the command line.
function varargout = parametros_simulacion_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 during object creation, after setting all properties.
function tipoVentana_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tipoVentana (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in tipoVentana.
function tipoVentana_Callback(hObject, eventdata, handles)
% hObject    handle to tipoVentana (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns tipoVentana contents as cell array
%        contents{get(hObject,'Value')} returns selected item from tipoVentana
global w;
w=get(handles.tipoVentana,'Value');

% --- Executes during object creation, after setting all properties.
function duracionVentana_CreateFcn(hObject, eventdata, handles)
% hObject    handle to duracionVentana (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in duracionVentana.
function duracionVentana_Callback(hObject, eventdata, handles)
% hObject    handle to duracionVentana (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns duracionVentana contents as cell array
%        contents{get(hObject,'Value')} returns selected item from duracionVentana
global N fs;
N=get(handles.duracionVentana,'Value');
switch N
    case 1
        N=2*floor(30e-3*fs/2);
end


% --- Executes during object creation, after setting all properties.
function ptosFFT_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ptosFFT (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in ptosFFT.
function ptosFFT_Callback(hObject, eventdata, handles)
% hObject    handle to ptosFFT (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns ptosFFT contents as cell array
%        contents{get(hObject,'Value')} returns selected item from ptosFFT
global NFFT;
NFFT=get(handles.ptosFFT,'Value');
if NFFT==1
    NFFT=1024;
else
    NFFT=512;
end


% --- Executes during object creation, after setting all properties.
function edit_d12_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_d12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_d12_Callback(hObject, eventdata, handles)
% hObject    handle to edit_d12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_d12 as text
%        str2double(get(hObject,'String')) returns contents of edit_d12 as a double
global d12;
d12=1e-2*str2double(get(gcbo,'String'));


% --- Executes during object creation, after setting all properties.
function edit_d23_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_d23 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_d23_Callback(hObject, eventdata, handles)
% hObject    handle to edit_d23 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_d23 as text
%        str2double(get(hObject,'String')) returns contents of edit_d23 as a double
global d23;
d23=1e-2*str2double(get(gcbo,'String'));



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

% Hint: get(hObject,'Value') returns toggle state of radioMasking
set(handles.radioAnemuller,'Value',0);
set(handles.radioSubbandas,'Value',0);


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

% Hint: get(hObject,'Value') returns toggle state of radioAnemuller

set(handles.radioMasking,'Value',0);
set(handles.radioSubbandas,'Value',0);

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

% Hint: get(hObject,'Value') returns toggle state of radioSubbandas

set(handles.radioAnemuller,'Value',0);
set(handles.radioMasking,'Value',0);

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

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

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

global x;
% Quito los ceros del final para el correcto escalado del espectrograma
for i=length(x(1,:)):-1:1
    if x(1,i)>1.5e-3
        break;
    end
end
aux=x(1,1:i+1);
specgramdemo(aux);

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

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

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

global x;
% Quito los ceros del final para el correcto escalado del espectrograma
for i=length(x(2,:)):-1:1
    if x(2,i)>1.5e-3
        break;
    end
end
aux=x(2,1:i+1);
specgramdemo(aux);

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

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

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

global x;
% Quito los ceros del final para el correcto escalado del espectrograma
for i=length(x(3,:)):-1:1
    if x(3,i)>1.5e-3
        break;
    end
end
aux=x(3,1:i+1);
specgramdemo(aux);

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

if get(handles.radioAnemuller,'Value')==0 && get(handles.radioMasking,'Value')==0 && get(handles.radioSubbandas,'Value')==0
    mensaje1;
else
    mensaje2;drawnow;
    global s fs d12 d23 theta1 theta2 theta3 w NFFT N n;
    global d;
    if get(handles.radioAnemuller,'Value')==1
        global metodo;
        metodo='a';
        d=d12;
        theta=[theta1,theta2];
    elseif get(handles.radioMasking,'Value')==1
        global metodo;
        metodo='m';
        if n==2
            theta=[theta1,theta2];
            d=d12;
        else
            theta=[theta1,theta2,theta3];
            d=[d12,d23];
        end
    elseif get(handles.radioSubbandas,'Value')==1
        global metodo;
        metodo='s';
        if n==2
            theta=[theta1,theta2];
            d=d12;
        else
            theta=[theta1,theta2,theta3];
            d=[d12,d23];
        end
    end
    
    % Ventana
    global win noverlap;
    switch w
        case 1
            win=window(@hann,N);
            noverlap=N/2;
        case 2
            win=window(@triang,N);
            noverlap=N/2;
        case 3
            win=window(@rectwin,N);
            noverlap=0;
        case 4
            win=window(@blackman,N);
            noverlap=floor(2*N/3);
    end
    
    global x X F T;
    [x,X,F,T]=genera_mezcla(s,fs,d,theta,win,NFFT,noverlap,metodo);
    
    % Ahora las graficas
    set(handles.grafica1,'Visible','on');
    set(handles.grafica2,'Visible','on');
    tiempo=(1/fs)*[1:length(x(1,:))];
    if length(x(:,1))==3
        set(handles.grafica3,'Visible','on');
        axes(handles.grafica3);
        plot(tiempo,x(3,:));axis tight;
        set(handles.reproducir3,'Enable','on');
        set(handles.specgram3,'Enable','on');
    end
    axes(handles.grafica1);
    plot(tiempo,x(1,:));axis tight;
    axes(handles.grafica2);
    plot(tiempo,x(2,:));axis tight;
    set(handles.reproducir1,'Enable','on');
    set(handles.specgram1,'Enable','on');
    set(handles.reproducir2,'Enable','on');
    set(handles.specgram2,'Enable','on');
    
    % Ahora deshabilito los botones correspondientes para que no se pueda
    % cambiar el metodo de separacion
    set(handles.radioMasking,'Enable','off');
    set(handles.radioAnemuller,'Enable','off');
    set(handles.radioSubbandas,'Enable','off');
    
    close(mensaje2);
end


% --- 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);

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

global metodo;
if get(handles.radioAnemuller,'Value')==0 && get(handles.radioMasking,'Value')==0 && get(handles.radioSubbandas,'Value')==0
    mensaje1;
else
    mensaje3;drawnow;
    global X F T fs n win N NFFT noverlap metodo;
    global y Y s S d;
    switch metodo
        % Masking
        case 'm'
            global maskb select Xgrid prob medias vars;
            [Y,maskb,select,Xgrid,prob,medias,vars]=separa_masking(X,F,T,fs,n);
            for k=1:n
                y(k,:)=real(ISTFT_overlapadd(squeeze(Y(:,:,k)),NFFT,fs,win,noverlap));
            end
            close(mensaje3);
            close(gcbf);
            resultados_masking(maskb,y,fs,n,select,Xgrid,prob,medias,vars,d);
        % Anemuller
        case 'a'
            Y=separa_anemuller(X,F);
            for k=1:n
                y(k,:)=real(ISTFT_overlapadd(squeeze(Y(:,:,k)),NFFT,fs,win,noverlap));
                % Ajusto el escalado
                y(k,:)=y(k,:)/max(y(k,:));
            end
            close(mensaje3);
            close(gcbf);
            resultados_separacion(y,fs);
        % Subbandas
        case 's'
            Y=separa_freq(X,n);
            for k=1:n
                S(:,:,k)=specgram(s(k,:),NFFT,fs,win,noverlap);
                y(k,:)=real(ISTFT_overlapadd(squeeze(Y(:,:,k)),NFFT,fs,win,noverlap));
            end
            close(mensaje3);
            close(gcbf);
            resultados_subbandas(y,Y,s,S,fs,F,T);
    end
end


% --- Executes during object creation, after setting all properties.
function slider_theta1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_theta1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider_theta1_Callback(hObject, eventdata, handles)
% hObject    handle to slider_theta1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

global theta1;
theta1=get(handles.slider_theta1,'Value');
set(handles.text_theta1,'String',num2str(theta1,3));

% --- Executes during object creation, after setting all properties.
function slider_theta2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_theta2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider_theta2_Callback(hObject, eventdata, handles)
% hObject    handle to slider_theta2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

global theta2;
theta2=get(handles.slider_theta2,'Value');
set(handles.text_theta2,'String',num2str(theta2,3));

% --- Executes during object creation, after setting all properties.
function slider_theta3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_theta3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider_theta3_Callback(hObject, eventdata, handles)
% hObject    handle to slider_theta3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


global theta3;
theta3=get(handles.slider_theta3,'Value');
set(handles.text_theta3,'String',num2str(theta3,3));
