Thursday, May 31, 2012

Approximating Continuous Time


Approximating Continuous Time

Summary: Approximating continuous time on a computer. Creating signals, doing things to them like convolution, and plotting them. This development of these labs was supported by the National Science Foundation under Grant No. DUE-0511635. Any opinions, conclusions or recommendations expressed in this material are those of the authors and do not necessarily reflect the views of the National Science Foundation.


Introduction

In MATLAB, we have so far been representing time functions for plotting purposes by storing time samples in a vector. For example, in the Introduction to Matlab lab, we used

t = -10:0.1:10; 
xo = t .* exp(-abs(t));
plot(t,xo);   
            
to plot the time function xo(t)=te(|t|) . We are essentially sampling the signal to obtain a discrete-time representation, since discrete things are easier for computers to handle. We will learn more about sampling later in the course and in EE341, but for now it is important to understand its impact in order to make sense of answers that we get for different MATLAB tools.

Plotting Artifacts

When we use a fine-grained time vector, then the plot looks continuous to our eyes. If the time samples are spaced too far apart, then the signal will look choppy. You can control the time granularity by the step size in defining your time vector. Try plotting the time function above using different time granularities, e.g., time steps of 0.05, 0.1, 0.5, 1. At what step size does the plotted signal start looking choppy?

EXERCISE 1

  1. Create a unitstep function in matlab. The function should have two parameters, a time vector and a time shift value.

    NOTE: 

    Calling unitstep([time],td) should be equivalent to u(t + td)
  2. Use the unitstep function to create a box-shaped time signal. Write a new function called boxt.m that creates a box with specified start and end times t1 and t2. In other words, your function should take three inputs: scalers t1 and t2, and a time vector t, and should output a vector of the same size as t, which contains the values of u(t-t1)-u(t-t2)evaluated at each point in t.
  3. Create a script file called boxtscript.m that uses the function to create a box that starts at time -1 and ends at time 1, given an input time vector that spans the time window [-3,3]. Generate 3 different versions of this box using 3 different time granularities, where the finest granularity has very sharp edges similar to the ideal box and the coarsest granularity has a step size of 0.5.

    NOTE: 

    The different versions should all span the same range; the difference in the plots should only be at the edges of the box.
  4. Add the code to plot all three versions in one figure using subplot in the script file and save it as boxtscript.tif.
  5. Theoretically, this box should be an even function; why is it not even in the coarse grain case? Put your answers in comments of boxt.m.
  6. Show the TA the following files:
    unitstep.m
    boxt.m
    boxtscript.m
    boxtscript.tif
                            

Convolution and Time

MATLAB has a function conv(u,v) that you can use to convolve two discrete time functions u(n) and v(n) .It assumes finite-length signals and ignores time information, assuming that the time steps are the same in both cases and treating them as if the time step is 1. You will learn more about discrete-time convolution in the UW EE 341 class. Here we explore two properties, dealing with time and amplitude:

EXERCISE 2

  1. Time: If u is a vector of length n with time span tu=t1:del:t2 and v is a vector of length m with time span tv=t3:del:t4, and both have the same time step del = Δ , then the result of res=conv(u,v)will be a vector of length n + m - 1 with a time span tc=(t1+t3):del:(t2+t4). Using the box function that you wrote above with a sufficiently fine grained step size, find and plot the result of the convolution of box(0,2) and box(-1,1). Include your name in the title of the plot and save as convplot.tif. Verify that the timing of signal rising and falling matches what you expect in theory.
  2. Amplitude: In the resulting plot from the previous step, you should notice that the amplitude is much higher than the max of 2 that you would expect from analytically computing the convolution. This is because it is thinking that the length of the box is n rather n Δ , which impacts the area computation in convolution. To get the correct height, you need to scale byΔ. Scale and plot the resulting function, and verify that the height is now 2. Save the figure with your name in the title as scaled.tif
  3. Show the TA the following files:
    
    convplot.tif
    scaled.tif
                            

Sums of Cosines

EXERCISE 3

For this problem, create a script file called cosines.m to write your commands in and do the following.
  1. Use dampedCosine.m from the Introduction to Matlab lab as a reference to generate a cosine at a given frequency. Your cosines will not be damped, so remember to remove the exponential. Generate a time index in increments of Ts = 1/8000 second using
     
    Ts = 1/8000; 
    timeIndex = 0:Ts:(100*Ts); 
                            
    Create a cosine corresponding to a frequency of 300 Hz over these 101 time instances.
  2. Create cosines at k multiples of this fundamental frequency, for k = 2, 3, 4. These are called the harmonics of the fundamental frequency.

    NOTE: 

    If you have two signals, say size 1x101, you can add them like you would any number, i.e. signalsAdded = signal1 + signal2.
  3. Generate a figure with a subplot of the fundamental alone and a subplot of the fundamental plus the 3 harmonics. Your plots should be versus time (not versus vector index), so that the highest time point on your plot is at 0.0125 seconds. Save this as cosines.tif.
  4. Show the TA the following files:
    
    cossums.m
    cosines.tif
                            

What to Show the TA

Show the TA ALL m-files that you created or edited and the files below.
unitstep.m
boxt.m
boxtscript.m
boxtscript.tif
convplot.tif
scaled.tif
cosines.m
cosines.tif
            

No comments:

Post a Comment