如何在 MATLAB 中找到矩阵中波的任意方向
How to find the arbitrary direction of waves in a matrix in MATLAB
我有一个二维数据,M
,类似于这段代码的结果:
xv = 1:.2:10;
yv = 1:.2:5;
[X, Y] = meshgrid(xv, yv);
M = sin(X - 4 * Y) + 0.5 * rand(size(X));
figure;
imagesc(xv, yv, M)
M
表示平面有周期性变化。我想找到这波的方向
我的方法是使用 circshift
,每列数据线性增加偏移,并调整偏移直到波对齐。但是我对最佳对齐没有很好的数学定义。
是否有任何替代方法来定义和查找波的方向?
我认为最简单的方法就是使用 imrotate
然后使用正常平均值。在这里,我写了一个小脚本,通过蛮力迭代角度,并试图通过最大化这个平均向量的电视范数来找到正确的角度:
xv = 1:.2:10;
yv = 1:.2:5;
[X, Y] = meshgrid(xv, yv);
M = sin(X - 4 * Y) + 0.5 * rand(size(X));
imagesc(xv, yv, M)
offset = -min(M(:))+1;
angles = 0:1:180; % use any step size you want
tv = zeros(size(angles));
for k = 1:numel(angles)
% use offset to ensure we can set all artificial zeros to NaN
imr = imrotate(M + offset, angles(k));
imr(imr==0) = NaN;
imr = imr - offset;
% measure tv norm of the mean vector
mean_vector = nanmean(imr, 1);
tv(k) = mean(abs(diff(mean_vector(~isnan(mean_vector)))));
subplot(2,2,1);
imagesc(xv, yv, imr)
subplot(2,2,2);
plot(mean_vector);
subplot(2,2,3);
plot(angles(1:k), tv(1:k),'-r')
ylim([0, 1])
drawnow
end
% find angle with maximal tv norm of means
[~, k] = max(tv);
angle = angles(k);
IMR = imrotate(M, angle);
imagesc(xv, yv, IMR);
subplot(2,2,4);
plot(nanmean(IMR));
卷积有用吗?对于这个具体案例,
size = 10;
a = -4; % y=-4x, direction
x = repmat([-size:1:size],2*size+1,1);
y = x';
d = 1-abs(a*x-y)/sqrt(a^2+1);
d(d<0.293)=0;
d2 = sum(d,1);
d(:,d2==0) = [];
d = d/sum(d2);
LM = conv2(M,d,'valid');
LM = [0,0,0,LM,0,0,0];
给你y=-4x
方向上的46个均值,对应y=3
上的46个网格点。零攻边界无效,因此设置为 0。
但这对接近45度角的人来说就不太好了。
我也可以想象使用插值,如果每条线上的一些样本点的坐标被保存到矩阵xq
和yq
的一列,那么就可以
V = interp2(X,Y,M,xq,yq);
LM = mean(V,1);
希望对您有所帮助。
我有一个二维数据,M
,类似于这段代码的结果:
xv = 1:.2:10;
yv = 1:.2:5;
[X, Y] = meshgrid(xv, yv);
M = sin(X - 4 * Y) + 0.5 * rand(size(X));
figure;
imagesc(xv, yv, M)
M
表示平面有周期性变化。我想找到这波的方向
我的方法是使用 circshift
,每列数据线性增加偏移,并调整偏移直到波对齐。但是我对最佳对齐没有很好的数学定义。
是否有任何替代方法来定义和查找波的方向?
我认为最简单的方法就是使用 imrotate
然后使用正常平均值。在这里,我写了一个小脚本,通过蛮力迭代角度,并试图通过最大化这个平均向量的电视范数来找到正确的角度:
xv = 1:.2:10;
yv = 1:.2:5;
[X, Y] = meshgrid(xv, yv);
M = sin(X - 4 * Y) + 0.5 * rand(size(X));
imagesc(xv, yv, M)
offset = -min(M(:))+1;
angles = 0:1:180; % use any step size you want
tv = zeros(size(angles));
for k = 1:numel(angles)
% use offset to ensure we can set all artificial zeros to NaN
imr = imrotate(M + offset, angles(k));
imr(imr==0) = NaN;
imr = imr - offset;
% measure tv norm of the mean vector
mean_vector = nanmean(imr, 1);
tv(k) = mean(abs(diff(mean_vector(~isnan(mean_vector)))));
subplot(2,2,1);
imagesc(xv, yv, imr)
subplot(2,2,2);
plot(mean_vector);
subplot(2,2,3);
plot(angles(1:k), tv(1:k),'-r')
ylim([0, 1])
drawnow
end
% find angle with maximal tv norm of means
[~, k] = max(tv);
angle = angles(k);
IMR = imrotate(M, angle);
imagesc(xv, yv, IMR);
subplot(2,2,4);
plot(nanmean(IMR));
卷积有用吗?对于这个具体案例,
size = 10;
a = -4; % y=-4x, direction
x = repmat([-size:1:size],2*size+1,1);
y = x';
d = 1-abs(a*x-y)/sqrt(a^2+1);
d(d<0.293)=0;
d2 = sum(d,1);
d(:,d2==0) = [];
d = d/sum(d2);
LM = conv2(M,d,'valid');
LM = [0,0,0,LM,0,0,0];
给你y=-4x
方向上的46个均值,对应y=3
上的46个网格点。零攻边界无效,因此设置为 0。
但这对接近45度角的人来说就不太好了。
我也可以想象使用插值,如果每条线上的一些样本点的坐标被保存到矩阵xq
和yq
的一列,那么就可以
V = interp2(X,Y,M,xq,yq);
LM = mean(V,1);
希望对您有所帮助。