Consider the following code:
function periodogram(x::Array)
n = length(x)
I_w = abs(fft(x)).^2 ./ n
w = 2pi * [0:n-1] ./ n # Fourier frequencies
# int rounds to nearest integer. We want to round up or take 1/2 + 1 to
# make sure we get the whole interval from [0, pi]
ind = iseven(n) ? int(n / 2 + 1) : int(n / 2)
w, I_w = w[1:ind], I_w[1:ind]
return w, I_w
end
function periodogram(x::Array; window::String="hanning", window_len::Int=7)
w, I_w = periodogram(x)
I_w = smooth(I_w, window_len=window_len, window=window)
return w, I_w
end
I would like the interface to work as follows:
- If I have an array x (say x = rand(40)) and I call periodogram(x) I
would like just the first function to run
- If I pass either the window or window_len keyword arguments I would
like to second function to run.
The problem is that keyword arguments are treated differently and when I
call periodogram(x) it jumps immediately into the second function and gets
stuck in an infinite recursion on the first line of that function and
results in a stack overflow.
I can think of a few ways around this problem, but I wanted to ask the
group if anyone has a good way to resolve this problem.
Also, if anyone can explain how keyward arguments are handled when it comes
to method dispatch that would be great.
Thanks.