A 5G cell search, end to end
This page walks through examples/5g-cell-search. A base station sends the
signal a phone looks for first. A channel adds noise and a carrier offset. A
receiver finds the signal, its timing, the offset and the identity of the cell.
It is a teaching model of one procedure. It is not a 5G stack, it decodes no PBCH, and it was never run on a radio. The waveform is written from the formulas of 3GPP TS 38.211 alone (clauses 7.4.2, 7.4.3, 7.4.1.4 and 5.2.1). No line comes from another implementation.
The model uses three extensions: fpga for the correlator
(fpga.md), requirements for its limits, and ui for the screen
(ui.md).
What you need#
archec is enough to check the model and run its tests. The correlator's VHDL
runs in GHDL, and is synthesised with Yosys and nextpnr:
fpga.md says where they come from. The comparison
with NumPy needs python3 with numpy, and is only run by the tests of
arche-lang.
1. What a cell sends#
A 5G cell sends a synchronisation signal block (an SS block) every 20 ms. It is 4 OFDM symbols of 240 subcarriers, 30 kHz apart here:
| Symbol | What is on it |
|---|---|
| 0 | The PSS on subcarriers 56 to 182: one of 3 sequences, N_ID2. |
| 1 | The PBCH, with its reference signal (DM-RS) on every fourth subcarrier. |
| 2 | The SSS on subcarriers 56 to 182: one of 336 sequences, N_ID1. PBCH beside it. |
| 3 | The PBCH again. |
The cell ID is 3 N_ID1 + N_ID2: 0 to 1007. A receiver finds the PSS first,
which gives the timing and N_ID2. Then it reads the SSS, which gives
N_ID1.
signals.arche has the sequences, each function with its formula:
component NrSequences "NR sequences" {
var x: int32[127]
/// d_PSS(n) = 1 - 2 x(m), m = (n + 43 N_ID2) mod 127.
fn pss(nid2: int32, n: int32) -> int32 ```arche
return 1 - 2 * x[(n + 43 * nid2) % 127]
```
}tools/reference.py computes the same sequences with NumPy, on its own. The
tests of arche-lang compare the two: the 3 PSS, the 1008 SSS, and the DM-RS
bits of 8 cells.
2. The chain#
gnb ─► tx ─► chan ─► adc ─► pss ─► sync ─► sss ─► search
GnbSsb OfdmTx Channel Adc PssCorrelator SyncFft SssDecoder CellSearch
| Part | What it does |
|---|---|
gnb |
Builds the SS block of its cell: what is on each subcarrier. |
tx |
4 inverse FFTs of 256 points, each with a cyclic prefix of 18 samples: 1096 samples at 7.68 Msps. |
chan |
The channel's settings: SNR, carrier offset, delay. |
adc |
The converter and its sample clock: one sample per cycle, noise from an LFSR. |
pss |
The PSS correlator. It has a clock: it is hardware. |
sync |
Timing, carrier offset, FFT, and the channel on each subcarrier. |
sss |
The 336 SSS tried, then the cell ID checked on the DM-RS. |
search |
The state machine: Searching, PssLocked, SssDecoded, Synced. |
Samples are complex numbers in fixed point. The type comes from the algorithms shelf, with the FFT and the LFSR (algorithms.md):
include "arche-algorithms/fft/fft.arche"
include "arche-algorithms/complex/complex.arche"
include "arche-algorithms/lfsr/lfsr.arche"
message BURST "SS block, in time" {
samples: Cplx[1096]
}
A part calls the FFT through a port, as it calls any algorithm of the shelf:
component OfdmTx "OFDM modulator" {
out fft: requires FftApi
fn modulate ```arche
fft.size(256)
fft.set(8, 384, 0)
fft.inverse()
```
}
The language has no complex type, and needs none: Cplx is a struct, and
Complex a component with mul, mul_conj and rotate.
3. The correlator is hardware#
The correlator sees every sample: 7.68 million a second. It is the one part that is hardware, so it has a clock:
component PssCorrelator "PSS correlator" {
clock clk 7.68MHz
reset rst_n active low async
step tick
in valid: bool "Sample valid"
in i: int16 "I"
in q: int16 "Q"
out found: data "PSS found" sends PSS_FOUND
var si: uint32[8]
var sq: uint32[8]
}
It keeps the sign of the last 256 samples. On every cycle it compares them
with the signs of the 3 PSS symbols as they are in time. A sample and a
reference that agree add to a count, so each of the 3 correlators is a count of
bits: no multiplier. The largest of the three is the metric, 256 for a perfect
PSS. Above 80 it follows the peak for 16 samples, then sends PSS_FOUND with
N_ID2 and the sample the PSS ends on.
A sign in place of a sample costs about 2 dB. The threshold of 80 is above what noise alone reached in 1.2 million tries (68), and below the weakest peak seen at 0 dB with a 10 kHz offset (88).
archec check warns that a cycle of 7.68 MHz is 130.208 ns, and that the
simulator counts whole nanoseconds. The model counts samples, not time, so
nothing in it depends on those 0.2 ns.
4. Requirements with numbers#
requirement REQ-PSS "Find the PSS" {
text "The receiver shall find the PSS and its N_ID2 in the first SS block it receives whole, so within one SS block period (20 ms) and one block (0.2 ms), at an SNR of 0 dB or more."
}
requirement REQ-CELLID "Decode the cell ID" {
text "The receiver shall decode the cell ID with no error over the test set, at an SNR of 3 dB or more and a carrier frequency offset within ±10 kHz."
}
requirement REQ-RATE "The correlator keeps up" {
text "The PSS correlator shall take one sample per clock cycle at 7.68 Msps, with as much margin again (15.36 MHz), in at most half of the device (12000 LUTs)."
limit PssCorrelator.fmax >= 15.36MHz
limit PssCorrelator.luts <= 12000
}
These are the numbers this model meets, measured on it. They are not those of a product. The SNR is the power of the PSS and SSS symbols over the power of the noise, both over the 7.68 MHz that are sampled.
5. Test it#
archec test main.arche --sim web
PASS TP-PSS/TC-PSS-PERIOD 0 dB, the SS block at the end of the period
PASS TP-CELL-ID/TC-CELL-PLUS 3 dB, +10 kHz, cell 503
PASS TP-TRACKING/TC-LOST The cell stops: back to searching after 25 ms
…
19/19 passed on the browser simulator, 0 failing, 0 known failure(s)
Requirements: 2 verified by passing cases, 0 failing
A case sets the cell and the channel, and says what the receiver must have found:
test-plan TP-CELL-ID "Decoding the cell ID" {
case TC-CELL-PLUS "3 dB, +10 kHz, cell 503" {
verifies REQ-CELLID
at 0ms do chan.snr = 3.0
at 0ms do chan.cfo = 10000.0
at 0ms do gnb.cell = 503
at 1ms expect search.state == Synced
at 1ms expect search.cell_id == 503
at 1ms expect abs(search.cfo - 10000.0) < 2000.0
}
}
The 19 cases run in about 3 seconds. A case is 1 ms long, or 7680 samples: the
SS block comes 200 µs after the start. Two cases are long. TC-PSS-PERIOD puts
the block at the end of a 20 ms period, behind 152 000 samples of noise.
TC-LOST waits 25 ms for the receiver to give up. Nothing is decimated: the
correlator runs on every sample.
The noise is an LFSR's, so a case gives the same samples at each run.
adc.lfsr chooses which. For more runs than the test plans hold:
node tools/sweep.js 300
It ran 2700 times here: 300 cell IDs, delays and noises at each of 0, 3 and 10 dB and -10, 0 and +10 kHz. Every run ended synchronised on the right cell. The offset was wrong by 1.9 kHz at most at 0 dB, and by 0.5 kHz at 10 dB.
Why not#
TC-WEAK is at -7 dB. The PSS is still found, and the receiver does not
synchronise:
$ archec why-not main.arche --test TC-WEAK "PssLocked -> SssDecoded"
search: PssLocked -> SssDecoded on CELL_ID [metric > 2.0 * second] receiver.arche:290
the guard [metric > 2.0 * second] was never true while search was in PssLocked (1 CELL_ID checked)
The strongest SSS did not stand out from the second, so the receiver did not trust it.
6. The correlator in GHDL#
The correlator has a pattern of its own: with test high it listens to the
PSS of test_nid2, then to noise, again and again. Its cases need nothing
around it, so they run on the generated VHDL as they are:
archec test main.arche --target vhdl
SKIP TP-CELL-ID/TC-CLEAN No noise, no offset
not run on the FPGA — `chan.snr = 60.0`: chan is not a part on the FPGA
PASS TP-CORRELATOR/TC-SELF-0 The PSS of N_ID2 = 0: a perfect peak, on its last sample — 6 expectation(s), 301 cycle(s) of 130ns, 1.6 s
PASS TP-CORRELATOR/TC-SELF-NOISY One sign in four wrong: a peak half as high, still found — 5 expectation(s), 301 cycle(s) of 130ns, 1.6 s
PASS TP-CORRELATOR/TC-SELF-AGAIN The pattern twice: one peak each, none in the noise between — 5 expectation(s), 1385 cycle(s) of 130ns, 3.2 s
5/5 passed on GHDL
The same 5 cases pass on the simulator, on the same cycles. The cases of the whole chain are left to the simulator: the cell and the channel are not hardware.
The deployment says what is in the bitstream: the correlator alone.
deployment OnBoard "The receiver on its board" {
system Bench
platform Bench
binary bitstream "bench_top.bit" on fpga {
thread logic "Logic" { runs pss }
}
}7. Synthesise it#
archec synth main.arche
Bench bench_top on LFE5U-25F-6BG381
LUTs 9315 of 24288 38 %
FFs 804 of 24288 3 %
BRAM 0 of 56 0 %
DSP 0 of 28 0 %
✓ clock clk: fmax 19.46 MHz, above the 7.68 MHz of PssCorrelator
✓ limit PssCorrelator.fmax >= 15.36MHz 19.46 MHz >= 15.36 MHz REQ-RATE
✓ limit PssCorrelator.luts <= 12000 9534 LUTs <= 12000 REQ-RATE
It takes about 3 minutes. The 3 correlators are 12 counts of 256 bits, all in
one cycle: that is where the LUTs go. archec status main.arche then shows
each requirement with what proves it.
8. See it#
archec sim main.arche --html sim.html --for 1ms
The page plots a number against time, not an array. So the scope part reads
the last SS block out one point per microsecond, and a spectrum is a trace:
| Plot | What it shows |
|---|---|
pss.metric |
The correlation, sample by sample: flat, then one peak. |
scope.level |
The spectrum of the PSS symbol: 127 subcarriers above the noise. |
scope.sss_i, scope.sss_q |
The SSS subcarriers: I at two levels, Q near 0. |
scope.pbch_i, scope.pbch_q |
The PBCH subcarriers: two levels each. |
The page keeps every step so that you can go back in time. It takes about 3 seconds for each millisecond of this model.
screen.arche adds a status screen: the state, the cell ID, the SNR and the
offset.
archec ui preview screen.arche
archec test screen.arche --sim web --plan TP-SCREEN
Limits: what it does not do#
- One procedure. No PBCH decoding, no SS block index, no frame timing, no neighbour cells. The PBCH subcarriers carry random QPSK symbols.
- A simple channel. One path, a fixed gain, white noise. No fading, no multipath, no sampling clock offset.
- One SS block. The receiver decides on a single block. A product averages several, and works at lower SNRs.
- Never on a radio. The correlator was simulated, run in GHDL and synthesised. No bitstream was loaded on a board.
- The converter's model is not hardware.
Adchas a clock so that the correlator runs sample by sample. It is not in the bitstream. - Plots are traces. The simulator's page has no plot of an array, and no plot of one value against another: no constellation diagram.
archec why-noton a long case. It records every variable that changes, on every sample, and stops after 100 000 events: about 1 ms of this model.- The FPGA's part of an array. A component with a clock cannot receive an array in a message. The channel hands the converter's model an SS block one sample per message.