Useful Matlab Functions and
Signals and Systems Laboratory EE422G
This page contains mfile and pfile downloads used in class examples. Mfiles are simply text (ascii) files. They can be copied and pasted into the Matlab editor or workspace, or downloaded to you computer from the links below. Pfiles are similar to Mfiles in that they can be executed in the Matlab workspace; however the code cannot be viewed or edited. All mfiles were hastily written by Kevin D. Donohue, so some debugging may be required.
kfourier.m – The script presents graphic examples summing up a series of odd harmonic sine waves to form a square wave.
aliasex1.m – The script presents graphic examples of aliasing and shows how the time axis vector in Matlab is effectively used to sample continuous functions.
quant_lab1.m – This script simulates the distortion from an R-bit quantizer and computes the SNR for the signal to quantization noise. The signal to be quantized is either a sine wave generated in the script or a signal read in from a wave file. The script also plots the average spectra for both the original and quantized signals. The resulting SNR is printed in the plot title. This script is useful for Lab 1.
sigclip.m – This function inputs signal vector and clips all values exceeding an upper value UC to UC and all values below a lower value LC to LC. Values of X between UC and LC are passed to the output vector unchanged. This function is used in Lab 2.
psdex.m – The script demonstrates the Power Spectral Density with an example using the pwelch function on a sinusoid in white Gaussian noise (WGN). The script generates a sinusoid, normalizes its power, adds to it WGN at a specified SNR level, and then computes the power spectral density (PSD) using PWELCH. Plots of the original time domain signal and PDS are provided. Parameters a well-commented so changes can be made to examine effects of window size, number of FFT points, tapering window, signal frequency, and SNR levels. This script is useful as an example of for experiments in Lab 2.
winlook.m –This script illustrates basic processing plotting functions for examining window functions. This script uses the Tukey window as an example. Different windows have different parameters, so adjustments must be made when inserting other window function into this script.
firlook.m – This script illustrates basic processing and plotting functions for examining filter properties. This script uses the FIR filters FIR1 (windowing method) and FIRPM (the Parks-McClellan) filter design program to create high-pass filters, FREQZ to compute the filter transfer functions, FILTER to compute the impulse response, and ROOTS to compute zero locations of the filters.
iirlook.m – This script illustrates basic processing and plotting functions for examining and specifying filter properties for the IIR filters. This script uses the IIR filters BUTTER (Butterworth maximally flat), CHEBY1 (Chebyshev Type 1 filter, ripple in passband), CHEBY2 (Chebyshev Type 2 filter, ripple in stopband) and ELLIP (Elliptical filter, ripple in both pass and stop bands). FREQZ is used to compute the filter transfer functions, FILTER is used to compute the impulse response, and ROOTS to compute zero and pole locations of the filters. All examples will show the filter design and analysis for a 6th order band pass filter with a sampling rate of 8000Hz and a passband between 1000 and 2500 Hz, or in the case where the stopband edge frequencies are required, they will be taken at 850 and 2900 Hz.
modulb.m – This function generates a sample line codes signals from an input binary sequence, a source data rate in bits per second and a sampling rate of output waveform in samples per second. The types of line codes can be selected from the following options: unipolar_nrz, bipolar_nrz, 4level_nrz, bipolar_rz, unipolar_rz, ami, manchester, miller, unipolar_nyquist, and bipolar_nyquist.
bittrans.m – % This script demonstrates encoding a binary sequence to a baseband line signal, and the effects of adding noise and distortion. Plots are generated to help understand the impact of noise and bandwidth limits on the received waveform and potential for bit errors in the decoded sequence. The script uses the function MODULB (must be downloaded from EE422G web site as well) to generate various line codes. The script initializes with critical parameters such as data rate, bit sequence, line code, channel bandwidth and signal to noise level. It then plots the line coded sequence (Figure 1), the distorted sequence (Figure 2) and the noise plus distortion sequence (Figure 3).
biterr.m – % This script simulates a digital communication system where noise and limited bandwidth are the primary sources of corruption resulting in bit errors. Binary sequences are randomly generated and encoded into baseband line signals. A matched filter is derived and implemented for decoding the bit from the noisy and distorted signals. The resulting bit error rate is computed along with a graphic showing where in the sequence bit errors actually occurred. The bit error rate is printed in the title of the figure. This script uses the function MODULB (must be downloaded from EE422G web site as well) to generate various line codes and to extract templates for the match filter design and implementation.
simpmod.m – % This script demonstrates the modulation properties of the real and complex sinusoid. A simple sinusoid is used as a test signal to allow for easy observation of the frequency shifts involved in the modulation process. The sampling rate should be set sufficiently high to account for the shift of the modulation frequency plus the frequency range of the original signal. The PSD is estimated with the PWELCH function. The 'twosided' option is selected for PWLECH and the FFTSHIFT is used to see both the positive and negative frequencies on an axis symmetric about zero. This is important to observe the shifting of both the positive and negative frequency component as well as handling the complex valued waveforms, whose spectra are not symmetric about 0 (DC). Plots of the spectra magnitudes for test signal, modulating (carrier) frequency, and the modulated signal are generated for the real (symmetric spectra) and complex-valued signals.
bbchan.p – % This hidden function written to simulate transmission over a band-pass channel, of which the band=pass parameters are unknown. The function adds noise after band-pass filtering to result in a a 20 dB SNR. Let Y be a vector containing the signal to be transmitted with sampling rate FS. The channel simulation is implemented with the following syntax:
yout = bbchan(y,fs)
where output vector YOUT is the same size as input signal Y with channel noise and distortion. (this function can’t be viewed, only downloaded and execute in the Matlab workspace.