Better Signal Sync with reference

cuts several sequences in the LAB
This commit is contained in:
Silas [Labor]
2024-09-24 11:30:12 +02:00
parent 179a7f9682
commit 0f0b4d2c66

View File

@@ -403,7 +403,7 @@ classdef Signal
end end
%% %%
function [obj,D,cuts] = tsynch(obj,options) function [obj,S] = tsynch(obj,options)
% time sync and cut % time sync and cut
arguments arguments
obj Signal obj Signal
@@ -419,29 +419,33 @@ classdef Signal
b = options.reference.resample("fs_in",options.fs_ref,"fs_out",obj.fs).normalize("mode","oneone").signal; b = options.reference.resample("fs_in",options.fs_ref,"fs_out",obj.fs).normalize("mode","oneone").signal;
%estimate delay between signals %estimate delay between signals
[co,lags] = xcorr(a,b,100); [co,lags] = xcorr(a,b);
[~,pos] = max(co); [~,pos] = max(co);
D = lags(pos); D = lags(pos);
if D == 100 %estimate start pos of signal
warning('Check Sync: Delay is found to be 100 which is the max. windowlength is xcorr function!') maxpeaknum = floor(length(a)/length(b));
[pks,pkpos] = findpeaks(co./max(co),'MinPeakDistance',length(b)/2,'MinPeakHeight',0.2,'NPeaks',maxpeaknum);
shifts = lags(pkpos);
%Cut occurences of ref signal from signal
S = {};
for c = shifts
sig = obj.delay(-c,'mode','samples');
sig.signal = sig.signal(1:length(b));
S{end+1,1} = sig;
end end
% delay by lagging samples %plot all synced signals and the ref signal
obj = obj.delay(-D,'mode','samples'); figure;hold on;
for i = 1:size(S,1)
cuts = obj.length-(options.reference.length*q); plot(S{i}.normalize('mode','oneone').signal(1000:1100),'LineWidth',0.1,'Color',[0.2157 0.4941 0.7216]);
plot(b(1000:1100),'LineWidth',1);
if cuts > 10
warning('Check Sync: Signal difference larger than 10.')
end end
if cuts < 0 %return the sinal with the highest correlation...
% warning('Check Sync: Reference Signal shorter than signal to sync.') [~,idx]=max(pks);
else obj.signal = S{idx}.signal;
% then cut out the length of the reference
obj.signal = obj.signal(1:end-cuts);
end
end end