r - Shifting and compressing diluted timed signal to achieve optimal correlation between two timed signals -


i've got 2 signals, 1 of signals lagging behind other, stretched in time. compress diluted signal , shift in time fits other signal, point see maximum correlation between two, i.e. largest r square.

i've generated example of i'm trying achieve:

library("lubridate")  ###### ## generate "dummy" data ######  s1.y = sin(seq(1,10,0.01)); s1.x = tseq <- seq(sys.time(), length.out = length(s1.y), = "secs")  ###### ## duplicate first signal, introduce lag , time dilution (depending on parameters , b) ## ## , b varying in reality, has been set constant illustration. ## constraints on these parameters +- 0.5.  ######  = 20.5;  b = 22.0;   fac = a/b lag = 14;   s2.y = s1.y * runif(length(s1.y), 0.95, 1.05); s2.x = s1.x; (i in 1:length(s1.x)) {    s2.x[i] = s1.x[i] + (minutes(lag)) + (seconds(fac*i));  }   ###### ## plot original signal , delayed , diluted signal ######  par(mfrow=c(2,1));  par(cex = 0.6); par(mar = c(4, 5, 2, 1), oma = c(1, 1, 1, 1));  ylimits = c(min(c(min(s1.y, na.rm=t), min(s2.y, na.rm=t))), max(c(max(s1.y, na.rm=t), max(s2.y, na.rm=t)))); xlimits = c(min(c(min(s1.x, na.rm=t), min(s2.x, na.rm=t))), max(c(max(s1.x, na.rm=t), max(s2.x, na.rm=t))));  plot(s1.x, s1.y, xlim = xlimits, ylim = ylimits, col = 'black', axes = t, xlab = '', ylab = 'concentration [ppm]', type = 'l') par(new=t); plot(s2.x, s2.y, xlim = xlimits, ylim = ylimits, col = 'red', axes = f, xlab = '', ylab = '', type = 'l') grid();   ###### ## shift signal minutes (guessing really), , introduce factor (guess) compress time series (also guess) ###### a.guess = 20.4; b.guess = 22.1; factor.guess = a/b; s3.x = s2.x; (i in 1:length(s2.x)){    s3.x[i] = s2.x[i] - seconds(factor.guess*i);     s3.x[i] = s3.x[i] - minutes(14); } s3.y = s2.y;  ####### ## determine r.square between 2 signals , plot them ####### base = data.frame(s1.y, s3.y) reg=lm(s1.y ~ s3.y, data=base) r.sq = summary(reg)$r.squared;  main.text = bquote(paste('r'^'2' == .(r.sq))) xlimits = c(min(c(min(s1.x, na.rm=t), min(s3.x, na.rm=t))), max(c(max(s1.x, na.rm=t), max(s3.x, na.rm=t))));  plot(s1.x, s1.y, xlim = xlimits, ylim = ylimits, col = 'black', axes = t, xlab = '', ylab = 'concentration [ppm]', type = 'l', main = main.text) par(new=t); plot(s3.x, s3.y, xlim = xlimits, ylim = ylimits, col = 'red', axes = f, xlab = '', ylab = '', type = 'l') grid(); 

the scripts ouputs figure:

figure showing 2 signals. top 1 shows before i've shifted , compressed signal, , bottom 1 after.

from example above, lag , dilution depends on 3 parameters, namely lag, , b. i've been choosing these guessing really, , seeing r square shows. i've been determining these variables using trial , error method, very silly, , time consuming.

what create function take in 2 signals (s1 , s2, both x , y variables), , find optimal values lag, , b largest correlation between 2 signals. have tried come function can this, using optim function of r, can't seem create works, , gives useful output. have suggestion go this, or better know how code such function?


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -