-
Notifications
You must be signed in to change notification settings - Fork 113
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
add in-place mt_pgram!
, and multitaper spectrogram, multitaper csd, multitaper coherence
#401
add in-place mt_pgram!
, and multitaper spectrogram, multitaper csd, multitaper coherence
#401
Conversation
Thanks for the contribution! Could you comment on how it differs or improves upon the |
Ah yes, it's a spectrogram. Nevermind! |
Made a bit of progress here today: I reworked things to have an I also changed the order to be |
I'll try to take a look this weekend, sorry for the delay. |
No worries! It wasn't really ready yet before anyway. Now is a good time to take a look though, if you get a chance! I'll try to address review comments and add tests on Monday (assuming other priorities don't come up first...). The cross-spectra stuff is probably best as a separate PR, so I'll move that out then too (right now it's just commented out and unfinished). |
I'm going to force push to clean up the git history and properly credit some of the code's origins with co-author commits. Hope you don't mind! |
Co-authored-by: Alex Arslan <[email protected]> Co-authored-by: Kalyan Palepu <[email protected]> Co-authored-by: kolia <[email protected]>
Co-authored-by: Alex Arslan <[email protected]> Co-authored-by: Kalyan Palepu <[email protected]> Co-authored-by: kolia <[email protected]> wip wip wip wip add WIP cross-spectra code so I don't lose it Co-authored-by: Alex Arslan <[email protected]> Co-authored-by: Kalyan Palepu <[email protected]> Co-authored-by: kolia <[email protected]> fix bug, change syntax for 1.0 format wip wip wip fix Rename `sample_rate` -> `fs` fixes
Co-authored-by: Alex Arslan <[email protected]> Co-authored-by: Kalyan Palepu <[email protected]> Co-authored-by: kolia <[email protected]>
Co-authored-by: mich11 <[email protected]> swap order to match Onda wip align better with existing api fix test Fix MATLAB reference test Co-authored-by: mich11 <[email protected]>
fix bugs rename add coherence reference test
mt_pgram!
, and multitaper spectrogram, multitaper csd, multitaper coherence
Ok, I think this is ready for review. Thanks for the git help, @palday! I don't have permission to formally ask for review on github, but I'd appreciate it if @kolia and @ararslan could give this a review too, since it's a big bunch of code. I will start making some PRs to Beacon private repos to try out this branch as well. Choices I made that possibly should be questioned:
Quick benchmarkUsing the first test from julia> using BenchmarkTools
julia> @btime mt_spectrogram!($out, $x0, $spec_config);
14.186 μs (1 allocation: 2.12 KiB)
julia> @btime mt_spectrogram(x0, 256, 128; fs=10);
638.610 μs (64 allocations: 118.73 KiB)
julia> @btime spectrogram(x0, 256, 128; fs=10);
32.185 μs (47 allocations: 10.73 KiB) You'll note that the in-place one still has one pesky allocation. Not sure where that is, but if someone spots it, let me know! And also the in-place multitaper spectrogram is faster than the existing out-of-place spectrogram, which is kind of fun (considering the multitaper one does like 7x more work), but almost certainly that advantage will go away as the data gets bigger. |
@@ -62,7 +68,7 @@ Base.collect(x::ArraySplit) = collect(copy(a) for a in x) | |||
## UTILITY FUNCTIONS | |||
|
|||
# Convert the output of an FFT to a PSD and add it to out | |||
function fft2pow!(out::Array{T}, s_fft::Vector{Complex{T}}, nfft::Int, r::Real, onesided::Bool, offset::Int=0) where T | |||
function fft2pow!(out::AbstractArray{T}, s_fft::AbstractVector{Complex{T}}, nfft::Int, r::Real, onesided::Bool, offset::Int=0) where T |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needed to work with views
@@ -184,12 +190,12 @@ end | |||
|
|||
## PERIODOGRAMS | |||
abstract type TFR{T} end | |||
struct Periodogram{T,F<:Union{Frequencies,AbstractRange}} <: TFR{T} | |||
power::Vector{T} | |||
struct Periodogram{T,F<:Union{Frequencies,AbstractRange}, V <: AbstractVector{T}} <: TFR{T} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needed to work with views! This is possibly breaking, if users were directly constructing Periodogram
objects by hand, since they will now get an abstract type if they don't add the third type parameter. However, I noticed the version is 0.7-dev
anyway, so presumably that's OK.
freq::F | ||
end | ||
struct Periodogram2{T,F1<:Union{Frequencies,AbstractRange},F2<:Union{Frequencies,AbstractRange}} <: TFR{T} | ||
power::Matrix{T} | ||
struct Periodogram2{T,F1<:Union{Frequencies,AbstractRange},F2<:Union{Frequencies,AbstractRange}, M<:AbstractMatrix{T}} <: TFR{T} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allow views for consistency with Periodogram
, but I don't actually need this here
src/multitaper.jl
Outdated
x_mt = config.x_mt | ||
|
||
for k in 1:config.n_channels | ||
tapered_spectra!(x_mt[:,:,k], signal[k, :], config.mt_config) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note these are views (due to the @views
annotation out front)
…amConfig constructor
Just seeing where this stands, were you going to add an option to weight each taper's estimation, and add a similar option to multitaper estimation beyond spectrograms? |
Yep! Just pushed up a commit. Some updates:
|
I am guessing the codecov will take a hit with the new methods I added, so I'll add tests for them (...tomorrrow). I pushed up what I had so far so you could take a look already in case you had any comments. |
I've added tests for the new methods and also added an error if the user does I think I've addressed all your great feedback, please let me know if I've missed anything or as new things come up :). |
Can you add the new functions to the docs? Unless I missed it in the long and impressive file changes. |
Good point, done! |
Bump 🙂 |
Would really love a re-review @galenlynch! Or maybe another DSP maintainer can help out, e.g. @ararslan? I've got a pile of branches depending on this branch and they've started to propagate further downstream... |
Hey sorry that I disappeared. I'm happy with this now! I'll approve the PR and merge it tomorrow unless someone objects. |
Whoo! Very exciting :). Could we tag a release? |
This code was orignally written by @ararslan at Beacon Biosignals (and modified by @kolia?) and I've modified it (and possibly introduced mistakes) by adapting it to match DSP.jl's
spectrogram
input and output type, for ease of use. It's still a work in progress becausetime x freq
matrix, whereasspectrogram
outputs afreq x time
matrixI'm not sure how to test it; is there another open source implementation I can compare against?