Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window Welch periodogram with Hann window by default. #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions doc/periodograms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,27 @@
equal to the uncentered variance (or average power) of the original
signal.

.. function:: welch_pgram(s, n=div(length(s), 8), noverlap=div(n, 2); onesided=eltype(s)<:Real, nfft=nextfastfft(n), fs=1, window=nothing)

Computes the Welch periodogram of a signal ``s`` based on segments with ``n`` samples
with overlap of ``noverlap`` samples, and returns a Periodogram
object. For a Bartlett periodogram, set ``noverlap=0``. See
:func:`periodogram` for description of optional keyword arguments.
.. function:: welch_pgram(s, n=div(length(s), 8), noverlap=div(n, 2); onesided=eltype(s)<:Real, nfft=nextfastfft(n), fs=1, window=hanning)

Computes the Welch periodogram of a signal ``s`` by dividing the signal
into overlapping segments of length ``n``, averaging the periodograms of
each segment, and returning a ``Periodogram`` object. ``noverlap`` is the
number of samples of overlap, with a new segment starting every
``n-noverlap`` samples of ``s``. Each segment is padded with zeros to a
length of ``nfft`` for efficiency.

By default, the signal is windowed using the hanning window as a moderate
tradeoff between high spectral resolution (the square window) and
sensitivity to signals near the noise floor. A window which falls to zero
also reduces the spurious ringing which occurs when ``n!=nfft`` for signals
with large mean.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems misleading - unless I'm misunderstanding something, using a rectangular window should give poor spectral resolution (lots of spectral leakage between bins) vs. the hanning, which should have less leakage. I suppose it depends on whether you mean main lobe width or energy in the sidelobes, but the sidelobes of the rectangular window fall off slowly enough that I definitely wouldn't consider it to have high spectral resolution.

a rectangular window will give you the same sort of ringing if the beginning of the windowed signal differs from the end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I honestly can't remember quite what I was thinking at the time I wrote this, or even the precise detail of what I was doing with this code.

I do know that the default caused me some confusion, and that some discussion of the inherent tradeoffs between different windows would be useful.

Feel free to reword it as you please, or close the PR.


Compared to ``periodogram()``, ``welch_pgram()`` reduces noise in the
estimated power spectral density as ``length(s)`` increases. In the basic
periodogram, we increase the frequency resolution instead. For a Bartlett
periodogram, set ``noverlap=0``.

See :func:`periodogram` for a description of the other keyword arguments.

.. function:: mt_pgram(s; onesided=eltype(s)<:Real, nfft=nextfastfft(n), fs=1, nw=4, ntapers=iceil(2nw)-1, window=dpss(length(s), nw, ntapers))

Expand Down
2 changes: 1 addition & 1 deletion src/periodograms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ forward_plan{T<:Union{Complex64, Complex128}}(X::AbstractArray{T}, Y::AbstractAr
function welch_pgram{T<:Number}(s::AbstractVector{T}, n::Int=length(s)>>3, noverlap::Int=n>>1;
onesided::Bool=eltype(s)<:Real,
nfft::Int=nextfastfft(n), fs::Real=1,
window::Union{Function,AbstractVector,Void}=nothing)
window::Union{Function,AbstractVector,Void}=hanning)
onesided && T <: Complex && error("cannot compute one-sided FFT of a complex signal")
nfft >= n || error("nfft must be >= n")

Expand Down