%file name: crcr.m %Written by Dr. James S. Kang, Cal Poly Pomona. %Calculate the remainder for the received data d and generator sequence g. %d = received data such as [1 0 1 0 0 1 0 1] = data + CRC %g = generator sequence such as [1 1 1 1 1]. %q = quotient sequence. %r = remainder. %To run the program, try % >>crcr([1 0 1 0 0 1 0 1],[1 1 1 1 1]) function[r] = crcr(d,g) nd=size(d,2); %nd = length of d. ng=size(g,2); %ng = length of g. fnzd=min(find(d)); %fnzd = index of first nonzero of d. fnzg=min(find(g)); %fnzg = index of first nonzero of g. ld=nd-fnzd+1; %ld = length of d from first nonzero. s = sum(d); g=g(1,fnzg:ng); %g starting from the first nonzero. ng=size(g,2); %ng = length of g. da=d(1,fnzd:nd); %da = data starting from the first nonzero. nda=size(da,2); %b = all zero vector of length ng - 1. fnzda=min(find(da)); %fnzda = index of the first nonzero of da. nz=nda-ng; %nz = length of da minus the length of g. b1=zeros(1,nz); %b1 = all zero vector of length nz. v=[g,b1]; %v = g augmented by b1. az=zeros(1,1); if s >0 nq=nda-ng+1; %nq = length of quotient. for i = 1:nq if da(i) == 1 q(i) = 1; da=[rem(da+v,2)]; %da = modulo 2 sum of da and v. else q(i) = 0; end v=[az,v(1,1:nda-1)]; %v is shifted to the right by 1. end for i=1:ng-1 r(i) = da(1,nda-ng+1+i); end else r=zeros(1,ng-1); end disp('The quotient = ') disp(q) disp('The remainder = ') disp(r) s = sum(r); if s==0 disp('There is no error'); else disp('There may be error(s)'); end