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

Signal processing v0 #300

Merged
merged 81 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
26c2b25
inital commit for signal proc - complex morelet and fft v0
kippfreud Jun 6, 2024
f080d4f
basic pywavelets functionality matched
kippfreud Jun 26, 2024
9aa05ac
different wavelet definition
kippfreud Jun 27, 2024
4af8d90
wavlet workaround fixed, tutorial added
kippfreud Jun 27, 2024
01c5435
tutorial cleaning
kippfreud Jun 28, 2024
027878e
linting
kippfreud Jun 28, 2024
18b3bc1
more linting
kippfreud Jun 28, 2024
ebdbe67
json removal
kippfreud Jun 28, 2024
75d3a46
basic tests added
kippfreud Jun 28, 2024
334e785
remove unused import
kippfreud Jul 1, 2024
a3ab81c
minor notebook changes
kippfreud Jul 2, 2024
80c12d7
spectogram now takes tdsframe
kippfreud Jul 2, 2024
c1a5a26
review changes
kippfreud Jul 3, 2024
dfe6e41
updated function names in test
kippfreud Jul 3, 2024
3a9173a
updated tests
kippfreud Jul 3, 2024
cfb6066
expanded test coverage
kippfreud Jul 3, 2024
4148d6d
notebook various changes
kippfreud Jul 8, 2024
19c9183
compute_wavelet_transform can now handle TsdTensor
kippfreud Jul 8, 2024
63f52b2
PR comment changes
kippfreud Jul 12, 2024
dda072f
filterbank changes
kippfreud Jul 12, 2024
600400b
using nap.convolve, and other review changes
kippfreud Jul 12, 2024
d0c0ddd
fixed test
kippfreud Jul 12, 2024
9c53af5
unused import removed
kippfreud Jul 12, 2024
2b4bc86
logspacing
kippfreud Jul 15, 2024
f110d1e
better tests, finished notebook 1
kippfreud Jul 17, 2024
b00bf23
linting
kippfreud Jul 17, 2024
81a076e
phase preference notebook added
kippfreud Jul 17, 2024
ebdb64c
remove unused import
kippfreud Jul 17, 2024
0f17883
simplified compute_wavelet_transform, added tests
kippfreud Jul 17, 2024
5af389d
removed time zeroing for doc examples
kippfreud Jul 17, 2024
072fbe3
minor changes, wavelet API v0
kippfreud Jul 17, 2024
ffdc3d9
linting
kippfreud Jul 17, 2024
54d8cb6
better wavelet API notebook
kippfreud Jul 18, 2024
3d1ab70
removed tkagg
kippfreud Jul 18, 2024
5df9ff0
linting
kippfreud Jul 18, 2024
c942a79
linting
kippfreud Jul 18, 2024
04e9d8a
wavelet api tutorial improved, generate_filterbank returns TdsFrame
kippfreud Jul 19, 2024
c49d767
welch removed
kippfreud Jul 19, 2024
917f932
welch import removed
kippfreud Jul 19, 2024
10e47fb
review comments addressed
kippfreud Jul 19, 2024
fa7952e
removing welch tests
kippfreud Jul 19, 2024
cac44ff
fixed broked phase notebook
kippfreud Jul 19, 2024
b0bb20f
better comments on phase notebook
kippfreud Jul 19, 2024
496fbe3
PR comments addressed, tests added
kippfreud Jul 29, 2024
647bbfc
unused import removed
kippfreud Jul 29, 2024
e63ffe8
removing integrate->conv->diff pipeline
kippfreud Jul 30, 2024
41905f1
pulling upstream dev
kippfreud Jul 30, 2024
16a3bc6
pulling dev
kippfreud Jul 31, 2024
059d2c4
Adding new notebook for psd
gviejo Jul 31, 2024
2fc09b5
Merge branch 'signal_processing' of https://github.com/kippfreud/pyna…
gviejo Jul 31, 2024
eec4740
Adding mean psd notebook
gviejo Jul 31, 2024
82dfc43
pushing some failing tests
gviejo Aug 1, 2024
6c3d6e5
Adding tests for mean PSD
gviejo Aug 1, 2024
c8a5fc4
linting
gviejo Aug 1, 2024
65a1a1a
param name changes
kippfreud Aug 2, 2024
d7283b4
pulling guillaume changes
kippfreud Aug 2, 2024
b16c7c8
fixed notebooks
gviejo Aug 2, 2024
e719740
guillaume changes
kippfreud Aug 2, 2024
74d9061
better tests
kippfreud Aug 5, 2024
73ee4de
one added case for tests
kippfreud Aug 5, 2024
a5b4f3b
Updating tutorial_signal_processing notebool
gviejo Aug 5, 2024
b1540ea
more concise plotting code in docs
kippfreud Aug 5, 2024
05a5996
signal processing tests to 100% coverage
kippfreud Aug 5, 2024
05e29b6
coverage actually to 100%
kippfreud Aug 6, 2024
d723b8a
changes to notebooks
kippfreud Aug 6, 2024
8d78bb5
doc plot neatening
kippfreud Aug 6, 2024
7fee0d6
Update tutorial_signal_processing.py and tests
gviejo Aug 6, 2024
c32276b
Fixing conflicts
gviejo Aug 6, 2024
265be7f
Missing test for sig processing
gviejo Aug 7, 2024
8885714
More update on wavelets
gviejo Aug 7, 2024
2793e1c
change title
gviejo Aug 7, 2024
5d3a340
Updating tests
gviejo Aug 8, 2024
5fab4dc
check that fft of wavelet is correct gaussian
kippfreud Aug 8, 2024
252d63b
mergin test changes
kippfreud Aug 8, 2024
a761f6e
linting
kippfreud Aug 8, 2024
bda5892
removed bad import
kippfreud Aug 8, 2024
33fd98f
Merge branch 'dev' into signal_processing
gviejo Aug 8, 2024
9651077
updating
gviejo Aug 8, 2024
ee558e5
addressing comments, slight notebook improvements
kippfreud Aug 9, 2024
0e8268b
merging changes
kippfreud Aug 9, 2024
861042a
suggested changes and wavelet arange rounding fix
kippfreud Aug 9, 2024
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
128 changes: 128 additions & 0 deletions docs/api_guide/tutorial_pynapple_spectrum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# -*- coding: utf-8 -*-
"""
Power spectral density
======================

See the [documentation](https://pynapple-org.github.io/pynapple/) of Pynapple for instructions on installing the package.

"""

# %%
# !!! warning
# This tutorial uses matplotlib for displaying the figure
#
# You can install all with `pip install matplotlib requests tqdm seaborn`
#
# Now, import the necessary libraries:
#
# mkdocs_gallery_thumbnail_number = 3

import matplotlib.pyplot as plt
import numpy as np
import seaborn

seaborn.set_theme()

import pynapple as nap

# %%
# ***
# Generating a signal
# ------------------
# Let's generate a dummy signal with 2Hz and 10Hz sinusoide with white noise.
#

F = [2, 10]

Fs = 2000
t = np.arange(0, 200, 1/Fs)
sig = nap.Tsd(
t=t,
d=np.cos(t*2*np.pi*F[0])+np.cos(t*2*np.pi*F[1])+np.random.normal(0, 3, len(t)),
time_support = nap.IntervalSet(0, 200)
)

# %%
# Let's plot it
plt.figure()
plt.plot(sig.get(0, 0.4))
plt.title("Signal")
plt.xlabel("Time (s)")



# %%
# Computing power spectral density (PSD)
# --------------------------------------
#
# To compute a PSD of a signal, you can use the function `nap.compute_power_spectral_density`. With `norm=True`, the output of the FFT is divided by the length of the signal.

psd = nap.compute_power_spectral_density(sig, norm=True)

# %%
# Pynapple returns a pandas DataFrame.

print(psd)

# %%
# It is then easy to plot it.

plt.figure()
plt.plot(np.abs(psd))
plt.xlabel("Frequency (Hz)")
plt.ylabel("Amplitude")


# %%
# Note that the output of the FFT is truncated to positive frequencies. To get positive and negative frequencies, you can set `full_range=True`.
# By default, the function returns the frequencies up to the Nyquist frequency.
# Let's zoom on the first 20 Hz.

plt.figure()
plt.plot(np.abs(psd))
plt.xlabel("Frequency (Hz)")
plt.ylabel("Amplitude")
plt.xlim(0, 20)


# %%
# We find the two frequencies 2 and 10 Hz.
#
# By default, pynapple assumes a constant sampling rate and a single epoch. For example, computing the FFT over more than 1 epoch will raise an error.
double_ep = nap.IntervalSet([0, 50], [20, 100])

try:
nap.compute_power_spectral_density(sig, ep=double_ep)
except ValueError as e:
print(e)


# %%
# Computing mean PSD
# ------------------
#
# It is possible to compute an average PSD over multiple epochs with the function `nap.compute_mean_power_spectral_density`.
#
# In this case, the argument `interval_size` determines the duration of each epochs upon which the FFT is computed.
# If not epochs is passed, the function will split the `time_support`.
#
# In this case, the FFT will be computed over epochs of 10 seconds.

mean_psd = nap.compute_mean_power_spectral_density(sig, interval_size=20.0, norm=True)


# %%
# Let's compare `mean_psd` to `psd`. In both cases, the ouput is normalized.

plt.figure()
plt.plot(np.abs(psd), label='PSD')
plt.plot(np.abs(mean_psd), label='Mean PSD (10s)')
plt.xlabel("Frequency (Hz)")
plt.ylabel("Amplitude")
plt.legend()
plt.xlim(0, 15)

# %%
# As we can see, `nap.compute_mean_power_spectral_density` was able to smooth out the noise.


Loading
Loading