demo_offset.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. close all; clear all; clc;
  2. T=150*1e-3; %[s] duration of post-stimulus time window
  3. Fs=1e3; %[Hz] sampling frequency
  4. Ncs=1; % Number of channels
  5. Nts=T*Fs; % Number of timepoints
  6. yoff=0.25; % Y-offset
  7. ts=linspace(0,T,Nts); %[s] time points after stimulus
  8. ft = @(p,t)(p(5)*exp(p(1)*p(3)+0.5*p(2)^2*p(3)^2-p(3)*t)...
  9. .*normcdf(t,p(1)+p(2)^2*p(3),p(2))+p(4)*normcdf(t,p(1),p(2))+p(6));
  10. % Generate some synthetic data with random latency ranging 20÷70 [ms]
  11. mu=zeros(1,Ncs); lt=zeros(1,Ncs); xt=zeros(Ncs,Nts);
  12. for ch=1:Ncs
  13. mu(ch)=(20+randi(50,1));
  14. xt(ch,:)=feval(ft,[mu(ch) 10 3 0.1 6 yoff],ts*1e3);
  15. lt(ch)=ts(find(xt(ch,:)>=(yoff+1/3*max(xt(ch,:)-yoff)),1,'first'));
  16. end
  17. yt=xt+.01*randn(Ncs,Nts);
  18. % Get Latencies
  19. [latTimes0, lsqCurveTimes0, lsqFittedCurve0, lsqFittedParam0, lsqCurveFitErr0] = computeLatencies_offset(xt,ts*1e3,10);
  20. [latTimes, lsqCurveTimes, lsqFittedCurve, lsqFittedParam, lsqCurveFitErr] = computeLatencies_offset(yt,ts*1e3,10);
  21. %% Plot results
  22. % multi-line plot offset
  23. ploffs0=0;%repmat((1:Ncs)'*max(yt(:)),1,Nts);
  24. ploffs=0;%repmat((1:Ncs)'*max(yt(:)),1,length(lsqCurveTimes));
  25. figure(); hold on
  26. plot(-inf,-inf,'b-'); plot(-inf,-inf,'k-'); plot(-inf,-inf,'r-');
  27. plot(-inf,-inf,'b*'); plot(-inf,-inf,'ko'); plot(-inf,-inf,'ro');
  28. plot(-inf,-inf,'bx'); plot(-inf,-inf,'kx'); plot(-inf,-inf,'rx');
  29. plot(ts*1e3,xt+ploffs0,'b-'); hold on;
  30. plot(ts*1e3,yt+ploffs0,'k-'); hold on;
  31. plot(lsqCurveTimes,lsqFittedCurve+ploffs,'r-'); hold on;
  32. plot(lt*1e3,ploffs0(:,1),'b*');
  33. plot(latTimes0,ploffs0(:,1),'ko');
  34. plot(latTimes,ploffs0(:,1),'ro');
  35. plot(mu,ploffs0(:,1),'bx');
  36. plot(lsqFittedParam0(:,1),ploffs0(:,1),'kx');
  37. plot(lsqFittedParam(:,1),ploffs0(:,1),'rx');
  38. xlabel('time (ms)');
  39. ylabel('channels'); %set(gca,'YTick',[]);
  40. hl=legend('x(t) sample signal','y(t)=x(t)+noise','f(t) fitted function','true latency',...
  41. 'latency fit(x)','latency fit(y)','true mean','mean fit(x)','mean fit(y)');
  42. hl.EdgeColor='none'; hl.Location='southeast';