@@ -58,29 +58,33 @@ def __init__(self, ap_recording_segment: BaseRecordingSegment, lfp_recording_seg
58
58
self .lfp_filter = lfp_filter
59
59
self .margin = margin
60
60
61
+ self .AP_TO_LFP = int (round (ap_recording_segment .sampling_frequency / lfp_recording_segment .sampling_frequency ))
62
+
61
63
62
64
def get_num_samples (self ) -> int :
63
- return self .ap_recording .get_num_samples ()
65
+ # Trunk the recording to have a number of samples that is a multiple of 'AP_TO_LFP'.
66
+ return self .ap_recording .get_num_samples () - (self .ap_recording .get_num_samples () % self .AP_TO_LFP )
64
67
65
68
66
69
def get_traces (self , start_frame : Union [int , None ] = None , end_frame : Union [int , None ] = None ,
67
70
channel_indices : Union [List , None ] = None ) -> np .ndarray :
68
- AP_TO_LFP = int (round (self .ap_recording .sampling_frequency / self .lfp_recording .sampling_frequency ))
69
71
if start_frame is None :
70
72
start_frame = 0
71
73
if end_frame is None :
72
74
end_frame = self .get_num_samples ()
73
75
74
- assert end_frame % AP_TO_LFP == 0 # Fix this.
75
-
76
- ap_traces , left_margin , right_margin = get_chunk_with_margin (self .ap_recording , start_frame , end_frame , channel_indices , self .margin + AP_TO_LFP )
76
+ ap_traces , left_margin , right_margin = get_chunk_with_margin (self .ap_recording , start_frame , end_frame , channel_indices , self .margin + self .AP_TO_LFP )
77
77
78
- left_leftover = (AP_TO_LFP - (start_frame - left_margin ) % AP_TO_LFP ) % AP_TO_LFP
78
+ left_leftover = (self . AP_TO_LFP - (start_frame - left_margin ) % self . AP_TO_LFP ) % self . AP_TO_LFP
79
79
left_margin -= left_leftover
80
+ right_leftover = (end_frame + right_margin ) % self .AP_TO_LFP
81
+ right_margin -= right_leftover
80
82
83
+ if right_leftover > 0 :
84
+ ap_traces = ap_traces [:right_leftover ]
81
85
ap_traces = ap_traces [left_leftover :]
82
86
83
- lfp_traces = self .lfp_recording .get_traces ((start_frame - left_margin ) // AP_TO_LFP , (end_frame + right_margin ) // AP_TO_LFP , channel_indices )
87
+ lfp_traces = self .lfp_recording .get_traces ((start_frame - left_margin ) // self . AP_TO_LFP , (end_frame + right_margin ) // self . AP_TO_LFP , channel_indices )
84
88
85
89
ap_fourier = np .fft .rfft (ap_traces , axis = 0 )
86
90
lfp_fourier = np .fft .rfft (lfp_traces , axis = 0 )
@@ -96,12 +100,11 @@ def get_traces(self, start_frame: Union[int, None] = None, end_frame: Union[int,
96
100
reconstructed_lfp_fourier = lfp_fourier / lfp_filter [:, None ]
97
101
98
102
# Compute aliasing of high frequencies on LFP channels
99
- # TODO: There may be a faster way than computing the Fourier transform
100
103
lfp_nyquist = self .lfp_recording .sampling_frequency / 2
101
104
fourier_aliased = reconstructed_ap_fourier .copy ()
102
105
fourier_aliased [ap_freq <= lfp_nyquist ] = 0.0
103
106
fourier_aliased *= self .lfp_filter (ap_freq )[:, None ]
104
- traces_aliased = np .fft .irfft (fourier_aliased , axis = 0 )[::AP_TO_LFP ]
107
+ traces_aliased = np .fft .irfft (fourier_aliased , axis = 0 )[::self . AP_TO_LFP ]
105
108
fourier_aliased = np .fft .rfft (traces_aliased , axis = 0 ) / lfp_filter [:, None ]
106
109
fourier_aliased = fourier_aliased [:np .searchsorted (ap_freq , lfp_nyquist , side = "right" )]
107
110
lfp_aa_fourier = reconstructed_lfp_fourier - fourier_aliased
@@ -116,7 +119,7 @@ def get_traces(self, start_frame: Union[int, None] = None, end_frame: Union[int,
116
119
fourier_reconstructed = np .empty (reconstructed_ap_fourier .shape , dtype = np .complex128 )
117
120
idx = np .searchsorted (ap_freq , lfp_nyquist , side = "right" )
118
121
fourier_reconstructed [idx :] = reconstructed_ap_fourier [idx :]
119
- fourier_reconstructed [:idx ] = AP_TO_LFP * lfp_aa_fourier * ratio [:idx ] + reconstructed_ap_fourier [:idx ] * (1 - ratio [:idx ])
122
+ fourier_reconstructed [:idx ] = self . AP_TO_LFP * lfp_aa_fourier * ratio [:idx ] + reconstructed_ap_fourier [:idx ] * (1 - ratio [:idx ])
120
123
121
124
# To get back to the 0.5 - 10,000 Hz original filter
122
125
# filter_reconstructed = generate_RC_filter(ap_freq, [0.5, 10000])[:, None]
0 commit comments