且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为图表添加适当的噪音

更新时间:2023-12-05 16:15:04

问题是您想让噪音具有某种特征。沿着曲线你有很多样本,并且你希望它保持连接。你会喜欢相当平滑的结果,并且你希望曲线保持关闭状态。所以,按顺序:随机行走噪音将保持连接点。低通滤波噪声将保持曲线平滑。并将噪声终点固定为零(平滑)以确保结果为封闭。以下是一些产生16种不同噪声(4x4)的代码,改变了总体规模和总体过滤量。您必须根据数据的采样率和形状的整体比例来调整这两个选项。

 %生成样本数据
[x,y] = pol2cart(0:0.01:2 * pi,1);

%选择一组4个噪声标度,并且噪声滤波器值为
scales = [.01 .05 .1 .5];
filterstrength = [.1 .5 .9 .98];

%绘制一个4x4网格,为每一个选择不同类型的噪音
对于i = 1:4
对于j = 1:4
scale = scale (一世);
f = filterstrength(j);

%通过过滤一个std 1高斯随机
%walk
nx = filter(scale *(1-f),[1 -f] ,cumsum(randn(size(x))));
ny = filter(scale *(1-f),[1 -f],cumsum(randn(size(y))));

%我们想要一个封闭的多边形,所以detrend结果使得
%最后一点与第一个点相同
nx = nx - linspace(0,如图1所示,长度(NX))*(NX(端)-NX(1))。
ny = ny - linspace(0,1,length(ny))。*(ny(end)-ny(1));

subplot(4,4,4 *(i-1)+ j);

%添加噪音
图(x + nx,y + ny);
end
end

您可以改变其他的东西:您有几乎无限的选择为滤镜形状,这会影响变形的风格。

I have a matlab graph. Something like a trajectory. I want to add noise to graph. I tried adding normal distribution noise. using rand. e.g.

x1=x+a*rand(size(x));

and similarly for y.

The results are attached below. This is not what I want. This gives me a either scatter plot, or completely noisy plot. As illustrated below. The first row is what I did, third row what I want.

Different graph columns stand for different standard deviation (value of a).

Q. How to obtain third type (row) of plot?

P.S. The first row is when I use plot(...,".",markersize,1); and second row for simple vector plot.

The issue is that you want the noise to have a certain characteristic. You have many samples along the curve, and you'd like it to stay "connected". You'd like fairly smooth results, and you want the curve to stay closed. So, in order: random walk noise will keep the points connected. Low-pass-filtered noise will keep the curve smooth. And fix the noise endpoint to be zero (smoothly) to ensure a closed result. Here's some code that generates 16 different kinds of noise (4x4), varying the overall scale and the overall amount of filtering. You'll have to adjust both of these choices based on the "sample rate" of your data, and the overall scale of the shape.

% Generate sample data
[x,y] = pol2cart(0:0.01:2*pi, 1);

% Pick a set of 4 noise scale, and noise filter values
scales = [.01 .05 .1 .5];
filterstrength = [.1 .5 .9 .98];

% Plot a 4x4 grid, picking a different type of noise for each one
for i=1:4
    for j=1:4
        scale = scales(i);
        f = filterstrength(j);

        % Generate noise for x and y, by filtering a std 1 gaussian random
        % walk
        nx = filter(scale*(1-f), [1 -f], cumsum(randn(size(x))));
        ny = filter(scale*(1-f), [1 -f], cumsum(randn(size(y))));

        % We want a closed polygon, so "detrend" the result so that
        % the last point is the same as the first point
        nx = nx - linspace(0,1,length(nx)).*(nx(end)-nx(1));
        ny = ny - linspace(0,1,length(ny)).*(ny(end)-ny(1));

        subplot(4,4,4*(i-1)+j);

        % Add the noise
        plot(x+nx,y+ny);
    end
end

Other things you could vary: You have nearly infinite choices for the filter shape, which will affect the style of deformation.