ECE4253 Digital Communications Department of Electrical and Computer Engineering - University of New Brunswick, Fredericton, NB, Canada

# MATLAB Reference for Digital Communications

This page summarizes some MATLAB operations that may be useful for digital communications.

Functions specific to the Communications Toolbox are shown highlighted as (CT).

For help on any MATLAB command, type > help command

# General

1. clc : clears the command window
2. clear all : deletes all user defined variables
3. close all : deletes all open figures

4. disp : displays a value/variable directly without the preceding 'ans =' line
disp( D ) ⇒ 1 1 0 1

5. whos : returns information on variables (dimension, type, etc)
whos D ⇒
```Name      Size            Bytes  Class     Attributes
D         1x4                32  double
```

6. diary on : copies all screen output to a file named 'diary' until the next diary off command.
(useful for recording a MATLAB session)

7. license('test','communication_toolbox') : returns 1 if the communications toolbox is available.

# Binary and Vector Operations

1. length : returns the length of a vector
length( [ 1 0 1 1 0 0 1 ] ) ⇒ ans = 7

2. sum : returns the sum of all elements in an array
sum( [ 1 0 1 1 0 0 1 ] ) ⇒ ans = 4

3. any : returns 1 if any element is non-zero
any( [ 0 0 0 0 0 ] ) ⇒ ans = 0

4. isequal : returns 1 if two vectors are identical
isequal( [ 1 0 1 1 ], [ 1 0 1 1 ] ) ⇒ ans = 1

5. fliplr : returns a row vector in reverse order
fliplr( [ 1 0 1 1 ] ) ⇒ ans = 1 1 0 1

6. zeros : returns an array filled with zeros
[ 1 , zeros(1,14), 1 ] ⇒ ans = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

7. ones : returns an array filled with ones
[ 0, ones(1,14), 0 ] ⇒ ans = 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0

8. eye : returns an identity matrix
eye(3) ⇒ ans = a (3x3) identity matrix

9. bitxor : returns the bitwise xor of two values
bitxor( 5, 3 ) ⇒ ans = 6

10. bitshift : returns a value shifted left by n bits
bitshift( 17, 1 ) ⇒ ans = 34

11. wshift : shift/rotate the rows/columns of a matrix
wshift(2,[1 2 3 4 5],3) ⇒ ans = 4 5 1 2 3

12. randi : returns an array of random bits
randi([0 1],1,8) ⇒ ans = 1 0 1 1 0 1 0 1

# Format Conversion

1. dec2bin : returns a binary string from a decimal number
dec2bin(29,8) ⇒ ans = '00011101'     % specify 8-bit output

2. de2bi : returns a binary array from a decimal number (CT)
de2bi(29,8,'left-msb') ⇒ ans = 0 0 0 1 1 1 0 1

3. dec2hex : returns a hexadecimal string from a decimal number
dec2bin(29,4) ⇒ ans = '001D'     % specify 4-digit output

4. bin2dec : returns a decimal number from a binary string
bin2dec('11101') ⇒ ans = 29

5. hex2dec : returns a decimal number from a hexadecimal string
hex2dec('001D') ⇒ ans = 29

6. bi2de : returns a decimal number from a binary array (CT)
bi2de( [ 1 1 1 0 1 ],'left-msb' ) ⇒ ans = 29

7. poly2sym : returns a polynomial string from a vector of coefficients
poly2sym([1 1 0 1]) ⇒ ans = x^3 + x^2 + 1

8. double : returns ASCII codes (decimal) from a character string
double( 'ABC' ) ⇒ ans = 65 66 67

9. char : returns a character string from ASCII codes (decimal)
char( [65 66 67] ) ⇒ ans = ABC

# Galois Fields

1. gf(X) : defines an array in GF(2) (CT)
a = gf( [ 1 0 1 1 0 1 ] ) ⇒ a = 1 0 1 1 0 1

2. gf(X,N) : defines an array in GF(2^N) with the default primitive polynomial (CT)
a = gf( [ 1 0 1 1 0 1 ], 3 ) ⇒ a = 1 0 1 1 0 1

3. gf(X,N,P) : defines a Galois Field GF(2^N) based on primitive polynomial (P) (CT)
a = gf( [ 1 0 1 1 0 1 ], 3, 13 ) ⇒ a = 1 0 1 1 0 1

4. conv : (convolution) multiplies two Galois Field polynomials (CT)
c = conv(a,b); ⇒ c = a * b;

5. deconv : (deconvolution) divides two Galois Field polynomials (CT)
[q r] = deconv(a,b); ⇒ q = quotient of a/b; r = remainder of a/b

6. .x : returns Galois Field elements as type uint16 integers (CT)
a.x ⇒ ans = 1 0 1 1 0 1

7. primpoly(N,'all') : returns as decimal all primitive polynomials of degree N (CT)
primpoly(4,'all') ⇒ ans = 19 25

8. primpoly(N) : returns as decimal the default primitive polynomial of degree N (CT)
primpoly(4) ⇒ ans = 19

# Error Control Coding

1. hammgen(N) : returns the check (and generating) matrix for codewords of length L=2N-1 defining a Hamming code. (CT)
[H,G] = hammgen(3) ⇒ ans = the corresponding 3 x 7 check matrix H and 4 x 7 generating matrix G.

2. cyclgen(L,P) : returns the check (and generating) matrix for cyclic codewords of length L=2^N-1 and a given primitive polynomial P(x) of degree N. Terms in P(x) are supplied in ascending order (i.e. 25 = '1+x^3+x^4' = [1 0 0 1 1]). (CT)
[H,G] = cyclgen(15,[1 0 0 1 1]) ⇒ the corresponding 4 x 15 check matrix H and 11 x 15 generating matrix G.

3. Example: cyclgen(7,gfprimdf(3)) is equivalent to hammgen(3).

4. gen2par(G) : returns the check matrix for a systematic generator matrix (and vice versa) (CT)
gen2par(G) ⇒ ans = the corresponding check matrix H for the generator matrix G

5. encode(msg,n,k) : returns an n-bit codeword for an k-bit msg using the default generator matrix (G) from hammgen(n-k). (CT)
encode([1 1 1 0]],7,4) ⇒ ans = 0 1 0 1 1 1 0

6. decode(codeword,n,k) : returns a k-bit msg from an (errored) n-bit codeword using the default check matrix (H) from hammgen(n-k). (CT)

decode([ 0 1 0 1 1 1 0],7,4) ⇒ ans = 1 1 1 0     % no errors

decode([ 0 1 0 1 1 0 0],7,4) ⇒ ans = 1 1 1 0     % single bit error correction

 Mon Mar 4 15:26:31 AST 2024 Last Updated: 31 Jan 2016 Richard Tervo [ tervo@unb.ca ]