From 0f0b4d2c66561c8822a4df2de1bec573f1539bfc Mon Sep 17 00:00:00 2001 From: "Silas [Labor]" Date: Tue, 24 Sep 2024 11:30:12 +0200 Subject: [PATCH] Better Signal Sync with reference cuts several sequences in the LAB --- Classes/00_signals/Signal.m | 42 ++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/Classes/00_signals/Signal.m b/Classes/00_signals/Signal.m index fa4c44a..95cd78b 100644 --- a/Classes/00_signals/Signal.m +++ b/Classes/00_signals/Signal.m @@ -403,7 +403,7 @@ classdef Signal end %% - function [obj,D,cuts] = tsynch(obj,options) + function [obj,S] = tsynch(obj,options) % time sync and cut arguments 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; %estimate delay between signals - [co,lags] = xcorr(a,b,100); + [co,lags] = xcorr(a,b); [~,pos] = max(co); D = lags(pos); - if D == 100 - warning('Check Sync: Delay is found to be 100 which is the max. windowlength is xcorr function!') + %estimate start pos of signal + 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 - - % delay by lagging samples - obj = obj.delay(-D,'mode','samples'); - - cuts = obj.length-(options.reference.length*q); - - if cuts > 10 - warning('Check Sync: Signal difference larger than 10.') - end - - if cuts < 0 - % warning('Check Sync: Reference Signal shorter than signal to sync.') - else - % then cut out the length of the reference - obj.signal = obj.signal(1:end-cuts); + + %plot all synced signals and the ref signal + figure;hold on; + for i = 1:size(S,1) + 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); end + + %return the sinal with the highest correlation... + [~,idx]=max(pks); + obj.signal = S{idx}.signal; end