Mathematics Previous page   Next Page

Example -- Digitized Signals

This section describes how you can use integer data types when modeling a digital communication system, such as a telephone network. A digital telephone converts an analog signal--your voice--to a digital signal before transmission. While the analog signal takes on real number values, the digital signal takes on only a finite set of integer values. If you are modeling a digital communication system using MATLAB, you can model these practical implementation effects and save memory by storing the digital signal as an integer data type rather than as type double.

Source Coding

To convert an analog, or source, signal to a digital signal, a digital telephone samples the signal at discrete time intervals and encodes, or quantizes, the sampled values, which are real numbers, as integers. The encoding process is called source coding. One simple way to quantize a sampled signal is to

  1. Partition the range of the signal into a finite number of intervals.
  2. Assign each sampled value an integer based on the interval of the partition the value lies in.

For example, if the signal is a sine wave, whose range is [-1 1], you could partition the range into four equal intervals, labeled 0, 1, 2, and 3, as shown in the following figure.

plot of a sine wave showing partitioning into four intervals

The vertical lines correspond to the sample times. For example, if you sample the signal at time -6, its value lies in the interval [0 0.5], so the quantized value is 2.

Typically, the sample times are closer together and the number of intervals in the partition is larger, to make the encoding more accurate. The following table defines a partition of [-1 1] into 256 intervals, which are assigned integer values from -128 to 127, the range of data type int8.

Interval
Quantized Value
[-Inf, -255/256]
-128
(-255/256, -253/256]
-127
(-253/256, -251/256]
-126
...
...
(-3/256, -1/256]
-1
(-1/256, 1/256)
0
[1/256 3/256)
1
...
...
[249/256, 251/256)
125
[251/256, 253/256)
126
[253/256, Inf]
127

You can use the function int8 to compute the quantized value of a sample whose value is x by the formula

For example,

Note that any samples greater than 1 have the quantized value 127, the maximum value for data type int8, due to saturation, so they cannot be distinguished by this quantization scheme. To distinguish such samples, you would need to enlarge the range of values that are partitioned. Similarly, any samples less than -1 have the quantized value -128, the minimum value for int8.

As an illustration, suppose you sample a sine wave signal at time intervals of .01. The following code converts the sampled values to integers of data type int8 and plots the result:

plot of sampled values of sine wave

While the curve appears to be smooth, you can magnify a portion of it by clicking the magnify icon picture of the zoom tool icon from the toolbaron the toolbar and then clicking the plot three times at one of the peaks, as shown in the following figure.

Figure shows one peak of the sine wave. At the peak is the notation Click 3 times.

The result shows that the plot is actually made up of discrete points with integer y-values.

Figure shows that the plot of the peak is not smooth. It is actually composed of discrete points.

The Communcations Toolbox provides several functions that implement more sophisticated source coding schemes.

Combining Two Signals

Suppose you want to model two signals transmitted over the same channel, such as two people speaking at the same time into separate telephones on the same line. When these signals are combined in a channel, their values are added together. To illustrate this, the following code creates two signals and plots them separately:

The following code adds the signals and plots the result.

Notice that the tops of the peaks are truncated at 127, the maximum value for int8, while the bottoms of the valleys are truncated at -128, the minimum value for int8. This occurs because the sum of the signals in the truncated regions lies outside the original range [-1 1], so it saturates to 127 or -128. One way to deal with this is to first average the source signals before quantizing them, so that their average lies in the range [-1 1]. The following code quantizes the average and plots the result along with the previous plot.

Plot shows signal 1 and signal 2 added together along with a plot of the average of the signals.


Previous page  Integer Arithmetic Warnings for Integer Data Types Next page

© 1994-2005 The MathWorks, Inc.