# When we take an fft of sine that does *not* oscillate
# an integral (1,2,3 etc.) # of times per N points, the
# energy is widely spread around the "true" frequency.
# This is caused by the discontinuity in the signal
# as it "wraps around" In some cases this effect is undesirable.
# Can lessen this "spreading" effect by multiplying by
# a "window" function that decays to 0 toward the endpoints.
N = 1024 # FFT works best with powers of 2
t = seq(from=0,by=2*pi/N,length=N) # N evenly spaced pts 0 -- 2*pi
win = (1 + cos(t-pi))/2
f = 100.3 # number of cycles per N pts
y = cos(f*t); # our "signal"
Y = fft(y) # take the fft
lo = floor(f-20) # examine only fft from lo to hi
hi = ceiling(f+20)
plot(Mod(Y[lo:hi]) / (N/2)) # plot of Modulus = amplitudes (solid line)
y = win*y # our "signal"
plot(y,type='l') # the windowed data
#lines(win) # the window function
Y = fft(y) # take the fft
plot(Mod(Y[lo:hi]) / (N/2),lty=2) # plot of Modulus = amplitudes