通訊
matlab
問題 : duration設定為4 symbol periods, 畫出在
Image Not Showing
Possible Reasons
|
Image Not Showing
Possible Reasons
|
定義信噪比
而範例程式這邊與我上述直覺理解不同點是在維持
問題 : 解釋程式Data_bit = (rand(1, data_number) > 0.5 )
為何可產生random bits?bit 0 和bit 1,其發生機率理論上應為何?實際於模擬是否符合理論預測?
指令rand(M,N)
代表返回一個logic
。
由於是均勻分布,0到0.5的區間機率是
問題 :
data_number = 10^5; % # of bits
Fs = 10; % sampling frequency (used to generate received samples)
Data_bit = (rand(1,data_number) > 0.5 ); % random bits
p1 = ones(1,Fs); % discrete‐time rectangular pulse that represents one symbol
Data_pulse_array = (p1') * Data_bit;
Data_pulse = reshape(Data_pulse_array,1,length(p1)*data_number);
其中Data_pulse = reshape(Data_pulse_array, 1, length(p1)*data_number)
代表意義。
Data_bit
為由機率各半(Data_pulse_array
為將Data_bit
的值拷貝成double
,最後reshape
將(1,length(p1)*data_number)
的矩陣,代表意義是每10個單位傳送一個symbol。
問題 : 沒有雜訊下,matched filter output
Data_receive = Data_pulse; % received samples
D_filtered = conv(Data_receive,p1); % MF output
D_demapping = D_filtered(10:10:end) / 10; % sampling at symbol rate
首先計算兩方波做convolution,如下圖所示
1101
,matched filter output 問題 : 解釋D_demapping=D_filtered(10:10:end)/10
。
同lab3問題,取matched filter在
問題 : 解釋sgma = sqrt( 0.5/EbN0/2*10 )
。
如lab1觀念,定義信噪比
SNR可改寫為
問題 : 畫出OOK中BER對SNR理論值與實際值做圖。
在機率equally likely
D_demap_N = (D_demapping > 0.5); % >0.5: 1; <=0.5: 0
% BER computation
Error_num = sum(xor(D_demap_N,Data_bit)); % same -> 0; diff -> 1
BER = Error_num/data_number;
繪圖如下
問題 : 在
index_0 = 1;
for i = 1:data_number
if Data_bit(i) == 0 % TX == 0
D_demapping_0(index_0) = D_demapping(i);
index_0 = index_0 + 1;
end
end
如果一開始傳送訊號是0
,提取輸出訊號
histogram(D_demapping_0, 'FaceColor', '#0072BD','Normalization','pdf');
下指令histogram(X,'Normalization','pdf')
畫各個數值區間內的統計數量,後面那2個參數是取數據X
的probability density function。
plot(xaxis,normpdf(xaxis,0,sgma / sqrt(10)), 'LineWidth',3);
理論值計算下指令normpdf
,代表使用常態(高斯)分布
分別就不同SNR
Image Not Showing
Possible Reasons
|
Image Not Showing
Possible Reasons
|
降低SNR,在維持
% matched filter
% ‐ on‐off keying (OOK) using rectangular pulse (T=1)
% That is, when OFF, assuming A=0, thus E1 = 0
% when ON: assuming A=1, thus E2 = 1
% The "average bit energy" (Eb) = (E1+E2)/2 = 1/2
%
clear,clc,close all;
%% parameters
data_number = 4; % # of bits
Fs = 10; % sampling frequency (used to generate received samples)
%% transmitter
Data_bit = (rand(1,data_number) > 0.5 ); % random bits
p1 = ones(1,Fs); % discrete‐time rectangular pulse that represents one symbol
Data_pulse_array = (p1')*Data_bit;
Data_pulse = reshape(Data_pulse_array,1,length(p1)*data_number);
%% AWGN channel and receiver
% AWGN channel
EbN0dB = 10; % 3 10
[a, b] = size(Data_pulse);
EbN0 = 10^(EbN0dB/10); % EbN0 is now in linear scale
sgma = sqrt( 0.5/EbN0/2*10);
noise = normrnd(0, sgma, a, b);
Data_receive = Data_pulse + noise; % received samplesa
%% generate plots
figure;
xais = 1:40;
hold on;
plot(xais, Data_pulse, 'LineWidth',2);
plot(xais, Data_receive, '--r', 'LineWidth',2);
ylim([-5 5]);
xlim([1 40]);
xlabel('sample index');
ylabel('sample value');
legend('TX waveform', 'RX waveform');
grid on;
title('waveform of OOK with Rectangular Pulse ($\frac{R_b}{N_0} = 10 \;\rm dB$)','Interpreter','latex','FontSize',15);
clear,clc,close all;
pro_1 = zeros(1, 20); % preallocate memory
for k = 1:10^4
sum_1 = 0;
data_number = k; % # of bits
Data_bit = (rand(1,data_number) > 0.5 ); % random bits
for i = 1:data_number
if Data_bit(i) == 1
sum_1 = sum_1 + 1;
end
end
pro_1(k) = sum_1 ./ data_number;
end
xais = 1:10^4;
plot(xais, pro_1);
ylim([0, 1]);
xlabel('data number');
ylabel('probibilty of bit 1');
grid on;
title('probibilty of bit 1 with data number increasing','FontSize',15);
% matched filter
% ‐ on‐off keying (OOK) using rectangular pulse (T=1)
% That is, when OFF, assuming A=0, thus E1 = 0
% when ON: assuming A=1, thus E2 = 1
% The "average bit energy" (Eb) = (E1+E2)/2 = 1/2
%
clear,clc,close all;
%% parameters
data_number = 4; % # of bits
EbN0dB_vec = [0:2:10 11.5]; % Eb/N0 in dB
Fs = 10; % sampling frequency (used to generate received samples)
%% transmitter
Data_bit = (rand(1,data_number) > 0.5 ); % random bits
p1 = ones(1,Fs); % discrete‐time rectangular pulse that represents one symbol
Data_pulse_array = (p1')*Data_bit;
Data_pulse = reshape(Data_pulse_array,1,length(p1)*data_number);
%% no AWGN channel and receiver
Data_receive = Data_pulse; % received samples
% receiver
D_filtered = conv(Data_receive,p1); % MF output
D_demapping = D_filtered(10:10:end) / 10; % sampling at symbol rate
% decsion based on D_demapping
D_demap_N = (D_demapping > 0.5); % >0.5: 1; <=0.5: 0
%% generate plots
figure;
xaxis = 1:40;
hold on;
plot(xaxis, Data_pulse, 'LineWidth',2);
plot(xaxis, Data_receive, '--r', 'LineWidth',2);
for i = 1:4
scatter(10*i, D_demapping(i),'b', 'LineWidth',3);
end
ylim([-5 5]);
xlim([1 40]);
xlabel('sample index');
ylabel('sample value');
legend('TX waveform', 'RX waveform', 'matched filter output at t = T','FontSize',15);
grid on;
title('matched filter output at $t = T$ with no noise channel','Interpreter','latex','FontSize',15);
hold off;
% xaxis_49 = 1:49;
% plot(xaxis_49, D_filtered, 'LineWidth',2);
% xlabel('sample index');
% ylabel('sample value');
% title('matched filter output $y(t)$','Interpreter','latex','FontSize',15);
% matched filter
% ‐ on‐off keying (OOK) using rectangular pulse (T=1)
% That is, when OFF, assuming A=0, thus E1 = 0
% when ON: assuming A=1, thus E2 = 1
% The "average bit energy" (Eb) = (E1+E2)/2 = 1/2
%
clear,clc,close all;
%% parameters
data_number = 10^5; % # of bits
EbN0dB_vec = [0:2:10 11.5]; % Eb/N0 in dB
Fs = 10; % sampling frequency (used to generate received samples)
%% transmitter
Data_bit = (rand(1,data_number) > 0.5 ); % random bits
p1 = ones(1,Fs); % discrete‐time rectangular pulse that represents one symbol
Data_pulse_array = (p1')*Data_bit;
Data_pulse = reshape(Data_pulse_array,1,length(p1)*data_number);
%% AWGN channel and receiver
BER = zeros(1, length(EbN0dB_vec)); % pre-allocate
BER_theory = zeros(1, length(EbN0dB_vec)); % pre-allocate
for kk=1:length(EbN0dB_vec)
% AWGN channel
[a, b] = size(Data_pulse);
EbN0dB = EbN0dB_vec(kk);
EbN0 = 10^(EbN0dB/10); % EbN0 is now in linear scale
sgma = sqrt( 0.5/EbN0/2*10);
noise = normrnd(0, sgma, a, b);
Data_receive = Data_pulse + noise; % received samples
% receiver
D_filtered = conv(Data_receive,p1); % MF output
D_demapping = D_filtered(10:10:end) / 10; % sampling at symbol rate
% decsion based on D_demapping
D_demap_N = (D_demapping > 0.5); % >0.5: 1; <=0.5: 0
% BER computation
Error_num = sum(xor(D_demap_N,Data_bit)); % same -> 0; diff -> 1
BER(kk) = Error_num/data_number;
% fprintf('EbN0 in dB is %g\n',EbN0dB);
% fprintf('Bit error rate is %g\n',BER);
BER_theory(kk) = qfunc(sqrt(EbN0));
end
%% generate plots
figure;
semilogy(EbN0dB_vec, BER, EbN0dB_vec, BER_theory, 'LineWidth',2);
xlabel('Eb/N0 (dB), where Eb: average energy per bit');
ylabel('Bit Error Rate');
legend('OOK (Simulation)', 'OOK (Theory)','FontSize',10);
grid on;
title('OOK with Rectangular Pulse','FontSize',15);
% matched filter
% ‐ on‐off keying (OOK) using rectangular pulse (T=1)
% That is, when OFF, assuming A=0, thus E1 = 0
% when ON: assuming A=1, thus E2 = 1
% The "average bit energy" (Eb) = (E1+E2)/2 = 1/2
%
clear,clc,close all;
%% parameters
data_number = 10^5; % # of bits
EbN0dB_vec = [0:2:10 11.5]; % Eb/N0 in dB
Fs = 10; % sampling frequency (used to generate received samples)
%% transmitter
Data_bit = (rand(1,data_number) > 0.5 ); % random bits
p1 = ones(1,Fs); % discrete‐time rectangular pulse that represents one symbol
Data_pulse_array = (p1')*Data_bit;
Data_pulse = reshape(Data_pulse_array,1,length(p1)*data_number);
%% AWGN channel and receiver
% AWGN channel
[a, b] = size(Data_pulse);
% EbN0dB = 3;
EbN0dB = 10;
EbN0 = 10^(EbN0dB/10); % EbN0 is now in linear scale
sgma = sqrt(0.5/EbN0/2*10);
noise = normrnd(0, sgma, a, b);
Data_receive = Data_pulse + noise; % received samples
% receiver
D_filtered = conv(Data_receive,p1); % MF output
D_demapping = D_filtered(10:10:end) / 10; % sampling at symbol rate
% decsion based on D_demapping
D_demap_N = (D_demapping > 0.5); % >0.5: 1; <=0.5: 0
%% split two parts
index_0 = 1;
for i = 1:data_number
if Data_bit(i) == 0 % TX == 0
D_demapping_0(index_0) = D_demapping(i);
index_0 = index_0 + 1;
end
end
index_1 = 1;
for i = 1:data_number
if Data_bit(i) == 1
D_demapping_1(index_1) = D_demapping(i);
index_1 = index_1 + 1;
end
end
%% generate plots
figure;
hold on;
xaxis = -2.5:0.01:2.5;
histogram(D_demapping_0, 'FaceColor', '#0072BD','Normalization','pdf');
histogram(D_demapping_1, 'FaceColor', '#D95319','Normalization','pdf');
plot(xaxis,normpdf(xaxis,0,sgma / sqrt(10)), 'LineWidth',3); %need to scaling 10 with line 30
plot(xaxis,normpdf(xaxis,1,sgma / sqrt(10)),'r', 'LineWidth',3); %need to scaling 10 with line 30
xlabel('y(T)');
ylabel('probability density');
legend('bit 0 (simulation)', 'bit 1 (simulation)', 'bit 0 (theory)','bit 1 (theory)','FontSize',13);
grid on;
title('conditional PDF of (y(T) | bit 0 sent) and f(y(T) | bit 1 sent)','FontSize',15);