%DSCEDW1D Demonstrates 1-D wavelet typical scenario in the Wavelet Toolbox. 

%       M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 12-Mar-96.
%	Last Revision: 01-Oct-97.
%       Copyright (c) 1995-98 by The MathWorks, Inc.
% $Revision: 1.4 $

% Demo initialization ====================
if ~exist('DRVShowGUIFlag'), figNumber = 0; end;

Figures_Stack = mextglob('get','Figures_Stack');

% Tag property of objects.
%------------------------
tag_dem_tool    = 'Demo_Tool';

clc;
sig_nam = 'noisdopp';
wav_nam = 'sym4';
lev_dec = 5;

%----------------------------------------------------%
% Tag property of objects (main Window).
%---------------------------------------
tag_pop_viewm   = 'View_Mode';
tag_valapp_scr  = 'ValApp_Scr';

% MemBloc1 of stored values.
%---------------------------
n_param_anal    = 'DWAn1d_Par_Anal';
ind_ssig_type   =  8;

% Tag property of objects (More Display Option).
%----------------------------------------------
tag_win_disp    = 'DispOpt1D';
tag_det_on      = 'Chk_Det';
tag_cfs_on      = 'Chk_Cfs';
tag_pop_ccfs    = 'Pop_CCfs';

% Tag property of objects (Histograms).
%--------------------------------------
tag_det_sig     = 'Det_sig';
tag_det_all     = 'Det_All';
tag_sel_cfs     = 'Sel_Cfs';
tag_show_hist   = 'Show_Hist';

% Tag property of objects (Compression).
%---------------------------------------
tag_sel_sli     = 'Sel_Sli';
tag_sel_edi     = 'Sel_Edi';
tag_norm_val_a  = 'Norm_Val_A';
tag_zeros_val_a = 'Zeros_Val_A';
tag_pop_mv      = 'Pop_ModeVal';

% MemBloc3 of stored values (Compression).
%-----------------------------------------
n_misc_comp     = 'DWComp1D_Misc';
ind_status      = 6;

% Tag property of objects (De-noising).
%--------------------------------------
tag_meth_val    = 'Meth_Val';
tag_noise_val   = 'Noise_Val';
tag_txt_mth     = 'Txt_mode_th';
tag_pop_mv      = 'Pop_ModeVal';
tag_lin_deno    = 'Lin_deno';

% MemBloc3 of stored values(De-noising).
%---------------------------------------
n_misc_deno     = 'DWDeno1D_Misc';
ind_hdls_lev    = 1;
%----------------------------------------------------%

if wshowdrv('init',figNumber)
    if figNumber==0  
        win_dw1d = dw1dtool;
        wenamngr('Inactive',win_dw1d);
    else
        win_dw1d = demoscen('startshow',figNumber,'dw1dtool');
        wenamngr('Inactive',win_dw1d);
        wshowdrv('setactfig',figNumber,win_dw1d);
        return;
    end
end
if exist('wait_mode')~=1 , wait_mode = 0; end
if figNumber , win_dw1d = wshowdrv('getactfig',figNumber); end
if exist('win_msg')~=1 , win_msg = 1; end
stop = demoscen('msg',figNumber,wait_mode,1,win_msg,win_dw1d);
if stop , return; end

pop_win_dw1d    = findobj(win_dw1d,'style','popupmenu');
pus_win_dw1d    = findobj(win_dw1d,'style','pushbutton');
pop_viewm       = findobj(pop_win_dw1d,'tag',tag_pop_viewm);
pop_app_scr     = findobj(pop_win_dw1d,'tag',tag_valapp_scr);
%----------------------------------------------------%
% For problems with handles
%--------------------------
f5 = colordef('new','none');
f4 = colordef('new','none');
f3 = colordef('new','none');
f2 = colordef('new','none');
f1 = colordef('new','none');
%----------------------------------------------------%

% figure 1 - Full decomposition.
%-------------------------------
dw1dmngr('demo',win_dw1d,sig_nam,wav_nam,lev_dec);
wenamngr('Inactive',win_dw1d);
stop = demoscen('msg',figNumber,wait_mode,2,win_msg,win_dw1d);
if stop , delete([f1 f2 f3 f4 f5]); return; end

% figure 2 - separate mode.
%--------------------------
set(pop_viewm,'value',3);
eval(get(pop_viewm,'Callback'));
stop = demoscen('msg',figNumber,wait_mode,6,win_msg,win_dw1d);
if stop , delete([f1 f2 f3 f4 f5]); return; end

% figure 3 - Show and Scroll mode (app5+sig+cfs).
%------------------------------------------------
set(pop_viewm,'value',1);
eval(get(pop_viewm,'Callback'));
wshowdrv('pause',0,wait_mode,2);
dw1ddisp('create',win_dw1d);
win_mopt        = wfindobj('figure','tag',tag_win_disp);
wenamngr('Inactive',win_mopt);
wshowdrv('pause',0,wait_mode,1);
uic_win_mopt    = findobj(win_mopt,'Type','uicontrol');
chk_win_mopt    = findobj(uic_win_mopt,'Style','checkbox');
pop_win_mopt    = findobj(uic_win_mopt,'Style','popupmenu');
chk_det_on      = findobj(chk_win_mopt,'Tag',tag_det_on);
set(chk_det_on,'Value',0);
wshowdrv('pause',0,wait_mode,1);
dw1ddisp('apply',win_dw1d,win_mopt);
set(pop_app_scr,'Value',5);
eval(get(pop_app_scr,'Callback'));
if wshowdrv('pause',figNumber,wait_mode), delete([f1 f2 f3 f4 f5]); return; end;

%------------------------------------------------

stop = demoscen('msg',figNumber,wait_mode,8,win_msg,win_dw1d);
if stop , delete([f1 f2 f3 f4 f5]); return; end

% figure 4 - Show and Scroll mode (app5+sig+cfs ).
%            coloration : init + all levels + abs
%------------------------------------------------
figure(win_mopt);
drawnow
pause(1)
pop_ccfs = findobj(pop_win_mopt,'Tag',tag_pop_ccfs);
set(pop_ccfs,'Value',3);
pause(1)
dw1ddisp('apply',win_dw1d,win_mopt);
if wshowdrv('pause',figNumber,wait_mode), delete([f1 f2 f3 f4 f5]); return; end;
dw1ddisp('close',win_dw1d,win_mopt);
wenamngr('Inactive',win_dw1d);
delete(f1);  mextglob('set','Figures_Stack',[]);

stop = demoscen('msg',figNumber,wait_mode,9,win_msg,win_dw1d);
if stop , return;  delete([f2 f3 f4 f5]); end

% figure 5 - Histograms - all the detail coefficients.
%-----------------------------------------------------
win_hist        = dw1dmngr('hist',win_dw1d);
wenamngr('Inactive',win_hist);
uic_win_hist    = findobj(win_hist,'Type','uicontrol');
chk_win_hist    = findobj(uic_win_hist,'Style','checkbox');
pus_win_hist    = findobj(uic_win_hist,'Style','pushbutton');
chk_det_sig     = findobj(chk_win_hist,'tag',tag_det_sig);
pus_det_all     = findobj(pus_win_hist,'tag',tag_det_all);
rad_sel_cfs     = findobj(uic_win_hist,'Style','radiobutton',...
                                'tag',tag_sel_cfs);
pus_show_hist   = findobj(pus_win_hist,'tag',tag_show_hist);
set(chk_det_sig,'value',1)      
eval(get(chk_det_sig,'Callback'));
pause(1)
eval(get(pus_det_all,'Callback'));
pause(1)
eval(get(rad_sel_cfs,'Callback'));
pause(1)
eval(get(pus_show_hist,'Callback'));
wenamngr('Inactive',win_hist);

stop = demoscen('msg',figNumber,wait_mode,10,win_msg,win_dw1d);
if stop , delete([f2 f3 f4 f5]); return; end

dw1dhist('close',win_hist);
delete(win_hist)
delete(f2); mextglob('set','Figures_Stack',[]);

% figure 6 - Compress - initial window.
%--------------------------------------
stop = demoscen('msg',figNumber,wait_mode,11,win_msg,win_dw1d);
if stop , delete([f3 f4 f5]); return; end

win_comp        = dw1dmngr('comp',win_dw1d);
wenamngr('Inactive',win_comp);
sli_comp        = findobj(win_comp,'Style','slider');
edi_comp        = findobj(win_comp,'Style','edit');
pop_comp        = findobj(win_comp,'Style','popupmenu');
sli_sel_sli     = findobj(sli_comp,'Tag',tag_sel_sli);
edi_sel_edi     = findobj(edi_comp,'Tag',tag_sel_edi);
edi_norm_val_a  = findobj(edi_comp,'Tag',tag_norm_val_a);
edi_zeros_val_a = findobj(edi_comp,'Tag',tag_zeros_val_a);
pop_mode_val    = findobj(pop_comp,'Tag',tag_pop_mv);

% figure 7 - Compress - after compression with 
%            the threshold default value.
%----------------------------------------------
stop = demoscen('msg',figNumber,wait_mode,12,win_msg,win_dw1d);
if stop , delete([f3 f4 f5]); return; end

dw1dcomp('compress',win_comp,win_dw1d);
wenamngr('Inactive',win_comp);
if wshowdrv('pause',figNumber,wait_mode), delete([f3 f4 f5]); return; end;

stop = demoscen('msg',figNumber,wait_mode,13,win_msg,win_dw1d);
if stop , delete([f3 f4 f5]); return; end

set(edi_zeros_val_a,'String',num2str(96.4))
eval(get(edi_zeros_val_a,'Callback'));
dw1dcomp('compress',win_comp,win_dw1d);
wenamngr('Inactive',win_comp);
if wshowdrv('pause',figNumber,wait_mode), delete([f3 f4 f5]); return; end;

% figure 8 - Compress - manual thresholding window.
%--------------------------------------------------
stop = demoscen('msg',figNumber,wait_mode,14,win_msg,win_dw1d);
if stop , delete([f3 f4 f5]); return; end

set(pop_mode_val,'Value',2)
eval(get(pop_mode_val,'Callback'))
wenamngr('Inactive',win_comp);
if wshowdrv('pause',figNumber,wait_mode), delete([f3 f4 f5]); return; end;
dw1dcomp('compress',win_comp,win_dw1d);
wenamngr('Inactive',win_comp);
if wshowdrv('pause',figNumber,wait_mode), delete([f3 f4 f5]); return; end;
wmemtool('wmb',win_comp,n_misc_comp,ind_status,0);
dw1dcomp('close',win_comp);
wenamngr('Inactive',win_dw1d);
delete(win_comp)
delete(f3) ; mextglob('set','Figures_Stack',[]);

% figure 9 - De-noise - automatic soft thresholding window.
%----------------------------------------------------------
stop = demoscen('msg',figNumber,wait_mode,15,win_msg,win_dw1d);
if stop , delete([f4 f5]); return; end

win_deno        = dw1dmngr('deno',win_dw1d);
wenamngr('Inactive',win_deno);
uic_deno        = findobj(win_deno,'Type','uicontrol');
pop_deno        = findobj(uic_deno,'Style','popupmenu');
sli_deno        = findobj(uic_deno,'Style','slider');
pop_meth_val    = findobj(pop_deno,'Tag',tag_meth_val);
pop_noise_val   = findobj(pop_deno,'Tag',tag_noise_val);
pop_mode_val    = findobj(pop_deno,'Tag',tag_pop_mv);
hdls_lev        = wmemtool('rmb',win_deno,n_misc_deno,ind_hdls_lev);
lev             = 2;
sli_deno_lev    = hdls_lev(2,lev);              
pause(1)
dw1ddeno('denoise',win_deno,win_dw1d);
wenamngr('Inactive',win_deno);
stop = demoscen('msg',figNumber,wait_mode,18,win_msg,win_dw1d);
if stop , delete([f4 f5]); return; end

% figure 10 - De-noise - automatic hard thresholding window.
%-----------------------------------------------------------
set(pop_mode_val,'Value',2)
eval(get(pop_mode_val,'Callback'))
dw1ddeno('denoise',win_deno,win_dw1d);
wenamngr('Inactive',win_deno);
stop = demoscen('msg',figNumber,wait_mode,19,win_msg,win_dw1d);
if stop , delete([f4 f5]); return; end

% figure 11 - De-noise - manual hard thresholding window.
%----------------------------------------------------------------------
set(pop_mode_val,'Value',4)
eval(get(pop_mode_val,'Callback'))
if wshowdrv('pause',figNumber,wait_mode), delete([f4 f5]); return; end;
dw1ddeno('denoise',win_deno,win_dw1d);
wenamngr('Inactive',win_deno);
stop = demoscen('msg',figNumber,wait_mode,20,win_msg,win_dw1d);
if stop , delete([f4 f5]); return; end

% Writing Synthesized. Signal Type.
%----------------------------------
wwaiting('msg',win_deno,'Wait ... computing');
axe_handles     = findobj(win_deno,'Type','axes');
lin_handles     = findobj(axe_handles,'Type','line');
lin_deno        = findobj(lin_handles,'tag',tag_lin_deno);
wmemtool('wmb',win_dw1d,n_param_anal,ind_ssig_type,'ds');
dw1dmngr('return_deno',win_dw1d,1,lin_deno);
wenamngr('Inactive',win_dw1d);
wwaiting('off',win_deno);
delete(win_deno)
delete(f4) ; mextglob('set','Figures_Stack',[]);
dmsgfun('close');
if wshowdrv('pause',figNumber,wait_mode), delete([f5]); return; end;

dw1ddisp('create',win_dw1d);
win_mopt        = wfindobj('figure','tag',tag_win_disp);
if wshowdrv('pause',figNumber,wait_mode), delete([f5]); return; end;
uic_win_mopt    = findobj(win_mopt,'Type','uicontrol');
chk_win_mopt    = findobj(uic_win_mopt,'Style','checkbox');
chk_cfs_on      = findobj(chk_win_mopt,'Tag',tag_cfs_on);
set(chk_cfs_on,'Value',0);
pause(1)
dw1ddisp('apply',win_dw1d,win_mopt);
if wshowdrv('pause',figNumber,wait_mode), delete([f5]); return; end;
dw1ddisp('close',win_dw1d,win_mopt);
wenamngr('Inactive',win_dw1d);
delete(f5);  mextglob('set','Figures_Stack',[]);

if figNumber==0
        wshowdrv('pause',figNumber,wait_mode);
        dw1dtool('close',win_dw1d);
        delete(win_dw1d)
        demoscen('close');
end
