Code covered by the BSD License  

Highlights from
Mu Law Compander

image thumbnail

Mu Law Compander

by

 

15 Feb 2014 (Updated )

This exercise shows the mu-law input-output characteristic for a range of values of mu from 1 to 500

Callbacks_mu_law_compander_GUI25(f,C,start_path)
function Callbacks_mu_law_compander_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);
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%USER CODE FOR THE VARIABLES AND CALLBACKS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize Variables
    curr_file=1;
    fs=8000;
    directory_name='abcd';
    wav_file_names='abce';
    fin_path='filename';
    fname='output';
    nsamp=1;
    ss=1;
    es=1;
    xin=[];
    y=[];
    mu=255;
    xmu=[];

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

% 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',...
%              '?download=true');
%          [saveloc,~,~] = fileparts(pwd); %save to one level up from current folder
%          % Create a waitbar during download
%          h = waitbar(0.35,'This may take several minutes...',...
%              'Name','Downloading Speech Files...');
%          % Download the zipped file
%          [filestr,status] = urlwrite(url,[saveloc filesep 'speech_files.zip'],...
%              'Timeout',10);
%          if status
%              delete(h);
%              hh1= helpdlg('Downloaded. Select a location to UNZIP the speech files.');
%              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)
%              addpref('SpeechApps','path',unziploc);
%              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
% 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 -- 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,'\',strvcat(wav_file_names(indexOfDrpDwnMenu)));

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

% read in speech/audio signal into curr_file; sampling rate is fs 
    [curr_file,fs]=wavread(fin_path);
    xin=curr_file;
    y=xin;
    
% 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 button3 -- mu: value for mu-law companding
 function button3Callback(h,eventdata)
     mu=str2num(get(button3,'string'));
     if ~((mu >= 0 && mu <= 500))
        waitfor(errordlg('mu must be a positive integer between 0 and 500'))
        return;
     end
     mu=round(mu);
     set(button3,'string',num2str(mu));
 end

% Callback for button4 -- Run Mu-Law Compander
 function button4Callback(h,eventdata)
     
% check editable buttons for changes
    button3Callback(h,eventdata);
    
% define starting and ending sample
    ss=1;
    es=length(y);
    
% setup for mu law compander run
    setup_mu_law_compander(y,fs,ss,es,fname,mu);
 end

%**********************************************************************
    function setup_mu_law_compander(y,fs,ss,es,filein,mu_run)
%
% program to show mu-law input-output characteristic for various values of
% mu from 0 to 500
%
% accepts a speech file and mu-law encodes the signal, showing plots of the
% original signal, the histogram of the original signal, and the
% histogram of the mu-law compressed signal (showing it approaches a 
% uniform distribution) 
    
% determine the input-output characteristic of the mu-law compressor for
% values of mu of [0 1 20 50 100 255 500], over the normalized input range of
% [-1 1].
        xin=-1:.001:1;
        muchoices=[0 1 20 50 100 255 500];
    
% set the background to white
        colordef white;
    
% clear graphics Panel 2
        reset(graphicPanel2);
        axes(graphicPanel2);
        cla;
        
% plot straight line for baseline of input-output characteristics of mu-law
% compander in graphics Panel 2
        % plot(xin,xin,'k','LineWidth',2)
    
% plot input-output characteristics of mu-law curves for range of mu values
% in graphics Panel 2
    for i=1:7
        mu=muchoices(i);
        xmu=mulaw(xin,mu);
        if (mu == mu_run)
            plot(xin,xmu,'r','LineWidth',2);
        else
            plot(xin,xmu,'k','LineWidth',1);
        end
        hold on;
    end
        xlabel('Input Level');
        ylabel('Value'); grid on; axis tight;
    legend('mu=0','mu=1','mu=20','mu=50','mu=100','mu=255','mu=500','Location','SouthEast');
    
% scale input to [-1 1] range
        y=y(ss:es)/max(max(y(ss:es)),-min(y(ss:es)));
    
% use mu=mu_run for mu-law encoding and decoding
        mu=mu_run;
        if (mu == 0)
            xmu=y;
        else
            xmu=mulaw(y,mu);
        end

% plot original signal (graphics Panel 4), original signal histogram
% (graphics Panel 2), mu-law encoded signal histogram (graphics Panel 3)

% clear graphics Panel 4
        reset(graphicPanel4);axes(graphicPanel4);cla;
        
% plot the original speech file in graphics Panel 3 (striplot format)
        striplot(y,fs,2000);
     
% clear graphics Panel 1
        reset(graphicPanel1);axes(graphicPanel1);cla;
        
% plot the histogram of original speech in graphics Panel 1
        nbins=51;
        hist(y,nbins);legend('original speech histogram');
        xlabel('Histogram Bin');ylabel('Count');
    
% clear graphics Panel 3
        reset(graphicPanel3);axes(graphicPanel3);cla;
        
% plot the histogram of mu-law companded speech in graphics Panel 3
        hist(xmu,nbins);legend('mu-law transformed speech histogram');
        xlabel('Histogram Bin');ylabel('Count');
    
% display fname, mu-law parameter in titleBox1
    stitle=sprintf('file: %s, mu: %d',filein,mu_run); 
    stitle1=strcat('Mu-Law Companding -- ',stitle);
    set(titleBox1,'string',stitle1);
    set(titleBox1,'FontSize',20);
    end

% Callback for button6 -- play out original and mu-law companded speech files
    function button6Callback(h,eventdata)
% play out speech file
    soundsc(y,fs);
    pause(length(y)/fs);
% play mu-law companded speech file
    soundsc(xmu,fs);
    end

% Callback for button5 -- close GUI
 function button5Callback(h,eventdata)
     fclose('all');
     close(gcf);
 end
end

Contact us