function varargout = touchpad(varargin)
% touchpad.m
% gui to generate and detect touch-tones
% author:    Nicholas Kottenstette
% date:      12/13/2004
% copyright: 2004 by Nicholas Kottenstette

%TOUCHPAD M-file for touchpad.fig
%      TOUCHPAD, by itself, creates a new TOUCHPAD or raises the existing
%      singleton*.
%
%      H = TOUCHPAD returns the handle to a new TOUCHPAD or the handle to
%      the existing singleton*.
%
%      TOUCHPAD('Property','Value',...) creates a new TOUCHPAD using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to touchpad_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      TOUCHPAD('CALLBACK') and TOUCHPAD('CALLBACK',hObject,...) call the
%      local function named CALLBACK in TOUCHPAD.M with the given input
%      arguments.
%
%      *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 touchpad

% Last Modified by GUIDE v2.5 14-Dec-2004 12:04:47

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @touchpad_OpeningFcn, ...
                   'gui_OutputFcn',  @touchpad_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 touchpad is made visible.
function touchpad_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

% Choose default command line output for touchpad
handles.output = hObject;
handles.Fs = 8000;
handles.add_noise = 0;
handles.ga_noise = 0;
handles.ntones_max = 12;
handles.dtmfa = [];
handles.dtmfa_index = 0;
handles.tones = [];
% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = touchpad_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 button press in kpb_1.
function kpb_1_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_2.
function kpb_2_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_3.
function kpb_3_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_A.
function kpb_A_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_A (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_4.
function kpb_4_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_5.
function kpb_5_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_6.
function kpb_6_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_B.
function kpb_B_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_B (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_7.
function kpb_7_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_8.
function kpb_8_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_9.
function kpb_9_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_C.
function kpb_C_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_C (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_star.
function kpb_star_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_star (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_0.
function kpb_0_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_0 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_pound.
function kpb_pound_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_pound (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

% --- Executes on button press in kpb_D.
function kpb_D_Callback(hObject, eventdata, handles)
% hObject    handle to kpb_D (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kpd_update(hObject,get(hObject,'UserData'),handles);

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

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

% --- Executes on slider movement.
function tp_min_Callback(hObject, eventdata, handles)
% hObject    handle to tp_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val = 1.0e-3*fix(1.0e3*get(hObject,'Value'));
set(hObject,'Value',val);
val_max = get(handles.tp_max,'Value');
if val_max < val
  set(handles.tp_max,'Value',val);
end


% --- Executes during object creation, after setting all properties.
function tp_min_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tp_min (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 tp_max_Callback(hObject, eventdata, handles)
% hObject    handle to tp_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val = 1.0e-3*fix(1.0e3*get(hObject,'Value'));
set(hObject,'Value',val);
val_min = get(handles.tp_min,'Value');
if val_min > val
  set(handles.tp_min,'Value',val);
end

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


% --- Executes during object creation, after setting all properties.
function tp_max_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tp_max (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 button press in en_gn.
function en_gn_Callback(hObject, eventdata, handles)
% hObject    handle to en_gn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  if get(hObject,'Value') == 0
    handles.ga_noise = 0;  
  else
    sigma = 10^(get(handles.gnoise,'Value')/20);
    m = ceil(2*(get(handles.td_max,'Max')+get(handles.tp_max,'Max'))*...
             handles.Fs*handles.ntones_max);
    handles.ga_noise = normrnd(0,sigma,m,1);
  end
  guidata(hObject, handles);
  

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


% --- Executes during object creation, after setting all properties.
function gnoise_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gnoise (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 td_min_Callback(hObject, eventdata, handles)
% hObject    handle to td_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val = 1.0e-3*fix(1.0e3*get(hObject,'Value'));
set(hObject,'Value',val);
val_max = get(handles.td_max,'Value');
if val_max < val
  set(handles.td_max,'Value',val);
end

% --- Executes during object creation, after setting all properties.
function td_min_CreateFcn(hObject, eventdata, handles)
% hObject    handle to td_min (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 td_max_Callback(hObject, eventdata, handles)
% hObject    handle to td_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val = 1.0e-3*fix(1.0e3*get(hObject,'Value'));
set(hObject,'Value',val);
val_min = get(handles.td_min,'Value');
if val_min > val
  set(handles.td_min,'Value',val);
end


% --- Executes during object creation, after setting all properties.
function td_max_CreateFcn(hObject, eventdata, handles)
% hObject    handle to td_max (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 button press in save.
function save_Callback(hObject, eventdata, handles)
% hObject    handle to save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  if(handles.dtmfa_index <= 0)
    return;
  end
  td_min = fix(1000*get(handles.td_min,'Value'));
  td_max = fix(1000*get(handles.td_max,'Value'));
  if td_min > (td_max - 1)
    td_min = td_max;
  end
  tp_min = fix(1000*get(handles.tp_min,'Value'));
  tp_max = fix(1000*get(handles.tp_max,'Value'));
  if tp_min > (tp_max - 1)
    tp_min = tp_max;
  end

  if(handles.ga_noise)
    gnoise = get(handles.gnoise,'Value');
    gnoise = .1*fix(10*gnoise);
  else
    gnoise = -inf;
  end
  if get(handles.en_nz,'Value')
    nsource = 'no';
  elseif get(handles.en_hz,'Value')
    nsource = '60Hz';
  elseif get(handles.en_fn,'Value')
    nsource = 'female_voice';
  elseif get(handles.en_mv,'Value')
    nsource = 'male_voice';
  end
  num_disp = get(handles.num_disp,'String');
  sug_fname = sprintf('dtmf_%s_%dms_%dms_%dms_%dms_%3.1fdB_%s.au',...
                      num_disp,td_min,td_max,tp_min,tp_max,gnoise,...
                      nsource);
  [filename, pathname] = uiputfile( ...
      {sug_fname, 'Suggested File Name';...
       '*.au',    'All au-files (*.au)'},'Save as');
  if isequal(filename,0) | isequal(pathname,0)
    disp('User pressed cancel')
    return;
  else
    fn = fullfile(pathname, filename);
    disp(['User selected ', fn])
  end
  y = gen_sig(handles.tones,handles.add_noise,handles.ga_noise);
  auwrite(y,handles.Fs,16,'linear',fn);


% --- Executes on button press in open.
function open_Callback(hObject, eventdata, handles)
% hObject    handle to open (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  [filename, pathname]=uigetfile('*.au','Pick an au-file');
  if isequal(filename,0) | isequal(pathname,0)
    disp('User Pressed cancel');
    return;
  else
    fn = fullfile(pathname, filename);
    disp(['User selected ', fn]);
  end
  [x,Fs,bits] = auread(fn);
  if Fs ~= handles.Fs
    msg = sprintf('Error file has wrong sampling Frequency %d Hz',Fs);
    disp(msg);
    return;
  end
  %spec_plot(x,Fs,2,fn,1);
  %ultimate parameters seem to be
  %fft_len=4096, D=32, M=256
  %Nt=6, Np=5, Threshold = 3 dB, Nwelch = 5
  D = get_str_val(handles.D);
  M = get_str_val(handles.M);
  Nw = get_str_val(handles.Nw);
  win = get_str_val(handles.win);
  fft_len = get_str_val(handles.fft_len);
  Nt = get_str_val(handles.Nt);
  Np = get_str_val(handles.Np);
  Thresh = 10^(get_str_val(handles.Thresh)/20);
  en_pt = get(handles.en_pt,'Value');
  en_fi = get(handles.en_fi,'Value');
  en_di = get(handles.en_di,'Value');
  tol = .015;
  [tones,t_start,t_finish] = dtmf_det(x,D,M,Nw,win,fft_len,Nt,Np,Thresh,...
                                      en_pt,tol,en_fi,en_di,filename);
  
  ind = strfind(fn,'/');
  fname = fn(ind(length(ind))+1:length(fn));
  disp(['Filename Opened: ' fname])
  disp(['Tones Detected: ' tones])
  disp(['Estimated Tone Start Time (seconds): ', num2str(t_start)])
  disp(['Estimated Tone Stop Time (seconds): ', num2str(t_finish)])
  disp(['Estimated Tone Length (seconds): ', num2str(t_finish-t_start)])
  disp(['Etimated Tone Pause Time (seconds): ',...
        num2str(t_start(2:length(t_start))-t_finish(1:length(t_finish)-1))]);
  if length(fname) < 5
    return;
  end
  if strcmp('dtmf_',fname(1:5))
    ind = strfind(fname,'_');
    tonesex = fname(ind(1)+1:ind(2)-1);
    Nex = length(tonesex);
    Nact = length(tones);
    disp(['Tones Expected: ',tonesex])
    sp = ' ' - 0;
    if Nact < Nex
      tones = [tones sp*ones(1,Nex-Nact)];
    elseif Nex < Nact
      tonesex = [tonesex sp*ones(1,Nact-Nex)];
    end
    dif = tonesex - tones;
    Ncor = length(find(dif==0));
    per_correct = 100*Ncor/Nex;
    msg = sprintf('Percent Correct %3.0f',per_correct);
    disp(msg)
  end
  
% --- Executes on button press in play.
function play_Callback(hObject, eventdata, handles)
  if(handles.dtmfa_index)
    x = gen_sig(handles.tones,handles.add_noise,handles.ga_noise);
    auwrite(x,handles.Fs,16,'linear','temp.au');
    try,
      !audioplay -v 100 temp.au
      !rm temp.au
    catch,
      !rm temp.au
    end
  end

% --- Updates dtmf array, kpd_display --- %
function kpd_update(hObject,button,handles)
  if length(button) == 1
    if button == 0
      %clear display
      set(handles.num_disp,'String','');
      %clear tones
      handles.tones = [];
      handles.dtmfa_index = 0;
      handles.dtmfa = [];
    else
      %delete the most recent character
      dis = get(handles.num_disp,'String');
      if length(dis) > 1
        set(handles.num_disp,'String',dis(1:length(dis)-1));
        %delete tone
        l = length(handles.tones);
        lt = handles.dtmfa(handles.dtmfa_index).length;
        handles.tones = handles.tones(1:l-lt);
      else
        handles.tones = [];
        set(handles.num_disp,'String','');
      end
      handles.dtmfa_index = handles.dtmfa_index - 1;
      if handles.dtmfa_index < 0
        handles.dtmfa_index = 0;
        handles.tones = [];
      end
      handles.dtmfa = handles.dtmfa(1:handles.dtmfa_index);
    end
  else
    ih = button(2,2);
    il = button(2,1);
    s = sprintf('%s',cast(button(1,1),'char'));
    dis = get(handles.num_disp,'String');
    dis = sprintf('%s%s',dis,s);
    set(handles.num_disp,'String',dis);
    %add and play tone
    handles.dtmfa_index = handles.dtmfa_index + 1;
    td_min = 1.0e-3*fix(1.0e3*get(handles.td_min,'Value'));
    td_max = 1.0e-3*fix(1.0e3*get(handles.td_max,'Value'));
    tp_min = 1.0e-3*fix(1.0e3*get(handles.tp_min,'Value'));
    tp_max = 1.0e-3*fix(1.0e3*get(handles.tp_max,'Value'));
    %Gui hack/fix
    if td_min > (td_max - .001)
      td_min = td_max;
    end
    if tp_min > (tp_max - .001)
      tp_min = tp_max;
    end
    td = random('uniform',td_min,td_max);
    tp = random('uniform',tp_min,tp_max);
    handles.dtmfa(handles.dtmfa_index).td = td;
    handles.dtmfa(handles.dtmfa_index).tp = tp;
    handles.dtmfa(handles.dtmfa_index).il = il;
    handles.dtmfa(handles.dtmfa_index).ih = ih;
    %display([tp_min,tp_max,td_min,td_max])
    x = dtmf(il,ih,td,tp,handles.Fs);
    x = x*(max(x)-min(x)/2);%scale
    handles.dtmfa(handles.dtmfa_index).length = length(x);
    handles.tones = [handles.tones;x];
  end
  guidata(hObject, handles);

% --- Executes on button press in en_nz.
function en_nz_Callback(hObject, eventdata, handles)
% hObject    handle to en_nz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% add no additional noise
  if get(hObject,'Value') == 1
    handles.add_noise = 0;
  end
  guidata(hObject, handles);
  
% --- Executes on button press in en_hz.
function en_hz_Callback(hObject, eventdata, handles)
% hObject    handle to en_hz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% add an additional 60 Hz noise
  if get(hObject,'Value') == 1
    handles.add_noise = sin(2*pi*(60/handles.Fs)*[0:1:handles.Fs-1])';
  end
  guidata(hObject, handles);
  
% --- Executes on button press in en_fn.
function en_fn_Callback(hObject, eventdata, handles)
% hObject    handle to en_fn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% add the 8 kHz female speech sample for noise
  if get(hObject,'Value') == 1
    x = auread('audio/wb_f1_src.au');
    x = x*2/(max(x)-min(x));
    handles.add_noise = x;
  end
  guidata(hObject, handles);

% --- Executes on button press in en_mv.
function en_mv_Callback(hObject, eventdata, handles)
% hObject    handle to en_mv (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% add the 8 kHz female speech sample for noise
  if get(hObject,'Value') == 1
    x = auread('audio/male-orig_8k.au');
    x = x*2/(max(x)-min(x));
    handles.add_noise = x;
  end
  guidata(hObject, handles);

function x = gen_sig(tones,add_noise,ga_noise)
  %display(max(add_noise)-min(add_noise))
  x = cadd(tones,add_noise);
  x = cadd(x,ga_noise);
  x = x*(2/(max(x)-min(x)));
  
function x = cadd(x,y);
  lx = length(x);
  ly = length(y);
  if ly < lx
    if ly == 0
      ly = 1;
      y = 0;
    end
    if ly == 1
      y = y*ones(lx,1);
      ly = lx;
    end
    n = floor(lx/ly);
    t = zeros((1+n)*ly,1);
    t(1:ly) = y;
    for i=1:n
      t((ly*i)+1:ly*(i+1)) = y;
    end
    y = t;
  end
  %disp(size(x))
  %disp(size(y))
  x = x + y(1:lx);

function fft_len_Callback(hObject, eventdata, handles)
% hObject    handle to fft_len (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 fft_len as text
%        str2double(get(hObject,'String')) returns contents of fft_len as a double
  try,
    res = eval(get(hObject,'String'));
    res = 2^ceil(log2(abs(res(1))));
  catch,
    res = 2^12;
  end
  res = max(eval(get(handles.M,'String')),res(1));
  res = 2^ceil(log2(res(1)));
  set(hObject,'String',sprintf('%d',res(1)));
  

% --- Executes during object creation, after setting all properties.
function fft_len_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fft_len (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 D_Callback(hObject, eventdata, handles)
% hObject    handle to D (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 D as text
%        str2double(get(hObject,'String')) returns contents of D as a double
  try,
    res = eval(get(hObject,'String'));
    res = fix(res(1));
  catch,
    res = 32;
  end
  M = eval(get(handles.M,'String'));
  if res < 1
    res = 1;
  elseif res > M
    res = M;
  end
  set(hObject,'String',sprintf('%d',res(1)));


% --- Executes during object creation, after setting all properties.
function D_CreateFcn(hObject, eventdata, handles)
% hObject    handle to D (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 M_Callback(hObject, eventdata, handles)
% hObject    handle to M (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  try,
    res = eval(get(hObject,'String'));
    res = fix(res(1));
  catch,
    res = 256;
  end
  D = eval(get(handles.D,'String'));
  fft_len = eval(get(handles.fft_len,'String'));
  if res < D
    res = D;
  elseif res > fft_len
    res = fft_len;
  end
  set(hObject,'String',sprintf('%d',res(1)));


% --- Executes during object creation, after setting all properties.
function M_CreateFcn(hObject, eventdata, handles)
% hObject    handle to M (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 win_Callback(hObject, eventdata, handles)
% hObject    handle to win (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  try,
    res = eval(get(hObject,'String'));
  catch,
    res = 0;
  end
  res = min(res(1),3);
  res = max(res(1),0);
  set(hObject,'String',sprintf('%d',res(1)));


% --- Executes during object creation, after setting all properties.
function win_CreateFcn(hObject, eventdata, handles)
% hObject    handle to win (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 Nt_Callback(hObject, eventdata, handles)
% hObject    handle to Nt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  try,
    res = eval(get(hObject,'String'));
  catch,
    res = 1;
  end
  res = max(res(1),1);
  set(hObject,'String',sprintf('%d',res(1)));


% --- Executes during object creation, after setting all properties.
function Nt_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Nt (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 Np_Callback(hObject, eventdata, handles)
% hObject    handle to Np (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  try,
    res = eval(get(hObject,'String'));
  catch,
    res = 1;
  end
  res = max(res(1),1);
  set(hObject,'String',sprintf('%d',res(1)));


% --- Executes during object creation, after setting all properties.
function Np_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Np (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 Thresh_Callback(hObject, eventdata, handles)
% hObject    handle to Thresh (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  try,
    res = eval(get(hObject,'String'));
  catch,
    res = -10;
  end
  set(hObject,'String',sprintf('%d',res(1)));


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


% --- Executes on button press in en_pt.
function en_pt_Callback(hObject, eventdata, handles)
% hObject    handle to en_pt (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 en_pt


% --- Executes on button press in en_fi.
function en_fi_Callback(hObject, eventdata, handles)
% hObject    handle to en_fi (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 en_fi


function val = get_str_val(h)
  val = eval(get(h,'String'));



function Nw_Callback(hObject, eventdata, handles)
% hObject    handle to Nw (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  try,
    res = eval(get(hObject,'String'));
  catch,
    res = 1;
  end
  res = max(res(1),1);
  set(hObject,'String',sprintf('%d',res(1)));



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


% --- Executes on button press in en_di.
function en_di_Callback(hObject, eventdata, handles)
% hObject    handle to en_di (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 en_di


