How to store the value of y in the loop being executed?

1 回表示 (過去 30 日間)
adriane duarte
adriane duarte 2020 年 9 月 3 日
コメント済み: Tamir Suliman 2020 年 9 月 12 日
I'm making a code and I don't know how to get the values that are being made inside the loop.
How do I see what the y results are?
Can anybody help me ?
% BPSK
M = 2;
% number of bits or symbols
N = 10^4;
% Generates random bits
Bits_ale = randi([0 M-1],N,1)>0.5;
% BPSK modulation 0 -> -1; 1 -> 1
s = 2 * Bits_ale-1;
% Generates random watermark bits
Bit_wat = randi([0 M-1],N,1)>0.5;
for k=1:N
if Bit_wat ==1
y = s * exp(1i * pi/4);
else if Bit_wat ==0
y = s * exp(1i *(- pi/4));
end
end
end

回答 (2 件)

KSSV
KSSV 2020 年 9 月 3 日
This is how you save y but you need to check your logic.
% BPSK
M = 2;
% number of bits or symbols
N = 10^4;
% Generates random bits
Bits_ale = randi([0 M-1],N,1)>0.5;
% BPSK modulation 0 -> -1; 1 -> 1
s = 2 * Bits_ale-1;
% Generates random watermark bits
Bit_wat = randi([0 M-1],N,1)>0.5;
y = zeros(1,N) ;
for k=1:N
if Bit_wat ==1
y(k) = s * exp(1i * pi/4);
elseif Bit_wat ==0
y(k) = s * exp(1i *(- pi/4));
end
end
  2 件のコメント
adriane duarte
adriane duarte 2020 年 9 月 3 日
Thank you KSSV.
I did it this way but the u only stores zeros.
It does not store the values of the loop.
Thank you very much for your attention.
then can I take another question?
KSSV
KSSV 2020 年 9 月 3 日
編集済み: KSSV 2020 年 9 月 3 日
It is because, if-else condition is never executed and the intialized y is as it is. Thats why I said you need to think of your code. You should follow like below:
% BPSK
M = 2;
% number of bits or symbols
N = 10^4;
% Generates random bits
Bits_ale = randi([0 M-1],N,1)>0.5;
% BPSK modulation 0 -> -1; 1 -> 1
s = 2 * Bits_ale-1;
% Generates random watermark bits
Bit_wat = randi([0 M-1],N,1)>0.5;
y = zeros(N,1) ;
y(Bit_wat==1) = s(Bit_wat==1) * exp(1i * pi/4);
y(Bit_wat==0) = s(Bit_wat==0) * exp(1i * -pi/4);

サインインしてコメントする。


Tamir Suliman
Tamir Suliman 2020 年 9 月 3 日
i think yo have so many end after the if statement I modified the number of bits per just to speed it up
clc
% BPSK
M = 2;
% number of bits or symbols
% N = 10^4;
N = 10^1;
% Generates random bits
Bits_ale = randi([0 M-1],N,1)>0.5;
% BPSK modulation 0 -> -1; 1 -> 1
s = 2 * Bits_ale-1;
% Generates random watermark bits
Bit_wat = randi([0 M-1],N,1)>0.5;
for k=1:N
if (Bit_wat ==1)
y = s * exp(1i * pi/4);
else (Bit_wat ==0)
y = s * exp(1i *(- pi/4));
end
end
y
  2 件のコメント
adriane duarte
adriane duarte 2020 年 9 月 3 日
Thank you Tamir Suliman !!!
the "y" stored the loop values.
can i take another doubt?
Tamir Suliman
Tamir Suliman 2020 年 9 月 12 日
please accept the answer

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeBPSK についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by