![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176821/image.jpeg)
How to add noise to rect fuction (rectangle) ?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
How to add noise to the edges of 2D rect function? While all the rest is 1 and 0. So that the lines are not straight but still have value of 1.
x = linspace(1,10,100);
y = x;
[X Y] = meshgrid(x,y);
T = rect(X/2).*rect(Y/2);
function A = rect(x)
% rectangle function
A = abs(x) <=1/2;
end
how to add noise to line edges (borders) of T only?
0 commentaires
Réponses (2)
Image Analyst
le 21 Nov 2014
Try this:
clc;
clearvars;
close all;
workspace;
verticalElements = 100;
horizontalElements = 150;
x1 = 4;
y1 = 2;
x2 = 12;
y2 = 8;
% Make x values.
xTop = linspace(x1,x2,horizontalElements);
xBottom = fliplr(xTop);
xLeft = x1+rand(1,verticalElements) - 0.5;
xRight = x2+rand(1,verticalElements) - 0.5;
% Make y values.
yLeft = linspace(y1, y2,verticalElements);
yRight = linspace(y2, y1,verticalElements);
yTop = 8 + rand(1, horizontalElements) - 0.5;
yBottom = 2 + rand(1, horizontalElements) - 0.5;
% Make a box with jagged edges
xBox = [xTop, xRight, xBottom, xLeft];
yBox = [yTop, yRight, yBottom, yLeft];
% Close it
xBox(end+1) = xBox(1);
yBox(end+1) = yBox(1);
% Plot it.
plot(xBox, yBox, 'LineWidth', 2);
grid on;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176821/image.jpeg)
0 commentaires
Youssef Khmou
le 20 Nov 2014
The solution exists for one dimensional function, when you apply the function, you obtain a logical array, you need to convert it to double and find non zero indexes :
rec=@(x) abs(x)<=1/2;
f=rec(-2:0.01:2);
[a,b]=find(f~=0);
N=length(b);
s=0.15;% noise power
f=double(f);
f(b)=1+s*randn(1,N);
plot(f);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176799/image.jpeg)
Voir également
Catégories
En savoir plus sur Particle & Nuclear Physics dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!