📄 commsdemo.html
字号:
</div>
<h2>Step 1: Recovering the Signal<a name="16"></a></h2>
<p>The
first step is to recover the signal from the RRC. One of the advantages
of an RRC is that we can use the same filter to recover the signal that
we used to encode it. After decoding, we downsample the signal, because
it was upsampled before it was filtered. After the data is filtered and
downsampled, we plot the transmitted signal and the received signals: </p><pre class="codeinput"><span class="comment">% Filter received signal using Tx filter</span>
yRx = filter(rrcFilter,1,yNoisy);
<span class="comment">% Downsample received signal</span>
yRxDown = downsample(yRx,overSamp);
<span class="comment">% Compensate for filter delay</span>
yRxDown = yRxDown(filtOrder/overSamp+1:end);
<span class="comment">% Plot the received signal, the received analog signal after the RRC, and</span>
<span class="comment">% the resulting digital signal</span>
figure;
subplot(2,1,1);
plot(real(yNoisy(delay*overSamp+1:delay*overSamp+80)),<span class="string">'ko-'</span>);
hold <span class="string">on</span>;
plot(real(yRx(2*delay*overSamp+1:2*delay*overSamp+80)),<span class="string">'bx-'</span>);
stem(upsample(real(yRxDown(1:80/overSamp)),overSamp),<span class="string">'r-'</span>);
grid <span class="string">on</span>;
axis([0 80 -4 4]);
legend(<span class="string">'Received signal'</span>,<span class="string">'Received filtered signal'</span>,<span class="string">'Rx sampling point'</span>);
<span class="comment">% For comparison, replot the transmitted signal</span>
subplot(2,1,2);
plot(real(yTx(1+delay*overSamp:80+delay*overSamp)),<span class="string">'rd-.'</span>);
hold <span class="string">on</span>;
stem(real(yModUp(1:80)),<span class="string">'b'</span>);
xlabel(<span class="string">'Samples'</span>);
ylabel(<span class="string">'Amplitude'</span>);
legend(<span class="string">'Transmitted filtered signal'</span>,<span class="string">'Original Tx signal'</span>);
axis([0 80 -4 4]);
grid <span class="string">on</span>;
set(gcf,<span class="string">'Color'</span>,<span class="string">'w'</span>)
</pre><img src="commsdemo_files/commsdemo_08.png" hspace="5" vspace="5"> <p><b>Figure 9:</b> Comparison of signal before and after RRC.
</p>
<h2>Step 2: Demodulating the Received Signal<a name="18"></a></h2>
<p>After filtering the signal with the RRC, we'll demodulate the signal using the <tt>qamdemod</tt> function in the Communications Toolbox.
</p><pre class="codeinput">ySym = qamdemod(yRxDown,M);
</pre><h2>Step 3: Recovering Bits from Symbols Using Gray Coding<a name="19"></a></h2>
<p>Next, let's demap the received data using the Gray mapping. Then we can compare the sequence to the original sequence after
it was encoded with the convolutional encoder. We use the <tt>biterr</tt> function to do this comparison:
</p><pre class="codeinput">[dummy demapping] = sort(mapping);
demapping = demapping - 1;
ySym = demapping(ySym+1);
<span class="comment">% Convert integers to bits</span>
yBits = de2bi(ySym,<span class="string">'left-msb'</span>);
yBits = reshape(yBits.',numel(yBits),1);
</pre><h2>Step 4: Decoding the Convolutional Code<a name="20"></a></h2>
<p>The last step in the system is to decode the code using a Viterbi decoder. To do this, we use the <tt>vitdec</tt> function in the Communications Toolbox. The <tt>vitdec</tt>
function uses the same trellis for encoding and decoding the sequence.
This function can specify both the initial state of a trellis and its
final state. It can also decode using hard-decision or soft-decision
decoding. Our demo uses hard-decision decoding: </p><pre class="codeinput"><span class="comment">% Define variables</span>
tblen = 32; <span class="comment">% Traceback length</span>
<span class="comment">% Decode received signal assuming an all-zero initial state</span>
y = vitdec(yBits, trellis, tblen, <span class="string">'cont'</span>,<span class="string">'hard'</span>);
</pre><h2>Calculating BERs with and Without Convolutional Code<a name="21"></a></h2>
<p>In this section, we calculate two BERs for the sequence: one for when the convolutional code is used and one for when it is
not. We'll use the <tt>biterr</tt> function in the Communications Toolbox for the calculations. Finally, we use the <tt>berawgn</tt> function to calculate the theoretical error for a non-coded AWGN channel with the specified parameters:
</p><pre class="codeinput"><span class="comment">% Compute BER without convolutional coding</span>
[numErrors_Sym_no_code, bitError_Sym_no_code] = biterr(xEnc,yBits)
<span class="comment">% Compute BER with convolutional coding</span>
[numErrors_with_code,bitError_with_code] = biterr(x(1:end-tblen),y(tblen+1:end))
<span class="comment">% Compute theoretical BER for an AWGN channel with parameters of this</span>
<span class="comment">% channel</span>
ber_theory = berawgn (EsNo, <span class="string">'qam'</span>, M)
</pre><pre class="codeoutput">numErrors_Sym_no_code =
1374
bitError_Sym_no_code =
0.0172
numErrors_with_code =
0
bitError_with_code =
0
ber_theory =
0.0168
</pre><h2>Functions Used in this Demo<a name="22"></a></h2>
<p>For more information about any of the functions used in this demo, click on the following links. All functions are in the
Communications Toolbox unless otherwise noted.
</p>
<div>
<ul>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/randint.shtml"><tt>randint</tt></a> - Creates a sequence of random bits.
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/poly2trellis.shtml"><tt>poly2trellis</tt></a> - Converts a convolutional code generator polynomial to a trellis.
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/convenc.shtml"><tt>convenc</tt></a> - Encodes a sequence of bits using a convolutional code with a given trellis.
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/scatterplot.shtml"><tt>scatterplot</tt></a> - Generates a scatter plot to show a signal constellation.
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/qammod.html"><tt>qammod</tt></a> - Modulates a sequence using QAM.
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/eyediagram.html"><tt>eyediagram</tt></a> - Displays an eyediagram of a modulated sequence.
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/rcosine.html"><tt>rcosine</tt></a> - Designs a raised cosine filter.
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/signal/fvtool.shtml"><tt>fvtool</tt></a> - Graphically shows a filter's impulse response, magnitude response, and several other representations. (Signal Processing
Toolbox)
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/signal/upsample.html"><tt>upsample</tt></a> - Upsamples a sequence by inserting zeros. (Signal Processing Toolbox)
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/filter.shtml"><tt>filter</tt></a> - Filters a sequence using a given FIR or IIR filter. (Included in MATLAB)
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/awgn.html"><tt>awgn</tt></a> - Transmits data over an AWGN channel.
</li>
<li>See this <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/a1069451372b1.shtml">overview</a> of channel features in the toolbox.
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/signal/downsample.html"><tt>downsample</tt></a> - Downsamples a sequence. (Signal Processing Toolbox)
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/qamdemod.html"><tt>qamdemod</tt></a> - Demodulates a sequence that was modulated using QAM.
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/vitdec.html"><tt>vitdec</tt></a> - Decodes a sequence using a Viterbi decoder.
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/biterr.html"><tt>biterr</tt></a> - Calculates the bit error rate of a sequence.
</li>
<li><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/berawgn.html"><tt>berawgn</tt></a> - Calculates the uncoded theoretical bit error rate over an AWGN channel without coding.
</li>
</ul>
</div>
<h2>Additional Information<a name="23"></a></h2>
<p>For more information on using the Communications Toolbox, see:</p>
<p><a href="http://www.mathworks.com/products/communications/demos.html">Additional Communications Toolbox Demos</a></p>
<p><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/comm/comm_product_page.shtml">Communications Toolbox Documentation</a></p>
<p>From <a href="http://www.mathworks.com/products/communications/index.html">the main Communications Toolbox page</a> you can download a free 30-day trial, read the documentation and user stories, request more information, and get pricing
information.
</p>
<!--
##### SOURCE BEGIN #####
%% Modeling a Communications System Using the Communications Toolbox
% In this demo, we use the Communications Toolbox to build a simple
% communications system. We'll start by formulating a random sequence of
% bits, encode those bits using a convolutional code, and then use a
% Gray-coded 16-QAM modulation scheme before pulse shaping using a raised
% cosine filter. Then we'll introduce noise using an AWGN channel. Next,
% we recover the signal and calculate the bit error rate.
%
% At the end of this demo, we provide a full list of links to the functions
% we use during this demo.
%
% Copyright 2005 The MathWorks, Inc.
% $Revision: 1.0.0 $ $Date: 2005/01/14 15:26:15 PM $
%% Generating a Random Binary Message
% Our first task in this communication system is to generate a random
% binary message to transmit. We do this by using the |randint| function
% in the Communications Toolbox. The bits will be modulated using 16-QAM,
% so each symbol will be 4 bits long. We use the following code to create
% the sequence and display the first 60 symbols:
M = 16; % Size of signal constellation
k = log2(M); % Number of bits per symbol
n = 1e4; % Number of total symbols to simulate
% Create a binary signal as a column vector
x = randint(n*k,1); % Random binary signal
% Display the first 60 random bits
stem(x(1:60),'filled');
title('Random Bits');
xlabel('Number of Bits');
ylabel('Binary Value');
set(gca,'yTick',[0 0.5 1],'XLim',[-2 62],'YLim',[-0.2 1.2],'Box','on');
set(gcf,'Color','w')
%%
% *Figure 1:* The first 60 random bits.
%% Encoding Bits with a Convolutional Code
% Now that we have created the random bits, our next step is to encode
% them. The Communications Toolbox includes a variety of coding schemes,
% including BCH codes, Reed-Solomon codes, cyclic codes, and convolutional
% codes. This demo uses an industry-standard rate 1/2 convolutional coder.
% This diagram illustrates the matrix representation of our code:
%
% $$
% {1 + D + D^2 + D^3 + D^4 + D^6\brack
% 1 + D^2 + D^3 + D^5 + D^6}$$
%
% *Figure 2:* The polynomial representation of our convolutional code.
%
% The |convenc| function in the Communications Toolbox uses the trellis
% representation of a convolutional code to encode a sequence. We use the
% |poly2trellis| function to convert from the polynomial representation of
% a code to the trellis representation. In the next code example, we use
% these commands to encode the bit sequence convolutionally. Note that the
% codegen function is in octal format. See the documentation on the
% |poly2trellis| function for more information.
% Declare variables
codeRate = 1/2; % Code Rate
constlen = 7; % Constraint Length
codegen = [171 133]; % Generator Polynomials
trellis = poly2trellis(constlen, codegen); % Create Trellis structure
% Encode transmitted sequence
xEnc = convenc(x, trellis);
%% Generating a Bit-to-Symbol Mapping Using a Gray Code
% After channel coding, we use a Gray code as the source code. Recall that
% one property of a Gray code is that any two adjacent signals in a
% constellation have a Hamming distance of 1. The first step in Gray
% coding is generating a map that satisfies the above condition. The
% |scatterplot| function in the Communications Toolbox shows the resulting
% constellation:
% Generate Gray code mapping
% Start from a PSK constellation with sqrt(M) points
grayPsk = bitxor(0:sqrt(M)-1,floor((0:sqrt(M)-1)/2))';
% Use it to create a QAM constellation with M points
mapping = repmat(grayPsk,1,sqrt(M))+repmat(sqrt(M)*grayPsk',sqrt(M),1);
mapping = mapping(:);
% Display Bit-to-Symbol mapping
t = qammod(mapping,M);
scatterplot(t);
set(get(gca,'Children'),'Marker','d','MarkerFaceColor','auto');
hold on;
% Add labels showing the binary sequence at each constellation point
for jj=1:length(t)
text(real(t(jj))-0.5,imag(t(jj))+0.5,dec2base(jj-1,2,4));
end
set(gca,'yTick',(-(k+1):2:k+1),'xTick',(-(k+1):2:k+1),...
'XLim',[-(k+1) k+1],'YLim',[-(k+1) k+1],'Box','on',...
'YGrid','on', 'XGrid','on');
xlabel ('In-Phase');
hold off;
set(gcf,'Color','w')
%%
% *Figure 3:* A scatter plot of our Gray-coded QAM modulation.
%
%
% We've generated the Gray code mapping. Next, we separate the sequence of
% bits into 4-bit symbols, convert each one to decimal format, and map it
% the appropriate Gray-coded sequence:
% Convert the sequence from a column vector to a 4-column matrix
xSym = reshape(xEnc,k,n/codeRate).';
% Convert each row of the matrix from binary to decimal
xSym = bi2de(xSym, 'left-msb');
% Permute the transmitted sequence using Gray code mapping
xSym = mapping(xSym+1);
% Display the first 35 transmitted symbols
figure; stem(xSym(1:35));
title('Random Symbols');
xlabel('Number of Symbols');
ylabel('Integer Value {0..15}');
set(gca,'yTick',(0:M-1),'XLim',[-2 35+2],'YLim',[-0.2 M-1+.2],...
'Box','on','YGrid','on');
set(gcf,'Color','w')
%%
% *Figure 4:* The first 35 random symbols.
%% Modulating Using 16-QAM
% Now we have convolutionally coded the sequence of bits and mapped them to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -