{"id":160,"date":"2012-12-02T19:30:32","date_gmt":"2012-12-03T03:30:32","guid":{"rendered":"http:\/\/192.168.1.2\/wordpress\/?p=160"},"modified":"2013-05-07T17:44:10","modified_gmt":"2013-05-08T00:44:10","slug":"a-link-level-simulator-over-wireless-channels","status":"publish","type":"post","link":"http:\/\/cywang.no-ip.org\/wordpress\/?p=160","title":{"rendered":"A Link-level Simulator over Wireless Channels"},"content":{"rendered":"<p>\n\tIn this project, we create a link-level Monte-Carlo simulator. Then, we analyze the performance of convolutional codes over different wireless channels: additive white Gaussian noise(AWGN) and Rayleigh fading.\n<\/p>\n<p>\n\tWe use following blocks to implement our simulator: source generator, encoder\/decoder, inter-leaver\/deinterleaver, modulator\/demodulator, and wireless channel.\n<\/p>\n<p>\n\tDevelopment tool: Matlab 2012a with Communication toolbox\n<\/p>\n<p>\n\t<!--more-->\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p><center><\/p>\n<p>\n\t\t&nbsp;\n\t<\/p>\n<p>\n\t\t<a href=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/03\/matlab_model.jpg\"><img loading=\"lazy\" decoding=\"async\" alt=\"matlab_model\" class=\"alignnone size-full wp-image-162\" height=\"184\" src=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/03\/matlab_model.jpg\" width=\"562\" srcset=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/03\/matlab_model.jpg 562w, http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/03\/matlab_model-300x98.jpg 300w, http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/03\/matlab_model-500x163.jpg 500w\" sizes=\"auto, (max-width: 562px) 100vw, 562px\" \/><\/a>\n\t<\/p>\n<p>\n\t\t&nbsp;\n\t<\/p>\n<p><\/center><\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p><center><\/p>\n<p>\n\t\t&nbsp;\n\t<\/p>\n<p>\n\t\t<a href=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/03\/matlab_gui.jpg\"><img loading=\"lazy\" decoding=\"async\" alt=\"matlab_gui\" class=\"alignnone size-full wp-image-161\" height=\"508\" src=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/03\/matlab_gui.jpg\" width=\"786\" srcset=\"http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/03\/matlab_gui.jpg 786w, http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/03\/matlab_gui-300x193.jpg 300w, http:\/\/cywang.no-ip.org\/wordpress\/wp-content\/uploads\/2013\/03\/matlab_gui-464x300.jpg 464w\" sizes=\"auto, (max-width: 786px) 100vw, 786px\" \/><\/a>\n\t<\/p>\n<p>\n\t\t&nbsp;\n\t<\/p>\n<p><\/center><\/p>\n<p>\n\t&nbsp;\n<\/p>\n<pre class=\"brush:php;\">\r\n\r\n\r\nfunction ObjectActive(Btn_handles, Line_handles, status)\r\n&nbsp;&nbsp;&nbsp; if strcmp(status , &#39;on&#39;)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set(Btn_handles, &#39;Enable&#39;,&#39;on&#39;);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set(Line_handles, &#39;ForegroundColor&#39;,&#39;r&#39;);\r\n&nbsp;&nbsp;&nbsp; elseif strcmp(status , &#39;off&#39;)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set(Btn_handles, &#39;Enable&#39;,&#39;off&#39;);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set(Line_handles, &#39;ForegroundColor&#39;,&#39;k&#39;);\r\n&nbsp;&nbsp;&nbsp; end\r\n%--------------------------------------------------------------------------\r\n% Set the delay time of the objects in the Model Panel&nbsp;&nbsp;&nbsp; \r\nfunction ActionDelay(status, sec)\r\n&nbsp;&nbsp;&nbsp; if status == 1\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pause(sec);\r\n&nbsp;&nbsp;&nbsp; elseif status == 0\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pause(0);\r\n&nbsp;&nbsp;&nbsp; end\r\n%--------------------------------------------------------------------------\r\n%Start Simulation\r\nfunction Btn_Simulation_Callback(hObject, eventdata, handles)\r\n&nbsp;&nbsp;&nbsp; % -----------------Start Initialization-----------------\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Bernoulli, handles.Line1_1, &#39;off&#39;);\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Encoder, handles.Line1, &#39;off&#39;);\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Interleaver, handles.Line2, &#39;off&#39;);\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Modulator, handles.Line3, &#39;off&#39;);\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_AWGN, handles.Line4, &#39;off&#39;);\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Rayleigh, handles.Line4, &#39;off&#39;);\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Demodulator, handles.Line5, &#39;off&#39;)\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Deinterleaver, handles.Line6, &#39;off&#39;);\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Decoder, handles.Line7, &#39;off&#39;);\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_ErrorRateCalc, handles.Line8, &#39;off&#39;);\r\n\r\n&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; dDelayTime = 0;\r\n&nbsp;&nbsp;&nbsp; DelayChecked = get(handles.CheckBox_Delay, &#39;value&#39;);\r\n&nbsp;&nbsp;&nbsp; ActionDelay(DelayChecked, dDelayTime);\r\n&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; global gFrameSize_F;\r\n&nbsp;&nbsp;&nbsp; FrameNum_K = str2double(get(handles.Edit_FrameNum,&#39;string&#39;));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % Number of Frames (K)\r\n&nbsp;&nbsp;&nbsp; gFrameSize_F = str2double(get(handles.Edit_FrameSize,&#39;string&#39;));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % Frame Size (F): bits\r\n&nbsp;&nbsp;&nbsp; ShiftBit_k = str2double(get(handles.Edit_NumBitShit,&#39;string&#39;));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % Number of Bits Shifted (k): default k=1\r\n&nbsp;&nbsp;&nbsp; EncoderOutputBit_N = str2double(get(handles.Edit_NumEncoderOutputBit,&#39;string&#39;));&nbsp;&nbsp;&nbsp; % Number of Encoder Output Bits (N)\r\n&nbsp;&nbsp;&nbsp; CodeRate_Rc = ShiftBit_k \/ EncoderOutputBit_N;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % Code Rate (Rc=k\/N) \r\n&nbsp;&nbsp;&nbsp; global gTimesOfOutputBits;\r\n&nbsp;&nbsp;&nbsp; gTimesOfOutputBits = EncoderOutputBit_N \/ ShiftBit_k;\r\n&nbsp;&nbsp;&nbsp; set(handles.Edit_CodeRate,&#39;string&#39;, CodeRate_Rc);\r\n&nbsp;&nbsp;&nbsp; GeneratorPoly = str2num(get(handles.Edit_GeneratorPoly,&#39;string&#39;));\r\n&nbsp;&nbsp;&nbsp; MemoryNum_M = str2double(get(handles.Edit_NumMemory,&#39;string&#39;));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % Number of Memory Elements (M)\r\n&nbsp;&nbsp;&nbsp; MatrixSize = size(GeneratorPoly);\r\n&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; % Verify Trellis Structure\r\n&nbsp;&nbsp;&nbsp; if&nbsp; MatrixSize(1,2) ~= gTimesOfOutputBits\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set(handles.Edit_NumEncoderOutputBit, &#39;ForegroundColor&#39;,&#39;r&#39;);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set(handles.Edit_GeneratorPoly, &#39;ForegroundColor&#39;,&#39;r&#39;);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; error_msg = &#39;Check the values of &quot;Number of Encoder Output Bits&quot; and &quot;Generator Polynomials&quot;&#39;;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msgbox(error_msg,&#39;Error&#39;,&#39;error&#39;);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;\r\n&nbsp;&nbsp;&nbsp; else \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set(handles.Edit_NumEncoderOutputBit, &#39;ForegroundColor&#39;,&#39;k&#39;);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set(handles.Edit_GeneratorPoly, &#39;ForegroundColor&#39;,&#39;k&#39;);\r\n&nbsp;&nbsp;&nbsp; end\r\n&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; SNR_dB = str2double(get(handles.Edit_SNR_dB,&#39;string&#39;));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % AWGN Parameter: Signal to Noise Ratio (SNR)\r\n&nbsp;&nbsp;&nbsp; MobileSpeed_Low = str2double(get(handles.Edit_LowMobility,&#39;string&#39;));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % Rayleigh Parameter: Mobile Speeds - Low (v)\r\n&nbsp;&nbsp;&nbsp; MobileSpeed_High = str2double(get(handles.Edit_HighMobility,&#39;string&#39;));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % Rayleigh Parameter: Mobile Speeds - High (v)\r\n&nbsp;&nbsp;&nbsp; CarrierFrequency = str2double(get(handles.Edit_CarrierFrequency,&#39;string&#39;));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % Rayleigh Parameter: Carrier Frequency (fc)\r\n&nbsp;&nbsp;&nbsp; BER_Target = str2double(get(handles.Edit_BER_Target,&#39;string&#39;));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % Target BER\r\n&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; SelectedVitebiDecisionType = (get(handles.ListBox_VitebiDecisionType,&#39;Value&#39;));&nbsp;&nbsp;&nbsp;% Viterbi Convolutional Encoder Parameter: Decision Type\r\n&nbsp;&nbsp;&nbsp; if SelectedVitebiDecisionType == 1\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VitebiDecisionType = &#39;hard&#39;;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(&#39;Vitebi Decision Type: Hard Decision Decoding (HDD)\\n&#39;);\r\n&nbsp;&nbsp;&nbsp; elseif SelectedVitebiDecisionType == 2\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VitebiDecisionType = &#39;soft&#39;;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(&#39;Vitebi Decision Type: Soft Decision Decoding&nbsp; (SDD)\\n&#39;);\r\n&nbsp;&nbsp;&nbsp; end\r\n&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; SelectedWirelessChannelType = (get(handles.WirelessChannel_AWGN,&#39;Value&#39;));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % Selecting Wireless Channel Type\r\n&nbsp;&nbsp;&nbsp; if SelectedWirelessChannelType == 1\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WirelessChannelType = 1;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(&#39;Wireless Channel Type: AWGN\\n&#39;);\r\n&nbsp;&nbsp;&nbsp; elseif SelectedWirelessChannelType == 0\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WirelessChannelType = 2;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(&#39;Wireless Channel Type: Rayleigh Fading\\n&#39;);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SelectedRayleighSpectrumType = (get(handles.ListBox_SpectrumType,&#39;Value&#39;));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % Rayleigh Parameter: Doppler Spectrum Type\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if SelectedRayleighSpectrumType == 1\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RayleighSpectrumType = &#39;flat&#39;;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(&#39;Rayleigh Spectrum Type: Flat\\n&#39;);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif SelectedRayleighSpectrumType == 2\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RayleighSpectrumType = &#39;jakes&#39;;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(&#39;Rayleigh Spectrum Type: Jakes\\n&#39;);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end\r\n&nbsp;&nbsp;&nbsp; end\r\n&nbsp;&nbsp;&nbsp; \r\n% -----------------End Initialization-----------------\r\n\r\n% Generate data and apply fading channel.\r\nM = 2; % DBPSK modulation order\r\nhModulator = comm.BPSKModulator;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % Create a DPSK modulator\r\nhDemod = comm.BPSKDemodulator;&nbsp;&nbsp;&nbsp;&nbsp; % Create a DPSK demodulator\r\n&nbsp;&nbsp;&nbsp; \r\n\r\n&nbsp;&nbsp;&nbsp; \r\nBernoulli = randi([0 1],gFrameSize_F,1); \r\ndpskSig = step(hModulator, Bernoulli);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % DPSK modulate the signal\r\n\r\nchan = rayleighchan(1\/10000,100);\r\nfadedSig = filter(chan,dpskSig);&nbsp;&nbsp;&nbsp; % Apply the channel effects\r\n\r\n% Compute error rate for different values of SNR.\r\nSNR = 0:2:20; % Range of SNR values, in dB.\r\nnumSNR = length(SNR);\r\nberVec = zeros(3, numSNR);\r\n\r\n% Create an AWGNChannel and ErrorRate calculator System object\r\nhChan = comm.AWGNChannel(&#39;NoiseMethod&#39;, &#39;Signal to noise ratio (SNR)&#39;);\r\nhErrorCalc = comm.ErrorRate;\r\n\r\nfor n = 1:numSNR\r\n&nbsp;&nbsp;&nbsp; hChan.SNR = SNR(n);\r\n&nbsp;&nbsp; rxSig = step(hChan,fadedSig);&nbsp;&nbsp; % Add Gaussian noise\r\n&nbsp;&nbsp; DeModData = step(hDemod,rxSig);\r\n&nbsp;&nbsp; reset(hErrorCalc)\r\n&nbsp;&nbsp; % Compute error rate.\r\n&nbsp;&nbsp; berVec(:,n) = step(hErrorCalc,Bernoulli,DeModData);\r\nend\r\nBER = berVec(1,:);\r\n% Compute theoretical performance results, for comparison.\r\nBERtheory = berfading(SNR,&#39;dpsk&#39;,2,1);\r\nfigure\r\n% Plot BER results.\r\nsemilogy(SNR,BERtheory,&#39;b-&#39;,SNR,BER,&#39;r*&#39;);\r\nlegend(&#39;Theoretical BER&#39;,&#39;Empirical BER&#39;);\r\nxlabel(&#39;SNR (dB)&#39;); ylabel(&#39;BER&#39;);\r\ntitle(&#39;Binary DPSK over Rayleigh Fading Channel&#39;);&nbsp;&nbsp;&nbsp; \r\n\r\n\r\n% Step 1. Generating Bernoulli Source\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Bernoulli, handles.Line1_1, &#39;on&#39;);\r\n&nbsp;&nbsp;&nbsp; global gBernoulli\r\n&nbsp;&nbsp;&nbsp; gBernoulli = Bernoulli_Binary_Generator(gFrameSize_F);\r\n&nbsp;&nbsp;&nbsp; ActionDelay(DelayChecked, dDelayTime);\r\n&nbsp;&nbsp; \r\n% Step 2. Convolutional Encoder\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Encoder, handles.Line1, &#39;on&#39;);\r\n&nbsp;&nbsp;&nbsp; trel = poly2trellis(MemoryNum_M , GeneratorPoly);\r\n&nbsp;&nbsp;&nbsp; PolySize = size(GeneratorPoly);\r\n&nbsp;&nbsp;&nbsp; global gConvolutionalCode;\r\n&nbsp;&nbsp;&nbsp; gConvolutionalCode = ConvolutionalEncoder(gFrameSize_F,gBernoulli,EncoderOutputBit_N,ShiftBit_k,MemoryNum_M,trel);\r\n&nbsp;&nbsp;&nbsp; ActionDelay(DelayChecked, dDelayTime);\r\n&nbsp;&nbsp;&nbsp; % 1 -&gt; -1 and 0 -&gt; 1\r\n\r\n% Step 3. Block Interleaver\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Interleaver, handles.Line2, &#39;on&#39;);\r\n&nbsp;&nbsp;&nbsp; permVec = randperm(gFrameSize_F*gTimesOfOutputBits)&#39;;\r\n&nbsp;&nbsp;&nbsp; global gInverleaverData;\r\n&nbsp;&nbsp;&nbsp; gInverleaverData = Interleaver(&#39;BlockInterleaver&#39;, gConvolutionalCode&#39;, permVec);\r\n&nbsp;&nbsp;&nbsp; ActionDelay(DelayChecked, dDelayTime);\r\n&nbsp;&nbsp;&nbsp; \r\n% Step 4. BPSK Modulator\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Modulator, handles.Line3, &#39;on&#39;);\r\n&nbsp;&nbsp;&nbsp; global gModData;\r\n&nbsp;&nbsp;&nbsp; gModData = BPSK_Modulator(gInverleaverData);\r\n&nbsp;&nbsp;&nbsp; ActionDelay(DelayChecked, dDelayTime);\r\n&nbsp;&nbsp;&nbsp; \r\n% Step 5. Wireless Channel: AWGN and Rayleigh \r\n&nbsp;&nbsp;&nbsp; global gNoiseCode;\r\n&nbsp;&nbsp;&nbsp; gNoiseCode = WirelessChannel_Rayleigh(RayleighSpectrumType,CarrierFrequency,MobileSpeed_Low,gModData); % not finished yet\r\n&nbsp;&nbsp;&nbsp; \r\n% Step 6. BPSK Demodulator \r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Demodulator, handles.Line5, &#39;on&#39;);\r\n&nbsp;&nbsp;&nbsp; global DeModData;\r\n&nbsp;&nbsp;&nbsp; DeModData = BPSK_Demodulator(gNoiseCode);\r\n&nbsp;&nbsp;&nbsp; ActionDelay(DelayChecked, dDelayTime);\r\n&nbsp;&nbsp;&nbsp; \r\n% Step 7. Block Deinterleaver\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Deinterleaver, handles.Line6, &#39;on&#39;);\r\n&nbsp;&nbsp;&nbsp; global gDegInverleaverData;\r\n&nbsp;&nbsp;&nbsp; gDegInverleaverData = Deinterleaver(&#39;BlockInterleaver&#39;,DeModData,permVec);\r\n&nbsp;&nbsp;&nbsp; ActionDelay(DelayChecked, dDelayTime);\r\n&nbsp;&nbsp;&nbsp; \r\n% Step 8. Viterbi Decoder\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_Decoder, handles.Line7, &#39;on&#39;);\r\n&nbsp;&nbsp;&nbsp; global gDecoded;\r\n&nbsp;&nbsp;&nbsp; gDecoded = ViterbiDecoder(gDegInverleaverData, VitebiDecisionType, &#39;cont&#39;, 2,trel);&nbsp; % Issue: soft doesn&#39;t work\r\n&nbsp;&nbsp;&nbsp; ActionDelay(DelayChecked, dDelayTime);\r\n\r\n% Step 9. Error Rate Calc\r\n&nbsp;&nbsp;&nbsp; ObjectActive(handles.Btn_Model_ErrorRateCalc, handles.Line8, &#39;on&#39;);\r\n&nbsp;&nbsp;&nbsp; rate = ErrorRateCalc(gBernoulli,gDecoded)\r\n&nbsp;&nbsp;&nbsp; ActionDelay(DelayChecked, dDelayTime);\r\n&nbsp;&nbsp;&nbsp; global gresult_msg;\r\n&nbsp;&nbsp;&nbsp; fprintf(&#39;Error Rate: %d\\n&#39;, rate);\r\n\r\n%--------------------------------------------------------------------------&nbsp;&nbsp;&nbsp; \r\n% Step 1. Generating Bernoulli Source\r\n% Sequence: 0 and 1\r\nfunction Bernoulli = Bernoulli_Binary_Generator(frame_size)\r\n&nbsp;&nbsp;&nbsp; %global Bernoulli;\r\n&nbsp;&nbsp;&nbsp; Bernoulli = randi([0 1],1,frame_size); \r\n%--------------------------------------------------------------------------\r\n% Step 2. Convolutional Encoder\r\nfunction ConvolutionalCode = ConvolutionalEncoder(leng, source, Output_N, shift_k, Memory_Element_M, Trellis)\r\n&nbsp;&nbsp;&nbsp; hConvEnc = comm.ConvolutionalEncoder(Trellis);\r\n&nbsp;&nbsp;&nbsp; ConvolutionalCode = step(hConvEnc,source&#39;)&#39;; % Encode the message.\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \r\n%--------------------------------------------------------------------------&nbsp;&nbsp;&nbsp; \r\n% tblen is a positive integer scalar that specifies the traceback depth. \r\n% If the code rate is 1\/2, a typical value for tblen is about 5 times the constraint length of the code. \r\n% http:\/\/www.mathworks.com\/help\/comm\/ref\/vitdec.html\r\nfunction Decoded = ViterbiDecoder(NoiseSource, DecissionType, Opmode, TracebackDepth, Trellis) \r\n&nbsp;&nbsp;&nbsp; if strcmp(DecissionType , &#39;hard&#39;)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hVitDec = comm.ViterbiDecoder(Trellis, &#39;InputFormat&#39;, &#39;hard&#39;,&#39;TracebackDepth&#39;, TracebackDepth, &#39;TerminationMethod&#39;, &#39;Truncated&#39;);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Decoded = step(hVitDec, NoiseSource); % Decode.\r\n&nbsp;&nbsp;&nbsp; elseif strcmp(DecissionType , &#39;soft&#39;)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % To prepare for soft-decision decoding, map to decision values.\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [x,qcode] = quantiz(1-2*NoiseSource,[-.75 -.5 -.25 0 .25 .5 .75],[7 6 5 4 3 2 1 0]);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Decoded = vitdec(qcode,Trellis,TracebackDepth,Opmode,DecissionType, 3); % Decode.\r\n&nbsp;&nbsp;&nbsp; end\r\n%--------------------------------------------------------------------------\r\n% Step 5. Wireless Channel: AWGN\r\nfunction NoiseCode = WirelessChannel_AWGN(leng,source, SNR)\r\n&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; hAWGN = comm.AWGNChannel(&#39;NoiseMethod&#39;, &#39;Signal to noise ratio (SNR)&#39;,&#39;SNR&#39;,SNR);\r\n&nbsp;&nbsp;&nbsp; %hAWGN = comm.AWGNChannel(&#39;NoiseMethod&#39;, &#39;Signal to noise ratio (SNR)&#39;,&#39;SNR&#39;,SNR);\r\n&nbsp;&nbsp;&nbsp; NoiseCode = step(hAWGN, source);\r\n&nbsp;&nbsp;&nbsp; \r\n%--------------------------------------------------------------------------\r\n% Step 5. Wireless Channel: Rayleigh\r\nfunction RayleighData = WirelessChannel_Rayleigh(DopplerType, Frequency, Speed, NoiseSignal)\r\n&nbsp;&nbsp;&nbsp; % ts is the sample time of the input signal, in seconds. fd is the maximum Doppler shift, in hertz. \r\n&nbsp;&nbsp;&nbsp; fm = DopplerShift((Speed*1000)\/3600,Frequency*10^6);\r\n&nbsp;&nbsp;&nbsp; if strcmp(DopplerType,&#39;flat&#39;) == 1\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; channel = rayleighchan(1\/Frequency,fm); % Create object.\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; channel.DopplerSpectrum = doppler.flat;\r\n&nbsp;&nbsp;&nbsp; elseif strcmp(DopplerType,&#39;jakes&#39;) == 1\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; channel = rayleighchan(1\/Frequency,fm); % Create object.\r\n&nbsp;&nbsp;&nbsp; end\r\n&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; data=filter(channel,NoiseSignal);\r\n&nbsp;&nbsp;&nbsp; % http:\/\/www.mathworks.com\/matlabcentral\/answers\/13373-ber-calculation-bpsk-simulation-with-rayleigh-channel\r\n&nbsp;&nbsp;&nbsp; hAWGN = comm.AWGNChannel(&#39;NoiseMethod&#39;, &#39;Signal to noise ratio (SNR)&#39;,&#39;SNR&#39;,10);\r\n&nbsp;&nbsp;&nbsp; %hAWGN = comm.AWGNChannel(&#39;NoiseMethod&#39;,&#39;Signal to noise ratio (SNR)&#39;,&#39;SNR&#39;,SNR);\r\n\r\n&nbsp;&nbsp; % NoiseCode = step(hAWGN, source);\r\n&nbsp;&nbsp; RayleighData = step(hAWGN,data);&nbsp;&nbsp; % Add Gaussian noise\r\n&nbsp;&nbsp; %RayleighData = step(hDemod, rxSig);&nbsp; % Demodulate\r\n&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; \r\n%--------------------------------------------------------------------------&nbsp; \r\nfunction intData = Interleaver(InterleaverType, SourceData, PermutationVector)\r\n&nbsp;&nbsp;&nbsp; if strcmp(InterleaverType,&#39;BlockInterleaver&#39;)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hInt = comm.BlockInterleaver(PermutationVector);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; intData = step(hInt, SourceData);\r\n&nbsp;&nbsp;&nbsp; end\r\n%--------------------------------------------------------------------------&nbsp;&nbsp;&nbsp; \r\nfunction deIntData = Deinterleaver(InterleaverType, SourceData, PermutationVector)\r\n&nbsp;&nbsp;&nbsp; if strcmp(InterleaverType,&#39;BlockInterleaver&#39;)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hDeInt = comm.BlockDeinterleaver(PermutationVector);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; deIntData = step(hDeInt, SourceData);\r\n&nbsp;&nbsp;&nbsp; end&nbsp;&nbsp;&nbsp; \r\n%--------------------------------------------------------------------------&nbsp;&nbsp;&nbsp; \r\nfunction gModData = BPSK_Modulator(Data)\r\n&nbsp;&nbsp;&nbsp; % Create a BPSK modulator System object\r\n&nbsp;&nbsp;&nbsp; hModulator = comm.BPSKModulator;\r\n&nbsp;&nbsp;&nbsp; %constellation(hModulator);\r\n&nbsp;&nbsp;&nbsp; % Modulate the data\r\n&nbsp;&nbsp;&nbsp; gModData = step(hModulator, Data);\r\n%--------------------------------------------------------------------------\r\nfunction DeModData = BPSK_Demodulator(noisySignal)\r\n&nbsp;&nbsp;&nbsp; hDemod = comm.BPSKDemodulator;\r\n&nbsp;&nbsp;&nbsp; DeModData = step(hDemod,noisySignal);\r\n\r\n%--------------------------------------------------------------------------&nbsp; \r\nfunction fm = DopplerShift(v,fc)\r\n&nbsp;&nbsp;fm = (v\/(3*10^8))*fc; %Hz\r\n%--------------------------------------------------------------------------&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \r\nfunction rate = ErrorRateCalc(SourceCode, DecodedCode)\r\n&nbsp;&nbsp;hErrorCalc = comm.ErrorRate;\r\n&nbsp;&nbsp;reset(hErrorCalc)\r\n&nbsp;&nbsp;[number,rate] = biterr(DecodedCode&#39;,SourceCode);\r\n&nbsp;\r\n%--------------------------------------------------------------------------&nbsp;&nbsp;&nbsp; \r\nfunction&nbsp; DrawSquareWave(title_name,x_axis_label, y_axis_label, bit_array, min_x, max_x, min_y, max_y)\r\n&nbsp;&nbsp;&nbsp; figure\r\n&nbsp;&nbsp;&nbsp; t= 0:max_x;\r\n&nbsp;&nbsp;&nbsp; bit_array(max_x+1) = bit_array(max_x); % draw the last bit\r\n&nbsp;&nbsp;&nbsp; stairs(t, bit_array,&#39;LineWidth&#39;,2);\r\n&nbsp;&nbsp;&nbsp; axis([min_x max_x min_y max_y])\r\n&nbsp;&nbsp;&nbsp; %grid on\r\n&nbsp;&nbsp;&nbsp; xlabel(x_axis_label);\r\n&nbsp;&nbsp;&nbsp; ylabel(y_axis_label);\r\n&nbsp;&nbsp;&nbsp; title(title_name);\r\n%--------------------------------------------------------------------------<\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this project, we create a link-level Monte-Carlo simulator. Then, we analyze the performance of convolutional codes over different wireless channels: additive white Gaussian noise(AWGN) and Rayleigh fading. We use following blocks to implement our simulator: source generator, encoder\/decoder, inter-leaver\/deinterleaver, &hellip; <a href=\"http:\/\/cywang.no-ip.org\/wordpress\/?p=160\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[14],"tags":[],"class_list":["post-160","post","type-post","status-publish","format-standard","hentry","category-projects"],"_links":{"self":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=160"}],"version-history":[{"count":11,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":422,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions\/422"}],"wp:attachment":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}