# Generate binary signal and convert to integer in Matlab

I am going to test an algorithm and need a test signals.

My first test signal is a binary signal of 1 or -1. I was thinking about how to implement such a sequence in matlab and wrote this:

binary_data = rand(1,M) > 0.5;
b = 2.*binary_data - 1;


My next step is:

dataInMatrix = reshape(b,length(b)/k,k); % reshape data into binary k-arrays, k=log2(M), M -size of signal constelation
dataSymbolsIn = bi2de(dataInMatrix);


error: Expected input number 1, binary vector b, to be nonnegative.

How to convert such binary sequence to integer if i need create symbols?

EDIT 1.

Guys, I have generated a test signal and it look like this:

• i generate a sequence 1 or -1
• use a raised cosine filter

How can I get a blue lines...show samples ( digital signal)?

• Hi. This is not a signal processing but a pure matlab programming question (Stackoverflow might be a better place), and honestly, it reads like you really should start with the tutorial that comes with matlab. Feb 24, 2021 at 18:50
• @Marcus Muller. Ali23's question certainly is a signal processing question! He needs help generating digital test signals. Rather than belittling Ali23, you should try to help him. Feb 25, 2021 at 10:52
• Hi @RichardLyons you're right, this question has come a long way since it was stated (back then it was really just containing a=ones(1,N): signal = [ a -a a -a];, and that really told me that prior to tackling the signal processing, Ali23 needed to get familiar with the tools :) I didn't mean to belittle! Feb 25, 2021 at 11:36
• Hi @Marcus Muller. Thanks for your comment. I didn't intend to sound "too strong". I merely want the very smart guys on this web site to be as friendly and helpful as they can be. Feb 25, 2021 at 16:02
• @MarcusMüller Unfortunately, you are always harsh in your wording, I see the majority of your comments are little bit "aggressive" specially when you don't know the answer. You usually evaluate the question itself instead of trying to help to get the answer. I am really sorry to say that , but I hope that will help in improving the quality of reply. please take it in a friendly way, I want just to advise you to take care of that point Feb 28, 2021 at 10:39

## 2 Answers

@Ali23. If by "How can I get a blue lines?" you mean how to generate samples of the your red curve, try the following:

m = [1, -1, -1, 1 1, -1]; m = [m m m m];

N = length(m);

M = 10; mup=zeros(1,NM); mup(1:M:NM)=m; % oversample by M

h = hamming(M); % blip pulse of width M

x = filter(h,1,mup); % convolve pulse shape with 'm' data

t = 1/M:1/M:length(x)/M;

figure(1), subplot(2,1,1), plot(0:0.1:0.9,h)

subplot(2,1,2), plot(t,x)

In the above I used a simple hamming sequence rather than the impulse response of a RRC filter. (You should use the impulse response of a RRC filter.) The key point here is that you have to upsample (zero stuffing) the 'm' sequence before you apply it to the filter.

@Ali23. your 'binary_data = rand(1,M) > 0.5;' command generates "logical" data. Try using 'binary_data = int8(rand(1,M));' and see if that helps you.