function GUI_plot_spectrogram_ucsb
    global filenamelist;
    global filename;
    global speech_sequence_list;
    global list_position;
    global speechdir;
    global fs;
    global fsn;
    global y1;
    global wlengwb;
    global wlengnb;
    global fftlengwb;
    global fftlengnb;
    global iloglin;
    global dynrange;
    global scalefac;
    global icolor;

% move is y-axis offset for buttons
% speechdir is initial search directory for speech files
    speechdir='f:';
    list_position=1;
    offset_rutgers=650;
    offset_ucsb=520;
    offset=offset_rutgers;
    % offset=520 % 650;
    offset1=-80;
    
% Plotting area divided into two panels:
%   total plotting range is (0,0) to (1500,1000) or equivalently (0,0) to
%   (1,1)
%   panel1: (0,0) to (300,1000)
%   panel2: (300,0) to (1500,1000)
   
% create GUI
    f = figure('Visible','on','Position',[0,0,1500,1000],...
    'MenuBar','none','NumberTitle','off');

% create components of UI inputs and outputs in panel1
    inputs=uipanel('Position',[0 0 .2 1]);

% select speech directory pushbutton
    h_select_dir=uicontrol('Style','Pushbutton','String','Speech Dir.',...
        'Position',[5 200+offset 90 50],...
        'Callback',{@h_select_dir_Callback});
    
% speech directory button
    h_dir= uicontrol('Style','edit','String',speechdir, ...
        'BackgroundColor',[1 1 1],...
        'Position',[100 200+offset 150 50], ...
        'Callback',{@h_dir_Callback});

% speech file popup menu.
    speech_file_list= { '... ' '.........'};
    h_speech_file= uicontrol('Style','popupmenu','String',speech_file_list, ...
        'BackgroundColor',[1 1 1],...
        'Position',[75 145+offset 140 50], ...
        'Callback',{@h_speech_file_Callback});
    
% speech sequence of files.
    % speech_sequence_list= {'...' '...'};
    % h_speech_sequence= uicontrol('Style','popupmenu','String',speech_sequence_list, ...
        % 'BackgroundColor',[1 1 1],...
        % 'Position',[75 100+offset 140 50], ...
        % 'Callback',{@h_speech_sequence_Callback});
      
% speech filename button
    S_File=uicontrol('parent',inputs,'Style','edit',...
        'String','-----','Position',[75 115+offset 140 50],...
        'BackgroundColor','white',...
        'Callback',{@S_File_Callback});
    Label1=uicontrol('parent',inputs,'Style','text',...
        'String','Speech File','Position',[100 55+offset,80,50]);
    
% load speech file button
    loadspeech = uicontrol('Style','pushButton',...
        'String','Load Speech File','Position',[20 40+offset 100 50],...
        'Callback',{@loadspeech_Callback});
    
% play speech files button
    playspeech = uicontrol('Style','pushButton',...
        'String','Play Speech File','Position',[140 40+offset 100 50],...
        'Callback',{@playspeech_Callback});
    
% current sampling rate button
    FSA=uicontrol('parent',inputs,'Style','edit',...
        'String','----','Position',[20 50+offset+offset1 100 50],...
        'BackgroundColor','white',...
        'Callback',{@FSA_Callback});
    Label3=uicontrol('parent',inputs,'Style','text',...
        'String','Original FS (Hz)','Position',[0 -10+offset+offset1,140,50]);
    
% new sampling rate button
    FSN=uicontrol('parent',inputs,'Style','edit',...
        'String','----','Position',[140 50+offset+offset1 100 50],...
        'BackgroundColor','white',...
        'Callback',{@FSN_Callback});
    Label4=uicontrol('parent',inputs,'Style','text',...
        'String','Desired FS (Hz)','Position',[120 -10+offset+offset1,140,50]);
    
% change sampling rate button
    changesr = uicontrol('Style','pushButton',...
        'String','Change Sampling Rate','Position',[75 -25+offset+offset1 140 50],...
        'Callback',{@changesr_Callback});
    
% window length (msec) for wideband spectrogram
    wlenwb=uicontrol('parent',inputs,'Style','edit',...
        'String','3','Position',[20 -90+offset+offset1 100 50],...
        'BackgroundColor','white',...
        'Callback',{@wlenwb_Callback});
    Label5=uicontrol('parent',inputs,'Style','text',...
        'String','windowlen (ms)-WB','Position',[0 -150+offset+offset1,140,50]);
    
% window length (msec) for narrowband spectrogram
    wlennb=uicontrol('parent',inputs,'Style','edit',...
        'String','30','Position',[140 -90+offset+offset1 100 50],...
        'BackgroundColor','white',...
        'Callback',{@wlennb_Callback});
    Label6=uicontrol('parent',inputs,'Style','text',...
        'String','windowlen (ms)-NB','Position',[120 -150+offset+offset1,140,50]);
    
% fft length (msec) for wideband spectrogram
    fftlenwb=uicontrol('parent',inputs,'Style','edit',...
        'String','1024','Position',[20 -170+offset+offset1 100 50],...
        'BackgroundColor','white',...
        'Callback',{@fftlenwb_Callback});
    Label7=uicontrol('parent',inputs,'Style','text',...
        'String','fft length-WB','Position',[0 -230+offset+offset1,140,50]);
    
% fft length (msec) for narrowband spectrogram
    fftlennb=uicontrol('parent',inputs,'Style','edit',...
        'String','1024','Position',[140 -170+offset+offset1 100 50],...
        'BackgroundColor','white',...
        'Callback',{@fftlennb_Callback});
    Label8=uicontrol('parent',inputs,'Style','text',...
        'String','fft length (ms)-NB','Position',[120 -230+offset+offset1 140 50]);
    
% log/linear magnitude push-button
    loglin_list={'log magnitude' 'linear magnitude'};
    h_loglin_list=uicontrol('Style','popupmenu','String',loglin_list,...
        'BackgroundColor','white','Position',[70 -245+offset+offset1 140 50],...
        'Callback',{@h_loglin_list_Callback});
    Label9=uicontrol('parent',inputs,'Style','text',...
        'String','log/linear magnitude','Position',[70 -280+offset+offset1 140 50]);
    
% dynamic range for log magnitude spectrogram
    dynrang=uicontrol('parent',inputs,'Style','edit',...
        'String','60','Position',[20 -300+offset+offset1 100 50],...
        'BackgroundColor','white',...
        'Callback',{@dynrang_Callback});
    Label10=uicontrol('parent',inputs,'Style','text',...
        'String','dynamic range (dB)','Position',[0 -355+offset+offset1 140 50]);
    
% scale factor for linear magnitude spectrum
    scalef=uicontrol('parent',inputs,'Style','edit',...
        'String','2','Position',[140 -300+offset+offset1 100 50],...
        'BackgroundColor','white',...
        'Callback',{@scalef_Callback});
    Label11=uicontrol('parent',inputs,'Style','text',...
        'String','scale factor (power)','Position',[120 -355+offset+offset1 140 50]);
    
% gray scale/color push-button
    color_list={'gray scale' 'color'};
    h_color_list=uicontrol('Style','popupmenu','String',color_list,...
        'BackgroundColor','white','Position',[70 -370+offset+offset1 140 50],...
        'Callback',{@h_color_list_Callback});
    Label12=uicontrol('parent',inputs,'Style','text',...
        'String','gray/color','Position',[60 -395+offset+offset1 140 50]);
    
% play speech files button
    createspec = uicontrol('Style','pushButton',...
        'String','Create Spectrograms','Position',[75 -410+offset+offset1 140 50],...
        'Callback',{@createspec_Callback});
    
% upper graphics panel1
    p1=uipanel('Position',[0.2 0.53 0.8 0.47]);
    h=axes('parent',p1,'Position',[0.11 0.11 0.84 0.79],...
        'GridLineStyle','--');

% lower graphics panel1
    p3=uipanel('Position',[0.2 0 0.8 0.525]);
    ha=axes('parent',p3,'Position',[0.11 0.21 0.84 0.69],...
        'GridLineStyle','--');
        
% Initialize the GUI.
    set([f,p1,p3],'Units','normalized');

% Assign the GUI a name to appear in the window title.
    set(f,'Name','GUI Plot Spectrograms');

% Move the GUI to the center of the screen.
    movegui(f,'center');

% select directory callback
function h_select_dir_Callback(source,eventdata);
    select_dir('f:\other_speech',h_dir);
    speechdir=get(h_dir,'String');
    clear filenamelist;
    clear speech_sequence_list;
    list_position=1;
    [filenamelist]=get_wave_file_names(speechdir,1,h_speech_file);
end

% speech files popup menu callback 
function h_speech_file_Callback(source,eventdata);
    speech_file_list= { '... ' '.........'};
    val=get(h_speech_file,'Value');
    curr_file=char(filenamelist(val));
    filename=curr_file;
    fprintf('speechdir: %s, filename: %s \n',speechdir,filename);
    if (list_position == 1)
        speech_sequence_list={filename,'...'};
    elseif (list_position >= 2)
        speech_sequence_list{list_position}=filename;
    end
    list_position=list_position+1;
    set(S_File,'string',curr_file);
    % set(h_speech_sequence,'String',speech_sequence_list);
end

    function loadspeech_Callback(source,eventdata);
        spfile=[];
        silence=zeros(1,40000);
        speechdirp=strcat(speechdir,'\');
        [y1,fs]=loadwav(strcat(speechdirp,speech_sequence_list{1}));
        FSA_Callback(source,eventdata);
    end

    function playspeech_Callback(source,eventdata);
        indx=find(y1);
        y1s=y1(indx(1):indx(length(indx)));
        soundsc(y1s,fs);
    end

    function createspec_Callback(source, eventdata);
        FSA_Callback(source,eventdata);
        FSN_Callback(source,eventdata);
        wlenwb_Callback(source,eventdata);
        wlennb_Callback(source,eventdata);
        fftlenwb_Callback(source,eventdata);
        fftlennb_Callback(source,eventdata);
        [STR,VAL]=h_loglin_list_Callback(source,eventdata);
        iloglin=VAL;
        dynrang_Callback(source,eventdata);
        scalef_Callback(source,eventdata);
        [STR,VAL]=h_color_list_Callback(source,eventdata);
        icolor=VAL;
        windowwb=fix(wlengwb*fs/1000);
        windownb=fix(wlengnb*fs/1000);
        [B,BA,F,T]=create_spectrogram(y1,fftlengwb,fs,windowwb,dynrange,iloglin);
% plot wideband spectrogram
        axes(h);
        if (iloglin == 1)
            imagesc(T,F,BA);
            stitle=sprintf('file: %s, file length (sec): %7.3f, NWB,NNB: %d %d, dynamic range: %d ',...
                filename,length(y1)/fs,windowwb,windownb,dynrange);
        else
            imagesc(T,F,abs(B).^scalefac);
            stitle=sprintf('file: %s, file length(sec): %7.3f, NWB,scwb,NNB,scnb: %d %5.2f %d %5.2f ',...
                filename,length(y1)/fs,windowwb,scalefac,windownb,scalefac);
        end
        axis xy,xlabel('time in seconds'),ylabel('frequency in Hz');
        axis([0 length(y1)/fs 0 fs/2]);
        title(stitle);
        if (icolor == 1)
            t=colormap(gray);
            colormap(1-t);
        else
            t=colormap;
            colormap(1-t);
            colormap(jet);
        end
        
        [B,BA,F,T]=create_spectrogram(y1,fftlengnb,fs,windownb,dynrange,iloglin);
% plot wideband spectrogram
        axes(ha);
        if (iloglin == 1)
            imagesc(T,F,BA);
            stitle=sprintf('file: %s, file length (sec): %7.3f, NWB,NNB: %d %d, dynamic range: %d ',...
                filename,length(y1)/fs,windowwb,windownb,dynrange);
        else
            imagesc(T,F,abs(B).^scalefac);
            stitle=sprintf('file: %s, file length(sec): %7.3f, NWB,scwb,NNB,scnb: %d %5.2f %d %5.2f ',...
                filename,length(y1)/fs,windowwb,scalefac,windownb,scalefac);
        end
        axis xy,xlabel('time in seconds'),ylabel('frequency in Hz');
        axis([0 length(y1)/fs 0 fs/2]);
        title(stitle);
        if (icolor == 1)
            t=colormap(gray);
            colormap(1-t);
        else
            t=colormap;
            colormap(1-t);
            colormap(jet);
        end
    end

% reset sampling rate
    function FSA_Callback(source,eventdata);
        set (FSA,'string',num2str(fs));
    end

% read in new sampling rate
    function FSN_Callback(source,eventdata);
        fsnn=get(FSN,'string');
        fsn=str2num(fsnn);
    end

% read in values for Wideband and Narrowband window durations
    function wlenwb_Callback(source,eventdata);
        w=get(wlenwb,'string');
        wlengwb=str2num(w);
    end

% read in values for Wideband and Narrowband window durations
    function wlennb_Callback(source,eventdata);
        w=get(wlennb,'string');
        wlengnb=str2num(w);
    end

% read in values for Wideband and Narrowband fft durations
    function fftlenwb_Callback(source,eventdata);
        w=get(fftlenwb,'string');
        fftlengwb=str2num(w);
    end

% read in values for Wideband and Narrowband fft durations
    function fftlennb_Callback(source,eventdata);
        w=get(fftlennb,'string');
        fftlengnb=str2num(w);
    end

% read in log/lin value
    function [str,val]=h_loglin_list_Callback(source,eentdata)
        str=get(h_loglin_list,'String');
        val=get(h_loglin_list,'Value');
    end

% read in dynamic range
    function dynrang_Callback(source,eventdata);
        w=get(dynrang,'string');
        dynrange=str2num(w);
    end

% read in scale factor
    function scalef_Callback(source,eventdata);
        w=get(scalef,'string');
        scalefac=str2num(w);
    end

% read in gray/color value
    function [str,val]=h_color_list_Callback(source,eentdata)
        str=get(h_color_list,'String');
        val=get(h_color_list,'Value');
    end

% change sampling rate
    function changesr_Callback(source,eventdata);
        srchange(y1,fs,fsn);
        FSA_Callback(source,eventdata);
    end

% srchange function
    function srchange(y1,fs,fsn)
        global y1;
        global fs;
        fprintf('fs,fsn,length(y1): %d %d %d  \n',fs,fsn,length(y1));
        if (fsn == 20000)
            if (fs == 16000)
                up=5; down=4;
            elseif (fs == 10000)
                up=2; down=1;
            elseif (fs == 8000)
                up=5; down=2;
            elseif (fs == 6000)
                up=10; down=3;
            else
                fprintf('improper sampling rate change: fs, fsn: %d %d \n',fs,fsn);
                pause
            end
        elseif (fsn == 16000)
            if (fs == 20000)
                up=4; down=5;
            elseif (fs == 10000)
                up=8; down=5;
            elseif (fs == 8000)
                up=2; down=1;
            elseif (fs == 6000)
                up=8; down=3;
            else
                fprintf('improper sampling rate change: fs, fsn: %d %d \n',fs,fsn);
                pause
            end
        elseif (fsn == 10000)
            if (fs == 20000)
                up=1; down=2;
            elseif (fs == 16000)
                up=5; down=8;
            elseif (fs == 8000)
                up=4; down=5;
            elseif (fs == 6000)
                up=5; down=3;
            else
                fprintf('improper sampling rate change: fs, fsn: %d %d \n',fs,fsn);
                pause
            end
        elseif (fsn == 8000)
            if (fs == 20000)
                up=2; down=5;
            elseif (fs == 16000)
                up=1; down=2;
            elseif (fs == 10000)
                up=4; down=5;
            elseif (fs == 6000)
                up=5; down=3;
            else
                fprintf('improper sampling rate change: fs, fsn: %d %d \n',fs,fsn);
                pause
            end
        elseif (fsn == 6000)
            if (fs == 20000)
                up=3; down=10;
            elseif (fs == 16000)
                up=3; down=8;
            elseif (fs == 10000)
                up=3; down=5;
            elseif (fs == 8000)
                up=3; down=4;
            else
                fprintf('improper sampling rate change: fs, fsn: %d %d \n',fs,fsn);
                pause
            end
        else
            fprintf('improper sampling rate change: fs, fsn: %d %d \n',fs,fsn);
            pause
        end
        y=y1;
        y1=resample(y,up,down);
        fs=fsn;
        fprintf('up,down,fs,length(y1): %d %d %d %d \n',up,down,fs,length(y1));
    end

% create spectrogram function
    function [B,BA,F,T]=create_spectrogram(y,fftlengwb,fs,windowwb,dynrange,iloglin)
        windowoverlap=fix(0.9*windowwb);
        [B,F,T]=specgram(y1,fftlengwb,fs,triang(windowwb),windowoverlap);
        BA=[];
        if (iloglin == 1)
            BA=20*log10(abs(B));
            BAM=max(BA);
            BAmax=max(BAM);
            BA(find(BA < BAmax-dynrange))=BAmax-dynrange;
        end
    end
end
