⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commsdemo.html

📁 如果大家觉得这个对大家有用的话就请下了看看吧
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><!--
This HTML is auto-generated from an M-file.
To make changes, update the M-file or the XSL and republish this document.
--><title>The MathWorks - Demos - The MathWorks - Demos - Modeling a Communications System Using the Communications Toolbox</title>


<meta name="generator" content="MATLAB 7.0">
<meta name="date" content="2004-03-12">
<meta name="m-file" content="Sim_Machines_Demo">
<script type="text/javascript">
			
// Determine if it is IE and Version
msieIndex = navigator.appVersion.indexOf("MSIE") + 5;
index = navigator.appVersion.substr(msieIndex,3);
index = parseFloat (index);
if ((navigator.appName == "Microsoft Internet Explorer") && (index < 6 ) && navigator.platform == "Win32" ) {
    document.write("<link rel=\"stylesheet\" href=\"/includes/css/ie4_5.css\" type=\"text/css\">");
    brows = "IE 5.5<";
    //alert ("you are IE 5.5 and DOWN on windows, you have the body td bug, and everyone in the world has you. You get Percentages!" + index);
} else if ((navigator.appName == "Microsoft Internet Explorer") && (index >= 6 ) && navigator.platform == "Win32" ) {
    document.write("<link rel=\"stylesheet\" href=\"/includes/css/ie6_up.css\" type=\"text/css\">");
    brows = "IE 6>";
    // alert ("you are IE 6 and UP on windows, you are in Standards Mode DTD and everyone in the world has you. You get Percentages with EM's!" + index);
} else if (document.layers) {
    document.write("<link rel=\"stylesheet\" href=\"/includes/css/nn_4.css\" type=\"text/css\">");
    brows = "NN4<";
    //alert ("you are Netscape 4 - you get points!" + index);
} else {
    document.write("<link rel=\"stylesheet\" href=\"/includes/css/dom.css\" type=\"text/css\">");
    brows = "DOM";
    //alert ("you are DOM or Mac, or Netscape 4 or up or some other thing I dont know - you get pixels!" + index);
} // end if
document.write("<link rel=\"stylesheet\" href=\"/includes/css/site4.css\" type=\"text/css\">");
</script><link rel="stylesheet" href="commsdemo_files/dom.css" type="text/css"><link rel="stylesheet" href="commsdemo_files/site4.css" type="text/css">
<style>
body {width: 760px; }
span.keyword {color: blue}
span.comment {color: green}
span.string {color: #B20000}
span.untermstring {color: purple}
span.syscmd {color: orange}
    </style>
<script language="JavaScript">
function openNewWindow(newContent) {
  popupWin = window.open(newContent,
    'DemoWindow2','width=850,height=680,scrollbars=yes,resizable=yes')
}
</script></head>

<body>
<table id="mainframe" summary="layout" border="0" cellpadding="0" cellspacing="0" width="100%">
	<tbody><tr>
		<td id="bodycol"><div id="mainbody">
			<a href="http://www.mathworks.com/"><img src="commsdemo_files/bannerLogo.jpg" alt="" align="left" border="0" height="55" width="195"></a>
			<div align="right">
			<a href="javascript:void(0)" onclick="self.close();return false">Close Window</a>
								</div>
								<br clear="all">

<div xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" class="demo_content">
   <h1>Modeling a Communications System Using the Communications Toolbox</h1>
   <introduction>
      </introduction><p>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. </p>
      <p>At the end of this demo, we provide a full list of links to the functions we use during this demo.</p>
   
   <h2>Contents</h2>
   <div>
      <ul>
         <li><a href="#1">Generating a Random Binary Message</a></li>
         <li><a href="#3">Encoding Bits with a Convolutional Code</a></li>
         <li><a href="#4">Generating a Bit-to-Symbol Mapping Using a Gray Code</a></li>
         <li><a href="#7">Modulating Using 16-QAM</a></li>
         <li><a href="#9">Pulse Shaping Signals Using an FIR RRC</a></li>
         <li><a href="#13">Adding AWG Noise to the Transmitted Signal</a></li>
         <li><a href="#15">Filtering Received Signals Using the RRC Filter</a></li>
         <ol><li><a href="#16">Recovering the Signal</a></li>
             <li><a href="#18">Demodulating the Received Signal</a></li>
             <li><a href="#19">Recovering Bits from Symbols Using Gray Coding</a></li>
             <li><a href="#20">Decoding the Convolutional Code</a></li></ol>
         <li><a href="#21">Calculating BERs with and Without Convolutional Code</a></li>
         <li><a href="#22">Functions Used in this Demo</a></li>
         <li><a href="#23">Additional Information</a></li>
      </ul>
   </div>
   <h2>Generating a Random Binary Message<a name="1"></a></h2>
   <p>Our first task in this communication system is to generate a random binary message to transmit.  We do this by using the <tt>randint</tt> 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:
   </p><pre class="codeinput">M = 16;                     <span class="comment">% Size of signal constellation</span>
k = log2(M);                <span class="comment">% Number of bits per symbol</span>
n = 1e4;                    <span class="comment">% Number of total symbols to simulate</span>

<span class="comment">% Create a binary signal as a column vector</span>
x = randint(n*k,1);         <span class="comment">% Random binary signal</span>

<span class="comment">% Display the first 60 random bits</span>
stem(x(1:60),<span class="string">'filled'</span>);
title(<span class="string">'Random Bits'</span>);
xlabel(<span class="string">'Number of Bits'</span>);
ylabel(<span class="string">'Binary Value'</span>);
set(gca,<span class="string">'yTick'</span>,[0 0.5 1],<span class="string">'XLim'</span>,[-2 62],<span class="string">'YLim'</span>,[-0.2 1.2],<span class="string">'Box'</span>,<span class="string">'on'</span>);
set(gcf,<span class="string">'Color'</span>,<span class="string">'w'</span>)
</pre><img src="commsdemo_files/commsdemo_01.png" hspace="5" vspace="5"> <p><b>Figure 1:</b> The first 60 random bits.
   </p>
   <h2>Encoding Bits with a Convolutional Code<a name="3"></a></h2>
   <p>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: </p>
   <p><img src="commsdemo_files/commsdemo_eq113383.png" hspace="5" vspace="5"> </p>
   <p><b>Figure 2:</b> The polynomial representation of our convolutional code.
   </p>
   <p>The <tt>convenc</tt> function in the Communications Toolbox uses the trellis representation of a convolutional code to encode a sequence. We use
      the <tt>poly2trellis</tt> 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 <tt>poly2trellis</tt> function for more information.
   </p><pre class="codeinput"><span class="comment">% Declare variables</span>
codeRate = 1/2;                             <span class="comment">% Code Rate</span>
constlen = 7;                               <span class="comment">% Constraint Length</span>
codegen = [171 133];                        <span class="comment">% Generator Polynomials</span>
trellis = poly2trellis(constlen, codegen);  <span class="comment">% Create Trellis structure</span>

<span class="comment">% Encode transmitted sequence</span>
xEnc = convenc(x, trellis);
</pre><h2>Generating a Bit-to-Symbol Mapping Using a Gray Code<a name="4"></a></h2>
   <p>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 <tt>scatterplot</tt> function in the Communications Toolbox shows the resulting constellation:
   </p><pre class="codeinput"><span class="comment">% Generate Gray code mapping</span>
<span class="comment">% Start from a PSK constellation with sqrt(M) points</span>
grayPsk = bitxor(0:sqrt(M)-1,floor((0:sqrt(M)-1)/2))';
<span class="comment">% Use it to create a QAM constellation with M points</span>
mapping = repmat(grayPsk,1,sqrt(M))+repmat(sqrt(M)*grayPsk',sqrt(M),1);
mapping = mapping(:);

<span class="comment">% Display Bit-to-Symbol mapping</span>
t = qammod(mapping,M);
scatterplot(t);
set(get(gca,<span class="string">'Children'</span>),<span class="string">'Marker'</span>,<span class="string">'d'</span>,<span class="string">'MarkerFaceColor'</span>,<span class="string">'auto'</span>);
hold <span class="string">on</span>;

<span class="comment">% Add labels showing the binary sequence at each constellation point</span>
<span class="keyword">for</span> jj=1:length(t)
    text(real(t(jj))-0.5,imag(t(jj))+0.5,dec2base(jj-1,2,4));
<span class="keyword">end</span>
set(gca,<span class="string">'yTick'</span>,(-(k+1):2:k+1),<span class="string">'xTick'</span>,(-(k+1):2:k+1),<span class="keyword">...</span>
    <span class="string">'XLim'</span>,[-(k+1) k+1],<span class="string">'YLim'</span>,[-(k+1) k+1],<span class="string">'Box'</span>,<span class="string">'on'</span>,<span class="keyword">...</span>
    <span class="string">'YGrid'</span>,<span class="string">'on'</span>, <span class="string">'XGrid'</span>,<span class="string">'on'</span>);
xlabel (<span class="string">'In-Phase'</span>);
hold <span class="string">off</span>;
set(gcf,<span class="string">'Color'</span>,<span class="string">'w'</span>)
</pre><img src="commsdemo_files/commsdemo_02.png" hspace="5" vspace="5"> <p><b>Figure 3:</b>  A scatter plot of our Gray-coded QAM modulation.
   </p>
   <p>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:
   </p><pre class="codeinput"><span class="comment">% Convert the sequence from a column vector to a 4-column matrix</span>
xSym = reshape(xEnc,k,n/codeRate).';

<span class="comment">% Convert each row of the matrix from binary to decimal</span>
xSym = bi2de(xSym, <span class="string">'left-msb'</span>);

<span class="comment">% Permute the transmitted sequence using Gray code mapping</span>
xSym = mapping(xSym+1);

<span class="comment">% Display the first 35 transmitted symbols</span>
figure; stem(xSym(1:35));
title(<span class="string">'Random Symbols'</span>);
xlabel(<span class="string">'Number of Symbols'</span>);
ylabel(<span class="string">'Integer Value {0..15}'</span>);
set(gca,<span class="string">'yTick'</span>,(0:M-1),<span class="string">'XLim'</span>,[-2 35+2],<span class="string">'YLim'</span>,[-0.2 M-1+.2],<span class="keyword">...</span>
    <span class="string">'Box'</span>,<span class="string">'on'</span>,<span class="string">'YGrid'</span>,<span class="string">'on'</span>);
set(gcf,<span class="string">'Color'</span>,<span class="string">'w'</span>)
</pre><img src="commsdemo_files/commsdemo_03.png" hspace="5" vspace="5"> <p><b>Figure 4:</b> The first 35 random symbols.
   </p>
   <h2>Modulating Using 16-QAM<a name="7"></a></h2>
   <p>Now we have convolutionally coded the sequence of bits and mapped them to the decimal numbers 1-16 using a Gray code.  Next,
      we use the <tt>qammod</tt> function to convert the decimal numbers 1-16 to points in the two-dimensional space that is used to transmit the code.  We'll
      also display an eyediagram with the <tt>eyediagram</tt> function in the Communications Toolbox.
   </p><pre class="codeinput"><span class="comment">% Modulate the signal</span>
xMod = qammod(xSym,M);

<span class="comment">% Display eyediagram plot of modulated symbols</span>
eyediagram(xMod(1:100),2);
subplot (2,1,2)
xlabel (<span class="string">'Time'</span>);
set (gcf, <span class="string">'Color'</span>, <span class="string">'w'</span>)
</pre><img src="commsdemo_files/commsdemo_04.png" hspace="5" vspace="5"> <p><b>Figure 5:</b> Eyediagram of our QAM modulation.
   </p>
   <h2>Pulse Shaping Signals Using an FIR RRC<a name="9"></a></h2>
   <p>We have modulated the sequence, and now we design an FIR root-raised cosine filter to reduce intersymbol interference (ISI).
       This is a common filter in communications systems.  We'll follow a two step process: first, we'll create the filter, and
      then we'll use it to filter the signal.  Let's use the Communications Toolbox function <tt>rcosine</tt> to create the filter.
   </p><pre class="codeinput"><span class="comment">% Define filter variables</span>
filtOrder = 40; overSamp = 4;
delay = filtOrder/(overSamp*2);
rollOff = .25;

<span class="comment">% Create filter</span>
rrcFilter = rcosine(1,overSamp,<span class="string">'fir/sqrt'</span>,rollOff,delay);
</pre><p>Next, let's use the Signal Processing Toolbox function <tt>fvtool</tt> to show the filter's impulse response.  We can also use <tt>fvtool</tt> to show the magnitude response, phase response, group delay, and other characteristics of a filter.
   </p><pre class="codeinput"><span class="comment">% Display impulse response of Transmit filter</span>
hFV = fvtool(rrcFilter,1,<span class="string">'Analysis'</span>,<span class="string">'Impulse'</span>);
xlabel (<span class="string">'Samples'</span>);
set (gcf, <span class="string">'Color'</span>, <span class="string">'w'</span>)
</pre><img src="commsdemo_files/commsdemo_05.png" hspace="5" vspace="5"> <p><b>Figure 6:</b> Impulse response of RRC filter.
   </p>
   <p>After creating the root-raised cosine filter, our next step is to filter the incoming data stream.  To further reduce the
      risk of ISI, we upsample the sequence using the Signal Processing Toolbox function <tt>upsample</tt>. Then we filter it with the RRC using the MATLAB <tt>filter</tt> function.  The resulting plots show the first 120 bits of the sequence before being filtered and after being filtered.  The
      real and imaginary parts are plotted separately.
   </p><pre class="codeinput"><span class="comment">% Upsample signal</span>
yModUp = upsample(xMod,overSamp);
<span class="comment">% Pad signal with zeros to flush filter</span>
yModUp = [yModUp; zeros(filtOrder,1)];
<span class="comment">% Filter upsampled signal</span>
yTx = filter(rrcFilter,1,yModUp);

<span class="comment">% Plot the signal before and after filter</span>
figure;
subplot(2,1,1); <span class="comment">% Real part</span>
stem(real(yModUp(1:120))); hold <span class="string">on</span>;
plot(real(yTx(1+delay*overSamp:80+delay*overSamp)),<span class="string">'r-'</span>);
xlabel(<span class="string">'Samples'</span>); ylabel(<span class="string">'Amplitude'</span>);
title (<span class="string">'Transmitted signal after Tx Filter - Real'</span>);
legend (<span class="string">'Digital signal before RRC'</span>, <span class="string">'Analog signal after RRC'</span>)
subplot(2,1,2); <span class="comment">% Imaginary part</span>
stem(imag(yModUp(1:120))); hold <span class="string">on</span>;
plot(imag(yTx(1+delay*overSamp:80+delay*overSamp)),<span class="string">'r-'</span>);
xlabel(<span class="string">'Samples'</span>);
ylabel(<span class="string">'Amplitude'</span>);
title (<span class="string">'Transmitted signal after Tx Filter - Imag'</span>);
legend (<span class="string">'Digital signal before RRC'</span>, <span class="string">'Analog signal after RRC'</span>)
hold <span class="string">off</span>;
set (gcf, <span class="string">'Color'</span>, <span class="string">'w'</span>)
</pre><img src="commsdemo_files/commsdemo_06.png" hspace="5" vspace="5"> <p><b>Figure 7:</b> Comparison of signal before and after RRC.
   </p>
   <h2>Adding AWG Noise to the Transmitted Signal<a name="13"></a></h2>
   <p>We have encoded and filtered the signal, and it is ready to be transmitted.  The Communications Toolbox includes several kinds
      of channels, including Rician channels, Rayleigh channels, and AWGN channels.  In this example, we use an AWGN channel to
      add white Gaussian noise to the signal.  We'll also use the <tt>scatterplot</tt> function to plot the received and transmitted signals for comparison:
   </p><pre class="codeinput"><span class="comment">% SNR per coded bit</span>
EbNo = 10;

<span class="comment">% SNR per uncoded bit</span>
EsNo = EbNo+10*log10(k)-10*log10(overSamp)-10*log10(1/codeRate);

<span class="comment">% Add the noise</span>
yNoisy = awgn(yTx,EsNo,<span class="string">'measured'</span>);

<span class="comment">% Plot received signal vs. transmitted signal</span>
h=scatterplot(yNoisy(1:overSamp:end).*sqrt(overSamp),1,0,<span class="string">'b.'</span>);
hold <span class="string">on</span>;
scatterplot(xMod,1,0,<span class="string">'rx'</span>,h);
set(get(get(h,<span class="string">'children'</span>),<span class="string">'children'</span>),<span class="string">'MarkerSize'</span>,10,<span class="string">'LineWidth'</span>,4);
title(<span class="string">'Received vs. Transmitted Signal (Downsampled for visualization)'</span>);
axis([-(k+1) k+1 -(k+1) k+1]);
xlabel (<span class="string">'In-Phase'</span>);
hold <span class="string">off</span>;
set (gcf, <span class="string">'Color'</span>, <span class="string">'w'</span>)
</pre><img src="commsdemo_files/commsdemo_07.png" hspace="5" vspace="5"> <p><b>Figure 8:</b> Symbols before and after noise.
   </p>
   <h2>Filtering Received Signals Using the RRC Filter<a name="15"></a></h2>
   <p>The signal has now been transmitted over the channel and it needs to be recovered.  The steps to recover the original signal
      are as follows:
   </p>
   <div>
      <ol>
         <li>Recover the signal from the RRC.</li>
         <li>Demodulate the signal.</li>
         <li>Undo the Gray code.</li>
         <li>Decode the convolutional code.</li>
      </ol>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -