How to introduce a time-shift into data?
36 次查看(过去 30 天)
显示 更早的评论
Dear Community!
I would like to introduce a time shift to my data. The goal is that the lower figure in the subplot gets a time shift of 4 ms.
Here is my code:
% time steps
DTms = 0.01; % [ms] time step -- 100kHz sampling rate
% time lengths
Tall = 2000; % [ms] entire duration of simulation
Tinit = 500; % [ms] starting time of stimulus
Tstim = 1000; % [ms] duration of entire stimulus
Tlast = 500; % [ms] time after stimulus
Nall = round(Tall /DTms);
Ninit = round(Tinit/DTms);
Nstim = round(Tstim/DTms);
Nlast = round(Tlast/DTms);
tvms = (0:Nall-1)*DTms-Tinit; % time vector (stimulus starts at time zero)
lmain = logical( [zeros(1,Ninit),ones(1,Nstim),zeros(1,Nlast)] );
tvmain = tvms(lmain);
fs = 1e5; % 100 [kHz] sampling rate
%% Here is now a part you don't have acces to, but i believe its not important
fibres_ipsi = zeros(25,length(lmain));
for i = 1:25
% Put all spikes of each row in one column vector;
fibres_ipsi (i,:) = ANout_ipsi (1,:,i);
end
fibres_contra = zeros(10,length(lmain));
for i = 1:10
% Put all spikes of each row in one column vector;
fibres_contra (i,:) = ANout_contra (1,:,i);
end
input_ipsi = sum(fibres_ipsi);
input_contra = sum(fibres_contra);
subplot(2,1,1)
plot(tvms, input_ipsi)
xlim([0 100])
subplot(2,1,2)
plot(tvms,input_contra)
xlim([0 100])
ITD = 4;
samples_per_itd = fs/ITD;
% Apply ITDs
spikes_indices = find(input_ipsi >= 1); % Get indices
spike_times = tvms(spikes_indices); % Get spike times
spike_time_ITD = spike_times + (ITD); % Apply ITD
The lower subplot datapoint should move to the right!
Thank you in advance,
Paul
0 个评论
采纳的回答
Star Strider
2022-7-23
I cannot run the code (missing data), however the conversion from milliseconds (in this instance) to samples is straightforward —
fs = 1e5; % 100 [kHz] sampling rate = Samples/Second
time_shift = 4E-3; % 4 ms
index_shift = fs * time_shift % Samples/Second * Seconds = Samples To Be Shifted
One way to implement that would be to use the circshift function (if there are more than 400 samples between the last data and the end of the vector). There may be other ways that are more applicable to your particular application.
.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!