Skip to content

Commit ee0ea5a

Browse files
committed
add oscillations as peak motivations piece
1 parent 65e67e1 commit ee0ea5a

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
"""
2+
'Oscillations' as Peaks
3+
=======================
4+
5+
Exploring the idea of oscillations as peaks of power.
6+
"""
7+
8+
###################################################################################################
9+
10+
# Imports from NeuroDSP to simulate & plot time series
11+
from neurodsp.sim import sim_powerlaw, sim_oscillation, sim_combined, set_random_seed
12+
from neurodsp.spectral import compute_spectrum
13+
from neurodsp.plts import plot_time_series, plot_power_spectra
14+
from neurodsp.utils import create_times
15+
16+
###################################################################################################
17+
18+
# Define simulation settings
19+
n_seconds = 30
20+
fs = 1000
21+
22+
# Create a times vector
23+
times = create_times(n_seconds, fs)
24+
25+
###################################################################################################
26+
# Frequency Specific Power
27+
# ------------------------
28+
#
29+
# Part of the motivation behind spectral parameterization is dissociating aperiodic
30+
# activity, with no characteristic frequency, to periodic power, which is defined as
31+
# having frequency specific power. This leads to the idea of oscillations as 'peaks'
32+
# of power in the power spectrum, which can be detected and measured.
33+
#
34+
# In this exploration, we will use simulated time series to examine how rhythmic signals
35+
# do display as 'peaks' of power in frequency domain representations. We will also
36+
# explore some limitations of this idea.
37+
#
38+
39+
###################################################################################################
40+
41+
# Simulate an ongoing sine wave
42+
sine_wave = sim_oscillation(n_seconds, fs, 10)
43+
44+
# Visualize the sine wave
45+
plot_time_series(times, sine_wave, xlim=[0, 5])
46+
47+
###################################################################################################
48+
#
49+
# In the above, we simulated a pure sinusoid, at 10 Hz.
50+
#
51+
52+
###################################################################################################
53+
54+
# Compute the power spectrum of the sine wave
55+
freqs, powers = compute_spectrum(sine_wave, fs)
56+
57+
# Visualize the power spectrum
58+
plot_power_spectra(freqs, powers)
59+
60+
###################################################################################################
61+
#
62+
# The power spectrum of the sine wave shows a clear peak of power at 10 Hz, reflecting
63+
# the simulated rhythm, with close to zero power at all other frequencies.
64+
#
65+
# This is characteristic of a sine wave, and demonstrates the basic idea of considering
66+
# oscillations as peaks of power in the power spectrum.
67+
#
68+
# Next lets examine a more complicated signal, with multiple components.
69+
#
70+
71+
###################################################################################################
72+
73+
# Define components for a combined signal
74+
components = {
75+
'sim_oscillation' : {'freq' : 10},
76+
'sim_powerlaw' : {'exponent' : -1},
77+
}
78+
79+
# Simulate a combined signal
80+
sig = sim_combined(n_seconds, fs, components)
81+
82+
# Visualize the time series
83+
plot_time_series(times, sig, xlim=[0, 5])
84+
85+
###################################################################################################
86+
#
87+
# Here we see a simply simulation meant to be closer to neural data, reflecting an oscillatory
88+
# component, as well as an aperiodic component, which contributes power across all frequencies.
89+
#
90+
91+
###################################################################################################
92+
93+
# Compute the power spectrum of the sine wave
94+
freqs, powers = compute_spectrum(sig, fs)
95+
96+
# Visualize the power spectrum
97+
plot_power_spectra(freqs, powers)
98+
99+
###################################################################################################
100+
# Interim Conclusion
101+
# ~~~~~~~~~~~~~~~~~~
102+
#
103+
# In the power spectrum of the combined signal, we can still the peak of power at 10 Hz, as
104+
# well as the pattern of power across all frequencies contributed by the aperiodic component.
105+
#
106+
# This basic example serves as the basic motivation for spectral parameterization. In this
107+
# simulated example, we know there are two components, and so a procedure for detecting the
108+
# peaks and measuring the pattern of aperiodic power (as is done in spectral parameterization)
109+
# provides a method to measuring these components in the data.
110+
#
111+
112+
###################################################################################################
113+
# Harmonic Peaks
114+
# --------------
115+
#
116+
# The above seeks to demonstrate the basic idea whereby a peak of power _may_ reflect
117+
# an oscillation at that frequency, where as patterns of power across all frequencies
118+
# likely reflect aperiodic activity.
119+
#
120+
# In the this section, we will further explore peaks of power in the frequency domain,
121+
# showing that not every peak necessarily reflect an independent oscillation.
122+
#
123+
# To do so, we will start by simulating a non-sinusoidal oscillation.
124+
#
125+
126+
###################################################################################################
127+
128+
# Simulate a sawtooth wave
129+
sawtooth = sim_oscillation(n_seconds, fs, 10, 'sawtooth', width=0.5)
130+
131+
# Visualize the sine wave
132+
plot_time_series(times, sawtooth, xlim=[0, 5])
133+
134+
###################################################################################################
135+
#
136+
# In the above, we can see that there is again an oscillation, although it is not sinusoidal.
137+
#
138+
139+
###################################################################################################
140+
141+
# Compute the power spectrum of the sine wave
142+
freqs, powers = compute_spectrum(sawtooth, fs)
143+
144+
# Visualize the power spectrum
145+
plot_power_spectra(freqs, powers)
146+
147+
###################################################################################################
148+
#
149+
# Note the 10 Hz peak, as well as the additional peaks in the frequency domain.
150+
#
151+
# Before further discussing this, let's create an example with an aperiodic component.
152+
#
153+
154+
###################################################################################################
155+
156+
# Define components for a combined signal
157+
components = {
158+
'sim_oscillation' : {'freq' : 10, 'cycle' : 'sawtooth', 'width' : 0.25},
159+
'sim_powerlaw' : {'exponent' : -1.},
160+
}
161+
162+
# Simulate a combined signal
163+
sig = sim_combined(n_seconds, fs, components)
164+
165+
# Visualize the time series
166+
plot_time_series(times, sig, xlim=[0, 5])
167+
168+
###################################################################################################
169+
170+
# Compute the power spectrum of the sine wave
171+
freqs, powers = compute_spectrum(sig, fs)
172+
173+
# Visualize the power spectrum
174+
plot_power_spectra(freqs, powers)
175+
176+
###################################################################################################
177+
#
178+
# In the power spectrum above, we see that there is a peak of power at the fundamental frequency
179+
# of the rhythm (10 Hz), but there are also numerous other peaks. These additional peaks are
180+
# 'harmonics', and that are components of the frequency domain representation that reflect
181+
# the non-sinusoidality of the time domain signal.
182+
#
183+
# This serves as the basic motivation for the claim that although a peak _may_ reflect an
184+
# independent oscillation, this need not be the case, as a given peak could simply be the harmonic
185+
# of an asymmetric oscillation at a different frequency. For this reason, the number of peaks in
186+
# a model can not be interpreted as the number of oscillations in a signal.
187+
#

0 commit comments

Comments
 (0)