How to control the variation speed of a generated random signal ?
Show older comments
Hello:
I want to generate a random signal that 1) follows a Normal distribution 2) has slow variations. So I have did this:
pd_lp = makedist('Normal','mu',2,'sigma',1);
R_lp_base = random(pd_lp,5000,1);
figure
plot(R_lp_base)
The variation of this signal is rapid and I need it to be slow. Does anyone knows how to control the variation´s speed of the random generated signals in matlab ?
Answers (1)
Start with sorting the random values. Then mix the result partially:
x = rand(1, 500);
y = sort(x);
z1 = [y(1:2:500), flip(y(2:2:500))];
z2 = [z1(1:2:500), flip(z1(2:2:500))];
t = 1:500;
plot(t, x, 'b', t, y+1.1, 'g', t, z1+2.2, 'c', t, z2+3.3, 'r')
This has the wanted distribution and you can increase the "speed" with applying [zi(1:2:500), flip(zi(2:2:500))] repeatedly.
Alternative:
x = rand(1, 500);
y = x;
s = 0.01;
for k = 2:500
match = find(abs(y(k:500) - y(k-1)) < s);
if ~isempty(match)
r = match(randi([1, numel(match)])) + k - 1;
[y(k), y(r)] = swap(y(k), y(r));
end
end
t = 1:500;
plot(t, x, 'b', t, y+1, 'c')
mean(abs(diff(x)))
mean(abs(diff(y)))
function [b,a] = swap(a,b)
end
What an ugly hack. Call it "mudsort".
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
