Skip to content

Commit

Permalink
RF: Slightly simplify cosine implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed May 30, 2024
1 parent 5107714 commit 17bac08
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions nipype/algorithms/confounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -1535,21 +1535,22 @@ def _cosine_drift(period_cut, frametimes):
Ref: http://en.wikipedia.org/wiki/Discrete_cosine_transform DCT-II
"""
len_tim = len(frametimes)
n_times = np.arange(len_tim)
hfcut = 1.0 / period_cut # input parameter is the period

# frametimes.max() should be (len_tim-1)*dt
dt = frametimes[1] - frametimes[0]
# hfcut = 1/(2*dt) yields len_time
# If series is too short, return constant regressor
order = max(int(np.floor(2 * len_tim * hfcut * dt)), 1)
cdrift = np.zeros((len_tim, order))
nfct = np.sqrt(2.0 / len_tim)
cdrift = np.ones((len_tim, order))

for k in range(1, order):
cdrift[:, k] = nfct * np.cos((np.pi / len_tim) * (n_times + 0.5) * k)
if order > 1:
nfct = np.sqrt(2.0 / len_tim)
support = (np.pi / len_tim) * (np.arange(len_tim) + 0.5)

for k in range(1, order):
cdrift[:, k] = nfct * np.cos(support * k)

cdrift[:, 0] = 1.0 # or 1./sqrt(len_tim) to normalize
return cdrift


Expand Down

0 comments on commit 17bac08

Please sign in to comment.