#9729: Moving the discrete fourier interact from wiki into the library.
--------------------------------+-------------------------------------------
Reporter: punchagan | Owner: itolkov, jason
Type: enhancement | Status: needs_review
Priority: minor | Milestone:
Component: interact | Keywords:
Author: Puneeth Chaganti | Upstream: N/A
Reviewer: | Merged:
Work_issues: |
--------------------------------+-------------------------------------------
Comment(by pang):
The data from the FFT seems very difficult to read, in my opinion. From
the documentation of Fourier.fft:
{{{
The packing of the result is "standard": If A = fft(a, n), then A[0]
contains the zero-frequency term, A[1:n/2+1] contains the
positive-frequency terms, and A[n/2+1:] contains the negative-
frequency
terms, in order of decreasingly negative frequency. So for an 8-point
transform, the frequencies of the result are [ 0, 1, 2, 3, 4, -3, -2,
-1]
}}}
This suggests that the second half of the array should go before the first
half. Also, for many common examples all but a few Fourier coefficients
are zero, and it could help visualize them if we plot only the non-zero
ones.
The labels on the x axis do not correspond to the coefficients, but this
can be fixed.
Also, I think a bar plot is more adequate than a line plot. You can
compare the result of the original code and the proposed changes in the
attached png files.
Last but not least, abs(myfft[j])==abs(my_fft[-j]) for all real functions,
so if only real functions are going to be used, we should only plot one
half: what was the intention?
I suggest something along the lines of:
{{{
def plot_bars(ls, lenght = 1):
return (sum(polygon2d([(x-lenght/2,0),(x+lenght/2,0),
(x+lenght/2,y),(x-lenght/2,y)])
for x,y in ls) +
sum(line2d([(x-lenght/2,0),(x+lenght/2,0),
(x+lenght/2,y),(x-lenght/2,y),(x-lenght/2,0)],
rgbcolor=(0,0,0))
for x,y in ls))
import scipy.fftpack as Fourier
var('x')
@interact
def discrete_fourier(f = input_box(default=sum([sin(k*x) for k in
range(1,5,2)])),
treshold = slider(0,0.1,0.01,0.01)):
pbegin = -float(pi)
pend = float(pi)
html("<h3>Function plot and its discrete Fourier transform</h3>")
show(plot(f, pbegin, pend, plot_points = 512), figsize = [4,3])
f_vals = [f(ind) for ind in srange(pbegin, pend,(pend-pbegin)/512.0)]
my_fft = Fourier.fft(f_vals)
ls = ([(j, abs(my_fft[j])) for j in
range(-floor(L/2)+1,floor(L/2)+1)])
M = max(v for j,v in ls)
max_index = max(j for j in range(L) if ls[j][1] > treshold*M)
min_index = min(j for j in range(L) if ls[j][1] > treshold*M)
show(plot_bars(ls[max(min_index-5,0):min(max_index+5,L)]), figsize =
[4,3])
}}}
Note: "scale" no longer works, but if you like the suggestions, it
shouldn't be hard to recover.
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/9729#comment:3>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en.