0

I am following this example code. I want to add a random noise signal with 2 values per bit. How can I solve that?

The Code looks like:

bits =[1,0,1,0,1,0,1,0];
bitrate = 1; % bits per second

figure;
[t,s] = pnrz(bits,bitrate);
plot(t,s,'LineWidth',3);
axis([0 t(end) -1.1 1.1])
grid on;
title(['Polar NRZ: [' num2str(bits) ']']);
T = length(bits)/bitrate; % full time of bit sequence
n = 200;
N = n*length(bits);
dt = T/N;
t = 0:dt:T;
x = zeros(1,length(t)); % output signal
for i = 0:length(bits)-1
    if bits(i+1) == 1
        x(i*n+1:(i+1)*n) = 1;
    else
        x(i*n+1:(i+1)*n) = -1;
    end
end
smyslov
  • 1,279
  • 1
  • 8
  • 29
Sanap
  • 3
  • 6

1 Answers1

0

if I well understood your question, you can use the random function :

random1or0 = randi(2)-1;

So randi will generate a random integer (in uniformly distributed manner) between 1 and 2, then subtracts 1 to get 0 or 1.

You can relate to this question Generate a random number in a certain range in MATLAB for more details about random numbers in matlab.

Community
  • 1
  • 1
  • yes it's right, but i want to know how to add 2 values per bit as noise signal? – Sanap Jul 09 '15 at 14:39
  • `x(i*n+1:(i+1)*n) = (1+ random1or0) % 2;` and that's it. a noise is just random 0 or 1 added to the output, but if you want more details you have to read about noise in signals ... –  Jul 09 '15 at 14:45
  • `x(i*n+1:(i+1)*n) = (1+ random1or0) % 2;` is another solution @Sanap –  Jul 15 '15 at 18:00
  • @Sanap does this answer your question ? can you please explain what you mean by `how to add 2 values per bit` ? to add to which variable ? –  Jul 17 '15 at 10:49