Question

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.

enter image description here

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top