-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathtransform.py
More file actions
272 lines (201 loc) · 8.79 KB
/
transform.py
File metadata and controls
272 lines (201 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""Functions and utilities for transforming power spectra."""
import numpy as np
from fooof.sim.params import update_sim_ap_params
###################################################################################################
###################################################################################################
def rotate_spectrum(freqs, power_spectrum, delta_exponent, f_rotation):
"""Rotate a power spectrum about a frequency point, changing the aperiodic exponent.
Parameters
----------
freqs : 1d array
Frequency axis of input power spectrum, in Hz.
power_spectrum : 1d array
Power values of the spectrum.
delta_exponent : float
Change in aperiodic exponent to be applied, where:
- positive is clockwise rotation (steepen)
- negative is counterclockwise rotation (flatten)
f_rotation : float
Frequency value, in Hz, about which rotation is applied, at which power is unchanged.
Returns
-------
rotated_spectrum : 1d array
Rotated power spectrum.
Raises
------
ValueError
If the rotation frequency is invalid.
Notes
-----
Rotating in log-log spacing is equivalent to multiplying with a 1/f shaped mask that is:
- unity at the rotation frequency
- has an exponent of the desired delta exponent
This mask, when applied to a spectrum as 'spectrum * mask', should result in:
- rotated_spectrum = 1/f^(original_exponent + delta_exponent), where
- spectrum[rotation_frequency] == rotated spectrum[rotation_frequency]
This mask is defined as:
- mask = (freqs / rotation_frequency) ** -delta_exponent
Note that this approach / function should only be applied to spectra without a knee:
- If using simulated data, this is spectra created in 'fixed' mode.
- This is because the rotation applied is inconsistent with the formulation of spectra
with a knee. This transformation will change them in an unspecified way, not just
limited to doing the rotation.
Examples
--------
Rotate a simulated spectrum, changing the exponent around a rotation point of 25 Hz:
>>> from fooof.sim.gen import gen_power_spectrum
>>> freqs, powers = gen_power_spectrum([1, 50], [1, 1], [10, 0.5, 1])
>>> rotated_powers = rotate_spectrum(freqs, powers, 0.5, 25)
"""
# Rotations are undefined for frequency value of exactly zero
# We also do not support (in this implementation) negative frequencies
if f_rotation <= 0.:
raise ValueError("The rotation frequency cannot be less than or equal to zero.")
mask = (np.abs(freqs) / f_rotation)**-delta_exponent
rotated_spectrum = mask * power_spectrum
return rotated_spectrum
def translate_spectrum(power_spectrum, delta_offset):
"""Translate a spectrum, changing the offset value.
Parameters
----------
power_spectrum : 1d array
Power values of the spectrum.
delta_offset : float
Amount to change the offset by, where:
- positive values are an upwards translation
- negative are are a downwards translation
Returns
-------
translated_spectrum : 1d array
Translated power spectrum.
Examples
--------
Translate a simulated spectrum, moving the offset up:
>>> from fooof.sim.gen import gen_power_spectrum
>>> freqs, powers = gen_power_spectrum([1, 50], [1, 1], [10, 0.5, 1])
>>> translated_powers = translate_spectrum(powers, 0.5)
"""
translated_spectrum = np.power(10, delta_offset, dtype='float') * power_spectrum
return translated_spectrum
def rotate_sim_spectrum(freqs, power_spectrum, delta_exponent, f_rotation, sim_params):
"""Rotate a simulated power spectrum, updating a SimParams object.
Parameters
----------
freqs : 1d array
Frequency axis of input power spectrum, in Hz.
power_spectrum : 1d array
Power values of the spectrum.
delta_exponent : float
Change in aperiodic exponent to be applied, where:
- positive is clockwise rotation (steepen)
- negative is counterclockwise rotation (flatten)
f_rotation : float
Frequency value, in Hz, about which rotation is applied, at which power is unchanged.
sim_params : SimParams
Object storing the current parameter definitions.
Returns
-------
rotated_spectrum : 1d array
Rotated power spectrum.
new_sim_params : SimParams
New parameter definitions.
Notes
-----
Warning: This function should only be applied to spectra without a knee.
If using simulated data, this is spectra created in 'fixed' mode.
This is because the rotation applied is inconsistent with
the formulation of knee spectra, and will change them in an
unspecified way, not just limited to doing the rotation.
Examples
--------
Rotate a simulated spectrum, changing the exponent around a rotation point of 25 Hz:
>>> from fooof.sim.gen import gen_power_spectrum
>>> freqs, powers, sp = gen_power_spectrum([1, 50], [1, 1], [10, 0.5, 1], return_params=True)
>>> rotated_powers, new_sp = rotate_sim_spectrum(freqs, powers, 0.5, 25, sp)
"""
rotated_spectrum = rotate_spectrum(freqs, power_spectrum, delta_exponent, f_rotation)
delta_offset = compute_rotation_offset(delta_exponent, f_rotation)
new_sim_params = update_sim_ap_params(sim_params, [delta_offset, delta_exponent])
return rotated_spectrum, new_sim_params
def translate_sim_spectrum(power_spectrum, delta_offset, sim_params):
"""Translate a simulated spectrum, updating a SimParams object.
Parameters
----------
power_spectrum : 1d array
Power values of the spectrum.
delta_offset : float
Amount to change the offset by, where:
- positive values are an upwards translation
- negative are are a downwards translation
sim_params : SimParams
Object storing the current parameter definitions.
Returns
-------
translated_spectrum : 1d array
Translated power spectrum.
new_sim_params : SimParams
New parameter definitions.
Examples
--------
Translate a simulated spectrum, moving the offset up:
>>> from fooof.sim.gen import gen_power_spectrum
>>> freqs, powers, sp = gen_power_spectrum([1, 50], [1, 1], [10, 0.5, 1], return_params=True)
>>> translated_powers, new_sp = translate_sim_spectrum(powers, 0.5, sp)
"""
translated_spectrum = translate_spectrum(power_spectrum, delta_offset)
new_sim_params = update_sim_ap_params(sim_params, delta_offset, 'offset')
return translated_spectrum, new_sim_params
def compute_rotation_offset(delta_exponent, f_rotation):
"""Calculate the change in offset from a given rotation.
Parameters
----------
delta_exponent : float
The change in aperiodic exponent value.
f_rotation : float
The frequency value, in Hz, where rotation is applied.
Returns
-------
float
The amount the offset will change for the specified exponent change.
Examples
--------
Calculate the induced change in offset of a change in exponent of 0.5 at 25 Hz:
>>> delta_offset = compute_rotation_offset(0.5, 25)
"""
return -np.log10(f_rotation) * -delta_exponent
def compute_rotation_frequency(delta_exponent_b, f_rotation_b, delta_exponent_c, f_rotation_c):
"""Calculate the rotation frequency between two rotated power spectra.
Parameters
----------
delta_exponent_b : float
The applied change in exponent value for power spectrum 'B'.
f_rotation_b : float
The rotation frequency applied to power spectrum 'B'.
delta_exponent_c : float
The applied change in exponent value for power spectrum 'C'.
f_rotation_c : float
The rotation frequency applied to power spectrum 'C'.
Returns
-------
float
The frequency rotation point between spectra 'B' & 'C'.
Notes
-----
**Code Notes**
This computes the rotation frequency for two power spectra 'B' & 'C',
under the assumption that they are both rotated versions of a the
same original power spectrum 'A'.
**Derivation**
Given an original power spectrum A, then:
- B = A*(f_rotation_b/freqs)^delta_exponent_b
- C = A*(f_rotation_c/freqs)^delta_exponent_c
Therefore, what you want is f_rotation_bc, which is the frequency where B==C.
To find this, we can plug everything back into the equation, to find where
B[freqs] == C[freqs], which is how we arrive at the solution below.
Examples
--------
Calculate the rotation frequency between two transformed power spectra:
>>> f_rotation = compute_rotation_frequency(0.5, 25, -0.25, 10)
"""
return (((f_rotation_c**delta_exponent_c) / (f_rotation_b**delta_exponent_b))) ** \
(1/(delta_exponent_c-delta_exponent_b))