This is an example of implementation. In this specific example, I used the case with single RB and group hopping/sequence hopping disabled just to simplify the code. You may revise/extend this code for more general case like 2 RB case or greather than 2 RB case.
A PUSCH of one RB carries a DMRS of 12 subcarriers in each slot. The sequence is short, so 36.211 does not build it from a Zadoff-Chu sequence and lists it in a table instead. The page walks through the formula, the Matlab code, and three sets of results.
Followings are the topics to be covered in this page.
How is the 1 RB DMRS built ?
The DMRS of one RB is the product of two parts: a base sequence, and a cyclic shift that rotates it. The base sequence depends on the cell, and the cyclic shift depends on the cell, the slot and the scheduling DCI. The code below follows 36.211 v19.3.0 clause 5.5 for both parts.
The base sequence is r̄u,v(n) = ejφ(n)π/4 for n = 0 to 11. Table 5.5.1.2-1 gives φ(n), and every entry is ±1 or ±3. So every sample sits at one of the four QPSK phases, ±π/4 or ±3π/4. Tables of this kind apply only to sequences of 12 and 24 subcarriers. From 3 RB upwards, the base sequence is a cyclically extended Zadoff-Chu sequence.
The group number u runs from 0 to 29. With group hopping disabled, u = fssPUSCH = NIDcell mod 30, when no virtual cell identity and no Δss apply. The base sequence number v is always 0 here, because each group has only one base sequence below 6 RB. Sequence hopping applies only from 6 RB, so it has no effect on this example either.
The cyclic shift is α = 2πncs/12, with ncs = (n(1)DMRS + n(2)DMRS + nPN(ns)) mod 12. The offset n(1)DMRS comes from the higher layer parameter cyclicShift through Table 5.5.2.1.1-2. The offset n(2)DMRS comes from the Cyclic Shift Field of the uplink DCI through Table 5.5.2.1.1-1. The last term, nPN(ns), is a cell-specific value. It takes 8 bits of the pseudo-random sequence of clause 7.2, which is initialized with cinit = ⌊NIDcell/30⌋ · 25 + fssPUSCH when no virtual cell identity is configured.
Base sequence : φ(n) from Table 5.5.1.2-1, one row per group u.Group number u : NIDcell mod 30, with group hopping disabled.Sequence number v : always 0 for fewer than 6 RB.Cyclic shift : α = 2πncs/12, where ncs adds the two DMRS offsets and nPN(ns).
Matlab Code
The code computes the same quantities in the same order: cinit, the Gold sequence, the 8 bit nPN(ns), ncs, α, and finally the 12 samples. It fixes the slot to 0 and the cyclicShift and DCI cyclic shift to 0, so only the cell identity changes the result.
clear all;
% 36.211 (v12.30) Table 5.5.1.2-1:
Psi_1RB = ...
[ ...
-1 1 3 -3 3 3 1 1 3 1 -3 3; ...
1 1 3 3 3 -1 1 -3 -3 1 -3 3; ...
1 1 -3 -3 -3 -1 -3 -3 1 -3 1 -1; ...
-1 1 1 1 1 -1 -3 -3 1 -3 3 -1; ...
-1 3 1 -1 1 -1 -3 -1 1 -1 1 3; ...
1 -3 3 -1 -1 1 1 -1 -1 3 -3 1; ...
-1 3 -3 -3 -3 3 1 -1 3 3 -3 1; ...
-3 -1 -1 -1 1 -3 3 -1 1 -3 3 1; ...
1 -3 3 1 -1 -1 -1 1 1 3 -1 1; ...
1 -3 -1 3 3 -1 -3 1 1 1 1 1; ...
-1 3 -1 1 1 -3 -3 -1 -3 -3 3 -1; ...
3 1 -1 -1 3 3 -3 1 3 1 3 3; ...
1 -3 1 1 -3 1 1 1 -3 -3 -3 1; ...
3 3 -3 3 -3 1 1 3 -1 -3 3 3; ...
-3 1 -1 -3 -1 3 1 3 3 3 -1 1; ...
3 -1 1 -3 -1 -1 1 1 3 1 -1 -3; ...
1 3 1 -1 1 3 3 3 -1 -1 3 -1; ...
-3 1 1 3 -3 3 -3 -3 3 1 3 -1; ...
-3 3 1 1 -3 1 -3 -3 -1 -1 1 -3; ...
-1 3 1 3 1 -1 -1 3 -3 -1 -3 -1; ...
-1 -3 1 1 1 1 3 1 -1 1 -3 -1; ...
-1 3 -1 1 -3 -3 -3 -3 -3 1 -1 -3; ...
1 1 -3 -3 -3 -3 -1 3 -3 1 -3 3; ...
1 1 -1 -3 -1 -3 1 -1 1 3 -1 1; ...
1 1 3 1 3 3 -1 1 -1 -3 -3 1; ...
1 -3 3 3 1 3 3 1 -3 -1 -1 3; ...
1 3 -3 -3 3 -3 1 -1 -1 3 -1 -3; ...
-3 -1 -3 -1 -3 3 1 -1 1 3 -3 -3; ...
-1 3 -3 3 -1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
% UL reference signal prime numbers less than 2048 from 3GPP TS 36.211 v8.x
% (ts_136211v080900p.pdf)section 5.5.1.1
PrimeNo = ...
[ ...
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ...
31, 37, 41, 43, 47, 53, 59, 61, 67, 71, ...
73, 79, 83, 89, 97, 101, 103, 107, 109, 113, ...
127, 131, 137, 139, 149, 151, 157, 163, 167, 173, ...
179, 181, 191, 193, 197, 199, 211, 223, 227, 229, ...
233, 239, 241, 251, 257, 263, 269, 271, 277, 281, ...
283, 293, 307, 311, 313, 317, 331, 337, 347, 349, ...
353, 359, 367, 373, 379, 383, 389, 397, 401, 409, ...
419, 421, 431, 433, 439, 443, 449, 457, 461, 463, ...
467, 479, 487, 491, 499, 503, 509, 521, 523, 541, ...
547, 557, 563, 569, 571, 577, 587, 593, 599, 601, ...
607, 613, 617, 619, 631, 641, 643, 647, 653, 659, ...
661, 673, 677, 683, 691, 701, 709, 719, 727, 733, ...
739, 743, 751, 757, 761, 769, 773, 787, 797, 809, ...
811, 821, 823, 827, 829, 839, 853, 857, 859, 863, ...
877, 881, 883, 887, 907, 911, 919, 929, 937, 941, ...
947, 953, 967, 971, 977, 983, 991, 997,1009,1013, ...
1019,1021,1031,1033,1039,1049,1051,1061,1063,1069, ...
1087,1091,1093,1097,1103,1109,1117,1123,1129,1151, ...
1153,1163,1171,1181,1187,1193,1201,1213,1217,1223, ...
1229,1231,1237,1249,1259,1277,1279,1283,1289,1291, ...
1297,1301,1303,1307,1319,1321,1327,1361,1367,1373, ...
1381,1399,1409,1423,1427,1429,1433,1439,1447,1451, ...
1453,1459,1471,1481,1483,1487,1489,1493,1499,1511, ...
1523,1531,1543,1549,1553,1559,1567,1571,1579,1583, ...
1597,1601,1607,1609,1613,1619,1621,1627,1637,1657, ...
1663,1667,1669,1693,1697,1699,1709,1721,1723,1733, ...
1741,1747,1753,1759,1777,1783,1787,1789,1801,1811, ...
1823,1831,1847,1861,1867,1871,1873,1877,1879,1889, ...
1901,1907,1913,1931,1933,1949,1951,1973,1979,1987, ...
1993,1997,1999,2003,2011,2017,2027,2029,2039 ...
];
% 36.211 v8.x (ts_136211v080900p.pdf) Table 5.5.2.1.1-2
N_1_DMRS = [0 2 3 4 6 8 9 10];
% 36.211 v8.x (ts_136211v080900p.pdf) Table 5.5.2.1.1-2
N_2_DMRS = [0 6 3 4 2 8 10 9];
cyclicShift = 0;
N_UL_symb = 7;
N_slot = 0;
% Calculate n_PRS_ns ====================================================
% Calculate c_init that will be used to initialize x2(n)
N_Cell_ID = 5;
Delta_ss = 0;
f_ss_PUCCH = mod(N_Cell_ID,30);
f_ss_PUSCH = mod(f_ss_PUCCH + Delta_ss,30);
c_init = floor(N_Cell_ID / 30) * 2^5 + f_ss_PUSCH;
% initialization vector for x1(n)
x1_init = [1 0 0 0 0 0 0 0 0 0 ...
0 0 0 0 0 0 0 0 0 0 ...
0 0 0 0 0 0 0 0 0 0 ...
0];
% Convert c_init (Decimal) into binary vector
% c_init : a decimal number to be converted into a binary sequence
% 31 indicates the length of the binary sequence
% 2 indicates the base of the sequence. Meaning this is a binary sequence
x2_init = de2bi(c_init, 31, 2,'right-msb');
% set the length of c(n). This can be from 1 to '8 * N_UL_symb * 20 + 7'
% I set the maximum possible number (this is derived from "8 * N_UL_symb * 20 + 7")
Mpn = 8 * N_UL_symb * 20 + 7;
% Nc as defined in 36.211 7.2
Nc = 1600;
% Create a vector(array) for x1() and x2() all initialized with 0
x1 = zeros(1,Nc + Mpn);
x2 = zeros(1,Nc + Mpn);
% Initialize x1() and x2()
x1(1:31) = x1_init;
x2(1:31) = x2_init;
% generate the m-sequence : x1()
for n = 1 : ((Mpn+Nc)-31)
x1(n+31) = mod(x1(n+3) + x1(n),2);
end;
% generate the m-sequence : x2()
for n = 1 : ((Mpn+Nc)-31)
x2(n+31) = mod(x2(n+3) + x2(n+2) + x2(n+1) + x2(n),2);
end;
% Calculate p_PRS
n_PRS_ns = 0;
for i = 0 : 7
n = 8 * N_UL_symb * N_slot + i
c = mod(x1(n+Nc+1) + x2(n+Nc+1),2)
n_PRS_ns = n_PRS_ns + c*(2^i)
end;
% Calculate n_cs =====================================================
% I put "cyclicShift + 1" in stead of cyclicShift since the array index
% in Matlab starts with 1 in stead of 0.
n_cs = mod( N_1_DMRS(cyclicShift+1) + N_2_DMRS(cyclicShift+1) + n_PRS_ns, 12);
% Calculate alpha ====================================================
alpha = 2*pi*n_cs / 12;
% Calculate r_a_u_v for 1 RB case ====================================
v = 0; % sequence number
u = f_ss_PUSCH; % group number
n = 0:11;
phaseShift = exp(j*alpha*n);
r_a_u_v = phaseShift .* exp(j*Psi_1RB(u+1,:)*pi/4);
% plot DMRS ===========================================================
plot(real(r_a_u_v),imag(r_a_u_v),'ro','MarkerFaceColor',[1 0 0]); axis([-1.5 1.5 -1.5 1.5]);

The DMRS for N_Cell_ID = 5, where u = 5 and ncs = 7. The 12 samples lie on the unit circle, and some of them fall on the same point.
Seven visible points for 12 samples : several samples land on the same phase.All points on the unit circle : the DMRS has constant amplitude.Phases at odd multiples of 15 degrees : the QPSK phases of φ(n) plus multiples of the 210 degree cyclic shift step.
A few lines of the code need a note when it is reused. The variable n_PRS_ns is called nPN(ns) in the current 36.211, and the prime number list and the 2 RB table Psi_2RB are not used for the 1 RB case. The 2 RB table becomes necessary for 2 RB, and the prime numbers for 3 RB or more, where the Zadoff-Chu length is the largest prime below the number of subcarriers.
The table numbers in the comments come from v8.9.0. In v19.3.0, n(1)DMRS is in Table 5.5.2.1.1-2 and n(2)DMRS is in Table 5.5.2.1.1-1, with one column for each layer. The values that the code uses have not changed. Table 5.5.1.2-1 in v19.3.0 also matches Psi_1RB row by row.
n_PRS_ns : nPN(ns) in the current specification.PrimeNo and Psi_2RB : unused here, needed for wider allocations.u is the group number and v the sequence number : the comments in the code say so.mod, not mode : f_ss_PUSCH uses the modulo operation.
Changing N_Cell_ID
How much does the DMRS change from one cell to the next? The table below runs the code for N_Cell_ID 0 to 12 and lists u, ncs and α for each one. Every other parameter stays at 0.
I did some experiment of the code with changing N_Cell_ID and got the result as follows.

u follows N_Cell_ID directly, while ncs changes in pairs. The alpha of 3.14519 for N_Cell_ID 6 and 7 should read 3.14159, which is 2π × 6/12 = π.
u = N_Cell_ID : because N_Cell_ID is below 30 and Δss is 0.ncs changes in pairs : IDs 0 and 1 share 4, 2 and 3 share 5, and so on.α = 2π ncs/12 : every value in the alpha column follows from ncs.
The pairs come from nPN(ns). With N_Cell_ID below 30, cinit equals N_Cell_ID, and the lowest bit of cinit does not change the 8 bits that slot 0 reads. So N_Cell_ID 2k and 2k + 1 share one nPN(ns), and the values in this table run from 64 to 71. A different slot reads different bits of the sequence, so this table shows the pairing for slot 0 only.
The group number repeats every 30 cells, because u = NIDcell mod 30. Two neighbouring cells whose identities differ by 30 therefore use the same base sequence. Δss and group hopping exist to separate such cells. That is why the code carries Delta_ss, although this example sets it to 0.
nPN(ns) pairs up adjacent IDs in slot 0 : the values run from 64 to 71 here.u repeats every 30 cells : Δss or group hopping separates cells that share u.
All 30 Patterns for 1 RB
The table above covers only 13 of the 30 groups. The plot below shows the base sequence of every group, all with the same cyclic shift, so the differences between the panels come from φ(n) alone.
This is just for reference. I just plotted the all the possible 30 patterns of PUSCH 1RB DMRS with the following condition.
Cell ID = 0
No of RB = 1
Slot Number = 0
Following is the routine for plotting. phaseShift is the value calculated in the routine listed above.
%plot DMRS for all u =================================================
for i = 1:30
r_a_u_v = phaseShift .* exp(j*Psi_1RB(i,:)*pi/4);
subplot(6,5,i);
plot(real(r_a_u_v),imag(r_a_u_v),'ro','MarkerFaceColor',[1 0 0]);
axis([-1.5 1.5 -1.5 1.5]);
end;

One panel per group, from u = 0 at the top left to u = 29 at the bottom right, read row by row. All 30 use the cyclic shift of Cell ID 0.
Every panel lies on the unit circle : all 30 sequences have constant amplitude.The panels differ in which phases are used : and in how many samples share a point.One cyclic shift for all panels : phaseShift is computed once, for Cell ID 0 and slot 0.
The subplot index runs across each row before it moves down, so the panel for group u is subplot u + 1. In slot 0, Cell ID 0 gives ncs = 4 and α = 2π/3. A real cell uses only one of these panels at a time, the one for its own u, and the other 29 belong to other cells.
The 30 rows of Table 5.5.1.2-1 were chosen to keep the cross-correlation between groups low and the peak-to-average power of each sequence low. A Zadoff-Chu sequence would not give enough of them. The largest prime below 12 is 11, which gives only 10 roots, and the largest prime below 24 is 23, which gives 22. Both are fewer than the 30 groups, so 1 RB and 2 RB use tables instead.
30 groups, one sequence each : for any PUSCH narrower than 6 RB.Only the phase changes : the amplitude of every sample is 1.A table instead of Zadoff-Chu : lengths of 12 and 24 are too short for 30 good Zadoff-Chu sequences.
Reference
[1] 3GPP TS 36.211 v19.3.0 - clause 5.5.1 for the base sequences, clause 5.5.2.1.1 for the PUSCH DMRS, and clause 7.2 for the pseudo-random sequence