ECE 5650/4650 Python Project 1 - eas.uccs. · PDF fileIntroduction 1 ECE 5650/4650 Python...

download ECE 5650/4650 Python Project 1 - eas.uccs. · PDF fileIntroduction 1 ECE 5650/4650 Python Project 1 This project is to be treated as a take-home exam, meaning each student is to due

If you can't read please download the document

Transcript of ECE 5650/4650 Python Project 1 - eas.uccs. · PDF fileIntroduction 1 ECE 5650/4650 Python...

  • ECE 5650/4650 Python Project 1 This project is to be treated as a take-home exam, meaning each student is to due his/her ownwork. The exception to this honor code is for ECE 4650 students I will allow you to work in teamsof two if desired. Still, teams do not talk to other teams. The project due date is no later than 5:00PM Tuesday, November 21, 2017. Getting it completed earlier is recommended. To work the proj-ect you will need access to the Jupyter qtconsole or Jupyter Notebook (preferred). All the neededfunctions can be found in numpy, the signal module of scipy, sk_dsp_comm.sigsys.py,and detect_peaks.py, found in the project ZIP package.

    IntroductionIn this project you are introduced to using Pythons Scipy Stack, which is a collection of Pythonpackages for doing engineering and scientific computing. The core portion of the SciPy stack isknown as PyLab. This Python DSP project will get you acquainted with portions of the Pythonlanguage and PyLab and then move into the exploration of

    LCCDEs with non-zero initial conditions

    Multi-rate sampling theory

    Polyphase rate changing class

    A minimum mean square error filter for recovering speech embedded in noise

    It is the students responsibility to learn the very basics of Python from one of the various tutorialson the Internet, such as Python Basics (a document I wrote and linked on the course Web Site).

    Python Basics with NumPy and SciPyTo get up and running with Python, or more specifically the PyLab environment engineering andscientific computing, please read through the tutorial I have written in an Jupyter notebook. ThePDF version of the notebook can be found at http://www.eas.uccs.edu/wickert/ece5650/notes/PythonBasics.pdf.

    Problems

    Causal Difference Equation Solver with Non-Zero Initial Conditions1. In this problem you gain some experience in Python coding by writing a causal difference

    equation solver. You will actually be writing a Python function (def) that you will input fil-ter coefficients and , the input signal , and initial condi-

    tions, and , to then obtain the output, . The starting point is

    b0 b1 1 a1 a2 x n xi yi y n

    Introduction 1

    http://www.eas.uccs.edu/wickert/ece5650/notes/PythonBasics.pdfhttp://www.eas.uccs.edu/wickert/ece5650/notes/PythonBasics.pdfhttp://www.eas.uccs.edu/wickert/ece5650/notes/PythonBasics.pdfhttp://www.eas.uccs.edu/wickert/ece5650/notes/PythonBasics.pdf

  • ECE 5650/4650 Python Project 1

    (1)

    The second line above contains the LCCDE form of interest for working this problem. Thereare two sum-of-products (SOP) that must be calculated for each new sample input to thesystem. Your responsibility is to writing the main number crunching algorithm. Adhere tothe following code template (found in a sample Jupyter notebook):

    aky n k k 0=

    N

    bkx n k k 0=

    M

    =

    y n 1a0----- bkx n k

    k 0=

    M

    aky n k k 1=

    N

    =

    Problems 2

  • ECE 5650/4650 Python Project 1

    In writing the main loop code consider how x_state and y_state are used:

    To update the state arrays on each loop iteration consider using the Numpy functionsroll() and sum() for ndarrays. a) Complete the function LCCDE(). Feel free to do all of your code development right

    inside an Jupyter notebook.

    b) Test the code with the two examples given in the doc string of the code template. Theprinted listing of output values needs to be included in your report for grading purposes.Feel free to validate your code by hand calculations or other means you can devise.

    c) Compare the execution speed LCCDE() with signal.lfilter() under zero initialconditions using the IPython magic $timeit. An example of the setup for LCCDE()is shown below:

    Code To Write

    x n x n 1 x n M x n 2 x_state

    y n 1 y n 2 y n N y n 3 y_state

    Product with b coefficient array

    Product with a coefficient array

    Problems 3

  • ECE 5650/4650 Python Project 1

    Multirate Systems with Python Using PyLab

    2. In this first task you will do some basic signals and systems problem solving using PyLabwith SciPy and the code the modules sigsys.py and detect_peaks.py found in theproject ZIP package. Note: In Task 1 I will be guiding you step-by-step. In the remainingtasks things become more open-ended.

    a) Generate 10,000 samples of . The large number of samples insures a high resolu-tion spectral estimate.

    b) Plot for using plot(x,y). Label your axis accordingly.c) Plot the power spectrum of using

    f,Sx = ss.simple_SA(x, NS, NFFT, fs, window='hanning')with NS=NFFT=2048. Then plot in dB, that is plot . Note: Here the powerspectrum as defined in notes Chapter 4, , is related to with additional scaling and averaging. Also, setting fs=1 means the frequency axisarray f corresponds to , which convenient for the present analysis.

    d) Next verify that the PSD spectral peaks are where you expect based on theory. Tonumerically find the peaks use index=detect_peaks(Sx) (the ZIP package con-tains detect_peaks.py):

    Note: In the above code snippet, the \ is Pythons line continuation character. It is possi-ble to break lines on , (commas) too.

    Figure 1: Downsampler/upsampler with lowpass interpolation filter blockdiagram.

    b=firwin()Lowpass65 Taps

    fc

    x n y n 4 4

    z1 n z2 n

    3 10------n cos 2 3---n

    cos+=

    x n

    x n 0 n 50x n

    10log10 Sx Pxx Pxx 2f = X e

    j 2

    2

    Problems 4

  • ECE 5650/4650 Python Project 1

    e) Using z1 = ss.downsample(x,M) produce and repeat parts (b) (d) forz1. You need to compare the experimental results for the spectral frequency locationswith theory. The amplitude values are not a concern at this point. The fact that you gen-erated sinusoids at two different amplitudes should help you keep track of whichfrquency is which, in spite of any aliasing that may be present.

    f) Using z2 = ss.upsample(z1,L) produce and repeat parts (b) (d) for z2. As aresult of the upsampling, modify the plot range from part (b) to .

    g) Design the lowpass interpolation filter as indicated in the block diagram. Choose the cut-off frequency accordingly. The firwin function is in the SciPy signal module:b = signal.firwin(numtaps, cutoff)

    where here numtaps=65 and cutoff=2*fc/fs, where and thesampling rate at this point. Plot the frequency response magnitude and phase ofthis filter. Also obtain a pole-zero plot. The relevant Python functions are shown in thecode snippet below:

    h) Finally, process through the lowpass interpolation filter to produce . The rel-evant Python function to perform filtering is:

    y = signal.lfilter(b,a,x)where for an FIR filter a=1. With y in hand, repeat parts (b) (d). To compensate forthe FIR filter delay, in part (b) change the plot range to . Comment on yourfinal results. Note: Your lowpass filter will push the amplitudes of the upsamplingimages down, but the detect_peak function will still find them. The argument mph=can be used to ignore peaks below the minimum peak height (mph).

    A Polyphase Decimator Class

    3. In this problem you will develop and test a polyphase decimator class in Python. The start-ing point will be a related Python class that performs FIR filtering without the use of sig-nal.lfilter. Recall that a standard decimate by operation first filters the input signal,

    , with a lowpass filter having a lowpass bandwidth of , followed by a

    z1 n

    z2 n 0 n 100

    fc c 2 =fs 1=

    z2 n y n

    50 n 100

    Mx n c M=

    Problems 5

  • ECE 5650/4650 Python Project 1

    downsample by as shown in Figure 2:

    The block diagram of a polyphase decimator can be found on text page 200, Figure 4.40 oron notes Chapter 4 page 55 (also see Figure 3 below),

    where the FIR filters have impulse responses which are obtained from the FIRfilter coefficients held in array b as follows:

    (2)

    where runs over the full extent of the array b. In the end each FIR filter has coeffi-cients that are a decimated and time shifted version of the original FIR coefficients, and allthe coefficients are unique across the M filters. The filter design and corresponding coeffi-cients can come from an FIR design tool such as:

    In the example above the filter order is low because the transition around the desired cutoffruns from 0.8 to 1.2 of or . For this problem the transition width is OK, butyou may want to experiment on your own with smaller values.

    Your job is to complete the class below that has an init method or constructor and a filteringmethod:

    M

    FIR Lowpass

    c M=Mx n w n

    b array holds FIRcoefficients

    y n

    Keep every Mth sample

    Figure 2: A decimate by M system implemented as a cascade of a lowpass anda downsampler.

    H z

    E0 z( )

    E1 z( )

    EM 1 z( )

    . . .

    x n[ ]

    w n[ ]

    z1

    z1

    z1

    M

    M

    M

    . . .

    Apply the Downsampling Identity

    Figure 3: Polyphase decimate by M where the filter coefficients arerelated to the FIR coefficients of by decimation with index phase shifting.

    Ei z H z

    Ei z ei n

    ei n b nM i+ i 0 M 1= =

    n Ei z

    c 20% 20%

    Problems 6

  • ECE 5650/4650 Python Project 1

    class polyphase_deci(object): """ Polyphase decimator built from the ground up Mark Wickert October 2017 """ def __init__ (self,b,M): """ Initialize the object with decimated filter coefficient and array of sample-by-sample FIR filtering object

    Mark Wickert October 2017 """ self.b = b self.M = M

    Nb = int(ceil(len(b)/M)) # E is a 2D array used to hold