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

% Last Modified by GUIDE v2.5 26-Nov-2006 17:58:40

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

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

% Update handles structure
guidata(hObject, handles);

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

global n fs N NFFT w;
n=2;
NFFT=1024;
w=1;
N=2*floor(30e-3*fs/2);


% --- Outputs from this function are returned to the command line.
function varargout = parametros_separacion_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;
v=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 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

global metodo;
set(handles.radioAnemuller,'Value',0);
set(handles.radioSubbandas,'Value',0);
metodo='m';

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

global metodo;
set(handles.radioMasking,'Value',0);
set(handles.radioSubbandas,'Value',0);
metodo='a';

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

global metodo;
set(handles.radioMasking,'Value',0);
set(handles.radioAnemuller,'Value',0);
metodo='s';

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

if get(handles.radioAnemuller,'Value')==0 && get(handles.radioMasking,'Value')==0 && get(handles.radioSubbandas,'Value')==0
    mensaje1;
else
    global s y X F T fs w NFFT N n metodo win noverlap;
        
    % Ventana
    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
    
    mensaje3;drawnow;
    [X(:,:,1),F,T]=specgram(s(1,:),NFFT,fs,win,noverlap);
    X(:,:,2)=specgram(s(2,:),NFFT,fs,win,noverlap);
    
    % Ahora la separacion
    switch metodo
        
        % Masking
        case 'm'
            global maskb y 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);
            
        % Anemuller    
        case 'a'
            Y=separa_anemuller(X,F);
            for k=1:n
                y(k,:)=real(ISTFT_overlapadd(squeeze(Y(:,:,k)),NFFT,fs,win,noverlap));
                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_separacion(y,fs);
    end
end

