Code covered by the BSD License  

Highlights from
SNR

image thumbnail

SNR

by

 

15 Feb 2014 (Updated )

This MATLAB exercise computes and plots the SNR of a quantized speech file.

qplot(s,nbits,mu,ncases,iplot)
function qplot(s,nbits,mu,ncases,iplot)
%
% function for plotting signal-to-noise ratio of uniform and mu-law 
% quantizers
%
% Inputs:
%	s: input signal
%	nbits: number of bits in the quantizer
%	mu: mu-law compression parameter
%	ncases: number of cases to plot
%   iplot: plot curves versus log10 (iplot=1), log (iplot=2), or on
%   semi-log axis (iplot=3)

% Initialize plotting arrays; there are P ranges of Xmax/sigmax, and 2
% cases, namely uniform and mu-law compression
    P=zeros(ncases,2);
    
% initialize signal for quantization
    x=s;
    
% pre-compute Xmax/sigmax for the ncases
    c(1)=5.33;
    for index=2:ncases
        c(index)=c(index-1)*2;
    end
            
% loop through the number of cases being plotted
    for i=1:ncases
        
% first quantize the input signal using a uniform quantizer with nbits,
% using rounding and saturation arithmetic
        sh=fxquant(x,nbits,'round','sat');
        
% save the snr results in complex array P(ncases,2)
% use appropriate format for log10(Xmax/sigmax), log(Xmax/sigmax), or just
% plain Xmax/sigmax
        if (iplot == 1)
            P(i,1)=((i-1)*0.301+0.72)+sqrt(-1)*snr(sh,x);
        elseif (iplot == 2)
            P(i,1)=((i-1)*0.693+1.67)+sqrt(-1)*snr(sh,x);
        elseif (iplot == 3)
            P(i,1)=c(i)+sqrt(-1)*snr(sh,x);
        end
        
% now compress the signal using the mu-law curve
        y=mulaw(x,mu);
        
% now quantize the compressed input signal using a uniform quantizer with
% nbits, using rounding and saturation arithmetic
        yh=fxquant(y,nbits,'round','sat');
        
% expand the quantized input using the inverse mu-law characteristic
        xh=mulawinv(yh,mu);

% save the snr results in complex array P(ncases,2)
% use appropriate format for log10(Xmax/sigmax), log(Xmax/sigmax), or just
% plain Xmax/sigmax
        if (iplot == 1)
            P(i,2)=((i-1)*0.301+0.72)+sqrt(-1)*snr(xh,x);
        elseif (iplot == 2)
            P(i,2)=((i-1)*0.693+1.67)+sqrt(-1)*snr(xh,x);
        elseif (iplot == 3)
            P(i,2)=c(i)+sqrt(-1)*snr(xh,x);
        end
        
% change the value of Xmax/sigmax by dividing x by 2 at each of the ncases
% of run, effectively adding the constant 1 to the log10(Xmax/sigmax)
        x=x/2;
    end
    
% plot the resulting curves of SNR versus ratio of Xmax to sigmax
    if (iplot == 1)
        plot(P,'k*-','LineWidth',2),hold on;
    elseif (iplot == 2)
        plot(P,'k*-','LineWidth',2),hold on;
    elseif (iplot == 3)
        semilogx(P,'k*-','LineWidth',2),hold on;
    end
end

Contact us