Sie sind auf Seite 1von 24

GS434 Spring 98

Lab Exercise I:
Introduction to Matlab & Fourier Analysis
Due 1/27
Solutions
1. Compute and plot a simple sinusoid of amplitude 1 and frequency f=1 for
0<t<1,
i.e. y= sin(2ft)
I wrote an m- file called sinusoid.m to compute and plot a simple sinusoid:
%An M file to compute and plot a simple sinusoid
freq =1;
T= 1/f;
npts=10;
n = npts-1;
dt = T/n
t=0:dt:T;
y=sin(2*pi*f*t);
plot(t,y, '*')
What time step did you use to digitize the function? Why?
I originally selected dt to give me 10 pts between 0 and T. The resulting plot
l ooked l i ke:
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
10 pts
10 pts.
Which wasnt so bad.
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 visually the plot loses all real definition below npts = 6. Note
however, that according to the sampling theorem that we should be able to
get by with only 3 pts.
2. Compute and plot a complex sinusoidal function consisting of the sum of 5
sine waves with equal amplitudes but whose frequencies are 1,3,5,10, and
20, 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 composite
sinusoids:
%sinsum
% compute, combine and plot a set of simple sinusoids
% first I load the desired frequencies and associated weights
f1=1;
f2=3;
f3=5;
f4=10;
f5=20;
% now I choose my sampling frequency to be equal to 10 times the highest
frequency
fsamp = 10*f5
dt = 1/fsamp
nsamp=2*pi/dt
% now I subsample (if required) by a factor of sub
sub=1
dtsub=sub*dt
%set max t to compute
tmax = 2*pi
%now compute my time variables
t=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 sinusoids
plot(t,y1,t,y2,t,y3,t,y4,t,y5,t,yall)
title(['dt=',num2str(dtsub)])
The resulting plot looked like:
0 1 2 3 4 5 6 7
-2
0
2
4
6
8
10
12
dt=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
12
dt=0.01
Since I oversampled greatly in the first place, my resampling by a factor of two
didnt 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
12
dt=0.05
Now I have really aliased the two higher frequencies (to 0 as a matter of fact).
To look at this from another perspective, I also resampled by a factor of 8 to
get:
0 1 2 3 4 5 6 7
-2
0
2
4
6
8
10
12
dt=0.04
This shows more clearly how the higher frequencies look like lower
frequencies.
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 simple
box function on the interval - to +:
y(t) = 0 for -<t<-/2
= 1 for -/2<t</2
= 0 for /2<t<
We want to approximate this function by a series of sines and cosines, e.g.

y(t) ~ a0/2 + (a
n
cos(2nft) + b
n
sin(2nft)) (p. 277 of Sheriff and Geldart)
n = 1
For T = 2 the coefficients of the various sines and cosines are given by
/2
a
n
= (1/)
-

y(t) cos(nt)dt = (1/)
-/2
1
.
cos(nt)dt
and /2
b
n
= (1/)
-

y(t) sin(nt)dt = (1/)
-/2
1
.
sin(nt)dt
evaluating the integrals yields
/2
a
n
= (1/) [sin nt/n]
-/2
= (1/n)[sin(n/2)- sin(-n/2)]
or
a
n
= (2/n) sin(n/2)= 2sin(n/2)/n
and /2
b
n
= (1/n) [-cos(n/2)]
-/2
= -(1/n)[cos(n/2)-cos(-n/2)]
or
b
n
= 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:
a
0
= 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
a
0
= (1/)
-/2
1
.
cos(0)dt = (1/)
-/2
1
.
1
.
dt = (1/) (/2-(-/2))= 1
a
1
= 2 sin(/2)/ = 2/
a
2
= 2 sin()/2 = 0
a
3
= 2 sin(3/2)/3 = -2/3
a
4
= 2 sin(2)/4 = 0
a
5
= 2 sin(5/2)/5 = 2/5
so the first five terms of the Fourier Series are
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 weights
f0=0; w0=1
f1=1; w1=2/pi
f2=2; w2=0
f3=3; w3=-2/(3*pi)
f4=4; w4=0
f5=5; w5=2/(5*pi)
% now I choose my sampling frequency to be equal to 5 times the
highest frequency
fsamp = 5*f5
dt = 1/fsamp
nsamp=2*pi/dt
% now I subsample (if required) by a factor of sub
sub=1
dtsub=sub*dt
%set max and min t to compute
tmin = -4*pi
tmax = 4*pi
%now compute my time variables
t=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 sinusoids
plot(t, y0, '.',t,y1,'.',t,y2,'.',t,y3,'.',t,y4,'.',t, y5, '.', t,yall,'-')
title(['dt=',num2str(dtsub)])
-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
1
dt=0.04
What assumptions have you made about the behavior of y(t) outside these
limits?
I have assumed that it is periodic with period = 2., i.e.
-15 -10 -5 0 5 10 15
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
dt=0.04
4. Digitize the box function with dt = 0.05. Compute the Fast Fourier
Transform of the function in 3. Plot the resulting amplitude and phase
spectra as a function of frequency. The amplitude spectrum is usually
defined as (ReY
f
2
+ImY
f
2
), where ReY
f
is the real part of the Fourier
Transform Y
f
and Im Y
f
is the imaginary part of the Fourier Transform Yf.
Likewise the phase spectrum can be given as -arctangent (ImY
f
/ReY
f
)
I used boxcar.m to digitize the boxcar.
%boxcar
%Generates a digital boxcar between - and + , then computes spectra
tmin = -pi
tmax = pi
dt = 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 values
npts = 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
y1 = zeros(1,31);
y2 = ones (1,64);
y3 = zeros (1,31);
%combine the three sections
aboxcar = [y1,y2,y3];
%sb = size(aboxcar)
%plot aboxcar
plot(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 aboxcar
fftbox = 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 plot
constant = .001
ampspec = ampspec+constant
figure
semilogy(f,ampspec)
figure
plot (f,phasespec)
axis ([0 df*(nfpts-1) -1 1])
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
10
0
10
1
10
2
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 phase
spectrum is not zero (i.e. no imaginary component) since this should be an
even (symmetric function).
I recomputed the boxcar by leaving off the last term (e.g. y3 = zeros (1,30)) and
found the following:
-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
10
0
10
1
10
2
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 the
phase spectrum.
Note that the Fourier Transform can be recovered from the amplitude and
phase spectrum by the relationship:
Xj = |Xj|
.
e
i(j)
where |Xj|

is the amplitude spectrum
and (j) is the phase spectrum.
5. Compute the Inverse Fourier Transform of the result in 4.
Here I will use the latter (even) version to invert for the original. Ill
concatenate the file boxback with the boxcar:
%boxback
%when run after boxcar it uses ifft to get the boxcar back from fftbox
aboxcar = ifft(fftbox)
%plot aboxcar
plot(t,aboxcar,'*')
axis([-pi pi 0 2])
which yields
-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
How does it compare to your original function?
Well this looks OK, BUT.......... there is a snake in the garden here. In
plotting the boxcar from its fft, I used the original series t to label the time
axis. But is this correct? NO! If we were given the time series aboxcar as
computed 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. Thus
the time function that corresponds to the fftbox is really:
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<x<2.
How does it compare with your original function?
Now this is a bit trickier, since the ifft is preprogrammed to only give you
back the interval which you used in the first place.
First lets take a look the equation for the inverse Fourier transform
N-1
x
k
= (1/N) X
j
e
(+2ikj/N)
j=0
Note that although the summation is limited to the interval 0 to N-1, there is
NO limit on the time index I. Although we normally compute values of x
i
for k = 0 to N-1 (which in this case corresponds to the time interval - to
+), there is no reason we couldnt calculate the inverse for any time
interval. In this case we want the interval -2 t +2, or k going from -2N
to +2N-1.
I used slowft.m to do this computation. Note that this program can be used to
compute both forward and inverse transforms, since the only difference
between the two is the sign of the exponent and the normalizing factor
1/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 series
N=npts
%series=aboxcar
series=fftbox
%select transform with flag: flag = 1 for forward, = -1 for reverse
flag = -1
%set factor = 1 for forward, 1/N for reverse transform
%factor = 1
factor = 1/N
w=exp(-2*pi*flag*i/N)
echo off
for 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 spectra
if flag ==1
%compute frequence step and series corresponding to dt
df = 1/(N*dt)
f=0:df:(N-1)*df
ampspec = 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 plot
constant = .001
ampspec = ampspec+constant;
figure
semilogy(f,ampspec)
figure
plot (f,phasespec)
axis ([0 df*(N-1) -1 1])
end
%if inverse transform, plot boxcar
if flag == -1
t=0:dt:(N-1)*dt
plot(t,sft,'*')
end
I used the version of slowft above to make sure it was working by
recomputing 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 little
monkeying around with the code since matlab does not like zero or
negative indices. The version of slowft I used to compute the following
figure was:
%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 series
N=npts
%series=aboxcar
series=fftbox
%select transform with flag: flag = 1 for forward, = -1 for reverse
flag = -1
%set factor = 1 for forward, 1/N for reverse transform
%factor = 1
factor = 1/N
w=exp(-2*pi*flag*i/N)
echo off
%add shift to allow computation over larger time interval
shift = N
for 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 on
sft
%if forward transform, compute and plot spectra
if flag ==1
%compute frequence step and series corresponding to dt
df = 1/(N*dt)
f=0:df:(N-1)*df
ampspec = 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 plot
constant = .001
ampspec = ampspec+constant;
figure
semilogy(f,ampspec)
figure
plot (f,phasespec)
axis ([0 df*(N-1) -1 1])
end
%if inverse transform, plot boxcar
if flag == -1
t=-(N-1)*dt:dt:(N-1)*dt
plot(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 the
Fourier transfrom presumes that our original function, as well as the
resulting transform is periodic. Moreover, since the indices in the
conventional 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 we
thought we were digitizing the boxcar starting at t= -, the fft assumes
wer were starting at t=0. Thus there is a time shift of - between the
funcction we wanted, and what the fft assumed we were giving it.
What is the effect of shifting our function along the time axis? If we replace k
in the equation for the Fourier transform with (k-k
shift
) we get
N-1
Xj

= x
k
e
(-2i(k-kshift)j/N)
k=0
N-1
= e
(-2ij(kshift)/N)
x
k
e
(-2ikj/N)
= e
s
Xj
k=0
That is a shift on the time axis results in a phase shift in the Fourier
transform., i.e.
Xj = |Xj|
.
e
i((j)- s)
By the way, there is another approach to computing Fourier transforms using
matrices:
First lets look at an explicit term in x
k
, say x
2
:
x
2
= (1/N) (X
0
e
(-2i(2)(0)/N)
+

X
1
e
(-2i(2)(0)/N)
+ X
2
e
(-2i(2)(2)/N)
+
........................+ X
N-1
e
(-2i(2)(N-1)/N)
This is just the multiplication of the column vector X = (X
0,
X
1,
X
2,
... X
N-1
)
with a vector W
2j
= (e
(-2i(2)(0)/N)
+ e
(-2i(2)(1)/N)
+....e
(-2i(2)(N-1)/N)
)
or W
2j
= e
(-2i2j/N)
.
To simplify the notation, let w =

e
(-2i/N)
, then W
2j
= w
2j
We can expand W by adding a row for each value of k, resulting in a matrix:
W
kj
= w
kj =
w
00
w
01

w
02

w
03
........
w
0N-1
w
10

.................................w
1N-1
.
.
.
w
(N-1)0
..........................
w
(N-1)(N-1)
Now the Fourier Transform can be written in matrix form as:
x
k
= W
kj

.
X
j
mft.m is a modfication of slowft.m Fourier Transform program that uses
such 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 series
N=npts
series=aboxcar
%select transform with flag: flag = 1 for forward, = -1 for reverse
flag = 1
%set factor = 1 for forward, 1/N for reverse transform
factor = 1
%factor = 1/N
w=exp(-2*pi*flag*i/N)
echo off
for k = 1:N
for j = 1:N;
w1(k,j)=w^((j-1)*(k-1));
end;
end;
echo on
sft= w1*series'
%if forward transform, compute and plot spectra
if flag ==1
%compute frequence step and series corresponding to dt
df = 1/(N*dt)
f=0:df:(N-1)*df
ampspec = 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 plot
constant = .001
ampspec = ampspec+constant;
figure
semilogy(f,ampspec)
figure
plot (f,phasespec)
axis ([0 df*(N-1) -1 1])
end
%if inverse transform, plot boxcar
if flag == -1
t=-(N-1)*dt:dt:(N-1)*dt
plot(t,sft,'*')
end
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 interval from 0 to 2 for both the original and
the 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 2 is therefore given as
% 1 for 0<t</2
% 0 for /2<t<3/2
% 1 for 3/2<t<2
%
%aboxcar2 1 is defined to be = 1 from -/4 to /4
%Its replica between 0 and 2 is therefore given as
% 1 for 0<t</4
% 0 for /2<t<3/4
% 1 for 3/4<t<2
dt = 0.05
t = 0:dt:2*pi-dt ;
npts = length(t)
% from inspection i = 32 and i = 95 correspond to /2 and +3p/2
% and i = 16 and i = 110 correspond to /4 and 3/4
% thus each sequence has three sections filled by zeros or ones as
appropriate
y1 = ones(1,31);
y2 = zeros (1,64);
y3 = ones (1,30);
y4 = ones(1,16);
y5 = zeros (1,94);
y6 = ones (1,15);
%combine the appropriate sections to form aboxcar1 and aboxcar2
aboxcar1 = [y1,y2,y3];
aboxcar2 = [y4,y5,y6];
%plot boxcars
plot(t,aboxcar1,'*',t,aboxcar2,'-r')
axis([0 2*pi 0 2])
The resulting boxcars look like:
0 1 2 3 4 5 6
0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
Note that I got the time interval correct this time, i.e. the portion of the
periodic function defined between 0 and 2.
It turns out that the Fourier transforms for both of these functions are real, as
we would expect for these even (symmetric functions). Since the imaginary
parts are zero, the phase spectra of both are zero and the amplitude spectra is
just equal to the real part of each. First lets plot the real part of the amplitude
spectra alone:
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 sinc function (as
expected!). 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 took
the absolute value of each transform and plotted on a semilog scale):
0 2 4 6 8 10 12 14 16 18 20
10
-2
10
-1
10
0
10
1
10
2

Das könnte Ihnen auch gefallen