Source Coding Matlab.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Source Coding Source Coding The Communications Toolbox includes some basic functions for source coding. Source coding, also known as quantization or signal formatting, includes the concepts of analog-to-digital conversion and data compression. Source coding divides into two basic procedures: source encoding and source decoding. Source encoding converts a source signal into a digital code using a quantization method. The source coded signal is represented by a set of integers {0, 1, 2, ..., N-1}, where N is finite. Source decoding recovers the original information signal sequence using the source coded signal. This toolbox includes two source coding quantization methods: scalar quantization and predictive quantization. A third source coding method, vector quantization, is not included in this toolbox. Scalar Quantization Scalar quantization is a process that assigns a single value to inputs that are within a specified range. Inputs that fall in a different range of values are assigned a different single value. An analog signal is in effect digitized by 3-13 3 Tutorial scalar quantization. For example, a sine wave, when quantized, will look like a rising and falling stair step: Quantized sine wave 1.5 1 0.5 0 Amplitude −0.5 −1 −1.5 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Time (sec) Scalar quantization requires the use of a mapping of N contiguous regions of the signal range into N discrete values. The N regions are defined by a partitioning that consists of N-1 distinction partition values within the signal range. The partition values are arranged in ascending order and assigned indices ranging from 1 to N-1. Each region has an index that is determined by this formula: ≤ () 0xpartition 1 indx() x =i partition()i <x≤partition()i1+ N1– partition()N1– <x For a signal value x, the index of the corresponding region is indx(x). To implement scalar quantization you must specify a length N-1 vector partition and a length N vector codebook. The vector partition, as its name 3-14 Source Coding implies, divides the signal input range into N regions using N-1 partition values. The partition values must be in strictly ascending order. The codebook is a vector that assigns a value, typically either an endpoint of the region or some average value of the interval, to each region defined in the partition. Since each region must have an assigned output value, the length of the codebook must equal the length of the partition. Another way to view this is that the codebook functions as a table lookup with each element assigned to a partition. The index value indx(x) is the output of the quantization encode function. The codebook contains the values that correspond to sample points. There is no function for quantization decoding, which is simply constructing the quantized signal using the stream of index values output by the quantizer. Construct the quantized signal by using the MATLAB command: y = codebook(indx+1); In general, a codebook has the following relation with the vector partition: codebook() 1 ≤≤≤≤partition() 1 codebook() 2 partition()2 … ...≤≤≤ codebook()N – 1 partition()N – 1 codebook()N The quality of the quantization, called the distortion, is the mean-square error between the original signal data sig and the quantized signal quan: M 1 distortion =----- ∑()sig() i –quan() i 2 M i=1 where M is the number of samples of the source signal sig. 3-15 3 Tutorial The computation procedure for the quantization source coding and decoding is shown in the figure below: > partition(1) > partition(2) sig indx + > partition(N-1) source encode quant codebook(indx) quantization decode + + - distor (.)2 + /M Memory distortion computation The dashed square at the top of this figure is the source encode algorithm, which assigns an index after deciding in which region the input signal value falls. The dashed square in the middle of the figure is the quantization decode algorithm, which maps the input index to whatever value the codebook assigns to that particular index. The dashed square at the bottom of the figure is the distortion computation, which calculates a cumulative average in which M is the total number of points used in the computation. The MATLAB function quantiz computes all three outputs shown in the above figure. The Simulink Scalar Quantizer block is available in the source code sublibrary. Training of Partition and Codebook Parameters The key functions in quantization are the assignment of the partition and codebook parameters. In large signal sets with a fine quantization scheme, the 3-16 Source Coding selection of all the correct parameters can be tedious. In the Communications Toolbox you can train these two parameters by using the MATLAB function lloyds. To train the parameters, you must prepare a training set, which typically represents function input data. The function lloyds finds the partition and codebook parameter vectors by minimizing the distortion using the provided training data. Here is an example of the data training for a sinusoidal signal: N = 2^3; % three bits transfer channel t = [0:1000]∗pi/50; sig = sin(t); % one complete period of sinusoidal signal [partition,codebook] = lloyds(sig,N); [indx,quant,distor] = quantiz(sig,partition,codebook); plot(t,sig,t,quant,'--'); In the above commands, sig is a sinusoidal signal to be quantized. The peak amplitude of the input signal must be one. The trained codebook and the partition can be used for sinusoidal signals of any frequency. The above code generates a figure that compares the original signal (the smooth curve) to the quantized signal (the digital curve): 1 0.5 0 −0.5 −1 0 10 20 30 40 50 60 70 The decoding procedure is simple using the basic MATLAB computation format. Use the following command to obtain the decoded result: quant = codebook(indx+1) 3-17 3 Tutorial Companders The quantization discussed above is linear. In certain applications, you may need to quantize a signal based on the power level of the input signal. In this case, it is common to use a logarithm computation before the quantization operation. Since a simple logarithm computation can only handle a positive signal, some modification of the input signal is needed. The logarithm computation is known as a compressor. The reverse computation of a compressor is called an expander. The combination of a compressor and expander is called a compander (compress and expand). This toolbox supports two companders: the µ-law and A-law companders. The selection of either method is a matter of user preference. The MATLAB function compand is designed for compander computation. The Simulink block library includes four blocks for the µ-law and A-law compander computations: µ-Law Compressor, µ-Law Expander, A-Law Compressor, and A-Law Expander. µ-law Compander For a given signal x, the output y of the µ-law compressor is Vlog()1 +µ xV⁄ y = --------------------------------------------- sgn()x log()1 + µ where V is the peak value of signal x, which is also the peak value of y. µ is the µ-law parameter of the compander. The function log is the natural logarithm and sgn is the sign function. The µ-law expander is the inverse of the compressor: ()µ ⁄ V()y log 1 + V () x = ---µ- e + 1 sgn y The MATLAB function for µ-law companding is compand. The corresponding Simulink µ-Law Compressor and µ-Law Expander blocks also support µ-law companding. 3-18 Source Coding A-law Compander For a given signal x, the output y of the A-law compressor is Ax ---------------------- sgn()x 1+ logA for 0 ≤≤xAV⁄ y = V()1 + logAx⁄ V for AV⁄ <xV≤ ---------------------------------------------- sgn()x 1+ logA where V is the peak value of signal x, which is also the peak value of y. A is the A-law parameter of the compander. The function log is the natural logarithm and sgn is the sign function. The A-law expander is the inverse of the compressor: 1+ logA V y---------------------- sgn()y for 0 ≤≤y ---------------------- A 1 + logA x= y ()1 + logA ⁄V–1V V e ---- sgn()y for ---------------------- <yV≤ A 1 + logA The MATLAB function for A-law companding is compand. The corresponding Simulink Α-Law Compressor and Α-Law Expander blocks also support Α-law companding. Predictive Quantization The quantization introduced in the “Scalar Quantization” section is usually implemented when there is no a priori knowledge about the transmitted signal. In practice, a communications engineer often has some a priori information about the message signals. An engineer can use this information to predict the next signal to be transmitted based on past signal transmissions; i.e., he or she can use the past data set x={x(k-m),...,x(k-2), x(k-1)} to predict x(k) by using some function f(.). The most common way to implement predictive quantization is to use the differential pulse code modulation (DPCM) method. The Communications Toolbox provides the tools necessary to implement a DPCM predictive quantizer. 3-19 3 Tutorial Differential Pulse Code Modulation Using the past data set and predictor as described above, the predicted value is assumed to be xˆ ()k =fxk()()– m,,... xk()–2 ,xk()–1 where k is the computation step index. The function f(.) is called the predictor; the integer m is the predictive order. The predictive error ek()=xk()–xˆ()k is quantized by using the method discussed in the “Scalar Quantization” section. The structure of predictive quantization is: source encoded index indx(k) source input x(k) e(k) Quantization + + - Source encode quantized y(k) + ^x(k) Predictor This method is known as the differential pulse code modulation method (DPCM).