MATLAB: How to configure crc.generator object from to implement X.25 Standard Cyclic Redundancy Check (CRC) coding with the Communications Toolbox 4.4 (R2009b)

Communications Toolbox

I would like to use crc.generator object to implement X.25 standard Cyclic Redundancy Check (CRC) codeing described in ITU-T Recommendation X.25:
but I am not able generate expected Frame Checking Sequence (FCS) described in the documentation (Example 2 in Appendix I, I.1, page 153/169):
% X.25 standard test input sequence
Address = [1 1 0 0 0 0 0 0];
UA = [1 1 0 0 1 1 1 0];
Input = [Address UA];
% X.25 standard expected frame check sequence (FCS)
FCS = [1 1 0 0 1 1 0 0 0 0 1 0 0 1 1 0];
% Configure Cyclic Redundancy Check Generator object
h=crc.generator;
crcgen.FinalXOR='0xFFFF';
% Generate FCS using crc.generator object
Output=generate(h,Input');
MATLAB_FCS=Output(end-15:end)';
% Result does not match expected FCS
isequal(FCS,MATLAB_FCS)

Best Answer

The capability of directly generating X.25 Frame Checking Sequence using the GENERATE method of CRC.GENERATOR object is not available in Communications Toolbox 4.4 (R2009b).
However, the X.25 standard can be implemented using the crc.generator object. By default, crc.generator object is a CRC-CCITT generator, which is what X.25 implementation uses. However, X.25 does something extra, which is described in Section 2.2.7.4 of the X.25 documentation:
The FCS field shall be a 16-bit sequence. It shall be the ones complement of the sum (modulo 2) of:
1) the remainder of xk (x15 + x14 + x13 + x12 + x11 + x10 + x9 + x8 + x7 + x6 + x5 + x4 + x3 + x2 + x + 1) divided (modulo 2) by the generator polynomial x16 + x12 + x5 + 1, where k is the number of bits in the frame existing between, but not including, the final bit of the opening flag and the first bit of the FCS, excluding bits (synchronous transmission) or octets (start/stop transmission) inserted for transparency, and bits inserted for transmission timing (i.e. start or stop bits); and
2) the remainder of the division (modulo 2) by the generator polynomial x16 + x12 + x5 + 1 of the product of x16 by the content of the frame, existing between but not including, the final bit of the opening flag and the first bit of the FCS, excluding bits (synchronous transmission) or octets (start/stop transmission) inserted for transparency and bits inserted for transmission timing (i.e. start or stop bits).
As a workaround, the following code implements X.25 standard using crc.generator object:
% Input
Address = [1 1 0 0 0 0 0 0];
UA = [1 1 0 0 1 1 1 0];
Input = [Address UA]';
% Expected FCS
FCS = [1 1 0 0 1 1 0 0 0 0 1 0 0 1 1 0];
k = length(Input);
h = crc.generator;
% Remainder of step 1)
cw1 = generate(h,[ones(16, 1); zeros(k-16,1)]);
% Remainder of step 2)
cw2 = generate(h,Input);
% Add the remainders and complement:
Workaround_FCS = not(mod(cw1(end-16+1:end) + cw2(end-16+1:end),2))';
% The result matches the FCS of X.25 standard test sequence
isequal(FCS,Workaround_FCS)