Mathematics |
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
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.
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
.
You can use the function int8
to compute the quantized value of a sample whose value is x
by
the formula
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:
sample_times = [-2*pi:.01: 2*pi]; source = sin(sample_times); signal = int8(128*source); plot(sample_times, signal, '.')
While the curve appears to be smooth, you can magnify a portion of it by clicking the magnify icon on the toolbar and then clicking the plot three times at one of the peaks, as shown in the following figure.
The result shows that the plot is actually made up of discrete points with integer y-values.
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:
source1=1/3*sin(2*sample_times)+2/3*cos(sample_times); source2=3/4*sin(3*sample_times)+1/4*cos(sample_times); signal1 = int8(128*source1); signal2 = int8(128*source2); plot(sample_times,signal1) hold on plot(sample_times,signal2,'color','red') legend('Signal1', 'Signal2') hold off
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.
hold on avg_signal=int8(128*(mean([source1; source2]))); plot(sample_times, avg_signal, 'color', 'magenta', ... 'linestyle', '--') legend('Signal1 + Signal2', 'Average of signals')
Integer Arithmetic | Warnings for Integer Data Types |
© 1994-2005 The MathWorks, Inc.