-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwireless-comms
133 lines (115 loc) · 5.15 KB
/
wireless-comms
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
121
122
123
124
125
126
127
128
129
130
131
132
133
%Simulate_basic_digital_communications_link
%implementing essential components of a single-carrier digital communication
%system
%y-morris 06-10-24
srcBits=randi([0,1],20000,1) %randi func to create col vector srcBits ...
%containing 20000 randomly generated bits/
modOrder=16 %create var with value = 16
modOut=qammod(srcBits,modOrder,"InputType","bit") %create modulated QAM signal
scatterplot(modOut) % creating constellation of modOut
chanOut = modOut %create variable chanOut
demodOut=qamdemod(chanOut,modOrder,"OutputType","bit") %demodulate the received signal
check=isequal(srcBits,demodOut) %check if input and output vectors are identical
modOut=qammod(srcBits,modOrder,"InputType","bit","UnitAveragePower",true)
%Created 16-QAM signal from the bit sequence srcBits ^^^^^
SNR=15;chanOut=awgn(modOut,SNR) %apply AWGN to modulated signal
scatterplot(chanOut)
demodOut=qamdemod(chanOut,modOrder,"OutputType","bit","UnitAveragePower",true)
%demodulated the signal and specififed that signal has unit avg power
check = isequal(srcBits,demodOut)
%checked if srcBits and demodOut were identitical
isBitError = srcBits~=demodOut %Compare the source bits and received bits...
%element-by-element to identify bit errors.
numBitErrors = nnz(isBitError) %
BER=numBitErrors/numBits %
txFilt=comm.RaisedCosineTransmitFilter
rxFilt=comm.RaisedCosineReceiveFilter
txFiltOut=txFilt(modOut)
chanOut=awgn(txFiltOut,7,"measured")
rxFiltOut=rxFilt(chanOut) %Apply the receive filter tochannel output.
if exist("rxFiltOut","var") % code runs after you complete Task 4
scatterplot(rxFiltOut)
title("Receive Filter Output")
demodOut = qamdemod(rxFiltOut,modOrder,"OutputType","bit","UnitAveragePower",true);
end
specAn = dsp.SpectrumAnalyzer(...
"NumInputPorts",2, ...
"SpectralAverages",50,...
"ShowLegend",true);
specAn(txFiltOut,chanOut)
bitsPerSymbol = log2(modOrder) % modOrder = 2^bitsPerSymbol
delayInSymbols = txFilt.FilterSpanInSymbols/2 + rxFilt.FilterSpanInSymbols/2
delayInBits = delayInSymbols*bitsPerSymbol
srcAligned = srcBits(1:(end-delayInBits))
demodAligned = demodOut((delayInBits+1):end)
BER = numBitErrors/length(srcAligned)
specAn = dsp.SpectrumAnalyzer("NumInputPorts",2, ...
"SpectralAverages",50,...
"ShowLegend",true);
specAn(txFiltOut,chanOut)
mpChan = [0.8 0 0 0 0 0 0 0 -0.5 0 0 0 0 0 0 0 0.34].'
%mpChan = [0.8; zeros(7,1); -0.5; zeros(7,1); 0.34]; % multipath channel
stem(mpChan) %stem plot for visualisation
mpChanOut = filter(mpChan, 1, txFiltOut)
if exist("mpChanOut","var") % code runs after you complete Task 3
SNR = 15; % dB
chanOut = awgn(mpChanOut,SNR,"measured");
rxFiltOut = rxFilt(chanOut);
scatterplot(rxFiltOut)
title("Receive Filter Output")
demodOut = qamdemod(rxFiltOut,modOrder,"OutputType","bit","UnitAveragePower",true);
% Calculate the BER
delayInSymbols = txFilt.FilterSpanInSymbols/2 + rxFilt.FilterSpanInSymbols/2;
delayInBits = delayInSymbols * bitsPerSymbol;
srcAligned = srcBits(1:(end-delayInBits));
demodAligned = demodOut((delayInBits+1):end);
numBitErrors = nnz(srcAligned~=demodAligned)
BER = numBitErrors/length(srcAligned)
end
%OFDM basic link - Inverse Fast Fourier Transform (FFT)
ofdmModOut=ifft(modOut) % compute IFFT of 16-QAM signal
if exist("ofdmModOut","var") % code runs after you complete Task 1
SNR = 15; % dB
chanOut = awgn(ofdmModOut,SNR,"measured");
end
ofdmDemodOut=fft(chanOut) %compute the FFT of received signal
scatterplot(ofdmDemodOut) %visualisation of oddmDemodOut OFDM demod output
title("OFDM Demodulator Output");
qamDemodOut = qamdemod(ofdmDemodOut,modOrder,"OutputType","bit","UnitAveragePower",true);
numBitErrors = nnz(srcBits~=qamDemodOut)
BER = numBitErrors/numBits
% Apply channel to transmitter output.
function chanOut = channel(sig,hasMP,hasAWGN,SNR)
% Apply multipath channel if selected
if hasMP
mpChan = [0.8; zeros(7,1); -0.5; zeros(7,1); 0.34];
mpChanOut = filter(mpChan,1,sig);
else
mpChanOut = sig;
end
% Apply AWGN if selected
if hasAWGN
chanOut = awgn(mpChanOut,SNR,"measured");
else
chanOut = mpChanOut;
end
end
%Freq domain equalization
numCarr=8192 %number of subcarriers
numBits=numCarr*bitsPerSymbol %calculate number of source bits to generate
if exist("numBits","var") % code r
srcBits = randi([0,1],numBits,1);
modOut = qammod(srcBits,modOrder,"InputType","bit","UnitAveragePower",true);
end
cycPrefLen=32 %define cyclic prefix length to 32
ofdmModOut = ofdmmod(modOut, numCarr, cycPrefLen)
if exist("ofdmModOut","var")
mpChanOut = filter(mpChan,1,ofdmModOut);
chanOut = awgn(mpChanOut,SNR,"measured");
end
ofdmDemodOut = ofdmdemod(chanOut, numCarr, cycPrefLen)
scatterplot(ofdmDemodOut) %perform OFDM demodulation and create constellation
mpChanFreq = fftshift(fft(mpChan,numCarr))
eqOut=ofdmDemodOut./mpChanFreq %Equalize by dividing signal by channel
% frequency response
scatterplot(eqOut)