image thumbnail

Quantizer Characteristic

by

 

20 Feb 2014 (Updated )

This exercisedemonstartes quantizing speech using uniform and a logarithmic (mu-law) quantizer.

Callbacks_quantizer_characteristic_GUI25(f,C)
function Callbacks_quantizer_characteristic_GUI25(f,C)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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);
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%USER CODE FOR THE VARIABLES AND CALLBACKS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize Variables    
    nbitsu=4;
    nbitsmu=4;
    mu=40;
    
% Name the GUI
    set(f,'Name','quantizer_characteristic');

% CALLBACKS

% Callback for button1 -- mu: value of mu for mu-law compression
 function button1Callback(h,eventdata)
     mu=str2num(get(button1,'string'));
     if ~((mu >= 0 && mu <= 800))
        waitfor(errordlg('mu must be a positive integer between 0 and 800'))
        return;
     end
     mu=round(mu);
     set(button1,'string',num2str(mu));
 end

% Callback for button2 -- nbitsu: number of bits in uniform quantizer
 function button2Callback(h,eventdata)
     nbitsu=str2num(get(button2,'string'));
     if ~((nbitsu >= 2 && nbitsu <= 16))
        waitfor(errordlg('nbitsu must be a positive integer between 2 and 16'))
        return;
     end
     nbitsu=round(nbitsu);
     set(button2,'string',num2str(nbitsu));
 end

% Callback for button3 -- nbitsmu: number of bits in mu-law compression
 function button3Callback(h,eventdata)
     nbitsmu=str2num(get(button3,'string'));
     if ~((nbitsmu >= 2 && nbitsmu <= 16))
        waitfor(errordlg('nbitsmu must be a positive integer between 2 and 16'))
        return;
     end
     nbitsmu=round(nbitsmu);
     set(button3,'string',num2str(nbitsmu));
 end

% Callback for button4 -- Run uniform quantizer characteristic
 function button4Callback(h,eventdata)
     
% check editable buttons for changes
    button2Callback(h,eventdata);
    
    setup_quantizer_uniform(nbitsu);
 end

%**********************************************************************
    function setup_quantizer_uniform(B)
%
% Plot the input-output quantizer characteristic for a uniform quantizer
%   - first use the process of rounding and saturation
%   - next use the process of truncation and saturation

% define the input range as [-1 1] and consider quantizer at intervals of
% 0.001; i.e., 2000 intervals from -1 to +1
    s=-1:0.001:1;
    
% consider a B=bit quantizer (2^B total quantization levels) 
% quantize the ramp signal using a uniform quantizer with rounding and
% saturation
    sq=fxquant(s,B,'round','sat');
    
% plot the input-output characteristic of the B=4 uniform quantizer with
% rounding and saturation

% clear graphics Panel 2
    reset(graphicPanel2);
    axes(graphicPanel2);
    cla;
    
% plot mid-tread quantizer characteristic in graphics Panel 2
    plot(s,sq,'b','LineWidth',2),grid on, xlabel('Unquantized Level'),...
        ylabel('Quantized Level');
        axis([-1 1 -1.1 1.1]);
    
% display quantizer characteristics in titleBox1
    stitle1=sprintf('Uniform Quantization -- B=%d bits', B);
    stitle2=sprintf(' Mid-Tread (Top),');
    stitle3=sprintf(' Mid-Riser (Bottom)');
    stitle=strcat(stitle1,stitle2,stitle3);
    set(titleBox1,'string',stitle);
    set(titleBox1,'FontSize',20);
    
% next quantize the ramp signal using a uniform quantizer with truncation
% and saturation
    delta=2/2^B;
    sq=fxquant(s,B,'trunc','sat')+delta/2;
    
% plot the input-output characteristic of the B=4 uniform quantizer with
% truncation and saturation

% clear graphics Panel 1
    reset(graphicPanel1);
    axes(graphicPanel1);
    cla;
    
% plot mid-riser quantizer characteristic in graphics Panel 1
    plot(s,sq,'r','LineWidth',2),grid on, xlabel('Unquantized Level'),...
        ylabel('Quantized Level');
        axis([-1 1 -1.1 1.1]);
    end

% Callback for button5 -- compute mu-law compression and quantization
% check editable buttons for changes
function button5Callback(h,eventdata);
        
    button1Callback(h,eventdata);
    button3Callback(h,eventdata);
    
    setup_quantizer_mu_law(nbitsmu);
end

%******************************************************************
function setup_quantizer_mu_law(B)

% define the input range as [-1 1] and consider quantizer at intervals of
% 0.001; i.e., 2000 intervals from -1 to +1
    s=-1:0.001:1;
    
% consider a B=bit quantizer (2^B total quantization levels) 
% quantize the ramp signal using a uniform quantizer with rounding and
% saturation
    sq=fxquant(s,B,'round','sat');
    Xmax=1;

% relate mu-law input (x) to compressed input, and mu-law quantized sample
% to quantized input
    x =((exp(log(1+mu).*abs(s)/Xmax)-1)*Xmax/mu).*sign(s);
    xq =((exp(log(1+mu).*abs(sq)/Xmax)-1)*Xmax/mu).*sign(sq);
    
% plot the input-output characteristic of the B=4 uniform quantizer with
% rounding and saturation

% clear graphics Panel 2
    reset(graphicPanel2);
    axes(graphicPanel2);
    cla;
    
% plot mid-tread quantizer characteristic
    plot(x,xq,'r','LineWidth',2);xlabel('Unquantized Level'),...
        ylabel('Quantized Level');
    grid on;axis([-1 1 -1.1 1.1]);
    
% next quantize the ramp signal using a uniform quantizer with truncation
% and saturation
    delta=2/2^B;
    sq=fxquant(s,B,'trunc','sat')+delta/2;

% relate mu-law input (x) to compressed input, and mu-law quantized sample
% to quantized input
    x =((exp(log(1+mu).*abs(s)/Xmax)-1)*Xmax/mu).*sign(s);
    xq =((exp(log(1+mu).*abs(sq)/Xmax)-1)*Xmax/mu).*sign(sq);
    
% plot the input-output characteristic of the B=4 uniform quantizer with
% truncation and saturation

% clear graphics Panel 1
    reset(graphicPanel1);
    axes(graphicPanel1);
    cla;
    
% plot mid-riser quantizer characteristic
    plot(x,xq,'b','LineWidth',2);xlabel('Unquantized Level'),...
        ylabel('Quantized Level');
    grid on;axis([-1 1 -1.1 1.1]);
    
% display quantizer information in titleBox1
    stitle1=sprintf('Mu-Law Compression/Uniform Quantization -- Mu=%d, B=%d bits', mu,B);
    stitle2=sprintf(' Mid-Tread (Top),');
    stitle3=sprintf(' Mid-Rising (Bottom)');
    stitle=strcat(stitle1,stitle2,stitle3);
    set(titleBox1,'string',stitle);
    set(titleBox1,'FontSize',18);
 end

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

Contact us