function R=imnoise2(type,M,N,a,b)%type是函数类型,M*N是噪声数组的大小,a,b为两个参数
%设置默认值
if nargin==1%如果函数的输入参数为1,则默认a=0;b=1;M=1;N=1
a=0;b=1;
M=1;N=1;
elseif nargin==3%如果函数的输入参数为3,则默认a=0;b=1
a=0;b=1;
end
%开始运行程序
switch lower(type)
case 'gaussian'%如果是高斯类型,执行下面方程
R=a+b*randn(M,N);
case 'salt & pepper'%如果是焦盐类型,当输入参数小于等于3,a=0.05,b=0.05
if nargin<=3
a=0.05;b=0.05;
end
%检验Pa+Pb是否大于1
if(a+b)>1
error('The sum Pa+Pb must be not exceed >1')
end
R(1:M,1:N)=0.5;
X=rand(M,N);%(0,1)范围内产生一个M*N大小的均匀随机数组
c=find(X<=a);%寻找X中小于等于a的数,并赋值为0
R(c)=0;
u=a+b;
c=find(X>a & X<=u);%寻找X中大于a并小于等于u的数,并赋值为1
R(c)=1;
case 'lognormal'%对数正态类型,当输入参数小于等于3,a=1,b=0.25,执行下面方程
if nargin<=3
a=1;b=0.25;
end
R=a*exp(b*randn(M,N));
case 'rayleigh'%瑞利类型,执行下面方程
R=a+(-b*log(1-rand(M,N))).^0.5;
case 'exponential'%指数类型,执行下面方程
if nargin<=3%如果输入参数小于等于3,a=1
a=1;
end
if a<=0%如果a=0,错误类型
error('Parameter a must be positive for exponential type.')
end
k=-1/a;
R=k*log(1-rand(M,N));
case 'erlang'%厄兰类型,如果输入参数小于等于3,a=2,b=5
if nargin<=3
a=2;b=5;
end
if(b~=round(b)|b<=0)%如果b=0,错误类型
error('Param b must a positive integer for Erlang.')
end
k=-1/a;
R=zeros(M,N);
for j=1:b
R=R+k*log(1-rand(M,N));
end
otherwise%如果不是以上类型,输出未知分配类型
error('Unknown distribution type.')
end
imnoise2.m
最新推荐文章于 2025-05-15 00:17:05 发布