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

% Last Modified by GUIDE v2.5 14-Nov-2016 14:20:20

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @FFTFiltSettings_OpeningFcn, ...
                   'gui_OutputFcn',  @FFTFiltSettings_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


function epsSliderCallback(hObject, eventdata,handles)
handles = guidata(handles.FFTFiltSettings);
handles.eps(handles.currentCoorIdx) = eventdata.AffectedObject.Value;
handles = fftSettingsDisplay( handles );
set(handles.epsTxt,'String',num2str(handles.eps(handles.currentCoorIdx)));
guidata(handles.FFTFiltSettings, handles);

function radSliderCallback(hObject, eventdata,handles)
handles = guidata(handles.FFTFiltSettings);
handles.radius(handles.currentCoorIdx) = eventdata.AffectedObject.Value;
handles = fftSettingsDisplay( handles );
set(handles.radTxt,'String',num2str(handles.radius(handles.currentCoorIdx)));
guidata(handles.FFTFiltSettings, handles);


% --- Executes just before FFTFiltSettings is made visible.
function FFTFiltSettings_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 FFTFiltSettings (see VARARGIN)

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

hFAD = findobj('Tag','FiltArrayDesigner');
handles.hFAD = hFAD;
hFADhandles = guidata(hFAD);
handles.hFADhandles = hFADhandles;

handles.filtFunc = @planckTaper2D;
handles.eps = handles.hFADhandles.eps;
handles.radius = handles.hFADhandles.radius;
handles.fourierFiltInd = handles.hFADhandles.fourierFiltInd;
handles.numD = length(handles.fourierFiltInd);
handles.currentCoorIdx = 1;
handles.currentCoor = handles.hFADhandles.dCoordinate(1,:);

if (length(handles.radius) < handles.numD) || (length(handles.eps) < handles.numD)
    curLen = length(handles.radius);
    for k = curLen:handles.numD
        handles.radius(k) = handles.radius(curLen);
        handles.eps(k) = handles.eps(curLen);
    end
end

if mod(handles.hFADhandles.numXpixels,2) == 0
    N = handles.hFADhandles.numXpixels + 1;
else
    N = handles.hFADhandles.numXpixels;
end
if mod(handles.hFADhandles.numYpixels,2) == 0
    M = handles.hFADhandles.numYpixels + 1;
else
    M = handles.hFADhandles.numYpixels;
end

handles.N = N;
handles.M = M;
x = linspace(-0.5,0.5,N);
xiStep = 1/(2*(x(2)-x(1)));
handles.xiVec = linspace(-xiStep,xiStep,N);
y = linspace(-0.5,0.5,M);
[X,Y] = meshgrid(x,y);
handles.x = x;
handles.y = y;
handles.X = X;
handles.Y = Y;
addlistener(handles.epsSlider,'Value','PostSet',@(hObj,event)epsSliderCallback(hObj,event,handles));
addlistener(handles.radiusSlider,'Value','PostSet',@(hObj,event)radSliderCallback(hObj,event,handles));
Fc = zeros(size(handles.X));
for k = 1:handles.numD
    dStr{k} = num2str(k);    
    F = handles.filtFunc(handles.radius(k),handles.eps(k),handles.X-handles.hFADhandles.dCoordinate(k,1),handles.Y-handles.hFADhandles.dCoordinate(k,2));
    handles.Fsave{k} = F;    
end
if ~isempty(handles.hFADhandles.imgTotFFTt)
    handles.imgTotFFTt = log10Mod(abs(imresize(handles.hFADhandles.imgTotFFTt,[M,N])));
    maxF = max(max(handles.imgTotFFTt));
    handles.imgTotFFTt = handles.imgTotFFTt./maxF;    
end
handles = fftSettingsDisplay( handles );
set(handles.dFcnSelect,'String',dStr);
set(handles.dFcnSelect,'Value',1);
set(handles.epsTxt,'String',num2str(handles.eps(handles.currentCoorIdx)));
set(handles.radTxt,'String',num2str(handles.radius(handles.currentCoorIdx)));
guidata(hObject, handles);
set(handles.epsSlider,'Value',handles.eps(handles.currentCoorIdx));
set(handles.radiusSlider,'Value',handles.radius(handles.currentCoorIdx));

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = FFTFiltSettings_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 selection change in filtFuncs.
function filtFuncs_Callback(hObject, eventdata, handles)
% hObject    handle to filtFuncs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns filtFuncs contents as cell array
%        contents{get(hObject,'Value')} returns selected item from filtFuncs


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


% --- Executes on slider movement.
function epsSlider_Callback(hObject, eventdata, handles)
% hObject    handle to epsSlider (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.eps(handles.currentCoorIdx) = get(hObject,'Value');
handles.radius(handles.currentCoorIdx) = get(handles.radiusSlider,'Value');
handles = fftSettingsDisplay( handles );
set(handles.epsTxt,'String',num2str(handles.eps(handles.currentCoorIdx)));
guidata(hObject, handles);

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

% --- Executes on slider movement.
function radiusSlider_Callback(hObject, eventdata, handles)
% hObject    handle to radiusSlider (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.radius(handles.currentCoorIdx) = get(hObject,'Value');
handles.eps(handles.currentCoorIdx) = get(handles.epsSlider,'Value');
handles = fftSettingsDisplay( handles );
set(handles.radTxt,'String',num2str(handles.radius(handles.currentCoorIdx)));
guidata(hObject, handles);


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


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

hFAD = findobj('Tag','FiltArrayDesigner');
handles.hFAD = hFAD;
hFADhandles = guidata(hFAD);
handles.hFADhandles = hFADhandles;
handles.hFADhandles.eps = handles.eps;
handles.hFADhandles.radius = handles.radius;
guidata(handles.hFAD,handles.hFADhandles);

function dFcnSelect_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dFcnSelect (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

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

% Hints: contents = cellstr(get(hObject,'String')) returns dFcnSelect contents as cell array
%        contents{get(hObject,'Value')} returns selected item from dFcnSelect
handles.currentCoorIdx = get(hObject,'Value');
handles.currentCoor = handles.hFADhandles.dCoordinate(handles.currentCoorIdx,:);

guidata(hObject, handles);
handles = fftSettingsDisplay( handles );
set(handles.radTxt,'String',num2str(handles.radius(handles.currentCoorIdx)));
set(handles.epsTxt,'String',num2str(handles.eps(handles.currentCoorIdx)));
set(handles.epsSlider,'Value',handles.eps(handles.currentCoorIdx));
set(handles.radiusSlider,'Value',handles.radius(handles.currentCoorIdx));
guidata(hObject, handles);

% --- Executes on button press in setAllFiltsToCurVals.
function setAllFiltsToCurVals_Callback(hObject, eventdata, handles)
% hObject    handle to setAllFiltsToCurVals (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.

eps = get(handles.epsSlider,'Value');
radius = get(handles.radiusSlider,'Value');
for k=1:handles.numD
    handles.radius(k) = radius;
    handles.eps(k) = eps;
    F = handles.filtFunc(radius,eps,handles.X-handles.hFADhandles.dCoordinate(k,1),handles.Y-handles.hFADhandles.dCoordinate(k,2));
    handles.Fsave{k} = F;    
end
handles = fftSettingsDisplay( handles );
guidata(hObject, handles);


% --- Executes on button press in genTikzFig.
function genTikzFig_Callback(hObject, eventdata, handles)
% hObject    handle to genTikzFig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.imgPlot);
filtStr = ['_filtRad_' num2str(handles.radius(handles.currentCoorIdx),'%1.3f')];
if handles.hFADhandles.useSynthTestImg
    matlab2tikz(['./figures/imgFFTSynth_' num2str(handles.hFADhandles.xSizeD)...
                 'x' num2str(handles.hFADhandles.ySizeD) '_' filtStr...
                 handles.hFADhandles.poissStr handles.hFADhandles.snrStr...
                 '.tikz'],'width','\fpFFTwidth','height','\fpFFTheight');
else
    matlab2tikz(['./figures/imgFFT_' num2str(handles.hFADhandles.xSizeD)...
                 'x' num2str(handles.hFADhandles.ySizeD) '_' filtStr...
                 handles.hFADhandles.poissStr handles.hFADhandles.snrStr...
                 '.tikz'],'width','\fpFFTwidth','height','\fpFFTheight');
end