lab1_soln.pdf

Upload: juan-carlos-merchan

Post on 02-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 lab1_soln.pdf

    1/24

    GS434 Spring 98Lab Exercise I:Introduction to Matlab & Fourier AnalysisDue 1/27

    Solutions

    1. Compute and plot a simple sinusoid of amplitude 1 and frequency f=1 for0

  • 8/11/2019 lab1_soln.pdf

    2/24

    What is the minimum time step you would use to represent this function?Plot the function with this time step.

    I tried several values for npts from 8 to 3, for example

    0 0.1

    0.2

    0.3 0.4 0.5

    0.6

    0.7

    0.8

    0.9

    1

    -1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    0 0.1 0.2 0.3

    0.4

    0.5 0.6

    0.7

    0.8 0.9 1

    -1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.

    2

    0.

    4

    0.6

    0.

    8

    1

    0 0.

    1

    0.

    2

    0.

    3

    0.4 0.5 0.6 0.

    7

    0.

    8

    0.

    9

    1

    -

    1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.

    4

    0.

    6

    0.

    8

    1

    0 0.1 0.2 0.3

    0.4

    0.5

    0.6

    0.7

    0.8

    0.9

    1

    -1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.

    2

    0.4

    0.

    6

    0.

    8

    1

    n=10 n=6

    n=4 n=3

    At least visual ly the plot loses al l real de fini tion be low npts = 6. Note

    however, that according to the sampling theorem that we should be able toget by with only 3 pts.

    2. Compute and plot a complex sinusoidal function consisting of the sum of 5sine waves with equal amplitudes but whose frequencies are 1,3,5,10, and20, again for t varying from 0 to 2.

    What time step did you use this time? Why?

    Based on the experience above, I decided to use at 10 pts per shortest period

    involved., i.e. Tshort = 1/20 = 0.05 or dt = 0.005.

    I made an m file called fseries.m to plot the individual and compositesinusoids:

    %sinsum% compute, combine and plot a set of simple sinusoids% first I load the desired frequencies and associated weightsf1=1;f2=3;

  • 8/11/2019 lab1_soln.pdf

    3/24

    f3=5;f4=10;f5=20;% now I choose my sampling frequency to be equal to 10 times the highest

    frequency

    fsamp = 10*f5dt = 1/fsampnsamp=2*pi/dt% now I subsample (if required) by a factor of subsub=1dtsub=sub*dt%set max t to computetmax = 2*pi%now compute my time variablest=0:dtsub:tmax;%compute sinusoids and offset each one

    y1=sin(2*pi*f1*t);y2=sin(2*pi*f2*t)+2;y3=sin(2*pi*f3*t)+4;y4=sin(2*pi*f4*t)+6;y5=sin(2*pi*f5*t)+8;yall=(y1+y2+y3+y4+y5-20)/5+10;%plot sinusoidsplot(t,y1,t,y2,t,y3,t,y4,t,y5,t,yall)title(['dt=',num2str(dtsub)])

    The resulting plot looked like:

  • 8/11/2019 lab1_soln.pdf

    4/24

    0 1 2 3 4 5 6 7-2

    0

    2

    4

    6

    8

    10

    12dt=0.005

    Repeat the plot with a time step twice as large.

    0 1 2 3 4 5 6 7-2

    0

    2

    4

    6

    8

    10

    12dt=0.01

  • 8/11/2019 lab1_soln.pdf

    5/24

    Since I oversampled greatly in the first place, my resampling by a factor of twodidnt do much damage.

    Repeat with a time step 10 times as large.

    0 1 2 3 4 5 6 7-2

    0

    2

    4

    6

    8

    10

    12dt=0.05

    Now I have really aliased the two higher frequencies (to 0 as a matter of fact).

  • 8/11/2019 lab1_soln.pdf

    6/24

    To look at this from another perspective, I also resampled by a factor of 8 toget:

    0 1 2 3 4 5 6 7-2

    0

    2

    4

    6

    8

    10

    12dt=0.04

    This shows more clearly how the higher frequenci es look like lower

    fr equencie s.

    3. Compute and plot the first 5 terms of the Fourier series representation(page 277 in text), both individually and as a cumulative sum, for a simplebox function on the interval -to +:

    y(t) = 0 for -

  • 8/11/2019 lab1_soln.pdf

    7/24

    and /2

    bn= (1/)-y(t) sin(nt)dt = (1/)

    -/21 .sin(nt)dt

    evaluating the integrals yields

    /2an= (1/) [sin nt/n]-/2

    = (1/n)[sin(n/2)- sin(-n/2)]

    or

    an= (2/n) sin(n/2)= 2sin(n/2)/n

    and /2bn= (1/n) [-cos(n/2)]

    -/2= -(1/n)[cos(n/2)-cos(-n/2)]

    or

    bn= 1(1//n) [cos(n/2)-cos(n/2)] = 0 for all n

    That is there are now sinusoidal contributions, only co-sinusoidal.This is not surprising since the function we are trying to fit is symmetric (or

    even).

    By explicit evaluation, the first five coefficients are:

    a0= 2 sin(0)/0 = oops, this one gets a little tricky. If we refer to p. 531 we see

    that we can back up a step and reduce the integral for n =0 to /2 /2

    a0= (1/) -/21.cos(0)dt = (1/)

    -/21 .1.dt = (1/) (/2-(-/2))= 1

    a1= 2 sin(/2)/= 2/

    a2= 2 sin()/2= 0

    a3= 2 sin(3/2)/3= -2/3

    a4= 2 sin(2)/4= 0

    a5= 2 sin(5/2)/5= 2/5

    so the first five terms of the Fourier Series are

  • 8/11/2019 lab1_soln.pdf

    8/24

    y(t) ~ 1/2 + (2/) cost - (3/2) cos3t + (2/5) cos5t

    I can now use coseries.m to compute and plot the sum of these, i.e.

    %coseries

    % compute, combine and plot a set of weighted cosines% first I load the desired frequencies and associated weightsf0=0; w0=1f1=1; w1=2/pif2=2; w2=0f3=3; w3=-2/(3*pi)f4=4; w4=0f5=5; w5=2/(5*pi)% now I choose my sampling frequency to be equal to 5 times thehighest frequencyfsamp = 5*f5

    dt = 1/fsampnsamp=2*pi/dt% now I subsample (if required) by a factor of subsub=1dtsub=sub*dt%set max and min t to computetmin = -4*pitmax = 4*pi%now compute my time variablest=tmin:dtsub:tmax;%compute sinusoids and offset each one

    y0=w0*cos(f0*t);y1=w1*cos(f1*t);y2=w2*cos(f2*t);y3=w3*cos(f3*t);y4=w4*cos(f4*t);y5=w5*cos(f5*t);yall=(y1+y2+y3+y4+y5);%plot sinusoidsplot(t, y0, '.',t,y1,'.',t,y2,'.',t,y3,'.',t,y4,'.',t, y5, '.', t,yall,'-')title(['dt=',num2str(dtsub)])

  • 8/11/2019 lab1_soln.pdf

    9/24

    -4 -3 -2 -1 0 1 2 3 4-0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1dt=0.04

    What assumptions have you made about the behavior of y(t) outside theselimits?

    I have assumed that it is periodic with period = 2., i.e.

  • 8/11/2019 lab1_soln.pdf

    10/24

    -15 -10 -5 0 5 10 15-0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1dt=0.04

    4. Digitize the box function with dt = 0.05. Compute the Fast FourierTransform of the function in 3. Plot the resulting amplitude and phasespectra as a function of frequency. The amplitude spectrum is usually

    defined as (ReYf2+ImYf2), where ReYfis the real part of the FourierTransform Yf and Im Yfis the imaginary part of the Fourier Transform Yf.Likewise the phase spectrum can be given as -arctangent (ImYf/ReYf)

    I used boxcar.m to digitize the boxcar.

    %boxcar%Generates a digital boxcar between -and + , then computes spectratmin = -pitmax = pidt = 0.05

    t = -pi:dt:pi-dt;% now I need to find out what indices correspond to -pi/2 and +pi/2% I'll use brute force and just look at the valuesnpts = length(t)%pi/2%t% from inspect i = 32 and i = 95 correspond to -pi/2 and +pi/2% thus the sequence has three sections, from i = 1 to 31 filled by% zeroes, from 32 to 95 filled by one's, and from 96 to 126 filled% by zeroes again

  • 8/11/2019 lab1_soln.pdf

    11/24

    y1 = zeros(1,31);y2 = ones (1,64);y3 = zeros (1,31);%combine the three sectionsaboxcar = [y1,y2,y3];

    %sb = size(aboxcar)%plot aboxcarplot(t,aboxcar,'*')axis([-pi pi 0 2])

    -3 -2 -1 0 1 2 3

    0

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    I use fftbox.m to compute the amplitude spectrum of the boxcar above:

    %fft of boxcar% take fft of aboxcarfftbox = fft(aboxcar)nfpts = length(fftbox)df = 1/(npts*dt)j=0:nfpts-1;f=j*df;ampspec = sqrt(real(fftbox).*real(fftbox) + imag(fftbox).*imag(fftbox))phasespec = -atan(imag(fftbox)./real(fftbox))%plot ampspec on log scale%first let's add a constant to avoid "holes" in the log plotconstant = .001ampspec = ampspec+constantfiguresemilogy(f,ampspec)figureplot (f,phasespec)axis ([0 df*(nfpts-1) -1 1])

  • 8/11/2019 lab1_soln.pdf

    12/24

    0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05

    10-3

    10-2

    10-1

    100

    101

    102

    Amplitude Spectrum

    0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05

    -2

    -1.5

    -1

    -0.5

    0

    0.5

    1

    1.5

    2

    Phase Spectrum

    While the amplitude spectrum looks ok, Im surprised that the phasespectrum is not zero (i.e. no imaginary component) since this should be aneven (symmetric function).

    I recomputed the boxcar by leaving off the last term (e.g. y3 = zeros (1,30)) andfound the following:

  • 8/11/2019 lab1_soln.pdf

    13/24

    -3 -2 -1 0 1 2 3

    0

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    \

    0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05

    10-2

    10-1

    100

    101

    102

    0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045-1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    Now this looks more like it. Just dropping 1 point has completely changed thephase sp ec tru m.

    Note that the Fourier Transform can be recovered from the amplitude andphase spectrum by the relationship:

  • 8/11/2019 lab1_soln.pdf

    14/24

    Xj = |Xj|.ei (j)

    where |Xj|is the amplitude spectrumand (j) is the phase spectrum.

    5. Compute the Inverse Fourier Transform of the result in 4.

    Here I wi ll use the la tter (even) vers ion to invert for the orig inal . I llconcatenate the file boxback with the boxcar:

    %boxback%when run after boxcar it uses ifft to get the boxcar back from fftboxaboxcar = ifft(fftbox)%plot aboxcarplot(t,aboxcar,'*')axis([-pi pi 0 2])

    which yields

    -3 -2 -1 0 1 2 30

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    How does it compare to your original function?

    Well this looks OK, BUT.......... t here is a snake in t he garden here. In

    plotting the boxcar from its ff t, I used the original series t to labe l the timeaxis. But is this correct? NO! If we were given the time series aboxcar ascomputed from the ifft, it would correspond to implict indices i = 0.... N-1.

    The corresponding time axes would therefore go from 0 to (N-1) .t. Thusthe time function that corresponds to the fftbox is really:

  • 8/11/2019 lab1_soln.pdf

    15/24

    0 1 2 3 4 5 6 7

    0

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

    0.8

    0.9

    1

    Note that t here goes from 0 to 2!

    So where did we go astray?

    The answer is in the next problem:

    6. Plot the Inverse Fourier Transform in 5 for -2

  • 8/11/2019 lab1_soln.pdf

    16/24

    between the two is the sign of the exponent and the normalizing factor1/N:

    %forward or inverse fourier transform%select series to transform (i.e. either boxcar or fftbox, comment out

    one of the following)%series = [4 2] used for debugging%set sample interval%dt=0.5%set number of points in seriesN=npts%series=aboxcarseries=fftbox%select transform with flag: flag = 1 for forward, = -1 for reverseflag = -1%set factor = 1 for forward, 1/N for reverse transform

    %factor = 1factor = 1/Nw=exp(-2*pi*flag*i/N)echo offfor k = 1:N;

    for j = 1:N; w1(j)=w^((j-1)*(k-1));end;

    sft(k) = (factor)*sum(series.*w1);end;

    echo on

    sft%if forward transform, compute and plot spectraif flag ==1%compute frequence step and series corresponding to dtdf = 1/(N*dt)f=0:df:(N-1)*dfampspec = sqrt(real(sft).*real(sft) + imag(sft).*imag(sft));phasespec = -atan(imag(sft)./real(sft));%plot ampspec on log scale%first let's add a constant to avoid "holes" in the log plotconstant = .001

    ampspec = ampspec+constant;figuresemilogy(f,ampspec)figureplot (f,phasespec)axis ([0 df*(N-1) -1 1])end%if inverse transform, plot boxcarif flag == -1

    t=0:dt:(N-1)*dt

  • 8/11/2019 lab1_soln.pdf

    17/24

    plot(t,sft,'*')end

    I used the version of slowft above to make sure it was working byrecomputing both the fourier transform of the boxcar, and the inverse

    boxcar.To compute the inverse of fftbox for the larger interval, I had to do a littlemonkeying around with the code since matlab does not like zero ornegative indices. The version of slowft I used to compute the followingfigure was:

    %forward or inverse fourier transform%select series to transform (i.e. either boxcar or fftbox, comment outone of the following)%series = [4 2] used for debugging%set sample interval

    %dt=0.5%set number of points in seriesN=npts%series=aboxcarseries=fftbox%select transform with flag: flag = 1 for forward, = -1 for reverseflag = -1%set factor = 1 for forward, 1/N for reverse transform%factor = 1factor = 1/Nw=exp(-2*pi*flag*i/N)

    echo off%add shift to allow computation over larger time intervalshift = Nfor k = 1:2*N-1

    for j = 1:N; w1(j)=w^((j-1)*(k-shift));end;

    sft(k) = (factor)*sum(series.*w1);end;

    echo onsft

    %if forward transform, compute and plot spectraif flag ==1%compute frequence step and series corresponding to dtdf = 1/(N*dt)f=0:df:(N-1)*dfampspec = sqrt(real(sft).*real(sft) + imag(sft).*imag(sft));phasespec = -atan(imag(sft)./real(sft));%plot ampspec on log scale%first let's add a constant to avoid "holes" in the log plotconstant = .001

  • 8/11/2019 lab1_soln.pdf

    18/24

    ampspec = ampspec+constant;figuresemilogy(f,ampspec)figureplot (f,phasespec)

    axis ([0 df*(N-1) -1 1])end%if inverse transform, plot boxcarif flag == -1

    t=-(N-1)*dt:dt:(N-1)*dtplot(t,sft,'*')

    end

    The resulting boxcar is:

    -8 -6 -4 -2 0 2 4 6 8

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    Now the cause of our problem becomes clear. Note that discrete from of theFourier transfrom presumes that our original function, as well as theresulting transform is periodic. Moreover, since the indices in theconventional formulation usually vary from 0 to N-1, it presumes we are

    inputing the first cycle corresponding to t = 0 to (N-1)dt. Thus while wethought we were digitizing the boxcar starting at t= -, the fft assumeswer were starting at t=0. Thus there is a time shift of -between thefuncc tion we wanted, and what the ff t as sumed we were giving it .

    What is the effect of shifting our function along the time axis? If we replace kin the equation for the Fourier transform with (k-kshift) we get

    N-1

  • 8/11/2019 lab1_soln.pdf

    19/24

    Xj= xke(-2i(k-kshift)j/N) k=0

    N-1

    = e(-2ij(kshift)/N)

    xke(-2ikj/N)

    = e s

    Xj k=0

    That is a shift on the time axis results in a phase shift in the Fouriertransform., i.e.

    Xj = |Xj|.ei( (j)- s)

    By the way, there is another approach to computing Fourier transforms usingmatrices:

    First lets look at an explicit term in xk, say x2 :

    x2 = (1/N) (X0e(-2i(2)(0)/N) +X1e

    (-2i(2)(0)/N)+ X2e(-2i(2)(2)/N)+

    ........................+ XN-1e(-2i(2)(N-1)/N)

    This is just the multiplication of the column vector X = (X0,X1,X2,... XN-1)

    with a vector W2j= (e(-2i(2)(0)/N) + e(-2i(2)(1)/N) +....e(-2i(2)(N-1)/N))

    or W2j = e(-2i2j/N).

    To simplify the notation, let w =e(-2i/N), then W2j = w2j

    We can expand W by adding a row for each value of k, resulting in a matrix:

    W kj= wkj = w00w01w

    02w

    03........ w

    0N-1

    w10.................................w1N-1

    .

    ..

    w(N-1)0..........................w(N-1)(N-1)

    Now the Fourier Transform can be written in matrix form as:

    xk= Wkj.Xj

  • 8/11/2019 lab1_soln.pdf

    20/24

    mft.m is a modfication of slowft.m Fourier Transform program that usessuch a matric formulation:

    %forward or inverse fourier transform using matrix formulation%select series to transform (i.e. either boxcar or fftbox, comment out

    one of the following)%series = [4 2] used for debugging%set sample interval%dt=0.5%set number of points in seriesN=nptsseries=aboxcar%select transform with flag: flag = 1 for forward, = -1 for reverseflag = 1%set factor = 1 for forward, 1/N for reverse transformfactor = 1

    %factor = 1/Nw=exp(-2*pi*flag*i/N)echo offfor k = 1:N

    for j = 1:N; w1(k,j)=w^((j-1)*(k-1));end;end;

    echo onsft= w1*series'%if forward transform, compute and plot spectra

    if flag ==1%compute frequence step and series corresponding to dtdf = 1/(N*dt)f=0:df:(N-1)*dfampspec = sqrt(real(sft).*real(sft) + imag(sft).*imag(sft));phasespec = -atan(imag(sft)./real(sft));%plot ampspec on log scale%first let's add a constant to avoid "holes" in the log plotconstant = .001ampspec = ampspec+constant;figure

    semilogy(f,ampspec)figureplot (f,phasespec)axis ([0 df*(N-1) -1 1])end%if inverse transform, plot boxcarif flag == -1

    t=-(N-1)*dt:dt:(N-1)*dtplot(t,sft,'*')

    end

  • 8/11/2019 lab1_soln.pdf

    21/24

    7. Truncate your boxcar to be non-zero only between -/4 and +/4. How does this affect the resulting amplitude and phase spectrum?

    In this case, I will use a more appropriate treatment: Ill use the boxcars based

    on the cycle defined on the in terval from 0 to 2for both the original andthe truncated versions. The file boxcar2.m was used to generate both boxcars:

    %boxcar%Generates two digital boxcars both with period 2%%aboxcar1 is defined to be = 1 from -/2 to /2%Its replica between 0 and 2is therefore given as% 1 for 0

  • 8/11/2019 lab1_soln.pdf

    22/24

  • 8/11/2019 lab1_soln.pdf

    23/24

    I

    0 2 4 6 8 10 12 14 16 18 20-20

    -10

    0

    10

    20

    30

    40

    50

    60

    70

    In case you havent yet noticed, this is just our friend the si nc funct i on(asexpected!). Note that the truncated function has a transform which is broader(less resolution) and a higher sidelobe to mainlobe amplitude ratio (Gibbs

    effect).

    Now well look at the amplitude spectra in log form (in this case I simply tookthe absolute value of each transform and plotted on a semilog scale):

  • 8/11/2019 lab1_soln.pdf

    24/24

    0 2 4 6 8 10 12 14 16 18 2010

    -2

    10-1

    100

    101

    102