Main Content

comm.BarkerCode

Generate bipolar Barker code

Description

The comm.BarkerCode System object™ generates a bipolar Barker code. Barker codes have low autocorrelation properties. The short length and low correlation sidelobes make Barker codes useful for frame synchronization in digital communications systems. For more information, see Barker Codes.

To generate a Barker code:

  1. Create the comm.BarkerCode object and set its properties.

  2. Call the object, as if it were a function.

To learn more about how System objects work, see What Are System Objects?.

Creation

Description

barkerCode = comm.BarkerCode creates a bipolar Barker code generator System object to generate a Barker code.

barkerCode = comm.BarkerCode(Name,Value) sets properties using one or more name-value pairs. For example, comm.BarkerCode('Length',11,'SamplesPerFrame','11') configures a bipolar Barker code generator System object to output a length 11 Barker code in an 11-sample frame. Enclose each property name in quotes.

example

Properties

expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and the release function unlocks them.

If a property is tunable, you can change its value at any time.

For more information on changing property values, see System Design in MATLAB Using System Objects.

Length of the generated code, specified as 1, 2, 3, 4, 5, 7, 11, or 13. For more information, see Barker Codes.

Example: 'Length',2 outputs the Barker code [–1;1].

Data Types: double

Samples per output frame, specified as a positive integer. If SamplesPerFrame is M, the object outputs a frame containing M samples comprised of length N Barker code sequences. If necessary, the object repeats the code sequence to reach M samples. N is the length of the generated code, which is set by the Length property.

Data Types: double

Output data type, specified as double or int8.

Data Types: char | string

Usage

For versions earlier than R2016b, use the step function to run the System object algorithm. The arguments to step are the object you created, followed by the arguments shown in this section.

For example, y = step(obj,x) and y = obj(x) perform equivalent operations.

Description

y = barkerCode outputs a Barker code frame, as a column vector. If the frame length exceeds the Barker code length, the object fills the frame by repeating the Barker code.

Set the data type of the output with the OutputDataType property.

example

Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named obj, use this syntax:

release(obj)

expand all

cloneCreate duplicate System object
isLockedDetermine if System object is in use
stepRun System object algorithm
releaseRelease resources and allow changes to System object property values and input characteristics
resetReset internal states of System object

Examples

collapse all

Create a Barker code System object with 10 samples per frame.

  barker = comm.BarkerCode('SamplesPerFrame',10)
barker = 
  comm.BarkerCode with properties:

             Length: 7
    SamplesPerFrame: 10
     OutputDataType: 'double'

Generate multiple frames by using the default Barker code sequence of length 7. The code wraps within the frame and continues in the next frame.

  for ii = 1:2
      seq = barker()
  end
seq = 10×1

    -1
    -1
    -1
     1
     1
    -1
     1
    -1
    -1
    -1

seq = 10×1

     1
     1
    -1
     1
    -1
    -1
    -1
     1
     1
    -1

Compute the peak sidelobe level for each Barker code.

CodeLength = [1 2 3 4 5 7 11 13]';
psl = zeros(length(CodeLength),1);
barker = comm.BarkerCode;
for ii=1:length(CodeLength)
    spf = CodeLength(ii);
    barker.Length = CodeLength(ii);
    barker.SamplesPerFrame = spf;
    seq = barker();
    sll_dB = 20*log10(abs(xcorr(seq)));
    psl(ii) = -(max(sll_dB));
    release(barker);
end
Sidelobe_dB = psl;
T = table(CodeLength,Sidelobe_dB)
T=8×2 table
    CodeLength    Sidelobe_dB
    __________    ___________

         1                0  
         2          -6.0206  
         3          -9.5424  
         4          -12.041  
         5          -13.979  
         7          -16.902  
        11          -20.828  
        13          -22.279  

Compensation of significant phase and frequency offsets for a 16-QAM signal in an AWGN channel is accomplished in two steps. First, correct the coarse frequency offset using the estimate provided by the coarse frequency compensator, and then fine-tune the correction using carrier synchronization. Because of the coarse frequency correction, the carrier synchronizer converges quickly even though the normalized bandwidth is set to a low value. Lower normalized bandwidth values enable better correction for small residual carrier offsets. After applying phase and frequency offset corrections to the received signal, resolve phase ambiguity using the preambles.

Define the simulation parameters.

fs = 10000;                  % Sample rate (Hz)
sps = 4;                     % Samples per symbol
M = 16;                      % Modulation order
k = log2(M);                 % Bits per symbol
rng(1996)                    % Set seed for repeatable results
barker = comm.BarkerCode(... % For preamble
    'Length',13,'SamplesPerFrame',13);
msgLen = 1e4;
numFrames = 10;
frameLen = msgLen/numFrames;

Generate data payloads and add the preamble to each frame. The preamble is later used for phase ambiguity resolution.

preamble = (1+barker())/2;  % Length 13, unipolar
data = zeros(msgLen, 1);
for idx = 1 : numFrames
    payload = randi([0 M-1],frameLen-barker.Length,1);
    data((idx-1)*frameLen + (1:frameLen)) = [preamble; payload];
end

Create a System object™ for the transmit pulse shape filtering, the receive pulse shape filtering, the QAM coarse frequency compensation, the carrier synchronization, and a constellation diagram.

txFilter = comm.RaisedCosineTransmitFilter( ...
    'OutputSamplesPerSymbol',sps);
rxFilter = comm.RaisedCosineReceiveFilter(...
    'InputSamplesPerSymbol',sps, ...
    'DecimationFactor',sps);
coarse = comm.CoarseFrequencyCompensator( ...
    'SampleRate',fs, ...
    'FrequencyResolution',10);
fine = comm.CarrierSynchronizer( ...
    'DampingFactor',0.4, ...
    'NormalizedLoopBandwidth',0.001, ...
    'SamplesPerSymbol',1, ...
    'Modulation','QAM');
axislimits = [-6 6];
constDiagram = comm.ConstellationDiagram( ...
    'ReferenceConstellation',qammod(0:M-1,M), ...
    'ChannelNames',{'Before convergence','After convergence'}, ...
    'ShowLegend',true, ...
    'XLimits',axislimits, ...
    'YLimits',axislimits);

Also create a System object for the AWGN channel, and the phase and frequency offset to add impairments to the signal. A phase offset greater than 90 degrees is added to induce a phase ambiguity that results in a constellation quadrant shift.

ebn0 = 8;
freqoffset = 110;
phaseoffset = 110;
awgnChannel = comm.AWGNChannel( ...
    'EbNo',ebn0, ...
    'BitsPerSymbol',k, ...
    'SamplesPerSymbol',sps);
pfo = comm.PhaseFrequencyOffset( ...
    'FrequencyOffset',freqoffset, ...
    'PhaseOffset',phaseoffset, ...
    'SampleRate',fs);

Generate random data symbols, apply 16-QAM modulation, and pass the modulated signal through the transmit pulse shaping filter.

txMod = qammod(data,M);
txSig = txFilter(txMod);

Apply phase and frequency offsets using the pfo System object, and then pass the signal through an AWGN channel to add white Gaussian noise.

txSigOffset = pfo(txSig);
rxSig = awgnChannel(txSigOffset);

The coarse frequency compensator System object provides a rough correction for the frequency offset. For the conditions in this example, correcting the frequency offset of the received signal correction to within 10 Hz of the transmitted signal is sufficient.

syncCoarse = coarse(rxSig);

Pass the signal through the receive pulse shaping filter, and apply fine frequency correction.

rxFiltSig = fine(rxFilter(syncCoarse));

Display the constellation diagram of the first and last 1000 symbols in the signal. Before convergence of the synchronization loop, the spiral nature of the diagram indicates that the frequency offset is not corrected. After the carrier synchronizer has converged to a solution, the symbols are aligned with the reference constellation.

constDiagram([rxFiltSig(1:1000) rxFiltSig(9001:end)])

Demodulate the signal. Account for the signal delay caused by the transmit and receive filters to align the received data with the transmitted data. Compute and display the total bit errors and BER. When checking the bit errors, use the later portion of the received signal to be sure the synchronization loop has converged.

rxData = qamdemod(rxFiltSig,M);
delay = (txFilter.FilterSpanInSymbols + ...
    rxFilter.FilterSpanInSymbols) / 2;
idxSync = 2000; % Check BER after synchronization loop has converged
[syncDataTtlErr,syncDataBER] = biterr( ...
    data(idxSync:end-delay),rxData(idxSync+delay:end))
syncDataTtlErr = 
16116
syncDataBER = 
0.5042

Depending on the random data used, there may be bit errors resulting from phase ambiguity in the received signal after the synchronization loop converges and locks. In this case, you can use the preamble to determine and then remove the phase ambiguity from the synchronized signal to reduce bit errors. If phase ambiguity is minimal, the number of bit errors may be unchanged.

idx = 9000 + (1:barker.Length);
phOffset = angle(txMod(idx) .* conj(rxFiltSig(idx+delay)));

phOffsetEst = mean(phOffset);
disp(['Phase offset = ',num2str(rad2deg(phOffsetEst)),' degrees'])
Phase offset = -90.1401 degrees
resPhzSig = exp(1i*phOffsetEst) * rxFiltSig;

Demodulate the signal after resolving the phase ambiguity. Recompute the total bit errors and BER.

resPhzData = qamdemod(resPhzSig,M);
[resPhzTtlErr,resPhzBER] = biterr( ...
    data(idxSync:end-delay),resPhzData(idxSync+delay:end))
resPhzTtlErr = 
5
resPhzBER = 
1.5643e-04

More About

expand all

Extended Capabilities

Version History

Introduced in R2012a