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

% Last Modified by GUIDE v2.5 25-Nov-2014 14:53:14

% Begin initialization code - DO NOT EDIT
%varargin={};
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Interface_TO_OpeningFcn, ...
                   'gui_OutputFcn',  @Interface_TO_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(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 Interface_TO is made visible.
function Interface_TO_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 Interface_TO (see VARARGIN)
 fig = handles.figure1;
% set(fig, 'Units', 'Normalized', 'Position', [0 0 1 1]);
% set(fig,'Units', 'normalized', 'Position', [0 0 1 1]);
% set(findobj(fig, '-property', 'Units'), 'Units', 'normalized');


%%-Handles definition
handles.RFs=varargin{1};
handles.rf=handles.RFs(:,:,1)
handles.pitch=varargin{2}.pitch*1000;
handles.Fe=varargin{2}.Fe*1e-6;
handles.c=varargin{2}.c;
[handles.Naxial,handles.Nlateral]=size(handles.rf(:,:,1));
handles.lambdax=varargin{2}.lambdax*1000; 
handles.sigmax=varargin{2}.sigmax*1000; 
handles.res_axial=1/handles.Fe*handles.c/2;
handles.y=(0:handles.res_axial:(handles.Naxial-1)*handles.res_axial)/1000;
handles.x=((-(handles.Nlateral/2):1:(handles.Nlateral-1)/2)*handles.pitch);
handles.faxial=(-handles.Fe/2:handles.Fe/(handles.Naxial):handles.Fe/2-1/handles.Naxial);
handles.flateral=(-1/(2*handles.pitch):1/(handles.Nlateral):1/(2*handles.pitch)-1/handles.Nlateral); 

%%-figure definition
set(handles.slider1,'Min',0)
set(handles.slider1,'Max',1/(2*handles.pitch));
set(handles.slider1,'Value',1/handles.lambdax)
set(handles.edit2,'String',num2str(handles.lambdax))
set(handles.slider4,'Min',handles.pitch)
set(handles.slider4,'Max',handles.pitch*handles.Nlateral/6);
set(handles.slider4,'Value',handles.sigmax)
set(handles.edit3,'String',num2str(handles.sigmax))

%%-Affichage
handles=affichage(handles);
 
% Choose default command line output for Interface_TO
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% This sets up the initial plot - only do when we are invisible
% so window can get raised using Interface_TO.
if strcmp(get(hObject,'Visible'),'off')
    plot(rand(5));
end

% UIWAIT makes Interface_TO wait for user response (see UIRESUME)
 uiwait(gcf)


% --- Outputs from this function are returned to the command line.
function varargout = Interface_TO_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} = real(handles.rfTO);
param.GX=handles.GX;
param.lambdax=handles.lambdax*1e-3;
param.sigmax=handles.sigmax*1e-3;
param.pitch=handles.pitch*1e-3;
param.Fe=handles.Fe*1e6;
param.c=handles.c;
varargout{2} = param;
% The figure can be deleted now
delete(handles.figure1);



% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%%
 h = waitbar(0,'Processing') 
for i=1:size(handles.RFs,3)
handles.fx=1/(handles.lambdax);
 [handles.rfTO(:,:,i) handles.GX] = FourierFiltTO(handles.RFs(:,:,i), handles);
waitbar(i/240,h) 
end
guidata(hObject, handles);
delete(h)
uiresume

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


% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
    open(file);
end

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

% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to CloseMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
                     ['Close ' get(handles.figure1,'Name') '...'],...
                     'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end

delete(handles.figure1)


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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 popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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

set(hObject,'Min',0)
set(hObject,'Max',1/(2*handles.pitch));
handles.lambdax=1/get(hObject,'Value');    
handles=affichage(handles)
guidata(hObject,handles)

set(handles.edit2, 'String', num2str(handles.lambdax));
%uiresume
% Interface_TO_OutputFcn(handles,handles,handles)
%  
 
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (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.


if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
    
% set(hObject,'Min',0)
% set(hObject,'Max',1/(2*handles.pitch));
% set(hObject,'Value',1/handles.lambdax);

end



% --- Executes on slider movement.
function slider4_Callback(hObject, eventdata, handles)
% hObject    handle to slider4 (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
handles.lambdax
handles.sigmax=get(hObject,'Value');
handles=affichage(handles)
guidata(hObject,handles)
set(handles.edit3, 'String', num2str(handles.sigmax));
%uiresume 

% --- Executes during object creation, after setting all properties.
function slider4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider4 (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.



if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double
handles.lambdax=str2double(get(hObject,'string'));
handles=affichage(handles)
guidata(hObject,handles)
set(handles.slider1, 'value', 1/handles.lambdax);
%uiresume



% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double
handles.sigmax=str2double(get(hObject,'string'));
handles=affichage(handles);
guidata(hObject,handles)
set(handles.slider4, 'Value', (handles.sigmax));
%uiresume
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function handles=affichage(handles)
int=2;
 handles.fx=1/(handles.lambdax);
 [handles.rfTO handles.GX] = FourierFiltTO(handles.rf, handles);

  handles.r = cos(2*pi*handles.fx*handles.x);
  handles.gx = (1/(handles.sigmax*sqrt(2*pi))).*(exp(-(handles.x.^2)/(2*handles.sigmax.^2)));


  
  axes(handles.axes1); 
    imagesc(handles.x,handles.y,((inter(handles.rf(:,:),int))))
    title('Original RF Image')
    xlabel('Depth (mm)')
    ylabel('Lateral Position (mm)')

  axes(handles.axes2);
    imagesc(handles.flateral,handles.faxial,abs(fftshift(fft2(handles.rf(:,:,1))))) 
    title('2D Fourier Transform of the RF Image')
    xlabel('1/lambdax (mm-1)')
    ylabel('Axial freqency (MHz)')
  
  axes(handles.axes3);
    imagesc(handles.flateral,handles.faxial,handles.GX)
    xlabel('1/lambdax (mm-1)')
    
  axes(handles.axes4);
    imagesc(handles.flateral,handles.faxial,abs(fftshift(fft2(handles.rfTO(:,:,1)))))
    title('2D Fourier Transform of the RF Image After Filtering')
    xlabel('1/lambdax (mm-1)')
    ylabel('Axial freqency (MHz)')
  
  axes(handles.axes5);
    imagesc(handles.x,handles.y,inter(real(handles.rfTO(:,:)),int))
    title('RF Image with Transverse Oscllations')
    xlabel('Depth (mm)')
    ylabel('Lateral Position (mm)')
    
 axes(handles.axes6);
    plot(handles.x,handles.r.*handles.gx);
    title('Theorical Oscillations')
        hold on
        plot(handles.x,handles.gx,'r');
        hold off
    axis([min(handles.x) max(handles.x) -1 +1])
    xlabel('Lateral Position (mm)')
    ylabel('Amplitude')
  
 


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
if isequal(get(hObject, 'waitstatus'), 'waiting')
    % The GUI is still in UIWAIT, us UIRESUME
    uiresume(hObject);
else
    % The GUI is no longer waiting, just close it
    delete(hObject);
end
