Matlab 图的标准化
我需要将绘图规格化为0到255的灰度。数据为.txt格式。这是我的粗略规划:Matlab 图的标准化,matlab,plot,matlab-figure,normalization,Matlab,Plot,Matlab Figure,Normalization,我需要将绘图规格化为0到255的灰度。数据为.txt格式。这是我的粗略规划: clf; %call importdata() after changing current directory B = importdata('sigpros.txt'); d = 1000; %vertical spacing Bs = B; %copy of the original data for i = 1 : size(Bs,2) %loop adding constant to ea
clf;
%call importdata() after changing current directory
B = importdata('sigpros.txt');
d = 1000; %vertical spacing
Bs = B; %copy of the original data
for i = 1 : size(Bs,2)
%loop adding constant to each column
Bs(:,i) = Bs(:,i) + (i-1) * d;
end
%plot the modified matrix
plot(Bs);
数据由349行和4007列组成。每列都是完整的a扫描数据(波形)。每个数据都有一个垂直间距,一整套这些绘制的数据构成一个B扫描数据(通过传感器位移获得的波形)。我不确定上面的代码是否正确,但数据应该类似:
这可以通过将上面的矩阵图标准化为0到255的灰度来实现。目前,我的绘图是这样的:。请帮助我获得所需的B扫描图,如上图所示!谢谢
更新
这是最新的。然而,它最初达到峰值的方式高于上图中的方式。这里有什么问题
零偏移移除
clf;
%call importdata() after changing current directory
B = importdata('A_scan1.txt');
Bd = detrend(B,0); %remove the zero offset
d = 1000; %vertical spacing
Bs = Bd; %copy of the original data
for i = 1 : size(Bs,2)
%loop adding constant to each column
Bs(:,i) = Bs(:,i) + (i-1) * d;
end
minV = min(Bs(:));
maxV = max(Bs(:));
Bs_scale = (Bs-minV)*255/(maxV-minV);
%plot the modified matrix
plot(Bs_scale, 'k');
但是,它仍然不是从0开始 这将设置为偏移,以便第一行的起点为零,并缩放所有数据,以便从最小值到最大值的范围为255个单位 我注释掉了,但包含了一些行,它们可以替代缩放数据,使其从0开始&峰值为255。但是,由于存在一些负值,因此总范围大于255
clf;
%call importdata() after changing current directory
B = importdata('sigpros.txt');
%NOTE: I am only plotting the first 59 lines as the rest don't look as good
Bd = detrend(B(:,1:59),0); %remove the zero offset
d = 1000; %vertical spacing
Bs = Bd; %copy of the original data
%Get the initial zero offset from the first line
initOffset = Bs(1,1);
%% xxx Alternatively take the mean across all starting points xxx
% initOffset = mean(Bs(1,:));
for i = 1 : size(Bs,2)
%loop adding constant to each column
Bs(:,i) = Bs(:,i) - initOffset + (i-1) * d ; %subtract the offset from each
end
minV = min(Bs(:));
maxV = max(Bs(:));
%This make the RANGE from min to make = 0-255 units.
Bs_scale = (Bs)*255/(maxV-minV);
%% xxxx If instead you want the peak to be at 255 xxxxx
% Bs_scale = (Bs)*255/(maxV);
%plot the modified matrix
plot(Bs_scale, 'k');
编辑/解释:
。它基本上是一系列的线相互叠加。在执行detrend
后,它将删除大部分恒定偏移量。
然而,由于这个信号不是完全对称的,这些线并不是从零开始的。。。它们比以前更接近,但并不完美。在detrend
之后,请注意每一行并不是从零开始的
接下来,您的
for
循环最初将每行间隔1000倍,增加了d的倍数。因为这些线不是从零开始的,这就是你要的,我加了初始偏移量。这些基本上取第一行的第一个点,然后从每一行中减去它。因此迫使它从零开始。我不完全理解你的要求。看这两个图,唯一的区别(除了更多的线)是你的是颜色。你只是想把所有的线条都变成黑色还是灰色?如果是这样的话,就做这个绘图(Bs,'k')代码>我还需要将绘图规格化为0到255。。这就是我被告知的。有什么方法可以规范化矩阵图吗?我不确定您希望它如何规范化。您可以重新缩放所有内容,使Bs
的最大值变为255,然后Bs
的最小值变为0。注意:目前在两个示例图中都有一些负值,因此将应用偏移量代码>maxV=max(Bs(:)代码>Bs_标度=(Bs最小值)*255/(最大最小值)代码>然后<代码>绘图(Bs_标度,'k')代码>。。。如果这就是你想要的,我可以把它放在一个答案里。。。但似乎有点武断,为什么武断?我可以试试你所做的,我会通知你的。你能详细说明一下initOffset=Bs(1,1);修正了零偏移?谢谢你的解释