-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw2z_link.m
120 lines (101 loc) · 4.8 KB
/
w2z_link.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
function [rxBits, rxSyms, txSyms] = w2z_link(txBits, SNR, isChannelIdea)
% This function simulates WiFi-to-ZigBee transmission of WEBee.
persistent freqDiff;
persistent OSR;
persistent FFTLength;
persistent numSampPerWiFiSym;
if isempty(freqDiff)
subcrWidth = 0.3125; % Bandwith of a WiFi subcarrier (MHz).
numShiftSubc = 0; % 16; % # of shift subcarriers. Set to zero for now.
freqDiff = (subcrWidth * numShiftSubc) / 20; % MHz
end
if isempty(OSR)
OSR = 20;
end
if isempty(FFTLength)
FFTLength = 64;
end
%% WiFi sender link
% Waveform configuration
cfgNonHT = wlanNonHTConfig;
cfgNonHT.Modulation = 'OFDM';
cfgNonHT.ChannelBandwidth = 'CBW20'; % Channel bandwidth
cfgNonHT.NumTransmitAntennas = 1; % Number of transmit antennas
cfgNonHT.MCS = 7; % Modulation and coding scheme
cfgNonHT.PSDULength = 3885;
cfgNonHT.BandwidthOperation = 'Static';
%% Channel
% Channel settings
Channel = wlanTGnChannel;
Channel.DelayProfile = 'Model-B';
Channel.CarrierFrequency = 2.44e9;
Channel.NumTransmitAntennas = cfgNonHT.NumTransmitAntennas;
Channel.NumReceiveAntennas = 1;
Channel.TransmitReceiveDistance = 5;
Channel.LargeScaleFadingEffect = 'None'; % 'Pathloss'?
fsWiFi = wlanSampleRate(cfgNonHT);
Channel.SampleRate = fsWiFi;
% Idea ZigBee signals that WiFi aimts to generate.
[objZigBeeSignal, txSyms, numPkt] = zigbee_txer(txBits, OSR, true);
% Shift the ideal ZigBee signal in the frequency domain so that it can be processed
% from WiFi's perspective.
freqShiftOptZ2W = repmat(exp(1i * 2 * pi * -freqDiff * (0:size(objZigBeeSignal, 1) - 1))', 1, size(objZigBeeSignal, 2));
objZigBeeSignal = objZigBeeSignal .* freqShiftOptZ2W;
% Generate waveform of WiFi packet payload.
txSignal = zeros(size(objZigBeeSignal, 1) - OSR / 2, size(objZigBeeSignal, 2));
field = 'NonHT-Data';
if isempty(numSampPerWiFiSym)
numSampPerWiFiSym = 80;
end
numWiFisym = ceil(size(txSignal, 1) ./ numSampPerWiFiSym);
for ithPkt = 1:1:size(objZigBeeSignal, 2)
focusObjZigBeeSignal = reshape(objZigBeeSignal(1:1:end - (OSR / 2), ithPkt), numSampPerWiFiSym, numWiFisym);
focusObjZigBeeSignal(1:1:16, :) = []; % Remove the CP part.
rxSignalWiFi = fftshift(fft(focusObjZigBeeSignal, [], 1), 1);
info = wlanNonHTOFDMInfo(field, cfgNonHT);
% Demodulation based on MATLAB WLAN toolbox.
rxSignalWiFi = rxSignalWiFi(info.ActiveFFTIndices, :);
rxSignalWiFi = rxSignalWiFi .* sqrt(FFTLength);
rxSignalWiFi = rxSignalWiFi(info.DataIndices, :);
rxSignalWiFi(1:1:(size(info.DataIndices, 1) / 2 - 5), :) = 0;
rxSignalWiFi((end - size(info.DataIndices, 1) / 2 + 6):1:end, :) = 0;
% WEBee part.
[txBitsW, scramInit] = webeeBitGenerator(rxSignalWiFi, cfgNonHT);
% Send out emulate signals.
cfgNonHT.PSDULength = length(txBitsW) / 8; % Payload length in bytes
txSignal(:, ithPkt) = wlanNonHTData(txBitsW, cfgNonHT, scramInit);
end
% Normalization so that the power of each signal is 1
for pkt_ith = 1:1:size(txSignal, 2)
tmp = txSignal(:, pkt_ith);
txSignal(:, pkt_ith) = tmp / sqrt(sum(abs(tmp.^2), 1)/size(tmp, 1));
end
if isChannelIdea == true
rxSignal = txSignal;
else
% % Create an instance of the AWGN channel per SNR point simulated
% awgnChannel = comm.AWGNChannel;
% awgnChannel.NoiseMethod = 'Signal to noise ratio (SNR)';
% awgnChannel.SignalPower = 1 / Channel.NumReceiveAntennas;
% % Account for noise energy in nulls so the SNR is defined per
% % active subcarrier
% awgnChannel.SNR = SNR - 10 * log10(FFTLength);
% reset(Channel); % Reset channel for different realization
% txSignal = [txSignal; zeros(50, cfgNonHT.NumTransmitAntennas)];
% rxSignal = Channel(txSignal);
% rxSignal = awgnChannel(rxSignal);
% rxSignal = rxSignal(1:end-50, 1);
% rxSignal = awgn(txSignal(1:end-50, 1), SNR, 'measured');
rxSignal = awgn(txSignal, SNR, 'measured');
end
% Shift emulated ZigBee signal in the frequency domain so that it can be processed
% from ZigBee's perspective.
freqShiftOptW2Z = repmat(exp(1i * 2 * pi * freqDiff * (0:size(rxSignal, 1) - 1))', 1, size(rxSignal, 2));
rxSignal = rxSignal .* freqShiftOptW2Z;
rxSignal = lowpass(rxSignal, 1.1e6, fsWiFi, 'Steepness', 0.99);
% ZigBee receiver link
rxSignal = rxSignal(1:5:end, :);
[rxBits, rxSyms] = zigbee_rxer(rxSignal, OSR/5, true, numPkt);
rxBits = reshape(rxBits, 1, size(rxBits, 1) * size(rxBits, 2));
rxSyms = reshape(rxSyms, 1, size(rxSyms, 1) * size(rxSyms, 2));
txSyms = reshape(txSyms, 1, size(txSyms, 1) * size(txSyms, 2));