Page 65 - Applied Numerical Methods Using MATLAB
P. 65
54 MATLAB USAGE AND COMPUTATIONAL ERRORS
function [y,xmax] = mulaw(x,mu,ymax)
xmax = max(abs(x));
y = ymax*log(1+mu*abs(x/xmax))./log(1+mu).*sign(x); % Eq.(P1.9a)
function x = mulaw_inv(y,mu,xmax)
%nm1p09: to plot the mulaw curve
clear, clf
x = [-1:.005:1];
mu = [10 50 255];
for i = 1:3
[y,xmax] = mulaw(x,mu(i),1);
plot(x,y,’b-’, x,x0,’r-’), hold on
x0 = mulaw_inv(y,mu(i),xmax);
discrepancy = norm(x-x0)
end
1.10 Analog-to-Digital Converter (ADC)
Below are two ADC routines adc1(a,b,c) and adc2(a,b,c), which assign
the corresponding digital value c(i) to each one of the analog data belong-
ing to the quantization interval [b(i), b(i+1)]. Let the boundary vector
and the centroid vector be, respectively,
b=[-3 -2 -10123]; c= [-2.5 -1.5 -0.5 0.5 1.5 2.5];
(a) Make a program that uses two ADC routines to find the output d for
the analog input data a = [-300:300]/100 and plots d versus a to see
the input-output relationship of the ADC, which is supposed to be like
Fig. P1.10a.
function d = adc1(a,b,c)
%Analog-to-Digital Converter
%Input a = analog signal, b(1:N + 1) = boundary vector
c(1:N)=centroid vector
%Output: d = digital samples
N = length(c);
for n = 1:length(a)
I = find(a(n) < b(2:N));
if ~isempty(I), d(n) = c(I(1));
else d(n) = c(N);
end
end
function d=adc2(a,b,c)
N = length(c);
d(find(a < b(2))) = c(1);
for i = 2:N-1
index = find(b(i) <=a&a<= b(i+1)); d(index) = c(i);
end
d(find(b(N) <= a)) = c(N);