Main Content

整数算术运算

此示例说明如何对表示信号和图像的整数数据执行算术运算。

加载整数信号数据

加载由四种乐器发出的信号组成的测量数据集,并将数据中 8 位和 16 位的 A 至 D 结果保存为 int8int16uint16。时间存储为 uint16

load integersignal

% Look at variables
whos Signal1 Signal2 Signal3 Signal4 Time1
  Name            Size            Bytes  Class     Attributes

  Signal1      7550x1              7550  int8                
  Signal2      7550x1              7550  int8                
  Signal3      7550x1             15100  int16               
  Signal4      7550x1             15100  uint16              
  Time1        7550x1             15100  uint16              

对数据绘图

首先,对两个信号绘图以查看信号范围。

plot(Time1, Signal1, Time1, Signal2);
grid;
legend('Signal1','Signal2');

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Signal1, Signal2.

可能需要对这些值进行缩放以计算信号代表的实际物理值,例如电压。

处理数据

可以对整数执行标准算术运算,例如 +-*/。假设要计算 Signal1 和 Signal2 的和。

SumSig = Signal1 + Signal2; % Here we sum the integer signals.

现在,对和信号绘图并查看饱和位置。

cla;
plot(Time1, SumSig);
hold on
Saturated = (SumSig == intmin('int8')) | (SumSig == intmax('int8')); % Find where it has saturated
plot(Time1(Saturated),SumSig(Saturated),'rd')
grid
hold off

Figure contains an axes object. The axes object contains 2 objects of type line. One or more of the lines displays its values using only markers

标记所示即信号饱和的位置。

加载整数图像数据

接下来,看一下对一些图像数据执行的算术运算。

street1 = imread('street1.jpg'); % Load image data
street2 = imread('street2.jpg');
whos street1 street2
  Name           Size                Bytes  Class    Attributes

  street1      480x640x3            921600  uint8              
  street2      480x640x3            921600  uint8              

可以看出,图像为 24 位颜色,存储为三个 uint8 数据平面。

显示图像

显示第一个图像。

cla;
image(street1); % Display image
axis equal
axis off

Figure contains an axes object. The axes object contains an object of type image.

显示第二个图像。

image(street2); % Display image
axis equal
axis off

Figure contains an axes object. The axes object contains an object of type image.

缩放图像

可以按一个双精度常量缩放图像,但仍保持以整数形式存储图像。例如,

duller = 0.5 * street2; % Scale image with a double constant but create an integer
whos duller
  Name          Size                Bytes  Class    Attributes

  duller      480x640x3            921600  uint8              
subplot(1,2,1);
image(street2);
axis off equal tight
title('Original');  % Display image

subplot(1,2,2);
image(duller);
axis off equal tight
title('Duller');    % Display image

Figure contains 2 axes objects. Axes object 1 with title Original contains an object of type image. Axes object 2 with title Duller contains an object of type image.

添加图像

现在,将两个街道图像叠加在一起,并对重影结果绘图。

combined = street1 + duller; % Add |uint8| images
subplot(1,1,1)
cla;
image(combined); % Display image
title('Combined');
axis equal
axis off

Figure contains an axes object. The axes object with title Combined contains an object of type image.