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
- clc : clears the command window
- clear all : deletes all user defined variables
- close all : deletes all open figures
- disp : displays a value/variable directly without the preceding 'ans =' line
disp( D ) ⇒ 1 1 0 1
- whos : returns information on variables (dimension, type, etc)
whos D ⇒
Name Size Bytes Class Attributes
D 1x4 32 double
- diary on : copies all screen output to a file named 'diary' until the next diary off command.
(useful for recording a MATLAB session)
- license('test','communication_toolbox') : returns 1 if the communications toolbox is available.
Binary and Vector Operations
- length : returns the length of a vector
length( [ 1 0 1 1 0 0 1 ] ) ⇒ ans = 7
- sum : returns the sum of all elements in an array
sum( [ 1 0 1 1 0 0 1 ] ) ⇒ ans = 4
- any : returns 1 if any element is non-zero
any( [ 0 0 0 0 0 ] ) ⇒ ans = 0
- isequal : returns 1 if two vectors are identical
isequal( [ 1 0 1 1 ], [ 1 0 1 1 ] ) ⇒ ans = 1
- fliplr : returns a row vector in reverse order
fliplr( [ 1 0 1 1 ] ) ⇒ ans = 1 1 0 1
- 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
- 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
- eye : returns an identity matrix
eye(3) ⇒ ans = a (3x3) identity matrix
- bitxor : returns the bitwise xor of two values
bitxor( 5, 3 ) ⇒ ans = 6
- bitshift : returns a value shifted left by n bits
bitshift( 17, 1 ) ⇒ ans = 34
- wshift : shift/rotate the rows/columns of a matrix
wshift(2,[1 2 3 4 5],3) ⇒ ans = 4 5 1 2 3
- randi : returns an array of random bits
randi([0 1],1,8) ⇒ ans = 1 0 1 1 0 1 0 1
Format Conversion
- dec2bin : returns a binary string from a decimal number
dec2bin(29,8) ⇒ ans = '00011101' % specify
8-bit output
- de2bi : returns a binary array from a decimal number (CT)
de2bi(29,8,'left-msb') ⇒ ans = 0 0 0 1 1 1 0 1
- dec2hex : returns a hexadecimal string from a decimal number
dec2bin(29,4) ⇒ ans = '001D' % specify 4-digit
output
- bin2dec : returns a decimal number from a binary string
bin2dec('11101') ⇒ ans = 29
- hex2dec : returns a decimal number from a hexadecimal string
hex2dec('001D') ⇒ ans = 29
- bi2de : returns a decimal number from a binary array
(CT)
bi2de( [ 1 1 1 0 1 ],'left-msb' ) ⇒ ans = 29
- poly2sym : returns a polynomial string from a vector of coefficients
poly2sym([1 1 0 1]) ⇒ ans = x^3 + x^2 + 1
- double : returns ASCII codes (decimal) from a character string
double( 'ABC' ) ⇒ ans = 65 66 67
- char : returns a character string from ASCII codes (decimal)
char( [65 66 67] ) ⇒ ans = ABC
Galois Fields
- gf(X) : defines an array in GF(2)
(CT)
a = gf( [ 1 0 1 1 0 1 ] ) ⇒ a = 1 0 1 1 0 1
- 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
- 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
- conv : (convolution) multiplies two Galois Field polynomials
(CT)
c = conv(a,b); ⇒ c = a * b;
- deconv : (deconvolution) divides two Galois Field polynomials
(CT)
[q r] = deconv(a,b); ⇒ q = quotient of a/b; r = remainder of a/b
- .x : returns Galois Field elements as type uint16 integers
(CT)
a.x ⇒ ans = 1 0 1 1 0 1
- primpoly(N,'all') : returns as decimal all primitive polynomials of degree N
(CT)
primpoly(4,'all') ⇒ ans = 19 25
- primpoly(N) : returns as decimal the default primitive polynomial of degree N
(CT)
primpoly(4) ⇒ ans = 19
Error Control Coding
- 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.
- 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.
Example:
cyclgen(7,gfprimdf(3)) is equivalent to
hammgen(3).
- 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
- 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
- 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