Code covered by the BSD License

# Statistical Analysis

### Speech Processing (view profile)

15 Feb 2014 (Updated )

This exercise estimates the statistical properties of a speech signal, namely the PDF.

Callbacks_statistical_analysis_GUI25(f,C,start_path)
function Callbacks_statistical_analysis_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','--');
case 3
graphicPanel3 = 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 4
graphicPanel4 = 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);
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%USER CODE FOR THE VARIABLES AND CALLBACKS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize Variables
curr_file=1;
fs=8000;
fsd=10000;
directory_name='abcd';
wav_file_names='abce';
fin_path='filename';
fname='output';
nsamp=1;
xin=[];
nbins=25;

% Name the GUI
set(f,'Name','statistical_analysis');

% CALLBACKS
% Callback for button1 -- Get Speech Files Directory
function button1Callback(h,eventdata)
directory_name=uigetdir(start_path,'dialog_title');
%      if isempty(getpref('SpeechApps'))
%          url = sprintf('%s%s%s',...
%              'http://www.mathworks.com/matlabcentral/fileexchange/',...
%              '42911-speech-and-audio-files-for-speech-processing-excercises',...
%          [saveloc,~,~] = fileparts(pwd); %save to one level up from current folder
%          h = waitbar(0.35,'This may take several minutes...',...
%          [filestr,status] = urlwrite(url,[saveloc filesep 'speech_files.zip'],...
%              'Timeout',10);
%          if status
%              delete(h);
%              uiwait(hh1);
%              unziploc = uigetdir(saveloc,'Select a location to unzip the speech files');
%              h2 = waitbar(0.2,'This may take a minute...',...
%                  'Name','Unzipping the Speech Files to Location Selected...');
%              unzip(filestr,unziploc);
%              delete(h2)
%              hh2= helpdlg('Ready. Select the speech_files folder in the next window');
%              uiwait(hh2);
%          else
%              warndlg('No Internet Connection to MATLAB Central!');
%          end
%
%      else
%      end
%      directory_name=uigetdir(getpref('SpeechApps','path'));
A=strvcat(strcat((directory_name),'\*.wav'));
struct_filenames=dir(A);
wav_file_names={struct_filenames.name};
set(button2,'String',wav_file_names);
set(button2,'val',1);

% once the popupmenu/drop down menu is created, by default, the first

end

% Callback for button2 -- Choose speech file for play and plot
function button2Callback(h,eventdata)
end

%*************************************************************************
% function -- load selection from designated directory and file
%
%
% fin_path is the complete path of the .wav file that is selected

% clear speech/audio file
clear curr_file;

% read in speech/audio signal into curr_file; sampling rate is fs
xin=curr_file;
xinmax=max(max(xin),-min(xin));
xin=xin/xinmax;

% create title information with file, sampling rate, number of samples
FS=num2str(fs);

% display speech file sampling rate on button 7; rate to be changed
% to fsd if fs not equal to fsd
set(button7,'string',FS);

nsamp=num2str(length(curr_file));
file_info_string=strcat('  file: ',fname,', fs: ',FS,' Hz, nsamp:',nsamp);

% load filename (fname) from cell array
end

% Callback for button3 -- fsd: signal processing rate
function button3Callback(h,eventdata)
fsd=str2num(get(button3,'string'));
a = [6000 8000 10000 16000 20000];
fsd = a(get(button3,'val'));
end

% Callback for button4 -- nbins: number of bins in signal histogram
function button4Callback(h,eventdata)
nbins=str2num(get(button4,'string'));
if ~((nbins >= 25 && nbins <= 101))
waitfor(errordlg('nbins must be a positive integer between 25 and 101'))
return;
end
nbins=round(nbins);
set(button4,'string',num2str(nbins));
end

% Callback for button5 -- Run statistical analysis
function button5Callback(h,eventdata)

% check editable buttons for changes
button3Callback(h,eventdata);
button4Callback(h,eventdata);

% setup and perform statistical analysis of speech file
set_statistical_analysis(xin,fs,fsd,nbins,fname);
end

%*********************************************************************
function set_statistical_analysis(xin,fsi,fsd,nbins,filein)
%
% function to perform statistical analysis of speech file
%
% Inputs:
%   xin: speech signal
%   fs: speech sampling rate
%   fsd: signal processing rate
%   nbins: number of bins for speech histogram
%   filein: speech filename

% resample if fsi ~= fsd
if (fsi ~= fsd)
fprintf('speech rate:%d, processing rate:%d \n',fsi,fsd);
xin=srconv(xin,fsi,fsd);
end

% process entire file from ss=1 to es=length(xin)
ss=1;
es=length(xin);

% determine minimum, maximum, mean and variance, and plot histogram of
% normalized samples, xinn=(xin-xmean)/xsigma
xmin=min(xin(ss:es));
xmax=max(xin(ss:es));
xmean=mean(xin(ss:es));
xsigma=std(xin(ss:es));

% normalize by subtracting the mean and dividing by the standard deviation
xinn=(xin-xmean)/xsigma;

% clear graphics Panel 3
reset(graphicPanel3);
axes(graphicPanel3);
cla;

% plot speech signal histogram on graphics Panel 3
hist(xinn(ss:es),nbins);
string1=sprintf(' file:%s, ss,es:%d %d',filein,ss,es);
string2=sprintf(' nbins:%d',nbins);
string3=sprintf(' min,max,mean,sigma:%d %d %7.2f %7.0f',....
xmin,xmax,xmean,xsigma);
stitle=strcat(string1,string2,string3);
grid on, xlabel('(x-m_{x})/\sigma_{x}'),ylabel('Count');
legend('speech histogram'); title('Speech Signal Histogram');

% display fname and signal processing parameters on titleBox1
stitle1=strcat('Statistical Analysis of Speech -- Speech Histogram,',...
' Power Spectrum Estimates, Autocorrelation Estimates,',...
' Expanded AC Estimate: ',string1,string2);
set(titleBox1,'string',stitle1);
set(titleBox1,'FontSize',15);

% obtain estimate of long-time average power spectrum using a range of
% window lengths from 32 to 512 samples
Nwinr=[512,  128,  32];
color=['k','r','b','g','m'];
index=1;
for Nwin=[512,128,32]
Nfft=2*Nwin;

% estimate power density spectrum in series of overlapped windows; convert
% to estimated autocorrelation function
[P,F,R,T]=pd_spect_U(xin(ss:es),fsd,Nfft,Nwin);

% plot power density spectral estimates for the
% different window lengths on graphics Panel 4
if (Nwin == 512)
reset(graphicPanel4); % clear graphics Panel 4
axes(graphicPanel4);
cla;
else
axes(graphicPanel4);
end

% plot series of power density spectral estimates on graphics Panel 4
plot(F,10*log10(P),color(index),'LineWidth',2),grid on;
ylabel('Log Magnitude in dB');
xlabel('Frequency in Hz');
title(['Speech Power Spectrum Estimates Nwin=512/128/32']);
hold on;

% clear graphics Panel 1
if (Nwin == 512)
reset(graphicPanel1);
axes(graphicPanel1);
cla;
else
axes(graphicPanel1);
end

% plot sequence of autocorrelation functions on graphics Panel 1
plot(T(1:Nfft/2+1)*1000,R(1:Nfft/2+1),color(index),'LineWidth',2);
grid on; ylabel('Autocorrelation Value');
xpp=['Time in msec;  fsd=',num2str(fsd),' samples/second'];
xlabel(xpp);
title(['Speech Autocorrelation Estimates Nwin = 512/128/32']);
hold on;

% clear graphics Panel 2
if (Nwin == 512)
reset(graphicPanel2);
axes(graphicPanel2);
cla;
else
axes(graphicPanel2);
end

% plot first 50 lags of sequence of autocorrelation functions on graphics
% Panel 2
plot(0:49,R(1:50),color(index),'LineWidth',2),grid on;
ylabel('Autocorrelation Value');
xpp=['Time in Samples; fsd=',num2str(fsd),' samples/second'];
xlabel(xpp);
title(['Speech Autocorrelation Estimates-Expanded Scale, Nwin = 512/128/32']);
hold on;

% iterate to next window size
index=index+1;
end

axes(graphicPanel4);
legend('Nwin=512','Nwin=128','Nwin=32');
axes(graphicPanel1);
legend('Nwin=512','Nwin=128','Nwin=32');
axes(graphicPanel2);
legend('Nwin=512','Nwin=128','Nwin=32');
end

% Callback for button6 -- close GUI
function button6Callback(h,eventdata)
close(gcf);
end

% Callback for button7 -- speech file sampling rate
function button7Callback(h,eventdata)
set(button7,'string',num2str(fs));
end
end