Code covered by the BSD License  

Highlights from
Narrowband/Wideband Spectra of a Speech Frame

image thumbnail

Narrowband/Wideband Spectra of a Speech Frame

by

 

30 Jan 2014 (Updated )

Compares and contrasts the effects of using either of two different windows

Callbacks_NB_WB_spectra_GUI25(f,C,start_path)
function Callbacks_NB_WB_spectra_GUI25(f,C,start_path)
%SENSE COMPUTER AND SET FILE DELIMITER
switch(computer)				
    case 'MACI64',		char= '/';
    case 'GLNX86',  char='/';
    case 'PCWIN',	char= '\';
    case 'PCWIN64', char='\';
    case 'GLNXA64', char='/';
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=C{1,1};
y=C{1,2};
a=C{1,3};
b=C{1,4};
u=C{1,5};
v=C{1,6};
m=C{1,7};
n=C{1,8};
lengthbutton=C{1,9};
widthbutton=C{1,10};
enterType=C{1,11};
enterString=C{1,12};
enterLabel=C{1,13};
noPanels=C{1,14};
noGraphicPanels=C{1,15};
noButtons=C{1,16};
labelDist=C{1,17};%distance that the label is below the button
noTitles=C{1,18};
buttonTextSize=C{1,19};
labelTextSize=C{1,20};
textboxFont=C{1,21};
textboxString=C{1,22};
textboxWeight=C{1,23};
textboxAngle=C{1,24};
labelHeight=C{1,25};
fileName=C{1,26};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%PANELS
for j=0:noPanels-1
uipanel('Parent',f,...
'Units','Normalized',...
'Position',[x(1+4*j) y(1+4*j) x(2+4*j)-x(1+4*j) y(3+4*j)-y(2+4*j)]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%GRAPHIC PANELS
for i=0:noGraphicPanels-1
switch (i+1)
case 1
graphicPanel1 = axes('parent',f,...
'Units','Normalized',...
'Position',[a(1+4*i) b(1+4*i) a(2+4*i)-a(1+4*i) b(3+4*i)-b(2+4*i)],...
'GridLineStyle','--');
case 2
graphicPanel2 = axes('parent',f,...
'Units','Normalized',...
'Position',[a(1+4*i) b(1+4*i) a(2+4*i)-a(1+4*i) b(3+4*i)-b(2+4*i)],...
'GridLineStyle','--');
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%TITLE BOXES
for k=0:noTitles-1
switch (k+1)
case 1
titleBox1 = uicontrol('parent',f,...
'Units','Normalized',...
'Position',[u(1+4*k) v(1+4*k) u(2+4*k)-u(1+4*k) v(3+4*k)-v(2+4*k)],...
'Style','text',...
'FontSize',textboxFont{k+1},...
'String',textboxString(k+1),...
'FontWeight',textboxWeight{k+1},...
'FontAngle',textboxAngle{k+1});
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%BUTTONS
for i=0:(noButtons-1)
enterColor='w';
if strcmp(enterType{i+1},'pushbutton')==1 ||strcmp(enterType{i+1},'text')==1
enterColor='default';
end
if (strcmp(enterLabel{1,(i+1)},'')==0 &&...
        strcmp(enterLabel{1,(i+1)},'...')==0) %i.e. there is a label
%creating a label for some buttons
uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i)-labelDist-labelHeight(i+1) ...
(m(2+2*i)-m(1+2*i)) labelHeight(i+1)],...
'Style','text',...
'String',enterLabel{i+1},...
'FontSize', labelTextSize(i+1),...
'HorizontalAlignment','center');
end
switch (i+1)
case 1
button1=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button1Callback);
case 2
button2=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button2Callback);
case 3
button3=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button3Callback);
case 4
button4=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button4Callback);
case 5
button5=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button5Callback);
case 6
button6=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button6Callback);
case 7
button7=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button7Callback);
case 8
button8=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button8Callback);
case 9
button9=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button9Callback);
case 10
button10=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button10Callback);
case 11
button11=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button11Callback);
case 12
button12=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button12Callback);
case 13
button13=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button13Callback);
case 14
button14=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button14Callback);
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%USER CODE FOR THE VARIABLES AND CALLBACKS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize Variables
    curr_file=1;
    fs=10000;
    directory_name='abcd';
    wav_file_names='abce';
    fin_path='filename';
    file_info_string=' ';
    fname='output';
%    nfft=1000;
    nfft=2048;
    
% Name the GUI
    set(f,'Name','NB_WB_spectra_GUI');

% CALLBACKS
% Callback for button1 -- Get Speech Files Directory
 function button1Callback(h,eventdata)
     directory_name=uigetdir(start_path,'dialog_title');
     A=strvcat(strcat((directory_name),[char,'*.wav']));
     struct_filenames=dir(A);
     wav_file_names={struct_filenames.name};
     set(button2,'String',wav_file_names);
     
% once the popupmenu/drop down menu is created, by default, the first
% selection from the popupmenu/drop down menu id not called
    indexOfDrpDwnMenu=1;
    
% by default first option from the popupmenu/dropdown menu will be loaded
    [curr_file,fs]=loadSelection(directory_name,wav_file_names,indexOfDrpDwnMenu);
 end

% Callback for button2 -- Choose speech file for play and plot
 function button2Callback(h,eventdata)
     indexOfDrpDwnMenu=get(button2,'val');
     [curr_file,fs]=loadSelection(directory_name,wav_file_names,indexOfDrpDwnMenu);
 end

% **********************************************************************
% function -- load selection from designated directory and file
function [curr_file,fs]=loadSelection(directory_name,wav_file_names,...
    indexOfDrpDwnMenu);
%
% read in speech/audio file
% fin_path is the complete path of the .wav file that is selected
    fin_path=strcat(directory_name,char,strvcat(wav_file_names(indexOfDrpDwnMenu)));

% clear speech/audio file
    clear curr_file;
    clear fs;

% read in speech/audio signal into curr_file; sampling rate is fs 
    [curr_file,fs]=wavread(fin_path);
    
% create title information with file, sampling rate, number of samples
    fname=wav_file_names(indexOfDrpDwnMenu);
    FS=num2str(fs);
    nsamp=num2str(length(curr_file));
    file_info_string=strcat('  file: ',fname,', fs: ',FS,' Hz, nsamp:',nsamp);

% read in filename (fname) from cell array
    fname=wav_file_names{indexOfDrpDwnMenu};
    end

% Callback for button10 -- Play Speech File
    function button10Callback(h,eventdata)
        soundsc(curr_file,fs);
    end

% Callback for button3 -- wtype: window type (1:Hamming, 2:rectangular)
 function button3Callback(h,eventdata)
     wtype=get(button3,'val');
 end

% Callback for button4 -- ss: starting sample in speech file
 function button4Callback(h,eventdata)
     ss=str2num(get(button4,'string'));
 end

% Callback for button5 -- LWB: window duration for wideband spectrum
 function button5Callback(h,eventdata)
     LWB=str2num(get(button5,'string'));
     if (LWB < 3 || LWB > 20)
         waitfor(errordlg('The window duration for wideband spectrum must be between 3 and 20'));
         return;
     end
 end

% Callback for button6 -- LNB: window duration for narrowband spectrum
 function button6Callback(h,eventdata)
     LNB=str2num(get(button6,'string'));
     if (LNB < 30 || LNB > 80)
         waitfor(errordlg('The window duration for narrowband spectrum must be between 30 and 80'));
         return;
     end
 end


% Callback for button11 -- RWB: frame shift for wideband spectrum
 function button11Callback(h,eventdata)
     RWB=str2num(get(button11,'string'));
     if (RWB < 0 || RWB > 20)
         waitfor(errordlg('The frame shift for wideband spectrum must be between 0 and 20'));
         return;
     end
 end

% Callback for button12 -- RNB: frame shift for narrowband spectrum
 function button12Callback(h,eventdata)
     RNB=str2num(get(button12,'string'));
     if (RNB < 3 || RNB > 20)
         waitfor(errordlg('The frame shift for narrowband spectrum must be between 0 and 20'));
         return;
     end
 end

% Callback for button7 -- log/linear spectral display popupmenu
 function button7Callback(h,eventdata)
     log_lin=get(button7,'val');
 end

% Callback for button8 -- Choose Frame Starting Sample
 function button8Callback(h,eventdata)
     ss=1;
     istart=1;
     x=curr_file; 
     ss=plot_speech_cursor(x,ss,istart,graphicPanel2);
     set(button4,'string',num2str(ss));
 end

% Callback to button13 -- Run Current Frame
    function button13Callback(h,eventdata)
     
% callbacks to buttons 2-6 for up-to-date data
    wtype=get(button3,'val');
    log_lin=get(button7,'val');
    ss=str2num(get(button4,'string'));
    LWB=str2num(get(button5,'string'));
    LNB=str2num(get(button6,'string'));
    
% save speech file
    x=curr_file;
    
% set up windows and window durations (in samples)
    [L,win1,win2]=setup_spectra(LWB,LNB,fs,wtype);
                  
% choose title for titleBox1 text
    if (wtype == 1)
        S1=sprintf('HW magnitude/log magnitude spectra -- file: %s',fname);
    else
        S1=sprintf('RW magnitude/log magnitude spectra -- file: %s',fname);
    end
        S2=sprintf(', middle sample: %d, window lengths (msec): %d %d ',ss,LWB,LNB);
        stitle=strcat(S1,S2);
    set(titleBox1,'String',stitle);
    set(titleBox1,'FontSize',15);
    
% save starting sample to button 4
    set(button4,'string',num2str(ss));
    
% create wideband and narrowband speech frames and plot
    x1=x(ss-L(1)/2:ss+L(1)/2-1)';
    x2=x(ss-L(2)/2:ss+L(2)/2-1)';
 
% clear graphics Panel 2
    reset(graphicPanel2);
    axes(graphicPanel2);
    cla;
    
% plot frame of speech on graphics Panel 3
    plot(ss-L(1)/2:ss+L(1)/2-1,x1,'r','LineWidth',2);
    xpp=['Time in Samples; fs=',num2str(fs),' samples/second'];
	xlabel(xpp),ylabel('Amplitude');
    hold on;
    xmax=max(x);
    ssl=ss-L(1)/2;
    ssh=ss+L(1)/2-1;
    w1e=win1(1:ssh-ssl+1);
    plot(ssl:ssh,w1e*xmax,'r','LineWidth',2);
    plot(ss-L(2)/2:ss+L(2)/2-1,x2,'b','LineWidth',2);
    ssl=ss-L(2)/2;
    ssh=ss+L(2)/2-1;
    w2e=win2(1:ssh-ssl+1);
    plot(ssl:ssh,w2e*xmax,'r','LineWidth',2);
    grid on; axis tight;

% convert analysis frame windows to spectra (linear and log magnitudes)
    [X1,X1L,X2,X2L,freq]=get_spectra(x1,x2,L,win1,win2,nfft,fs);

% plot either log magnitude (log_lin=1) or linear magnitude (log_lin=2) on
% graphicPanel1
% clear graphics Panel 1
    reset(graphicPanel1);
    axes(graphicPanel1);
    cla;
    
    if (log_lin == 2)
    
% plot analysis frame spectral magnitudes on graphics Panel 2
 plot(freq,X1(1:nfft/2+1),'r','LineWidth',2);
    xlabel('Frequency'),ylabel('Magnitude Spectra');
    grid on; hold on;
     plot(freq,X2(1:nfft/2+1),'b','LineWidth',2);
   axis tight; grid on;
    
% legend for plot
    s1='LWB:'; s2=num2str(LWB); s3=' msec';
    str1=strcat(s1,s2,s3);
    s1='LNB:'; s2=num2str(LNB);
    str2=strcat(s1,s2,s3);
    legend(str1,str2);
    
    elseif (log_lin == 1)
    
% plot analysis frame spectral log magnitudes
    plot(freq,X1L(1:nfft/2+1),'r','LineWidth',2);
    xlabel('Frequency'),ylabel('Log Magnitude Spectra');
    grid on; hold on;
    plot(freq,X2L(1:nfft/2+1),'b','LineWidth',2);
    axis tight; grid on;
    
% legend for plot
    s1='LWB:'; s2=num2str(LWB); s3=' msec';
    str1=strcat(s1,s2,s3);
    s1='LNB:'; s2=num2str(LNB);
    str2=strcat(s1,s2,s3);
    legend(str1,str2);
    end
    end

% Callback for button14 -- Run Next Frame
    function button14Callback(h,eventdata)
        RNB=str2num(get(button12,'string'));
        R=round(RNB*fs/1000);
        ss=str2num(get(button4,'string'));
        ss=ss+R;
        set(button4,'string',num2str(ss));
        button13Callback(h,eventdata)
    end

% Callback for button9 -- close GUI
 function button9Callback(h,eventdata)
     close(gcf);
 end
 end

Contact us