Generate a random signal in Matlab

I need to generate a transmit signal of 10,000 random bits in MATLAB with values of -1 or +1, with both having a equal probabilty of occuring and then verify the probability.
I am using y=randi([-1 1],10000)
But I keep getting 0's and do not know how to take them out and am unsure if my function is even correct.
I tried verifying the probabilty by doing 2 different matrices and adding them together and should be equal to 0, but bc i have zeros this did not work.
Any suggestions?

 Accepted Answer

y = (rand(10000,1) > 0.5)*2 -1;

8 Comments

Alternately,
y = randi([0 1], 10000, 1)*2 - 1;
Note: do not expect an exact balance of -1 and +1: the balance is statistical, not exact.
Thank you all! How would I go about verifying afterwards, that it is about =0? Like trying to add them together, as I mentioned I could do 2 different vectors and add together, but unsure how to do this
To determine the proportion of 1s in y,
p = sum(y==1)/numel(y);
I'll add a 3rd option that requires the Machine Learning Toolbox:
y = randsample([-1,1],10000,true);
Each of these methods have about the same probability of choosing 1. This verification produces 10k values of [-1,1] and it repeats that process 10000 times. It then returns the mean proportions of the value 1 in each iteration.
% number of reps
n = 10000;
prob = [0,0,0];
fprintf('Testing first method...\n')
tempProb = zeros(1,n);
for i = 1:n
y = (rand(10000,1) > 0.5)*2 -1;
tempProb(i) = sum(y==1)/10000;
end
prob(1) = mean(tempProb);
% prob(1) = sum(tempProb==.5); % alternative
fprintf('Testing second method...\n')
tempProb = zeros(1,n);
for i = 1:n
y = randi([0 1], 10000, 1)*2 - 1;
tempProb(i) = sum(y==1)/10000;
end
prob(2) = mean(tempProb);
% prob(2) = sum(tempProb==.5); % alternative
fprintf('Testing third method...\n')
tempProb = zeros(1,n);
for i = 1:n
y = randsample([-1,1],10000,true);
tempProb(i) = sum(y==1)/10000;
end
prob(3) = mean(tempProb);
% prob(3) = sum(tempProb==.5); % alternative
%display results
disp(prob)
I was currious about the speed of these methods so I ran each of the (rand, randi, randsamp) 100,000 times. Below are the median execution times and the 95% confidence intervals (percentile method).
191017 133743-Figure 1.png
For a binomial distribution with n number of trials and p probability of success:
mean = n*p
variance = n*p*(1-p)
So in this example the mean would be 10000*0.5 = 5000. The variance is 2500, and the standard deviation is 50. Therefore, you can check if your values for y fall within 1 standard deviation. This is a simplification that makes some assumptions, but should be fine for your situation.
y = randi([0 1], 10000, 1)*2 - 1;
y1 = sum(y==1) % ans = 5025, but will be different every time
The value of y1 should be within 5000 +/- 50 about 68% of the time.
How would you transmit this signal over a channel z[n] of 1 at n=0 and .5 at n=+/-1
p = sum(y==1)/numel(y);
for vector y that can be abbreviated to
p = mean(y==1)
for non-vector toss in a (:) after the y or use mean2() instead of mean()
for non-vector toss in a (:) after the y or use mean2() instead of mean()
You can specify 'all' as the dimension input to mean if you're using release R2018b or newer of MATLAB.
>> A = magic(5);
>> mean(A, 'all')
ans =
13
>> mean(A(:))
ans =
13
>> mean(A)
ans =
13 13 13 13 13

Sign in to comment.

More Answers (0)

Categories

Find more on Random Number Generation 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!