Digital Signal Processing Reference
In-Depth Information
% Note: the midtread method is used in this quantizer.
%
delta
¼
2*abs(Xmax)/(2^NoBits-1);
pq
¼
indx*delta;
Program 11.7. MATLAB function for calculation of signal to quantization noise ratio (SNR).
function snr
¼
calcsnr(speech, qspeech)
% function snr
calcsnr(speech, qspeech)
% This routine was created for calculation of SNR.
%
% speech: original speech waveform.
% qspeech: quantized speech.
% snr: output SNR in dB.
%
% Note: the midrise method is used in this quantizer.
%
¼
qerr
¼
speech-qspeech;
snr
¼
10*log10(sum(speech.*speech)/sum(qerr.*qerr))
Program 11.8. Main program for digital
m
-law encoding and decoding.
load we12b.dat
for i
¼
1:1:length(we12b)
code8b(i)
¼
dmuenc(12, we12b(i));
% Encoding
qwe12b(i)
¼
dmudec(code8b(i));
% Decoding
end
subplot(4,1,1),plot(we12b);grid
ylabel(
'
a
'
);axis([0 length(we12b) -1024 1024]);
subplot(4,1,2),plot(code8b);grid
ylabel(
'
b
'
);axis([0 length(we12b) -128 128]);
subplot(4,1,3),plot(qwe12b);grid
ylabel(
'
c
'
);axis([0 length(we12b) -1024 1024]);
subplot(4,1,4),plot(qwe12b-we12b);grid
ylabel(
);axis([0 length(we12b) -40 40]);
Program 11.9. The digital
m
-law compressor.
d
);xlabel(
Sample number
'
'
'
'
function [cmp_code ]
dmuenc(NoBits, value)
% This routine is created for simulation of 12-bit mu law compression.
% function [cmp_code ]
¼
dmuenc(NoBits, value)
% NoBits
¼
number of bits for the data
% value
¼
input value
% cmp_code
¼
output code
%
¼
scale
¼
NoBits-12;
value
¼
value*2^(-scale); % Scale to 12 bit
if (abs(value) >
¼
0) & (abs(value)<16)
cmp_code
¼
value;
end
if (abs(value) >
¼
16) & (abs(value)<32)
Search WWH ::
Custom Search