matlab生成blocks、bumps和doppler标准测试信号

来源:互联网 发布:算法分析的主要方面是 编辑:程序博客网 时间:2024/05/16 10:20

关键函数:wnoise

使用方式

X = wnoise(FUN,N)
[X,XN] = wnoise(FUN,N,SQRT_SNR)
[X,XN] = wnoise(FUN,N,SQRT_SNR,INIT)

X = wnoise(FUN,N) 返回由 FUN给出的信号,长度为2^N,幅值范围[0,1].

[X,XN] = wnoise(FUN,N,SQRT_SNR) ,返回了一个测试信号X,以及一个加噪信号XN,信噪比为 SNR=(SQRT_SNR)2.

[X,XN] = wnoise(FUN,N,SQRT_SNR,INIT) ,返回了一个测试信号X,以及一个加噪信号XN,信噪比为 SNR=(SQRT_SNR)2.但是产生的噪声是由 INIT 设定的.(这样每次出来的结果就恒定了)

六个函数如下

FUN = 1 or ‘blocks’
FUN = 2 or ‘bumps’
FUN = 3 or ‘heavy sine’
FUN = 4 or ‘doppler’
FUN = 5 or ‘quadchirp’
FUN = 6 or ‘mishmash’

举个栗子

% Generate 2^10 samples of ‘Heavy sine’ (item 3).
x = wnoise(3,10);

% Generate 2^10 samples of ‘Doppler’ (item 4) and of
% noisy ‘Doppler’ with a square root of signal-to-noise
% ratio equal to 7.
[x,noisyx] = wnoise(4,10,7);

% To introduce your own rand seed, a fourth
% argument is allowed:
init = 2055415866;
[x,noisyx] = wnoise(4,10,7,init);

% Plot all the test functions.
ind = linspace(0,1,2^10);
for i = 1:6
x = wnoise(i,10);
subplot(6,1,i), plot(ind,x)
end

实践得知,init你可以随心所欲的设置!反正值一样,产生的噪声就一样啦~
因为wnoise 内部调用了randstream
而MATLAB中的伪随机数都是由一个或者多个random number streams产生的,比如常用的
rand, randn, 或者randi. 都是由相同的均匀随机数产生的,这是所谓的 global stream. 常用随机数发生器列表可用 RandStream.list 查看

随机数产生方法:

RandStream 产生随机数流
RandStream.create 产生多个独立的随机数流.
get 获取产生的随机数流的性质.
list 列举随机数产生算法.
set 设置随机数流的参数.
RandStream.getGlobalStream 获取全局随机数流.
RandStream.setGlobalStream 设置全局随机数流.
reset 恢复默认值
rand 产生符合uniform distribution的伪随机数
randn 产生符合standard normal distribution的伪随机数
randi 产生符合uniform discrete distribution的伪随机整数
randperm 给一列数产生随机扰动

举几个栗子

Example 1
Create a single stream and designate it as the current global stream:

s = RandStream(‘mt19937ar’,’Seed’,1);
RandStream.setGlobalStream(s);
Example 2
Create three independent streams:

[s1,s2,s3] = RandStream.create(‘mrg32k3a’,’NumStreams’,3);
r1 = rand(s1,100000,1);
r2 = rand(s2,100000,1);
r3 = rand(s3,100000,1);
corrcoef([r1,r2,r3])
Example 3
Create only one stream from a set of three independent streams, and designate it as the current global stream:

s2 = RandStream.create(‘mrg32k3a’,’NumStreams’,3,…
‘StreamIndices’,2);
RandStream.setGlobalStream(s2);
Example 4
Reset the global random number stream that underlies rand, randi, and randn back to its beginning, to reproduce previous results:

stream = RandStream.getGlobalStream;
reset(stream);
Example 5
Save and restore the current global stream’s state to reproduce the output of rand:

stream = RandStream.getGlobalStream;
savedState = stream.State;
u1 = rand(1,5)
u1 =
0.8147 0.9058 0.1270 0.9134 0.6324

stream.State = savedState;
u2 = rand(1,5)
u2 =
0.8147 0.9058 0.1270 0.9134 0.6324
u2 contains exactly the same values as u1.

Example 6
Reset the global random number stream to its initial settings. This causes rand, randi, and randn to start over, as if in a new MATLAB session:

s = RandStream(‘mt19937ar’,’Seed’,0);
RandStream.setGlobalStream(s);
Example 7
Reinitialize the global random number stream using a seed based on the current time. This causes rand, randi, and randn to return different values in different MATLAB sessions. It is usually not desirable to do this more than once per MATLAB session as it may affect the statistical properties of the random numbers MATLAB produces:

s = RandStream(‘mt19937ar’,’Seed’,’shuffle’);
RandStream.setGlobalStream(s);
Example 8
Change the transformation algorithm that randn uses to create normal pseudorandom values from uniform values. This does not replace or reset the global stream.

stream = RandStream.getGlobalStream;
stream.NormalTransform = ‘inversion’

更多关于产生随机数的小知识,请移步-》上一篇博文

0 0