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

% Last Modified by GUIDE v2.5 07-Nov-2016 10:08:55

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

% Choose default command line output for DFcnEditor
handles.output = hObject;
hFAD = findobj('Tag','FiltArrayDesigner');
handles.hFAD = hFAD;
hFADhandles = guidata(hFAD);
handles.hFADhandles = hFADhandles;
handles.filtNum = 1;
for k = 1:hFADhandles.numFiltersD
    filtStr{k} = num2str(k);
end
set(handles.filterPopup,'String',filtStr);
d = zeros(hFADhandles.latticeEtaSteps,hFADhandles.latticeXiSteps);
colEditable = true(1,hFADhandles.latticeXiSteps);
handles.dVal = d;
t = uitable(hObject,'Data',d,'ColumnWidth',{100});
t.CellEditCallback = @myDCellEditCallback;
t.Units = 'Normalized';
if t.Extent(3) <= 0.95
    t.Position(3) = t.Extent(3);
    addBit = 0;
else
    t.Position(3) = 0.95;
    addBit = 0.025;
end
if t.Extent(4) <= 0.8
    t.Position(4) = t.Extent(4) + addBit;
else
    t.Position(4) = 0.8 + addBit;
end
t.ColumnEditable = colEditable;
% Update handles structure
handles.t = t;
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = DFcnEditor_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 filterPopup.
function filterPopup_Callback(hObject, eventdata, handles)
% hObject    handle to filterPopup (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 filterPopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from filterPopup
handles.filtNum = get(hObject,'Value');
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function filterPopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to filterPopup (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 button press in genFilt.
function genFilt_Callback(hObject, eventdata, handles)
% hObject    handle to genFilt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.hFADhandles = guidata(handles.hFAD);
Lt = handles.hFADhandles.L.';
LtInv = pinv(Lt);
y = reshape(flipud(handles.dVal).',handles.hFADhandles.latticeXiSteps*handles.hFADhandles.latticeEtaSteps,1);
fVec = LtInv*y;
totFVal = sum(abs(fVec));
if totFVal < 10^-10*length(fVec)
    msgbox('Generated filter ill formed, try again.');
end
totFIVal = sum(abs(imag(fVec)));
totFRVal = sum(abs(real(fVec)));
if totFIVal > (10^-8/length(fVec))
    choice = questdlg('Filter has large imaginary component and is not realizable. Would you like me to attempt to rectify?',...
	'Rectify Imaginary Filter',...
    'Yes', ...
	'No','Yes');
    switch choice
        case 'Yes'
            if totFRVal < 10^-8*length(fVec)
                fVec = imag(fVec);
            else
                msgbox('Could not rectify the filter, try again.');
            end
        case 'No'
    end
else
    fVec = real(fVec);
end
fVec(abs(fVec) < 10^-9) = 0;
fMat = flipud(reshape(fVec,handles.hFADhandles.xSizeD,handles.hFADhandles.ySizeD).');
handles.hFADhandles.filtVal{handles.filtNum} = fMat;
set(handles.hFADhandles.uiT{handles.filtNum},'Data',fMat);
guidata(handles.hFAD, handles.hFADhandles);

% --- Executes on button press in zeroAllFilts.
function zeroAllFilts_Callback(hObject, eventdata, handles)
% hObject    handle to zeroAllFilts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.hFADhandles = guidata(handles.hFAD);
fMat = zeros(handles.hFADhandles.ySizeD,handles.hFADhandles.xSizeD);
for k = 1:handles.hFADhandles.numFiltersD
    handles.hFADhandles.filtVal{k} = fMat;
    set(handles.hFADhandles.uiT{k},'Data',fMat);
    guidata(handles.hFAD, handles.hFADhandles);
end
guidata(handles.hFAD, handles.hFADhandles);

    
